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
138 lines
5.3 KiB
Python
138 lines
5.3 KiB
Python
# -*- encoding: utf-8 -*-
|
|
# Copyright (c) 2016 b<>com
|
|
#
|
|
# Authors: Vincent FRANCOISE <vincent.francoise@b-com.com>
|
|
#
|
|
# 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 apscheduler.schedulers import background
|
|
from apscheduler.triggers import interval as interval_trigger
|
|
import eventlet
|
|
from unittest import mock
|
|
|
|
from oslo_config import cfg
|
|
from oslo_utils import uuidutils
|
|
|
|
from watcher.decision_engine.loading import default as default_loading
|
|
from watcher.decision_engine import scheduling
|
|
from watcher.decision_engine.strategy.strategies import dummy_strategy
|
|
from watcher import notifications
|
|
from watcher import objects
|
|
from watcher.tests import base
|
|
from watcher.tests.db import base as db_base
|
|
from watcher.tests.decision_engine.model import faker_cluster_state
|
|
from watcher.tests.objects import utils as obj_utils
|
|
|
|
|
|
class TestCancelOngoingAudits(db_base.DbTestCase):
|
|
|
|
def setUp(self):
|
|
super(TestCancelOngoingAudits, 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)
|
|
cfg.CONF.set_override("host", "hostname1")
|
|
|
|
@mock.patch.object(objects.audit.Audit, 'save')
|
|
@mock.patch.object(objects.audit.Audit, 'list')
|
|
def test_cancel_ongoing_audits(self, m_list, m_save):
|
|
m_list.return_value = [self.audit]
|
|
scheduler = scheduling.DecisionEngineSchedulingService()
|
|
|
|
scheduler.cancel_ongoing_audits()
|
|
m_list.assert_called()
|
|
m_save.assert_called()
|
|
self.assertEqual(self.audit.state, objects.audit.State.CANCELLED)
|
|
|
|
|
|
@mock.patch.object(objects.audit.Audit, 'save')
|
|
@mock.patch.object(objects.audit.Audit, 'list')
|
|
class TestDecisionEngineSchedulingService(base.TestCase):
|
|
|
|
@mock.patch.object(
|
|
default_loading.ClusterDataModelCollectorLoader, 'load')
|
|
@mock.patch.object(
|
|
default_loading.ClusterDataModelCollectorLoader, 'list_available')
|
|
@mock.patch.object(background.BackgroundScheduler, 'start')
|
|
def test_start_de_scheduling_service(self, m_start, m_list_available,
|
|
m_load, m_list, m_save):
|
|
m_list_available.return_value = {
|
|
'fake': faker_cluster_state.FakerModelCollector}
|
|
fake_collector = faker_cluster_state.FakerModelCollector(
|
|
config=mock.Mock(period=777))
|
|
m_load.return_value = fake_collector
|
|
|
|
scheduler = scheduling.DecisionEngineSchedulingService()
|
|
|
|
scheduler.start()
|
|
|
|
m_start.assert_called_once_with(scheduler)
|
|
jobs = scheduler.get_jobs()
|
|
self.assertEqual(2, len(jobs))
|
|
|
|
job = jobs[0]
|
|
self.assertTrue(bool(fake_collector.cluster_data_model))
|
|
|
|
self.assertIsInstance(job.trigger, interval_trigger.IntervalTrigger)
|
|
|
|
@mock.patch.object(
|
|
default_loading.ClusterDataModelCollectorLoader, 'load')
|
|
@mock.patch.object(
|
|
default_loading.ClusterDataModelCollectorLoader, 'list_available')
|
|
@mock.patch.object(background.BackgroundScheduler, 'start')
|
|
def test_execute_sync_job_fails(self, m_start, m_list_available,
|
|
m_load, m_list, m_save):
|
|
fake_config = mock.Mock(period=.01)
|
|
fake_collector = faker_cluster_state.FakerModelCollector(
|
|
config=fake_config)
|
|
fake_collector.synchronize = mock.Mock(
|
|
side_effect=lambda: eventlet.sleep(.5))
|
|
m_list_available.return_value = {
|
|
'fake': faker_cluster_state.FakerModelCollector}
|
|
m_load.return_value = fake_collector
|
|
|
|
scheduler = scheduling.DecisionEngineSchedulingService()
|
|
|
|
scheduler.start()
|
|
|
|
m_start.assert_called_once_with(scheduler)
|
|
jobs = scheduler.get_jobs()
|
|
self.assertEqual(2, len(jobs))
|
|
|
|
job = jobs[0]
|
|
job.func()
|
|
self.assertFalse(bool(fake_collector.cluster_data_model))
|
|
|
|
self.assertIsInstance(job.trigger, interval_trigger.IntervalTrigger)
|