Merge "Debugging information when dpdk bind fails should be improved"
[yardstick.git] / yardstick / tests / fixture.py
1 # Copyright 2017 Intel Corporation
2 # All Rights Reserved.
3 #
4 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
5 #    not use this file except in compliance with the License. You may obtain
6 #    a copy of the License at
7 #
8 #         http://www.apache.org/licenses/LICENSE-2.0
9 #
10 #    Unless required by applicable law or agreed to in writing, software
11 #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 #    License for the specific language governing permissions and limitations
14 #    under the License.
15
16 import fixtures
17 import mock
18 import six
19
20 from yardstick.common import task_template
21
22
23 class PluginParserFixture(fixtures.Fixture):
24     """PluginParser fixture.
25
26     This class is intended to be used as a fixture within unit tests and
27     therefore consumers must register it using useFixture() within their
28     unit test class.
29     """
30
31     def __init__(self, rendered_plugin):
32         super(PluginParserFixture, self).__init__()
33         self._rendered_plugin = rendered_plugin
34
35     def _setUp(self):
36         self.addCleanup(self._restore)
37         self._mock_tasktemplate_render = mock.patch.object(
38             task_template.TaskTemplate, 'render')
39         self.mock_tasktemplate_render = self._mock_tasktemplate_render.start()
40         self.mock_tasktemplate_render.return_value = self._rendered_plugin
41         self._mock_open = mock.patch.object(six.moves.builtins, 'open', create=True)
42         self.mock_open = self._mock_open.start()
43         self.mock_open.side_effect = mock.mock_open()
44
45     def _restore(self):
46         self._mock_tasktemplate_render.stop()
47         self._mock_open.stop()