Merge "Module to manage pip packages"
[yardstick.git] / yardstick / tests / unit / benchmark / core / test_plugin.py
1 ##############################################################################
2 # Copyright (c) 2016 Huawei Technologies Co.,Ltd and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10 import copy
11 import os
12 import pkg_resources
13
14 import mock
15 import testtools
16
17 from yardstick import ssh
18 from yardstick.benchmark.core import plugin
19 from yardstick.tests import fixture
20
21
22 class PluginTestCase(testtools.TestCase):
23
24     FILE = """
25 schema: "yardstick:plugin:0.1"
26
27 plugins:
28     name: sample
29
30 deployment:
31     ip: 10.1.0.50
32     user: root
33     password: root
34 """
35
36     NAME = 'sample'
37     DEPLOYMENT = {'ip': '10.1.0.50', 'user': 'root', 'password': 'root'}
38
39     def setUp(self):
40         super(PluginTestCase, self).setUp()
41         self.plugin_parser = plugin.PluginParser(mock.Mock())
42         self.plugin = plugin.Plugin()
43         self.useFixture(fixture.PluginParserFixture(PluginTestCase.FILE))
44
45         self._mock_ssh_from_node = mock.patch.object(ssh.SSH, 'from_node')
46         self.mock_ssh_from_node = self._mock_ssh_from_node.start()
47         self.mock_ssh_obj = mock.Mock()
48         self.mock_ssh_from_node.return_value = self.mock_ssh_obj
49         self.mock_ssh_obj.wait = mock.Mock()
50         self.mock_ssh_obj._put_file_shell = mock.Mock()
51
52         self.addCleanup(self._cleanup)
53
54     def _cleanup(self):
55         self._mock_ssh_from_node.stop()
56
57     def test_install(self):
58         args = mock.Mock()
59         args.input_file = [mock.Mock()]
60         with mock.patch.object(self.plugin, '_install_setup') as \
61                 mock_install, \
62                 mock.patch.object(self.plugin, '_run') as mock_run:
63             self.plugin.install(args)
64             mock_install.assert_called_once_with(PluginTestCase.NAME,
65                                                  PluginTestCase.DEPLOYMENT)
66             mock_run.assert_called_once_with(PluginTestCase.NAME)
67
68     def test_remove(self):
69         args = mock.Mock()
70         args.input_file = [mock.Mock()]
71         with mock.patch.object(self.plugin, '_remove_setup') as \
72                 mock_remove, \
73                 mock.patch.object(self.plugin, '_run') as mock_run:
74             self.plugin.remove(args)
75             mock_remove.assert_called_once_with(PluginTestCase.NAME,
76                                                 PluginTestCase.DEPLOYMENT)
77             mock_run.assert_called_once_with(PluginTestCase.NAME)
78
79     @mock.patch.object(pkg_resources, 'resource_filename',
80                        return_value='script')
81     def test__install_setup(self, mock_resource_filename):
82         plugin_name = 'plugin_name'
83         self.plugin._install_setup(plugin_name, PluginTestCase.DEPLOYMENT)
84         mock_resource_filename.assert_called_once_with(
85             'yardstick.resources', 'scripts/install/' + plugin_name + '.bash')
86         self.mock_ssh_from_node.assert_called_once_with(
87             PluginTestCase.DEPLOYMENT)
88         self.mock_ssh_obj.wait.assert_called_once_with(timeout=600)
89         self.mock_ssh_obj._put_file_shell.assert_called_once_with(
90             'script', '~/{0}.sh'.format(plugin_name))
91
92     @mock.patch.object(pkg_resources, 'resource_filename',
93                        return_value='script')
94     @mock.patch.object(os, 'environ', return_value='1.2.3.4')
95     def test__install_setup_with_ip_local(self, mock_os_environ,
96                                           mock_resource_filename):
97         plugin_name = 'plugin_name'
98         deployment = copy.deepcopy(PluginTestCase.DEPLOYMENT)
99         deployment['ip'] = 'local'
100         self.plugin._install_setup(plugin_name, deployment)
101         mock_os_environ.__getitem__.assert_called_once_with('JUMP_HOST_IP')
102         mock_resource_filename.assert_called_once_with(
103             'yardstick.resources',
104             'scripts/install/' + plugin_name + '.bash')
105         self.mock_ssh_from_node.assert_called_once_with(
106             deployment, overrides={'ip': os.environ["JUMP_HOST_IP"]})
107         self.mock_ssh_obj.wait.assert_called_once_with(timeout=600)
108         self.mock_ssh_obj._put_file_shell.assert_called_once_with(
109             'script', '~/{0}.sh'.format(plugin_name))
110
111     @mock.patch.object(pkg_resources, 'resource_filename',
112                        return_value='script')
113     def test__remove_setup(self, mock_resource_filename):
114         plugin_name = 'plugin_name'
115         self.plugin._remove_setup(plugin_name, PluginTestCase.DEPLOYMENT)
116         mock_resource_filename.assert_called_once_with(
117             'yardstick.resources',
118             'scripts/remove/' + plugin_name + '.bash')
119         self.mock_ssh_from_node.assert_called_once_with(
120             PluginTestCase.DEPLOYMENT)
121         self.mock_ssh_obj.wait.assert_called_once_with(timeout=600)
122         self.mock_ssh_obj._put_file_shell.assert_called_once_with(
123             'script', '~/{0}.sh'.format(plugin_name))
124
125     @mock.patch.object(pkg_resources, 'resource_filename',
126                        return_value='script')
127     @mock.patch.object(os, 'environ', return_value='1.2.3.4')
128     def test__remove_setup_with_ip_local(self, mock_os_environ,
129                                          mock_resource_filename):
130         plugin_name = 'plugin_name'
131         deployment = copy.deepcopy(PluginTestCase.DEPLOYMENT)
132         deployment['ip'] = 'local'
133         self.plugin._remove_setup(plugin_name, deployment)
134         mock_os_environ.__getitem__.assert_called_once_with('JUMP_HOST_IP')
135         mock_resource_filename.assert_called_once_with(
136             'yardstick.resources',
137             'scripts/remove/' + plugin_name + '.bash')
138         self.mock_ssh_from_node.assert_called_once_with(
139             deployment, overrides={'ip': os.environ["JUMP_HOST_IP"]})
140         self.mock_ssh_obj.wait.assert_called_once_with(timeout=600)
141         self.mock_ssh_obj._put_file_shell.mock_os_environ(
142             'script', '~/{0}.sh'.format(plugin_name))