1 # Copyright (c) 2016-2017 Intel Corporation
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
20 from six.moves import builtins
23 from yardstick.cmd import NSBperf
26 @mock.patch('six.moves.input', return_value='0')
27 class TestHandler(unittest.TestCase):
29 def test_handler(self, *args):
30 subprocess.call = mock.Mock(return_value=0)
31 self.assertRaises(SystemExit, NSBperf.sigint_handler)
34 class TestYardstickNSCli(unittest.TestCase):
37 self._mock_print = mock.patch.object(builtins, 'print')
38 self.mock_print = self._mock_print.start()
39 self.addCleanup(self._stop_mocks)
41 def _stop_mocks(self):
42 self._mock_print.stop()
44 def test___init__(self):
45 yardstick_ns_cli = NSBperf.YardstickNSCli()
46 self.assertIsNotNone(yardstick_ns_cli)
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))
55 def test_generate_kpi_results(self):
56 yardstick_ns_cli = NSBperf.YardstickNSCli()
58 tgen = {"cpu": {"ipc": 0}}
59 self.assertIsNone(yardstick_ns_cli.generate_kpi_results(tkey, tgen))
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))
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,
74 args = {"list_vnfs": False, "list": True}
75 self.assertRaises(SystemExit,
76 yardstick_ns_cli.handle_list_options,
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())
87 @mock.patch.object(argparse.ArgumentParser, 'parse_args')
88 def test_parse_arguments(self, mock_parse):
89 class DummyArgs(object):
92 mock_parse.return_value = DummyArgs
93 yardstick_ns_cli = NSBperf.YardstickNSCli()
94 self.assertIn('var1', yardstick_ns_cli.parse_arguments())
96 def test_run_test(self):
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))
108 args = {"vnf": "vpe1"}
109 self.assertIsNone(yardstick_ns_cli.run_test(args, test_path))
111 args = {"vnf": "vpe",
112 "test": "tc_baremetal_rfc2544_ipv4_1flow_1518B.yaml."}
113 self.assertIsNone(yardstick_ns_cli.run_test(args, test_path))
116 self.assertIsNone(yardstick_ns_cli.run_test(args, test_path))
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)
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)