82 lines
2.7 KiB
Python
82 lines
2.7 KiB
Python
# Copyright (C) 2014 OpenStack Foundation
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
# implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
|
|
import os
|
|
import tempfile
|
|
import threading
|
|
import time
|
|
|
|
from nodepool import tests
|
|
from nodepool import nodedb
|
|
import nodepool.nodepool
|
|
|
|
|
|
class TestNodepool(tests.DBTestCase):
|
|
def setup_config(self, filename):
|
|
super(TestNodepool, self).setUp()
|
|
configfile = os.path.join(os.path.dirname(tests.__file__),
|
|
'fixtures', filename)
|
|
config = open(configfile).read()
|
|
(fd, path) = tempfile.mkstemp()
|
|
os.write(fd, config.format(dburi=self.dburi))
|
|
os.close(fd)
|
|
return path
|
|
|
|
def wait_for_threads(self):
|
|
whitelist = ['APScheduler',
|
|
'MainThread',
|
|
'NodePool',
|
|
'NodeUpdateListener',
|
|
'Gearman client connect',
|
|
'Gearman client poll',
|
|
'fake-provider',
|
|
'fake-jenkins',
|
|
'fake-target',
|
|
]
|
|
|
|
while True:
|
|
done = True
|
|
for t in threading.enumerate():
|
|
if t.name not in whitelist:
|
|
done = False
|
|
if done:
|
|
return
|
|
time.sleep(0.1)
|
|
|
|
def test_db(self):
|
|
db = nodedb.NodeDatabase(self.dburi)
|
|
with db.getSession() as session:
|
|
session.getNodes()
|
|
|
|
def test_node(self):
|
|
"""Test that an image and node are created"""
|
|
configfile = self.setup_config('node.yaml')
|
|
pool = nodepool.nodepool.NodePool(configfile)
|
|
pool.start()
|
|
time.sleep(2)
|
|
while True:
|
|
self.wait_for_threads()
|
|
with pool.getDB().getSession() as session:
|
|
nodes = session.getNodes(provider_name='fake-provider',
|
|
label_name='fake-label',
|
|
target_name='fake-target',
|
|
state=nodedb.READY)
|
|
if len(nodes) == 1:
|
|
break
|
|
nodes = session.getNodes()
|
|
time.sleep(1)
|
|
self.wait_for_threads()
|
|
pool.stop()
|