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
87 lines
3.3 KiB
Python
87 lines
3.3 KiB
Python
# -*- encoding: utf-8 -*-
|
|
# Copyright (c) 2018 SBCloud
|
|
#
|
|
# Authors: Alexander Chadin <aschadin@sbcloud.ru>
|
|
#
|
|
# 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.
|
|
|
|
from unittest import mock
|
|
|
|
from oslo_config import cfg
|
|
from oslo_utils import uuidutils
|
|
|
|
from watcher.applier import sync
|
|
from watcher.decision_engine.strategy.strategies import dummy_strategy
|
|
from watcher.tests.db import base as db_base
|
|
|
|
from watcher import notifications
|
|
from watcher import objects
|
|
from watcher.tests.objects import utils as obj_utils
|
|
|
|
|
|
class TestCancelOngoingActionPlans(db_base.DbTestCase):
|
|
|
|
def setUp(self):
|
|
super(TestCancelOngoingActionPlans, self).setUp()
|
|
p_audit_notifications = mock.patch.object(
|
|
notifications, 'audit', autospec=True)
|
|
self.m_audit_notifications = p_audit_notifications.start()
|
|
self.addCleanup(p_audit_notifications.stop)
|
|
|
|
self.goal = obj_utils.create_test_goal(
|
|
self.context, id=1, name=dummy_strategy.DummyStrategy.get_name())
|
|
self.strategy = obj_utils.create_test_strategy(
|
|
self.context, name=dummy_strategy.DummyStrategy.get_name(),
|
|
goal_id=self.goal.id)
|
|
audit_template = obj_utils.create_test_audit_template(
|
|
self.context, strategy_id=self.strategy.id)
|
|
self.audit = obj_utils.create_test_audit(
|
|
self.context,
|
|
id=999,
|
|
name='My Audit 999',
|
|
uuid=uuidutils.generate_uuid(),
|
|
audit_template_id=audit_template.id,
|
|
goal_id=self.goal.id,
|
|
audit_type=objects.audit.AuditType.ONESHOT.value,
|
|
goal=self.goal,
|
|
hostname='hostname1',
|
|
state=objects.audit.State.ONGOING)
|
|
self.actionplan = obj_utils.create_test_action_plan(
|
|
self.context,
|
|
state=objects.action_plan.State.ONGOING,
|
|
audit_id=999,
|
|
hostname='hostname1')
|
|
self.action = obj_utils.create_test_action(
|
|
self.context,
|
|
action_plan_id=1,
|
|
state=objects.action.State.PENDING)
|
|
cfg.CONF.set_override("host", "hostname1")
|
|
|
|
@mock.patch.object(objects.action.Action, 'save')
|
|
@mock.patch.object(objects.action_plan.ActionPlan, 'save')
|
|
@mock.patch.object(objects.action.Action, 'list')
|
|
@mock.patch.object(objects.action_plan.ActionPlan, 'list')
|
|
def test_cancel_ongoing_actionplans(self, m_plan_list, m_action_list,
|
|
m_plan_save, m_action_save):
|
|
m_plan_list.return_value = [self.actionplan]
|
|
m_action_list.return_value = [self.action]
|
|
syncer = sync.Syncer()
|
|
|
|
syncer._cancel_ongoing_actionplans(self.context)
|
|
m_plan_list.assert_called()
|
|
m_action_list.assert_called()
|
|
m_plan_save.assert_called()
|
|
m_action_save.assert_called()
|
|
self.assertEqual(self.action.state, objects.audit.State.CANCELLED)
|