aboutsummaryrefslogtreecommitdiffstats
path: root/yardstick/tests/unit/benchmark/runner/test_base.py
blob: 49ba1efe450acdf123f412eec588b7753ec1763f (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
149
150
151
152
##############################################################################
# Copyright (c) 2017 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 time
import uuid

import mock
from oslo_config import cfg
import oslo_messaging
import subprocess

from yardstick.benchmark.runners import base as runner_base
from yardstick.benchmark.runners import iteration
from yardstick.common import messaging
from yardstick.common.messaging import payloads
from yardstick.tests.unit import base as ut_base


class ActionTestCase(ut_base.BaseUnitTestCase):

    def setUp(self):
        self._mock_log = mock.patch.object(runner_base.log, 'error')
        self.mock_log = self._mock_log.start()
        self.addCleanup(self._stop_mocks)

    def _stop_mocks(self):
        self._mock_log.stop()

    @mock.patch.object(subprocess, 'check_output')
    def test__execute_shell_command(self, mock_subprocess):
        mock_subprocess.side_effect = subprocess.CalledProcessError(-1, '')
        self.assertEqual(runner_base._execute_shell_command("")[0], -1)

    @mock.patch.object(subprocess, 'check_output')
    def test__single_action(self, mock_subprocess):
        mock_subprocess.side_effect = subprocess.CalledProcessError(-1, '')
        runner_base._single_action(0, 'echo', mock.Mock())

    @mock.patch.object(subprocess, 'check_output')
    def test__periodic_action(self, mock_subprocess):
        mock_subprocess.side_effect = subprocess.CalledProcessError(-1, '')
        runner_base._periodic_action(0, 'echo', mock.Mock())


class RunnerTestCase(ut_base.BaseUnitTestCase):

    def setUp(self):
        config = {
            'output_config': {
                'DEFAULT': {
                    'dispatcher': 'file'
                }
            }
        }
        self.runner = iteration.IterationRunner(config)

    @mock.patch("yardstick.benchmark.runners.iteration.multiprocessing")
    def test_get_output(self, *args):
        self.runner.output_queue.put({'case': 'opnfv_yardstick_tc002'})
        self.runner.output_queue.put({'criteria': 'PASS'})

        idle_result = {
            'case': 'opnfv_yardstick_tc002',
            'criteria': 'PASS'
        }

        for _ in range(1000):
            time.sleep(0.01)
            if not self.runner.output_queue.empty():
                break
        actual_result = self.runner.get_output()
        self.assertEqual(idle_result, actual_result)

    @mock.patch("yardstick.benchmark.runners.iteration.multiprocessing")
    def test_get_result(self, *args):
        self.runner.result_queue.put({'case': 'opnfv_yardstick_tc002'})
        self.runner.result_queue.put({'criteria': 'PASS'})

        idle_result = [
            {'case': 'opnfv_yardstick_tc002'},
            {'criteria': 'PASS'}
        ]

        for _ in range(1000):
            time.sleep(0.01)
            if not self.runner.result_queue.empty():
                break
        actual_result = self.runner.get_result()
        self.assertEqual(idle_result, actual_result)

    def test__run_benchmark(self):
        runner = runner_base.Runner(mock.Mock())

        with self.assertRaises(NotImplementedError):
            runner._run_benchmark(mock.Mock(), mock.Mock(), mock.Mock(), mock.Mock())


class RunnerProducerTestCase(ut_base.BaseUnitTestCase):

    @mock.patch.object(oslo_messaging, 'Target', return_value='rpc_target')
    @mock.patch.object(oslo_messaging, 'RPCClient')
    @mock.patch.object(oslo_messaging, 'get_rpc_transport',
                       return_value='rpc_transport')
    @mock.patch.object(cfg, 'CONF')
    def test__init(self, mock_config, mock_transport, mock_rpcclient,
                   mock_target):
        _id = uuid.uuid1().int
        runner_producer = runner_base.RunnerProducer(_id)
        mock_transport.assert_called_once_with(
            mock_config, url='rabbit://yardstick:yardstick@localhost:5672/')
        mock_target.assert_called_once_with(topic=messaging.TOPIC_RUNNER,
                                            fanout=True,
                                            server=messaging.SERVER)
        mock_rpcclient.assert_called_once_with('rpc_transport', 'rpc_target')
        self.assertEqual(_id, runner_producer._id)
        self.assertEqual(messaging.TOPIC_RUNNER, runner_producer._topic)

    @mock.patch.object(oslo_messaging, 'Target', return_value='rpc_target')
    @mock.patch.object(oslo_messaging, 'RPCClient')
    @mock.patch.object(oslo_messaging, 'get_rpc_transport',
                       return_value='rpc_transport')
    @mock.patch.object(payloads, 'RunnerPayload', return_value='runner_pload')
    def test_start_iteration(self, mock_runner_payload, *args):
        runner_producer = runner_base.RunnerProducer(uuid.uuid1().int)
        with mock.patch.object(runner_producer,
                               'send_message') as mock_message:
            runner_producer.start_iteration(version=10)

        mock_message.assert_called_once_with(
            messaging.RUNNER_METHOD_START_ITERATION, 'runner_pload')
        mock_runner_payload.assert_called_once_with(version=10, data={})

    @mock.patch.object(oslo_messaging, 'Target', return_value='rpc_target')
    @mock.patch.object(oslo_messaging, 'RPCClient')
    @mock.patch.object(oslo_messaging, 'get_rpc_transport',
                       return_value='rpc_transport')
    @mock.patch.object(payloads, 'RunnerPayload', return_value='runner_pload')
    def test_stop_iteration(self, mock_runner_payload, *args):
        runner_producer = runner_base.RunnerProducer(uuid.uuid1().int)
        with mock.patch.object(runner_producer,
                               'send_message') as mock_message:
            runner_producer.stop_iteration(version=15)

        mock_message.assert_called_once_with(
            messaging.RUNNER_METHOD_STOP_ITERATION, 'runner_pload')
        mock_runner_payload.assert_called_once_with(version=15, data={})