Ian Cordasco 7562ab3e3a Refactor shell unittests and add hosts shell tests
This refactors our shell unittest helpers into three base classes for
further unit test classes to subclass and extend. This also corrects
some problems with the hosts shell parameters that either didn't
previous work or were wrong and no longer belong. Finally, it removes
project_id from places where it no longer belongs.

Change-Id: Ibd6a36714135d14355c3b4f48fced83b852581a9
2016-11-03 10:55:18 -05:00

407 lines
13 KiB
Python

# -*- coding: utf-8 -*-
# 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.
"""Tests for the shell functions for the cells resource."""
import mock
from cratonclient import exceptions
from cratonclient.shell.v1 import cells_shell
from cratonclient.tests.unit.shell import base
from cratonclient.v1 import cells
class TestDoShellShow(base.TestShellCommandUsingPrintDict):
"""Unit tests for the cell show command."""
def test_simple_usage(self):
"""Verify the behaviour of do_cell_show."""
args = self.args_for(
region=123,
id=456,
)
cells_shell.do_cell_show(self.craton_client, args)
self.craton_client.inventory.assert_called_once_with(123)
self.inventory.cells.get.assert_called_once_with(456)
self.print_dict.assert_called_once_with(
{f: mock.ANY for f in cells.CELL_FIELDS},
wrap=72,
)
class TestDoCellList(base.TestShellCommandUsingPrintList):
"""Unit tests for the cell list command."""
def assertNothingWasCalled(self):
"""Assert inventory, list, and print_list were not called."""
super(TestDoCellList, self).assertNothingWasCalled()
self.assertFalse(self.print_list.called)
def test_with_defaults(self):
"""Verify the behaviour of do_cell_list with mostly default values."""
args = self.args_for(
region=123,
detail=False,
limit=None,
sort_key=None,
sort_dir='asc',
fields=[],
)
cells_shell.do_cell_list(self.craton_client, args)
self.craton_client.inventory.assert_called_once_with(123)
self.inventory.cells.list.assert_called_once_with(sort_dir='asc')
self.assertTrue(self.print_list.called)
self.assertEqual(['id', 'name'],
sorted(self.print_list.call_args[0][-1]))
def test_negative_limit(self):
"""Ensure we raise an exception for negative limits."""
args = self.args_for(
region=123,
detail=False,
limit=-1,
sort_key=None,
sort_dir='asc',
fields=[],
)
self.assertRaisesCommandErrorWith(cells_shell.do_cell_list, args)
self.assertNothingWasCalled()
def test_positive_limit(self):
"""Verify that we pass positive limits to the call to list."""
args = self.args_for(
region=123,
detail=False,
limit=5,
sort_key=None,
sort_dir='asc',
fields=[],
)
cells_shell.do_cell_list(self.craton_client, args)
self.craton_client.inventory.assert_called_once_with(123)
self.inventory.cells.list.assert_called_once_with(
limit=5,
sort_dir='asc',
)
self.assertTrue(self.print_list.called)
self.assertEqual(['id', 'name'],
sorted(self.print_list.call_args[0][-1]))
def test_valid_sort_key(self):
"""Verify that we pass on our sort key."""
args = self.args_for(
region=123,
detail=False,
limit=None,
sort_key='name',
sort_dir='asc',
fields=[],
)
cells_shell.do_cell_list(self.craton_client, args)
self.craton_client.inventory.assert_called_once_with(123)
self.inventory.cells.list.assert_called_once_with(
sort_dir='asc',
sort_key='name',
)
self.assertTrue(self.print_list.called)
self.assertEqual(['id', 'name'],
sorted(self.print_list.call_args[0][-1]))
def test_invalid_sort_key(self):
"""Verify that do not we pass on our sort key."""
args = self.args_for(
region=123,
detail=False,
limit=None,
sort_key='fake-sort-key',
sort_dir='asc',
fields=[],
)
self.assertRaisesCommandErrorWith(cells_shell.do_cell_list, args)
self.assertNothingWasCalled()
def test_detail(self):
"""Verify the behaviour of specifying --detail."""
args = self.args_for(
region=123,
detail=True,
limit=None,
sort_key=None,
sort_dir='asc',
fields=[],
)
cells_shell.do_cell_list(self.craton_client, args)
self.craton_client.inventory.assert_called_once_with(123)
self.inventory.cells.list.assert_called_once_with(
sort_dir='asc',
detail=True,
)
self.assertEqual(sorted(list(cells.CELL_FIELDS)),
sorted(self.print_list.call_args[0][-1]))
def test_raises_exception_with_detail_and_fields(self):
"""Verify that we fail when users specify --detail and --fields."""
args = self.args_for(
region=123,
detail=True,
limit=None,
sort_key=None,
sort_dir='asc',
fields=['id', 'name'],
)
self.assertRaisesCommandErrorWith(cells_shell.do_cell_list, args)
self.assertNothingWasCalled()
def test_fields(self):
"""Verify that we print out specific fields."""
args = self.args_for(
region=123,
detail=False,
limit=None,
sort_key=None,
sort_dir='asc',
fields=['id', 'name', 'note'],
)
cells_shell.do_cell_list(self.craton_client, args)
self.craton_client.inventory.assert_called_once_with(123)
self.inventory.cells.list.assert_called_once_with(
sort_dir='asc',
)
self.assertEqual(['id', 'name', 'note'],
sorted(self.print_list.call_args[0][-1]))
def test_invalid_fields(self):
"""Verify that we error out with invalid fields."""
args = self.args_for(
region=123,
detail=False,
limit=None,
sort_key=None,
sort_dir='asc',
fields=['uuid', 'not-name', 'nate'],
)
self.assertRaisesCommandErrorWith(cells_shell.do_cell_list, args)
self.assertNothingWasCalled()
class TestDoCellCreate(base.TestShellCommandUsingPrintDict):
"""Unit tests for the cell create command."""
def test_create_without_note(self):
"""Verify our parameters to cells.create."""
args = self.args_for(
name='New Cell',
region_id=123,
note=None,
)
cells_shell.do_cell_create(self.craton_client, args)
self.craton_client.inventory.assert_called_once_with(123)
self.inventory.cells.create.assert_called_once_with(
name='New Cell',
region_id=123,
)
self.print_dict.assert_called_once_with(mock.ANY, wrap=72)
def test_create_with_note(self):
"""Verify that we include the note argument when present."""
args = self.args_for(
name='New Cell',
region_id=123,
note='This is a note',
)
cells_shell.do_cell_create(self.craton_client, args)
self.craton_client.inventory.assert_called_once_with(123)
self.inventory.cells.create.assert_called_once_with(
name='New Cell',
region_id=123,
note='This is a note',
)
self.print_dict.assert_called_once_with(mock.ANY, wrap=72)
class TestDoCellUpdate(base.TestShellCommandUsingPrintDict):
"""Unit tests for the cell update command."""
def test_update_without_name_region_or_note_fails(self):
"""Verify we raise a command error when there's nothing to update."""
args = self.args_for(
id=123,
region=345,
name=None,
region_id=None,
note=None,
)
self.assertRaisesCommandErrorWith(cells_shell.do_cell_update, args)
self.assertNothingWasCalled()
def test_update_with_name(self):
"""Verify we update with only the new name."""
args = self.args_for(
id=123,
region=345,
name='New name',
region_id=None,
note=None,
)
cells_shell.do_cell_update(self.craton_client, args)
self.craton_client.inventory.assert_called_once_with(345)
self.inventory.cells.update.assert_called_once_with(
123,
name='New name',
)
self.print_dict.assert_called_once_with(mock.ANY, wrap=72)
def test_update_with_new_region(self):
"""Verify we update with only the new region id."""
args = self.args_for(
id=123,
region=345,
name=None,
region_id=678,
note=None,
)
cells_shell.do_cell_update(self.craton_client, args)
self.craton_client.inventory.assert_called_once_with(345)
self.inventory.cells.update.assert_called_once_with(
123,
region_id=678,
)
self.print_dict.assert_called_once_with(mock.ANY, wrap=72)
def test_update_with_new_note(self):
"""Verify we update with only the new note text."""
args = self.args_for(
id=123,
region=345,
name=None,
region_id=None,
note='A new note',
)
cells_shell.do_cell_update(self.craton_client, args)
self.craton_client.inventory.assert_called_once_with(345)
self.inventory.cells.update.assert_called_once_with(
123,
note='A new note',
)
self.print_dict.assert_called_once_with(mock.ANY, wrap=72)
def test_update_with_everything(self):
"""Verify we update with everything."""
args = self.args_for(
id=123,
region=345,
name='A new name for a new region',
region_id=678,
note='A new note',
)
cells_shell.do_cell_update(self.craton_client, args)
self.craton_client.inventory.assert_called_once_with(345)
self.inventory.cells.update.assert_called_once_with(
123,
name='A new name for a new region',
region_id=678,
note='A new note',
)
self.print_dict.assert_called_once_with(mock.ANY, wrap=72)
class TestDoCellDelete(base.TestShellCommand):
"""Tests for the do_cell_delete command."""
def setUp(self):
"""Initialize our print mock."""
super(TestDoCellDelete, self).setUp()
self.print_func_mock = mock.patch(
'cratonclient.shell.v1.cells_shell.print'
)
self.print_func = self.print_func_mock.start()
def tearDown(self):
"""Clean up our print mock."""
super(TestDoCellDelete, self).tearDown()
self.print_func_mock.stop()
def test_successful(self):
"""Verify the message we print when successful."""
self.inventory.cells.delete.return_value = True
args = self.args_for(
region=123,
id=456,
)
cells_shell.do_cell_delete(self.craton_client, args)
self.craton_client.inventory.assert_called_once_with(123)
self.inventory.cells.delete.assert_called_once_with(456)
self.print_func.assert_called_once_with(
'Cell 456 was successfully deleted.'
)
def test_failed(self):
"""Verify the message we print when deletion fails."""
self.inventory.cells.delete.return_value = False
args = self.args_for(
region=123,
id=456,
)
cells_shell.do_cell_delete(self.craton_client, args)
self.craton_client.inventory.assert_called_once_with(123)
self.inventory.cells.delete.assert_called_once_with(456)
self.print_func.assert_called_once_with(
'Cell 456 was not deleted.'
)
def test_failed_with_exception(self):
"""Verify the message we print when deletion fails."""
self.inventory.cells.delete.side_effect = exceptions.NotFound
args = self.args_for(
region=123,
id=456,
)
self.assertRaisesCommandErrorWith(cells_shell.do_cell_delete, args)
self.craton_client.inventory.assert_called_once_with(123)
self.inventory.cells.delete.assert_called_once_with(456)
self.assertFalse(self.print_func.called)