nodepool/nodepool/tests/test_commands.py

162 lines
6.3 KiB
Python

# Copyright (C) 2015 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.path
import sys # noqa making sure its available for monkey patching
import fixtures
import mock
from nodepool.cmd import nodepoolcmd
from nodepool import tests
class TestNodepoolCMD(tests.DBTestCase):
def patch_argv(self, *args):
argv = ["nodepool", "-s", self.secure_conf]
argv.extend(args)
self.useFixture(fixtures.MonkeyPatch('sys.argv', argv))
def assert_images_listed(self, configfile, image_cnt, status="ready"):
self.patch_argv("-c", configfile, "image-list")
with mock.patch('prettytable.PrettyTable.add_row') as m_add_row:
nodepoolcmd.main()
images_with_status = 0
# Find add_rows with the status were looking for
for args, kwargs in m_add_row.call_args_list:
row = args[0]
status_column = 7
if row[status_column] == status:
images_with_status += 1
self.assertEquals(images_with_status, image_cnt)
def assert_nodes_listed(self, configfile, node_cnt, status="ready"):
self.patch_argv("-c", configfile, "list")
with mock.patch('prettytable.PrettyTable.add_row') as m_add_row:
nodepoolcmd.main()
nodes_with_status = 0
# Find add_rows with the status were looking for
for args, kwargs in m_add_row.call_args_list:
row = args[0]
# this is the major difference between the two assert
# functions.
status_column = 9
if row[status_column] == status:
nodes_with_status += 1
self.assertEquals(nodes_with_status, node_cnt)
def test_snapshot_image_update(self):
configfile = self.setup_config("node.yaml")
self.patch_argv("-c", configfile, "image-update",
"fake-provider", "fake-image")
nodepoolcmd.main()
self.wait_for_threads()
self.assert_images_listed(configfile, 1)
def test_dib_image_update(self):
configfile = self.setup_config("node_dib.yaml")
self.patch_argv("-c", configfile, "image-update",
"fake-dib-provider", "fake-dib-image")
nodepoolcmd.main()
self.wait_for_threads()
self.assert_images_listed(configfile, 1)
def test_dib_snapshot_image_update(self):
configfile = self.setup_config("node_dib_and_snap.yaml")
self.patch_argv("-c", configfile, "image-update",
"fake-provider1", "fake-dib-image")
nodepoolcmd.main()
self.patch_argv("-c", configfile, "image-update",
"fake-provider2", "fake-dib-image")
nodepoolcmd.main()
self.wait_for_threads()
self.assert_images_listed(configfile, 2)
def test_dib_snapshot_image_update_all(self):
configfile = self.setup_config("node_dib_and_snap.yaml")
self.patch_argv("-c", configfile, "image-update",
"all", "fake-dib-image")
nodepoolcmd.main()
self.wait_for_threads()
self.assert_images_listed(configfile, 2)
def test_image_update_all(self):
configfile = self.setup_config("node_cmd.yaml")
self.patch_argv("-c", configfile, "image-update",
"all", "fake-image1")
nodepoolcmd.main()
self.wait_for_threads()
self.assert_images_listed(configfile, 1)
def test_image_list_empty(self):
self.assert_images_listed(self.setup_config("node_cmd.yaml"), 0)
def test_image_delete_invalid(self):
configfile = self.setup_config("node_cmd.yaml")
self.patch_argv("-c", configfile, "image-delete", "invalid-image")
nodepoolcmd.main()
def test_image_delete_snapshot(self):
configfile = self.setup_config("node_cmd.yaml")
self.patch_argv("-c", configfile, "image-update",
"all", "fake-image1")
nodepoolcmd.main()
pool = self.useNodepool(configfile, watermark_sleep=1)
# This gives us a nodepool with a working db but not running which
# is important so we can control image building
pool.updateConfig()
self.waitForImage(pool, 'fake-provider1', 'fake-image1')
self.patch_argv("-c", configfile, "image-delete", '1')
nodepoolcmd.main()
self.wait_for_threads()
self.assert_images_listed(configfile, 0)
def test_alien_list_fail(self):
def fail_list(self):
raise RuntimeError('Fake list error')
self.useFixture(fixtures.MonkeyPatch('nodepool.fakeprovider.FakeList'
'.list', fail_list))
configfile = self.setup_config("node_cmd.yaml")
self.patch_argv("-c", configfile, "alien-list")
nodepoolcmd.main()
self.wait_for_threads()
def test_alien_image_list_fail(self):
def fail_list(self):
raise RuntimeError('Fake list error')
self.useFixture(fixtures.MonkeyPatch('nodepool.fakeprovider.FakeList'
'.list', fail_list))
configfile = self.setup_config("node_cmd.yaml")
self.patch_argv("-c", configfile, "alien-image-list")
nodepoolcmd.main()
self.wait_for_threads()
def test_list_nodes(self):
configfile = self.setup_config('node.yaml')
pool = self.useNodepool(configfile, watermark_sleep=1)
pool.start()
self.waitForImage(pool, 'fake-provider', 'fake-image')
self.waitForNodes(pool)
self.assert_nodes_listed(configfile, 1)
def test_config_validate(self):
config = os.path.join(os.path.dirname(tests.__file__),
'fixtures', 'config_validate', 'good.yaml')
self.patch_argv('-c', config, 'config-validate')
nodepoolcmd.main()