/usr/lib/python2.7/dist-packages/octaviaclient/tests/unit/osc/v2/test_load_balancer.py is in python-octaviaclient 1.4.0-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 | # 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 itertools
import mock
from osc_lib import exceptions
from oslo_utils import uuidutils
from octaviaclient.osc.v2 import constants
from octaviaclient.osc.v2 import load_balancer
from octaviaclient.tests.unit.osc.v2 import constants as attr_consts
from octaviaclient.tests.unit.osc.v2 import fakes
class TestLoadBalancer(fakes.TestOctaviaClient):
def setUp(self):
super(TestLoadBalancer, self).setUp()
self._lb = fakes.createFakeResource('loadbalancer')
self.lb_info = copy.deepcopy(attr_consts.LOADBALANCER_ATTRS)
self.columns = copy.deepcopy(constants.LOAD_BALANCER_COLUMNS)
self.api_mock = mock.Mock()
self.api_mock.load_balancer_list.return_value = copy.deepcopy(
{'loadbalancers': [attr_consts.LOADBALANCER_ATTRS]})
lb_client = self.app.client_manager
lb_client.load_balancer = self.api_mock
lb_client.neutronclient = mock.MagicMock()
class TestLoadBalancerList(TestLoadBalancer):
def setUp(self):
super(TestLoadBalancerList, self).setUp()
self.datalist = (tuple(
attr_consts.LOADBALANCER_ATTRS[k] for k in self.columns),)
self.cmd = load_balancer.ListLoadBalancer(self.app, None)
def test_load_balancer_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.load_balancer_list.assert_called_with()
self.assertEqual(self.columns, columns)
self.assertEqual(self.datalist, tuple(data))
def test_load_balancer_list_with_options(self):
arglist = ['--name', 'rainbarrel']
verifylist = [('name', 'rainbarrel')]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
columns, data = self.cmd.take_action(parsed_args)
self.api_mock.load_balancer_list.assert_called_with(name='rainbarrel')
self.assertEqual(self.columns, columns)
self.assertEqual(self.datalist, tuple(data))
class TestLoadBalancerDelete(TestLoadBalancer):
def setUp(self):
super(TestLoadBalancerDelete, self).setUp()
self.cmd = load_balancer.DeleteLoadBalancer(self.app, None)
def test_load_balancer_delete(self):
arglist = [self._lb.id]
verifylist = [
('loadbalancer', self._lb.id)
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
self.cmd.take_action(parsed_args)
self.api_mock.load_balancer_delete.assert_called_with(
lb_id=self._lb.id)
def test_load_balancer_delete_failure(self):
arglist = ['unknown_lb']
verifylist = [
('loadbalancer', 'unknown_lb')
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
self.assertRaises(exceptions.CommandError, self.cmd.take_action,
parsed_args)
self.assertNotCalled(self.api_mock.load_balancer_delete)
class TestLoadBalancerCreate(TestLoadBalancer):
def setUp(self):
super(TestLoadBalancerCreate, self).setUp()
self.api_mock.load_balancer_create.return_value = {
'loadbalancer': self.lb_info
}
lb_client = self.app.client_manager
lb_client.load_balancer = self.api_mock
self.cmd = load_balancer.CreateLoadBalancer(self.app, None)
@mock.patch('octaviaclient.osc.v2.utils.get_loadbalancer_attrs')
def test_load_balancer_create(self, mock_client):
mock_client.return_value = self.lb_info
arglist = ['--name', self._lb.name,
'--vip-network-id', self._lb.vip_network_id,
'--project', self._lb.project_id]
verifylist = [
('name', self._lb.name),
('vip_network_id', self._lb.vip_network_id),
('project', self._lb.project_id)
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
self.cmd.take_action(parsed_args)
self.api_mock.load_balancer_create.assert_called_with(
json={'loadbalancer': self.lb_info})
@mock.patch('octaviaclient.osc.v2.utils.get_loadbalancer_attrs')
def test_load_balancer_create_with_qos_policy(self, mock_client):
qos_policy_id = 'qos_id'
lb_info = copy.deepcopy(self.lb_info)
lb_info.update({'vip_qos_policy_id': qos_policy_id})
mock_client.return_value = lb_info
arglist = [
'--name', self._lb.name,
'--vip-network-id', self._lb.vip_network_id,
'--project', self._lb.project_id,
'--vip-qos-policy-id', qos_policy_id,
]
verifylist = [
('name', self._lb.name),
('vip_network_id', self._lb.vip_network_id),
('project', self._lb.project_id),
('vip_qos_policy_id', qos_policy_id),
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
self.cmd.take_action(parsed_args)
self.api_mock.load_balancer_create.assert_called_with(
json={'loadbalancer': lb_info})
@mock.patch('octaviaclient.osc.v2.utils.get_loadbalancer_attrs')
def test_load_balancer_create_missing_args(self, mock_client):
attrs_list = self.lb_info
# init missing keys
args = ("vip_subnet_id", "vip_network_id", "vip_port_id")
for a in args:
# init missing keys
attrs_list[a] = ''
# verify all valid combinations of args
for n in range(len(args)+1):
for comb in itertools.combinations(args, n):
# subtract comb's keys from attrs_list
filtered_attrs = {k: v for k, v in attrs_list.items() if (
k not in comb)}
mock_client.return_value = filtered_attrs
if not any(k in filtered_attrs for k in args) or all(
k in filtered_attrs for k in ("vip_network_id",
"vip_port_id")
):
self.assertRaises(
exceptions.CommandError,
self.cmd.take_action,
filtered_attrs)
else:
try:
self.cmd.take_action(filtered_attrs)
except exceptions.CommandError as e:
self.fail("%s raised unexpectedly" % e)
class TestLoadBalancerShow(TestLoadBalancer):
def setUp(self):
super(TestLoadBalancerShow, self).setUp()
self.api_mock.load_balancer_show.return_value = {
'loadbalancer': self.lb_info}
lb_client = self.app.client_manager
lb_client.load_balancer = self.api_mock
self.cmd = load_balancer.ShowLoadBalancer(self.app, None)
def test_load_balancer_show(self):
arglist = [self._lb.id]
verifylist = [
('loadbalancer', self._lb.id),
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
self.cmd.take_action(parsed_args)
self.api_mock.load_balancer_show.assert_called_with(lb_id=self._lb.id)
class TestLoadBalancerSet(TestLoadBalancer):
def setUp(self):
super(TestLoadBalancerSet, self).setUp()
lb_client = self.app.client_manager
lb_client.load_balancer = self.api_mock
self.cmd = load_balancer.SetLoadBalancer(self.app, None)
@mock.patch('octaviaclient.osc.v2.utils.get_loadbalancer_attrs')
def test_load_balancer_set(self, mock_attrs):
qos_policy_id = uuidutils.generate_uuid()
mock_attrs.return_value = {
'loadbalancer_id': self._lb.id,
'name': 'new_name',
'vip_qos_policy_id': qos_policy_id,
}
arglist = [self._lb.id, '--name', 'new_name',
'--vip-qos-policy-id', qos_policy_id]
verifylist = [
('loadbalancer', self._lb.id),
('name', 'new_name'),
('vip_qos_policy_id', qos_policy_id),
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
self.cmd.take_action(parsed_args)
self.api_mock.load_balancer_set.assert_called_with(
self._lb.id, json={
'loadbalancer': {
'name': 'new_name',
'vip_qos_policy_id': qos_policy_id,
}
})
@mock.patch('octaviaclient.osc.v2.utils.get_loadbalancer_attrs')
def test_load_balancer_remove_qos_policy(self, mock_attrs):
mock_attrs.return_value = {
'loadbalancer_id': self._lb.id,
'vip_qos_policy_id': None,
}
arglist = [self._lb.id, '--vip-qos-policy-id', 'None']
verifylist = [
('loadbalancer', self._lb.id),
('vip_qos_policy_id', 'None'),
]
try:
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
self.cmd.take_action(parsed_args)
except Exception as e:
self.fail("%s raised unexpectedly" % e)
class TestLoadBalancerStats(TestLoadBalancer):
def setUp(self):
super(TestLoadBalancerStats, self).setUp()
lb_stats_info = {'stats': {'bytes_in': '0'}}
self.api_mock.load_balancer_stats_show.return_value = {
'stats': lb_stats_info['stats']}
lb_client = self.app.client_manager
lb_client.load_balancer = self.api_mock
self.cmd = load_balancer.ShowLoadBalancerStats(self.app, None)
def test_load_balancer_stats_show(self):
arglist = [self._lb.id]
verifylist = [
('loadbalancer', self._lb.id),
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
self.cmd.take_action(parsed_args)
self.api_mock.load_balancer_stats_show.assert_called_with(
lb_id=self._lb.id)
class TestLoadBalancerFailover(TestLoadBalancer):
def setUp(self):
super(TestLoadBalancerFailover, self).setUp()
self.cmd = load_balancer.FailoverLoadBalancer(self.app, None)
def test_load_balancer_failover(self):
arglist = [self._lb.id]
verifylist = [
('loadbalancer', self._lb.id)
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
self.cmd.take_action(parsed_args)
self.api_mock.load_balancer_failover.assert_called_with(
lb_id=self._lb.id)
|