Add group unit tests

Add group unit tests.

Also, while writing the group unit tests, found that there was some
dead code in the commands/group module. That code is removed.

Change-Id: I7a4abfe26d3a3c55a02cbac6642008225285d2f9
This commit is contained in:
Steve Noyes 2018-06-07 11:36:39 -04:00
parent 635c978694
commit 88bc11320c
3 changed files with 99 additions and 49 deletions

View File

@ -135,55 +135,6 @@ class GroupListhosts(Lister):
raise Exception(traceback.format_exc()) raise Exception(traceback.format_exc())
class GroupAddservice(Command):
"""Add service to group."""
def get_parser(self, prog_name):
parser = super(GroupAddservice, self).get_parser(prog_name)
parser.add_argument('groupname', metavar='<groupname>',
help=u._('Group name'))
parser.add_argument('servicename', metavar='<servicename>',
help=u._('Service name'))
return parser
def take_action(self, parsed_args):
try:
groupname = parsed_args.groupname.strip()
servicename = parsed_args.servicename.strip()
group = CLIENT.group_get([groupname])[0]
group.add_service(servicename)
except ClientException as e:
raise CommandError(str(e))
except Exception as e:
raise Exception(traceback.format_exc())
class GroupRemoveservice(Command):
"""Remove service group from group."""
def get_parser(self, prog_name):
parser = super(GroupRemoveservice, self).get_parser(prog_name)
parser.add_argument('groupname', metavar='<groupname>',
help=u._('Group name'))
parser.add_argument('servicename', metavar='<servicename>',
help=u._('Service name'))
return parser
def take_action(self, parsed_args):
try:
groupname = parsed_args.groupname.strip()
servicename = parsed_args.servicename.strip()
group = CLIENT.group_get([groupname])[0]
group.remove_service(servicename)
except ClientException as e:
raise CommandError(str(e))
except Exception as e:
raise Exception(traceback.format_exc())
class GroupListservices(Lister): class GroupListservices(Lister):
"""List all groups and their services.""" """List all groups and their services."""

View File

@ -13,6 +13,7 @@
# under the License. # under the License.
import testtools import testtools
from kolla_cli.api.group import Group
from kolla_cli.api.host import Host from kolla_cli.api.host import Host
from kolla_cli.common.ansible.job import AnsibleJob from kolla_cli.common.ansible.job import AnsibleJob
from kolla_cli import shell from kolla_cli import shell
@ -30,3 +31,7 @@ class KollaCliUnitTest(testtools.TestCase):
def get_fake_host(self, hostname='foo'): def get_fake_host(self, hostname='foo'):
return Host(hostname) return Host(hostname)
def get_fake_group(self, groupname='group1', servicenames=[],
hostnames=[]):
return Group(groupname, servicenames, hostnames)

View File

@ -0,0 +1,94 @@
# Copyright (c) 2018 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 mock
from kolla_cli.tests.unit.common import KollaCliUnitTest
class TestUnit(KollaCliUnitTest):
@mock.patch('kolla_cli.api.client.ClientApi.group_add')
@mock.patch('kolla_cli.shell.KollaCli._is_inventory_present',
return_value=True)
def test_group_add(self, _, mock_add):
groupname = 'group1'
ret = self.run_cli_command('group add %s' % groupname)
self.assertEqual(ret, 0)
mock_add.assert_called_once_with([groupname])
@mock.patch('kolla_cli.api.client.ClientApi.group_remove')
@mock.patch('kolla_cli.shell.KollaCli._is_inventory_present',
return_value=True)
def test_group_remove(self, _, mock_remove):
groupname = 'group1'
ret = self.run_cli_command('group remove %s' % groupname)
self.assertEqual(ret, 0)
mock_remove.assert_called_once_with([groupname])
@mock.patch('kolla_cli.api.client.ClientApi.group_get_all')
@mock.patch('kolla_cli.shell.KollaCli._is_inventory_present',
return_value=True)
def test_group_listhosts(self, _, mock_group_get_all):
# list all groups and their hosts
hostname = 'foo'
groupname = 'group1'
fake_group = self.get_fake_group(groupname, hostnames=[hostname])
mock_group_get_all.return_value = [fake_group]
ret = self.run_cli_command('group listhosts')
self.assertEqual(ret, 0)
mock_group_get_all.assert_called_once_with()
@mock.patch('kolla_cli.api.group.Group.add_host')
@mock.patch('kolla_cli.api.client.ClientApi.group_get')
@mock.patch('kolla_cli.shell.KollaCli._is_inventory_present',
return_value=True)
def test_group_addhost(self, _, mock_group_get, mock_group_add_host):
hostname = 'foo'
groupname = 'group1'
fake_group = self.get_fake_group(groupname)
mock_group_get.return_value = [fake_group]
ret = self.run_cli_command('group addhost %s %s'
% (groupname, hostname))
self.assertEqual(ret, 0)
mock_group_get.assert_called_once_with([groupname])
mock_group_add_host.assert_called_once_with(hostname)
@mock.patch('kolla_cli.api.group.Group.remove_host')
@mock.patch('kolla_cli.api.client.ClientApi.group_get')
@mock.patch('kolla_cli.shell.KollaCli._is_inventory_present',
return_value=True)
def test_group_removehost(self, _, mock_group_get,
mock_group_remove_host):
hostname = 'foo'
groupname = 'group1'
fake_group = self.get_fake_group(groupname, hostnames=[hostname])
mock_group_get.return_value = [fake_group]
ret = self.run_cli_command('group removehost %s %s'
% (groupname, hostname))
self.assertEqual(ret, 0)
mock_group_get.assert_called_once_with([groupname])
mock_group_remove_host.assert_called_once_with(hostname)
@mock.patch('kolla_cli.api.client.ClientApi.group_get_all')
@mock.patch('kolla_cli.shell.KollaCli._is_inventory_present',
return_value=True)
def test_group_listservices(self, _, mock_group_get_all):
# list all groups and their services
servicename = 'service1'
groupname = 'group1'
fake_group = self.get_fake_group(groupname,
servicenames=[servicename])
mock_group_get_all.return_value = [fake_group]
ret = self.run_cli_command('group listservices')
self.assertEqual(ret, 0)
mock_group_get_all.assert_called_once_with()