Merge "Sample VNF version need to be updated to Fraser"
[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 six
16 import testtools
17
18 from yardstick import ssh
19 from yardstick.benchmark.core import plugin
20 from yardstick.tests import fixture
21
22
23 class PluginTestCase(testtools.TestCase):
24
25     FILE = """
26 schema: "yardstick:plugin:0.1"
27
28 plugins:
29     name: sample
30
31 deployment:
32     ip: 10.1.0.50
33     user: root
34     password: root
35 """
36
37     NAME = 'sample'
38     DEPLOYMENT = {'ip': '10.1.0.50', 'user': 'root', 'password': 'root'}
39
40     def setUp(self):
41         super(PluginTestCase, self).setUp()
42         self.plugin_parser = plugin.PluginParser(mock.Mock())
43         self.plugin = plugin.Plugin()
44         self.useFixture(fixture.PluginParserFixture(PluginTestCase.FILE))
45
46         self._mock_ssh_from_node = mock.patch.object(ssh.SSH, 'from_node')
47         self.mock_ssh_from_node = self._mock_ssh_from_node.start()
48         self.mock_ssh_obj = mock.Mock()
49         self.mock_ssh_from_node.return_value = self.mock_ssh_obj
50         self.mock_ssh_obj.wait = mock.Mock()
51         self.mock_ssh_obj._put_file_shell = mock.Mock()
52         self._mock_log_info = mock.patch.object(plugin.LOG, 'info')
53         self.mock_log_info = self._mock_log_info.start()
54
55         self.addCleanup(self._cleanup)
56
57     def _cleanup(self):
58         self._mock_ssh_from_node.stop()
59         self._mock_log_info.stop()
60
61     @mock.patch.object(six.moves.builtins, 'print')
62     def test_install(self, *args):
63         args = mock.Mock()
64         args.input_file = [mock.Mock()]
65         with mock.patch.object(self.plugin, '_install_setup') as \
66                 mock_install, \
67                 mock.patch.object(self.plugin, '_run') as mock_run:
68             self.plugin.install(args)
69             mock_install.assert_called_once_with(PluginTestCase.NAME,
70                                                  PluginTestCase.DEPLOYMENT)
71             mock_run.assert_called_once_with(PluginTestCase.NAME)
72
73     @mock.patch.object(six.moves.builtins, 'print')
74     def test_remove(self, *args):
75         args = mock.Mock()
76         args.input_file = [mock.Mock()]
77         with mock.patch.object(self.plugin, '_remove_setup') as \
78                 mock_remove, \
79                 mock.patch.object(self.plugin, '_run') as mock_run:
80             self.plugin.remove(args)
81             mock_remove.assert_called_once_with(PluginTestCase.NAME,
82                                                 PluginTestCase.DEPLOYMENT)
83             mock_run.assert_called_once_with(PluginTestCase.NAME)
84
85     @mock.patch.object(pkg_resources, 'resource_filename',
86                        return_value='script')
87     def test__install_setup(self, mock_resource_filename):
88         plugin_name = 'plugin_name'
89         self.plugin._install_setup(plugin_name, PluginTestCase.DEPLOYMENT)
90         mock_resource_filename.assert_called_once_with(
91             'yardstick.resources', 'scripts/install/' + plugin_name + '.bash')
92         self.mock_ssh_from_node.assert_called_once_with(
93             PluginTestCase.DEPLOYMENT)
94         self.mock_ssh_obj.wait.assert_called_once_with(timeout=600)
95         self.mock_ssh_obj._put_file_shell.assert_called_once_with(
96             'script', '~/{0}.sh'.format(plugin_name))
97
98     @mock.patch.object(pkg_resources, 'resource_filename',
99                        return_value='script')
100     @mock.patch.object(os, 'environ', return_value='1.2.3.4')
101     def test__install_setup_with_ip_local(self, mock_os_environ,
102                                           mock_resource_filename):
103         plugin_name = 'plugin_name'
104         deployment = copy.deepcopy(PluginTestCase.DEPLOYMENT)
105         deployment['ip'] = 'local'
106         self.plugin._install_setup(plugin_name, deployment)
107         mock_os_environ.__getitem__.assert_called_once_with('JUMP_HOST_IP')
108         mock_resource_filename.assert_called_once_with(
109             'yardstick.resources',
110             'scripts/install/' + plugin_name + '.bash')
111         self.mock_ssh_from_node.assert_called_once_with(
112             deployment, overrides={'ip': os.environ["JUMP_HOST_IP"]})
113         self.mock_ssh_obj.wait.assert_called_once_with(timeout=600)
114         self.mock_ssh_obj._put_file_shell.assert_called_once_with(
115             'script', '~/{0}.sh'.format(plugin_name))
116
117     @mock.patch.object(pkg_resources, 'resource_filename',
118                        return_value='script')
119     def test__remove_setup(self, mock_resource_filename):
120         plugin_name = 'plugin_name'
121         self.plugin._remove_setup(plugin_name, PluginTestCase.DEPLOYMENT)
122         mock_resource_filename.assert_called_once_with(
123             'yardstick.resources',
124             'scripts/remove/' + plugin_name + '.bash')
125         self.mock_ssh_from_node.assert_called_once_with(
126             PluginTestCase.DEPLOYMENT)
127         self.mock_ssh_obj.wait.assert_called_once_with(timeout=600)
128         self.mock_ssh_obj._put_file_shell.assert_called_once_with(
129             'script', '~/{0}.sh'.format(plugin_name))
130
131     @mock.patch.object(pkg_resources, 'resource_filename',
132                        return_value='script')
133     @mock.patch.object(os, 'environ', return_value='1.2.3.4')
134     def test__remove_setup_with_ip_local(self, mock_os_environ,
135                                          mock_resource_filename):
136         plugin_name = 'plugin_name'
137         deployment = copy.deepcopy(PluginTestCase.DEPLOYMENT)
138         deployment['ip'] = 'local'
139         self.plugin._remove_setup(plugin_name, deployment)
140         mock_os_environ.__getitem__.assert_called_once_with('JUMP_HOST_IP')
141         mock_resource_filename.assert_called_once_with(
142             'yardstick.resources',
143             'scripts/remove/' + plugin_name + '.bash')
144         self.mock_ssh_from_node.assert_called_once_with(
145             deployment, overrides={'ip': os.environ["JUMP_HOST_IP"]})
146         self.mock_ssh_obj.wait.assert_called_once_with(timeout=600)
147         self.mock_ssh_obj._put_file_shell.mock_os_environ(
148             'script', '~/{0}.sh'.format(plugin_name))