rally/tests/benchmark/test_test_engine.py
Mikhail Dubov 0e3ddbfcba Fixing incorrect mocks usage in unit tests
This patch fixes some incorrect mocks usage in the unit tests for Rally.
Those mocks were initialized in a wrong way (via the start() method in the
test case setUp()) which presented an opportunity for a race condition.
Here, this get replaced with "with ... as ..." blocks.

Change-Id: Ifad16ee6454abcb42e05bb187842a6682d7924db
Closes-Bug: #1236725
2013-10-08 15:36:29 +04:00

141 lines
5.2 KiB
Python

# vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright 2013: Mirantis Inc.
# 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.
"""Tests for the Test engine."""
import json
import mock
import os
from rally.benchmark import engine
from rally import consts
from rally import exceptions
from rally import test
class TestEngineTestCase(test.NoDBTestCase):
def setUp(self):
super(TestEngineTestCase, self).setUp()
self.valid_test_config = {
'verify': {
'tests_to_run': ['sanity', 'smoke']
},
'benchmark': {
'tests_to_run': {}
}
}
self.invalid_test_config_bad_test_name = {
'verify': {
'tests_to_run': ['sanity', 'some_not_existing_test']
},
'benchmark': {}
}
self.invalid_test_config_bad_key = {
'verify': {
'tests_to_run': ['sanity', 'smoke']
},
'benchmarck': {}
}
self.valid_cloud_config = {
'identity': {
'admin_name': 'admin',
'admin_password': 'admin'
},
'compute': {
'controller_nodes': 'localhost'
}
}
self.run_success = {
'proc': {'msg': ['msg'], 'status': 0, 'proc_name': 'proc'}
}
def test_verify_test_config(self):
try:
engine.TestEngine(self.valid_test_config, mock.MagicMock())
except Exception as e:
self.fail("Unexpected exception in test config" +
"verification: %s" % str(e))
self.assertRaises(exceptions.NoSuchTestException,
engine.TestEngine,
self.invalid_test_config_bad_test_name,
mock.MagicMock())
self.assertRaises(exceptions.InvalidConfigException,
engine.TestEngine,
self.invalid_test_config_bad_key,
mock.MagicMock())
def test_bind(self):
test_engine = engine.TestEngine(self.valid_test_config,
mock.MagicMock())
with test_engine.bind(self.valid_cloud_config):
self.assertTrue(os.path.exists(test_engine.cloud_config_path))
self.assertTrue(os.path.exists(test_engine.test_config_path))
self.assertFalse(os.path.exists(test_engine.cloud_config_path))
self.assertFalse(os.path.exists(test_engine.test_config_path))
def test_verify(self):
test_engine = engine.TestEngine(self.valid_test_config,
mock.MagicMock())
with mock.patch('rally.benchmark.utils.Verifier.run') as mock_run:
mock_run.return_value = self.run_success
with test_engine.bind(self.valid_cloud_config):
try:
test_engine.verify()
except Exception as e:
self.fail("Exception in TestEngine.verify: %s" % str(e))
def test_benchmark(self):
test_engine = engine.TestEngine(self.valid_test_config,
mock.MagicMock())
with test_engine.bind(self.valid_cloud_config):
test_engine.benchmark()
def test_task_status_basic_chain(self):
fake_task = mock.MagicMock()
test_engine = engine.TestEngine(self.valid_test_config, fake_task)
with mock.patch('rally.benchmark.utils.Verifier.run') as mock_run:
mock_run.return_value = self.run_success
with test_engine.bind(self.valid_cloud_config):
test_engine.verify()
test_engine.benchmark()
s = consts.TaskStatus
expected = [
mock.call.update_status(s.TEST_TOOL_VERIFY_OPENSTACK),
mock.call.update_verification_log(json.dumps(
[self.run_success, self.run_success])),
mock.call.update_status(s.TEST_TOOL_BENCHMARKING)
]
# NOTE(msdubov): Ignore task['uuid'] calls which are used for logging
mock_calls = filter(lambda call: '__getitem__' not in call[0],
fake_task.mock_calls)
self.assertEqual(mock_calls, expected)
def test_task_status_invalid_config(self):
fake_task = mock.MagicMock()
try:
engine.TestEngine(self.invalid_test_config_bad_key, fake_task)
except exceptions.InvalidConfigException:
pass
expected = []
# NOTE(msdubov): Ignore task['uuid'] calls which are used for logging
mock_calls = filter(lambda call: '__getitem__' not in call[0],
fake_task.mock_calls)
self.assertEqual(mock_calls, expected)