python-octaviaclient/octaviaclient/tests/unit/osc/v2/test_flavor.py
yangjianfeng 7a9f6f2f32 Add some commands for octavia flavor and flavor_profile
Depends-On: I99a673438458757d0acdaa46dd8ee041edb3be9c
Change-Id: Ib9867583545b48be828c9f5c3705e3a6a16f879c
2019-01-30 00:25:07 +00:00

173 lines
5.6 KiB
Python

# Copyright (c) 2018 China Telecom Corporation
# All Rights Reserved.
#
# 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 copy
import mock
from osc_lib import exceptions
from octaviaclient.osc.v2 import constants
from octaviaclient.osc.v2 import flavor
from octaviaclient.tests.unit.osc.v2 import constants as attr_consts
from octaviaclient.tests.unit.osc.v2 import fakes
class TestFlavor(fakes.TestOctaviaClient):
def setUp(self):
super(TestFlavor, self).setUp()
self._flavor = fakes.createFakeResource('flavor')
self.flavor_info = copy.deepcopy(attr_consts.FLAVOR_ATTRS)
self.columns = copy.deepcopy(constants.FLAVOR_COLUMNS)
self.api_mock = mock.Mock()
self.api_mock.flavor_list.return_value = copy.deepcopy(
{'flavors': [attr_consts.FLAVOR_ATTRS]})
lb_client = self.app.client_manager
lb_client.load_balancer = self.api_mock
class TestFlavorList(TestFlavor):
def setUp(self):
super(TestFlavorList, self).setUp()
self.datalist = (tuple(
attr_consts.FLAVOR_ATTRS[k] for k in self.columns),)
self.cmd = flavor.ListFlavor(self.app, None)
def test_flavor_list_no_options(self):
arglist = []
verifylist = []
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
columns, data = self.cmd.take_action(parsed_args)
self.api_mock.flavor_list.assert_called_with()
self.assertEqual(self.columns, columns)
self.assertEqual(self.datalist, tuple(data))
def test_flavor_list_with_options(self):
arglist = ['--name', 'flavor1']
verifylist = [('name', 'flavor1')]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
columns, data = self.cmd.take_action(parsed_args)
self.api_mock.flavor_list.assert_called_with(name='flavor1')
self.assertEqual(self.columns, columns)
self.assertEqual(self.datalist, tuple(data))
class TestFlavorDelete(TestFlavor):
def setUp(self):
super(TestFlavorDelete, self).setUp()
self.cmd = flavor.DeleteFlavor(self.app, None)
def test_flavor_delete(self):
arglist = [self._flavor.id]
verifylist = [
('flavor', self._flavor.id)
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
self.cmd.take_action(parsed_args)
self.api_mock.flavor_delete.assert_called_with(
flavor_id=self._flavor.id)
def test_flavor_delete_failure(self):
arglist = ['unknown_flavor']
verifylist = [
('flavor', 'unknown_flavor')
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
self.assertRaises(exceptions.CommandError, self.cmd.take_action,
parsed_args)
self.assertNotCalled(self.api_mock.flavor_delete)
class TestFlavorCreate(TestFlavor):
def setUp(self):
super(TestFlavorCreate, self).setUp()
self.api_mock.flavor_create.return_value = {
'flavor': self.flavor_info}
lb_client = self.app.client_manager
lb_client.load_balancer = self.api_mock
self.cmd = flavor.CreateFlavor(self.app, None)
@mock.patch('octaviaclient.osc.v2.utils.get_flavor_attrs')
def test_flavor_create(self, mock_client):
mock_client.return_value = self.flavor_info
arglist = ['--name', self._flavor.name,
'--flavorprofile', 'mock_fvpf_id',
'--description', 'description for flavor']
verifylist = [
('flavorprofile', 'mock_fvpf_id'),
('name', self._flavor.name),
('description', 'description for flavor')
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
self.cmd.take_action(parsed_args)
self.api_mock.flavor_create.assert_called_with(
json={'flavor': self.flavor_info})
class TestFlavorShow(TestFlavor):
def setUp(self):
super(TestFlavorShow, self).setUp()
self.api_mock.flavor_show.return_value = self.flavor_info
lb_client = self.app.client_manager
lb_client.load_balancer = self.api_mock
self.cmd = flavor.ShowFlavor(self.app, None)
def test_flavor_show(self):
arglist = [self._flavor.id]
verifylist = [
('flavor', self._flavor.id),
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
self.cmd.take_action(parsed_args)
self.api_mock.flavor_show.assert_called_with(
flavor_id=self._flavor.id)
class TestFlavorSet(TestFlavor):
def setUp(self):
super(TestFlavorSet, self).setUp()
self.cmd = flavor.SetFlavor(self.app, None)
def test_flavor_set(self):
arglist = [self._flavor.id, '--name', 'new_name']
verifylist = [
('flavor', self._flavor.id),
('name', 'new_name'),
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
self.cmd.take_action(parsed_args)
self.api_mock.flavor_set.assert_called_with(
self._flavor.id, json={
'flavor': {
'name': 'new_name'
}})