Merge "Refactor remote command execution in pktgen_dpdk"
[yardstick.git] / yardstick / tests / unit / test_cmd / test_NSBperf.py
1 # Copyright (c) 2016-2017 Intel Corporation
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import argparse
16 import os
17 import subprocess
18
19 import mock
20 from six.moves import builtins
21 import unittest
22
23 from yardstick.cmd import NSBperf
24
25
26 @mock.patch('six.moves.input', return_value='0')
27 class TestHandler(unittest.TestCase):
28
29     def test_handler(self, *args):
30         subprocess.call = mock.Mock(return_value=0)
31         self.assertRaises(SystemExit, NSBperf.sigint_handler)
32
33
34 class TestYardstickNSCli(unittest.TestCase):
35
36     def setUp(self):
37         self._mock_print = mock.patch.object(builtins, 'print')
38         self.mock_print = self._mock_print.start()
39         self.addCleanup(self._stop_mocks)
40
41     def _stop_mocks(self):
42         self._mock_print.stop()
43
44     def test___init__(self):
45         yardstick_ns_cli = NSBperf.YardstickNSCli()
46         self.assertIsNotNone(yardstick_ns_cli)
47
48     def test_generate_final_report(self):
49         yardstick_ns_cli = NSBperf.YardstickNSCli()
50         test_case = "tc_baremetal_rfc2544_ipv4_1flow_1518B.yaml"
51         if os.path.isfile("/tmp/yardstick.out"):
52             os.remove('/tmp/yardstick.out')
53         self.assertIsNone(yardstick_ns_cli.generate_final_report(test_case))
54
55     def test_generate_kpi_results(self):
56         yardstick_ns_cli = NSBperf.YardstickNSCli()
57         tkey = "cpu"
58         tgen = {"cpu": {"ipc": 0}}
59         self.assertIsNone(yardstick_ns_cli.generate_kpi_results(tkey, tgen))
60
61     def test_generate_nfvi_results(self):
62         yardstick_ns_cli = NSBperf.YardstickNSCli()
63         nfvi = {"collect_stats": {"cpu": {"ipc": 0, "Hz": 2.6}}}
64         self.assertIsNone(yardstick_ns_cli.generate_nfvi_results(nfvi))
65
66     def test_handle_list_options(self):
67         yardstick_ns_cli = NSBperf.YardstickNSCli()
68         CLI_PATH = os.path.dirname(os.path.realpath(__file__))
69         repo_dir = CLI_PATH + "/../../../"
70         test_path = os.path.join(repo_dir, "../samples/vnf_samples/nsut/")
71         args = {"list_vnfs": True, "list": False}
72         self.assertRaises(SystemExit, yardstick_ns_cli.handle_list_options,
73                           args, test_path)
74         args = {"list_vnfs": False, "list": True}
75         self.assertRaises(SystemExit,
76                           yardstick_ns_cli.handle_list_options,
77                           args, test_path)
78
79     def test_main(self):
80         yardstick_ns_cli = NSBperf.YardstickNSCli()
81         yardstick_ns_cli.parse_arguments = mock.Mock(return_value=0)
82         yardstick_ns_cli.handle_list_options = mock.Mock(return_value=0)
83         yardstick_ns_cli.terminate_if_less_options = mock.Mock(return_value=0)
84         yardstick_ns_cli.run_test = mock.Mock(return_value=0)
85         self.assertIsNone(yardstick_ns_cli.main())
86
87     @mock.patch.object(argparse.ArgumentParser, 'parse_args')
88     def test_parse_arguments(self, mock_parse):
89         class DummyArgs(object):
90             var1 = 'value1'
91
92         mock_parse.return_value = DummyArgs
93         yardstick_ns_cli = NSBperf.YardstickNSCli()
94         self.assertIn('var1', yardstick_ns_cli.parse_arguments())
95
96     def test_run_test(self):
97         cur_dir = os.getcwd()
98         CLI_PATH = os.path.dirname(os.path.realpath(__file__))
99         YARDSTICK_REPOS_DIR = os.path.join(CLI_PATH + "/../../")
100         test_path = os.path.join(YARDSTICK_REPOS_DIR,
101                                  "../samples/vnf_samples/nsut/")
102         yardstick_ns_cli = NSBperf.YardstickNSCli()
103         subprocess.check_output = mock.Mock(return_value=0)
104         args = {"vnf": "vpe",
105                 "test": "tc_baremetal_rfc2544_ipv4_1flow_1518B.yaml"}
106         self.assertIsNone(yardstick_ns_cli.run_test(args, test_path))
107         os.chdir(cur_dir)
108         args = {"vnf": "vpe1"}
109         self.assertIsNone(yardstick_ns_cli.run_test(args, test_path))
110         os.chdir(cur_dir)
111         args = {"vnf": "vpe",
112                 "test": "tc_baremetal_rfc2544_ipv4_1flow_1518B.yaml."}
113         self.assertIsNone(yardstick_ns_cli.run_test(args, test_path))
114         os.chdir(cur_dir)
115         args = []
116         self.assertIsNone(yardstick_ns_cli.run_test(args, test_path))
117         os.chdir(cur_dir)
118
119     def test_terminate_if_less_options(self):
120         yardstick_ns_cli = NSBperf.YardstickNSCli()
121         args = {"vnf": False}
122         self.assertRaises(SystemExit,
123                           yardstick_ns_cli.terminate_if_less_options, args)
124
125     def test_validate_input(self):
126         yardstick_ns_cli = NSBperf.YardstickNSCli()
127         self.assertEqual(1, yardstick_ns_cli.validate_input("", 4))
128         NSBperf.input = lambda _: 'yes'
129         self.assertEqual(1, yardstick_ns_cli.validate_input(5, 4))
130         subprocess.call = mock.Mock(return_value=0)
131         self.assertEqual(0, yardstick_ns_cli.validate_input(2, 4))
132         subprocess.call = mock.Mock(return_value=0)