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