aboutsummaryrefslogtreecommitdiffstats
path: root/yardstick/tests/unit/benchmark/core/test_plugin.py
blob: 53621316be7b0d7f410bfe02cdb0b045d9c6a6f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
##############################################################################
# Copyright (c) 2016 Huawei Technologies Co.,Ltd and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################

import copy
import os
import pkg_resources

import mock
import six
import testtools

from yardstick import ssh
from yardstick.benchmark.core import plugin
from yardstick.tests import fixture


class PluginTestCase(testtools.TestCase):

    FILE = """
schema: "yardstick:plugin:0.1"

plugins:
    name: sample

deployment:
    ip: 10.1.0.50
    user: root
    password: root
"""

    NAME = 'sample'
    DEPLOYMENT = {'ip': '10.1.0.50', 'user': 'root', 'password': 'root'}

    def setUp(self):
        super(PluginTestCase, self).setUp()
        self.plugin_parser = plugin.PluginParser(mock.Mock())
        self.plugin = plugin.Plugin()
        self.useFixture(fixture.PluginParserFixture(PluginTestCase.FILE))

        self._mock_ssh_from_node = mock.patch.object(ssh.SSH, 'from_node')
        self.mock_ssh_from_node = self._mock_ssh_from_node.start()
        self.mock_ssh_obj = mock.Mock()
        self.mock_ssh_from_node.return_value = self.mock_ssh_obj
        self.mock_ssh_obj.wait = mock.Mock()
        self.mock_ssh_obj._put_file_shell = mock.Mock()
        self._mock_log_info = mock.patch.object(plugin.LOG, 'info')
        self.mock_log_info = self._mock_log_info.start()

        self.addCleanup(self._cleanup)

    def _cleanup(self):
        self._mock_ssh_from_node.stop()
        self._mock_log_info.stop()

    @mock.patch.object(six.moves.builtins, 'print')
    def test_install(self, *args):
        args = mock.Mock()
        args.input_file = [mock.Mock()]
        with mock.patch.object(self.plugin, '_install_setup') as \
                mock_install, \
                mock.patch.object(self.plugin, '_run') as mock_run:
            self.plugin.install(args)
            mock_install.assert_called_once_with(PluginTestCase.NAME,
                                                 PluginTestCase.DEPLOYMENT)
            mock_run.assert_called_once_with(PluginTestCase.NAME)

    @mock.patch.object(six.moves.builtins, 'print')
    def test_remove(self, *args):
        args = mock.Mock()
        args.input_file = [mock.Mock()]
        with mock.patch.object(self.plugin, '_remove_setup') as \
                mock_remove, \
                mock.patch.object(self.plugin, '_run') as mock_run:
            self.plugin.remove(args)
            mock_remove.assert_called_once_with(PluginTestCase.NAME,
                                                PluginTestCase.DEPLOYMENT)
            mock_run.assert_called_once_with(PluginTestCase.NAME)

    @mock.patch.object(pkg_resources, 'resource_filename',
                       return_value='script')
    def test__install_setup(self, mock_resource_filename):
        plugin_name = 'plugin_name'
        self.plugin._install_setup(plugin_name, PluginTestCase.DEPLOYMENT)
        mock_resource_filename.assert_called_once_with(
            'yardstick.resources', 'scripts/install/' + plugin_name + '.bash')
        self.mock_ssh_from_node.assert_called_once_with(
            PluginTestCase.DEPLOYMENT)
        self.mock_ssh_obj.wait.assert_called_once_with(timeout=600)
        self.mock_ssh_obj._put_file_shell.assert_called_once_with(
            'script', '~/{0}.sh'.format(plugin_name))

    @mock.patch.object(pkg_resources, 'resource_filename',
                       return_value='script')
    @mock.patch.object(os, 'environ', return_value='1.2.3.4')
    def test__install_setup_with_ip_local(self, mock_os_environ,
                                          mock_resource_filename):
        plugin_name = 'plugin_name'
        deployment = copy.deepcopy(PluginTestCase.DEPLOYMENT)
        deployment['ip'] = 'local'
        self.plugin._install_setup(plugin_name, deployment)
        mock_os_environ.__getitem__.assert_called_once_with('JUMP_HOST_IP')
        mock_resource_filename.assert_called_once_with(
            'yardstick.resources',
            'scripts/install/' + plugin_name + '.bash')
        self.mock_ssh_from_node.assert_called_once_with(
            deployment, overrides={'ip': os.environ["JUMP_HOST_IP"]})
        self.mock_ssh_obj.wait.assert_called_once_with(timeout=600)
        self.mock_ssh_obj._put_file_shell.assert_called_once_with(
            'script', '~/{0}.sh'.format(plugin_name))

    @mock.patch.object(pkg_resources, 'resource_filename',
                       return_value='script')
    def test__remove_setup(self, mock_resource_filename):
        plugin_name = 'plugin_name'
        self.plugin._remove_setup(plugin_name, PluginTestCase.DEPLOYMENT)
        mock_resource_filename.assert_called_once_with(
            'yardstick.resources',
            'scripts/remove/' + plugin_name + '.bash')
        self.mock_ssh_from_node.assert_called_once_with(
            PluginTestCase.DEPLOYMENT)
        self.mock_ssh_obj.wait.assert_called_once_with(timeout=600)
        self.mock_ssh_obj._put_file_shell.assert_called_once_with(
            'script', '~/{0}.sh'.format(plugin_name))

    @mock.patch.object(pkg_resources, 'resource_filename',
                       return_value='script')
    @mock.patch.object(os, 'environ', return_value='1.2.3.4')
    def test__remove_setup_with_ip_local(self, mock_os_environ,
                                         mock_resource_filename):
        plugin_name = 'plugin_name'
        deployment = copy.deepcopy(PluginTestCase.DEPLOYMENT)
        deployment['ip'] = 'local'
        self.plugin._remove_setup(plugin_name, deployment)
        mock_os_environ.__getitem__.assert_called_once_with('JUMP_HOST_IP')
        mock_resource_filename.assert_called_once_with(
            'yardstick.resources',
            'scripts/remove/' + plugin_name + '.bash')
        self.mock_ssh_from_node.assert_called_once_with(
            deployment, overrides={'ip': os.environ["JUMP_HOST_IP"]})
        self.mock_ssh_obj.wait.assert_called_once_with(timeout=600)
        self.mock_ssh_obj._put_file_shell.mock_os_environ(
            'script', '~/{0}.sh'.format(plugin_name))