From 92b9842951f874f78fa6de55908d638ded996691 Mon Sep 17 00:00:00 2001 From: "James E. Blair" Date: Thu, 27 Mar 2014 15:03:22 -0700 Subject: [PATCH] Add a test for subnodes Some misc changes related to running this: * Set log/stdout/err capture vars as in ZUUL * Give the main loop a configurable sleep value so tests can run faster * Fix a confusing typo in the node.yaml config Additionally, a better method for waiting for test completion is added which permits us to use assert statements in the tests. Change-Id: Icddd2afcd816dbd5ab955fa4ab5011ac8def8faf --- .testr.conf | 2 +- nodepool/nodepool.py | 5 ++- nodepool/tests/__init__.py | 6 ++- nodepool/tests/fixtures/node.yaml | 4 +- nodepool/tests/fixtures/subnodes.yaml | 53 ++++++++++++++++++++++++++ nodepool/tests/test_nodepool.py | 55 +++++++++++++++++++++------ 6 files changed, 106 insertions(+), 19 deletions(-) create mode 100644 nodepool/tests/fixtures/subnodes.yaml diff --git a/.testr.conf b/.testr.conf index 15cea5e..817745d 100644 --- a/.testr.conf +++ b/.testr.conf @@ -1,4 +1,4 @@ [DEFAULT] -test_command=OS_TEST_TIMEOUT=60 ${PYTHON:-python} -m subunit.run discover -t ./ nodepool/tests/ $LISTOPT $IDOPTION +test_command=OS_STDOUT_CAPTURE=${OS_STDOUT_CAPTURE:-1} OS_STDERR_CAPTURE=${OS_STDERR_CAPTURE:-1} OS_LOG_CAPTURE=${OS_LOG_CAPTURE:-1} OS_TEST_TIMEOUT=60 ${PYTHON:-python} -m subunit.run discover -t ./ nodepool/tests/ $LISTOPT $IDOPTION test_id_option=--load-list $IDFILE test_list_option=--list diff --git a/nodepool/nodepool.py b/nodepool/nodepool.py index 2111462..ba16740 100644 --- a/nodepool/nodepool.py +++ b/nodepool/nodepool.py @@ -836,9 +836,10 @@ class GearmanServer(ConfigValue): class NodePool(threading.Thread): log = logging.getLogger("nodepool.NodePool") - def __init__(self, configfile): + def __init__(self, configfile, watermark_sleep=WATERMARK_SLEEP): threading.Thread.__init__(self, name='NodePool') self.configfile = configfile + self.watermark_sleep = watermark_sleep self._stopped = False self.config = None self.zmq_context = None @@ -1275,7 +1276,7 @@ class NodePool(threading.Thread): self._run(session) except Exception: self.log.exception("Exception in main loop:") - time.sleep(WATERMARK_SLEEP) + time.sleep(self.watermark_sleep) def _run(self, session): self.checkForMissingImages(session) diff --git a/nodepool/tests/__init__.py b/nodepool/tests/__init__.py index 60caf40..5e7824e 100644 --- a/nodepool/tests/__init__.py +++ b/nodepool/tests/__init__.py @@ -49,8 +49,10 @@ class BaseTestCase(testtools.TestCase, testresources.ResourcedTestCase): if os.environ.get('OS_STDERR_CAPTURE') in TRUE_VALUES: stderr = self.useFixture(fixtures.StringStream('stderr')).stream self.useFixture(fixtures.MonkeyPatch('sys.stderr', stderr)) - - self.useFixture(fixtures.FakeLogger(level=logging.DEBUG)) + if os.environ.get('OS_LOG_CAPTURE') in TRUE_VALUES: + self.useFixture(fixtures.FakeLogger(level=logging.DEBUG)) + else: + logging.basicConfig(level=logging.DEBUG) self.useFixture(fixtures.NestedTempfile()) diff --git a/nodepool/tests/fixtures/node.yaml b/nodepool/tests/fixtures/node.yaml index 821a07d..836d0dd 100644 --- a/nodepool/tests/fixtures/node.yaml +++ b/nodepool/tests/fixtures/node.yaml @@ -14,7 +14,7 @@ gearman-servers: labels: - name: fake-label - image: fake-label + image: fake-image min-ready: 1 providers: - name: fake-provider @@ -32,7 +32,7 @@ providers: - net-id: 'some-uuid' rate: 0.0001 images: - - name: fake-label + - name: fake-image base-image: 'Fake Precise' min-ram: 8192 name-filter: 'Fake' diff --git a/nodepool/tests/fixtures/subnodes.yaml b/nodepool/tests/fixtures/subnodes.yaml new file mode 100644 index 0000000..518f457 --- /dev/null +++ b/nodepool/tests/fixtures/subnodes.yaml @@ -0,0 +1,53 @@ +script-dir: . +dburi: '{dburi}' + +cron: + check: '*/15 * * * *' + cleanup: '*/1 * * * *' + update-image: '14 2 * * *' + +zmq-publishers: + - tcp://localhost:8881 + +gearman-servers: + - host: localhost + +labels: + - name: fake-label + image: fake-image + min-ready: 2 + providers: + - name: fake-provider + - name: multi-fake + image: fake-image + ready-script: multinode_setup.sh + subnodes: 2 + min-ready: 2 + providers: + - name: fake-provider + +providers: + - name: fake-provider + keypair: 'if-present-use-this-keypair' + username: 'fake' + password: 'fake' + auth-url: 'fake' + project-id: 'fake' + max-servers: 96 + pool: 'fake' + networks: + - net-id: 'some-uuid' + rate: 0.0001 + images: + - name: fake-image + base-image: 'Fake Precise' + min-ram: 8192 + name-filter: 'Fake' + setup: prepare_node_devstack.sh + +targets: + - name: fake-target + jenkins: + url: https://jenkins.example.org/ + user: fake + apikey: fake diff --git a/nodepool/tests/test_nodepool.py b/nodepool/tests/test_nodepool.py index 7b9b014..0263b32 100644 --- a/nodepool/tests/test_nodepool.py +++ b/nodepool/tests/test_nodepool.py @@ -60,22 +60,53 @@ class TestNodepool(tests.DBTestCase): 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) + def waitForNodes(self, pool): 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: + needed = pool.getNeededNodes(session) + if not needed: break - nodes = session.getNodes() time.sleep(1) self.wait_for_threads() + + def test_node(self): + """Test that an image and node are created""" + configfile = self.setup_config('node.yaml') + pool = nodepool.nodepool.NodePool(configfile, watermark_sleep=0.5) + pool.start() + time.sleep(2) + self.waitForNodes(pool) + + with pool.getDB().getSession() as session: + nodes = session.getNodes(provider_name='fake-provider', + label_name='fake-label', + target_name='fake-target', + state=nodedb.READY) + self.assertEqual(len(nodes), 1) + pool.stop() + + def test_subnodes(self): + """Test that an image and node are created""" + configfile = self.setup_config('subnodes.yaml') + pool = nodepool.nodepool.NodePool(configfile, watermark_sleep=0.5) + pool.start() + time.sleep(2) + self.waitForNodes(pool) + + with pool.getDB().getSession() as session: + nodes = session.getNodes(provider_name='fake-provider', + label_name='fake-label', + target_name='fake-target', + state=nodedb.READY) + self.assertEqual(len(nodes), 2) + nodes = session.getNodes(provider_name='fake-provider', + label_name='multi-fake', + target_name='fake-target', + state=nodedb.READY) + self.assertEqual(len(nodes), 2) + for node in nodes: + self.assertEqual(len(node.subnodes), 2) + for subnode in node.subnodes: + self.assertEqual(subnode.state, nodedb.READY) pool.stop()