Now that we no longer support py27, we can use the standard library
unittest.mock module instead of the third party mock lib.
The remainder was auto-generated with the following (hacky) script, with
one or two manual tweaks after the fact:
import glob
for path in glob.glob('watcher/tests/**/*.py', recursive=True):
with open(path) as fh:
lines = fh.readlines()
if 'import mock\n' not in lines:
continue
import_group_found = False
create_first_party_group = False
for num, line in enumerate(lines):
line = line.strip()
if line.startswith('import ') or line.startswith('from '):
tokens = line.split()
for lib in (
'ddt', 'six', 'webob', 'fixtures', 'testtools'
'neutron', 'cinder', 'ironic', 'keystone', 'oslo',
):
if lib in tokens[1]:
create_first_party_group = True
break
if create_first_party_group:
break
import_group_found = True
if not import_group_found:
continue
if line.startswith('import ') or line.startswith('from '):
tokens = line.split()
if tokens[1] > 'unittest':
break
elif tokens[1] == 'unittest' and (
len(tokens) == 2 or tokens[4] > 'mock'
):
break
elif not line:
break
if create_first_party_group:
lines.insert(num, 'from unittest import mock\n\n')
else:
lines.insert(num, 'from unittest import mock\n')
del lines[lines.index('import mock\n')]
with open(path, 'w+') as fh:
fh.writelines(lines)
Co-Authored-By: Sean McGinnis <sean.mcginnis@gmail.com>
Change-Id: Icf35d3a6c10c529e07d1a4edaa36f504e5bf553a
93 lines
3.4 KiB
Python
93 lines
3.4 KiB
Python
# -*- encoding: 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.
|
|
|
|
import jsonschema
|
|
from unittest import mock
|
|
|
|
from watcher.applier.actions import base as baction
|
|
from watcher.applier.actions import resize
|
|
from watcher.common import clients
|
|
from watcher.common import nova_helper
|
|
from watcher.tests import base
|
|
|
|
|
|
class TestResize(base.TestCase):
|
|
|
|
INSTANCE_UUID = "94ae2f92-b7fd-4da7-9e97-f13504ae98c4"
|
|
|
|
def setUp(self):
|
|
super(TestResize, self).setUp()
|
|
|
|
self.r_osc_cls = mock.Mock()
|
|
self.r_helper_cls = mock.Mock()
|
|
self.r_helper = mock.Mock(spec=nova_helper.NovaHelper)
|
|
self.r_helper_cls.return_value = self.r_helper
|
|
self.r_osc = mock.Mock(spec=clients.OpenStackClients)
|
|
self.r_osc_cls.return_value = self.r_osc
|
|
|
|
r_openstack_clients = mock.patch.object(
|
|
clients, "OpenStackClients", self.r_osc_cls)
|
|
r_nova_helper = mock.patch.object(
|
|
nova_helper, "NovaHelper", self.r_helper_cls)
|
|
|
|
r_openstack_clients.start()
|
|
r_nova_helper.start()
|
|
|
|
self.addCleanup(r_openstack_clients.stop)
|
|
self.addCleanup(r_nova_helper.stop)
|
|
|
|
self.input_parameters = {
|
|
"flavor": "x1",
|
|
baction.BaseAction.RESOURCE_ID: self.INSTANCE_UUID,
|
|
}
|
|
self.action = resize.Resize(mock.Mock())
|
|
self.action.input_parameters = self.input_parameters
|
|
|
|
def test_parameters(self):
|
|
params = {baction.BaseAction.RESOURCE_ID:
|
|
self.INSTANCE_UUID,
|
|
self.action.FLAVOR: 'x1'}
|
|
self.action.input_parameters = params
|
|
self.assertTrue(self.action.validate_parameters())
|
|
|
|
def test_parameters_exception_empty_fields(self):
|
|
parameters = {baction.BaseAction.RESOURCE_ID:
|
|
self.INSTANCE_UUID,
|
|
self.action.FLAVOR: None}
|
|
self.action.input_parameters = parameters
|
|
self.assertRaises(jsonschema.ValidationError,
|
|
self.action.validate_parameters)
|
|
|
|
def test_parameters_exception_flavor(self):
|
|
parameters = {baction.BaseAction.RESOURCE_ID:
|
|
self.INSTANCE_UUID,
|
|
self.action.FLAVOR: None}
|
|
self.action.input_parameters = parameters
|
|
self.assertRaises(jsonschema.ValidationError,
|
|
self.action.validate_parameters)
|
|
|
|
def test_parameters_exception_resource_id(self):
|
|
parameters = {baction.BaseAction.RESOURCE_ID: "EFEF",
|
|
self.action.FLAVOR: 'x1'}
|
|
self.action.input_parameters = parameters
|
|
self.assertRaises(jsonschema.ValidationError,
|
|
self.action.validate_parameters)
|
|
|
|
def test_execute_resize(self):
|
|
self.r_helper.find_instance.return_value = self.INSTANCE_UUID
|
|
self.action.execute()
|
|
self.r_helper.resize_instance.assert_called_once_with(
|
|
instance_id=self.INSTANCE_UUID, flavor='x1')
|