Remove main() and __main__ from tests.
[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
16 from __future__ import absolute_import
17 import unittest
18 import mock
19 import subprocess
20 import os
21
22 from yardstick.cmd.NSBperf import YardstickNSCli
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     def test___init__(self):
36         yardstick_ns_cli = YardstickNSCli()
37         self.assertIsNotNone(yardstick_ns_cli)
38
39     def test_generate_final_report(self):
40         yardstick_ns_cli = YardstickNSCli()
41         test_case = "tc_baremetal_rfc2544_ipv4_1flow_1518B.yaml"
42         if os.path.isfile("/tmp/yardstick.out"):
43             os.remove('/tmp/yardstick.out')
44         self.assertIsNone(yardstick_ns_cli.generate_final_report(test_case))
45
46     def test_generate_kpi_results(self):
47         yardstick_ns_cli = YardstickNSCli()
48         tkey = "cpu"
49         tgen = {"cpu": {"ipc": 0}}
50         self.assertIsNone(yardstick_ns_cli.generate_kpi_results(tkey, tgen))
51
52     def test_generate_nfvi_results(self):
53         yardstick_ns_cli = YardstickNSCli()
54         nfvi = {"collect_stats": {"cpu": {"ipc": 0, "Hz": 2.6}}}
55         self.assertIsNone(yardstick_ns_cli.generate_nfvi_results(nfvi))
56
57     def test_handle_list_options(self):
58         yardstick_ns_cli = YardstickNSCli()
59         CLI_PATH = os.path.dirname(os.path.realpath(__file__))
60         repo_dir = CLI_PATH + "/../../../"
61         test_path = os.path.join(repo_dir, "../samples/vnf_samples/nsut/")
62         args = {"list_vnfs": True, "list": False}
63         self.assertRaises(SystemExit, yardstick_ns_cli.handle_list_options,
64                           args, test_path)
65         args = {"list_vnfs": False, "list": True}
66         self.assertRaises(SystemExit,
67                           yardstick_ns_cli.handle_list_options,
68                           args, test_path)
69
70     def test_main(self):
71         yardstick_ns_cli = YardstickNSCli()
72         yardstick_ns_cli.parse_arguments = mock.Mock(return_value=0)
73         yardstick_ns_cli.handle_list_options = mock.Mock(return_value=0)
74         yardstick_ns_cli.terminate_if_less_options = mock.Mock(return_value=0)
75         yardstick_ns_cli.run_test = mock.Mock(return_value=0)
76         self.assertIsNone(yardstick_ns_cli.main())
77
78     def test_parse_arguments(self):
79         yardstick_ns_cli = YardstickNSCli()
80         self.assertRaises(SystemExit, yardstick_ns_cli.parse_arguments)
81
82     def test_run_test(self):
83         cur_dir = os.getcwd()
84         CLI_PATH = os.path.dirname(os.path.realpath(__file__))
85         YARDSTICK_REPOS_DIR = os.path.join(CLI_PATH + "/../../")
86         test_path = os.path.join(YARDSTICK_REPOS_DIR,
87                                  "../samples/vnf_samples/nsut/")
88         yardstick_ns_cli = YardstickNSCli()
89         subprocess.check_output = mock.Mock(return_value=0)
90         args = {"vnf": "vpe",
91                 "test": "tc_baremetal_rfc2544_ipv4_1flow_1518B.yaml"}
92         self.assertIsNone(yardstick_ns_cli.run_test(args, test_path))
93         os.chdir(cur_dir)
94         args = {"vnf": "vpe1"}
95         self.assertIsNone(yardstick_ns_cli.run_test(args, test_path))
96         os.chdir(cur_dir)
97         args = {"vnf": "vpe",
98                 "test": "tc_baremetal_rfc2544_ipv4_1flow_1518B.yaml."}
99         self.assertIsNone(yardstick_ns_cli.run_test(args, test_path))
100         os.chdir(cur_dir)
101         args = []
102         self.assertIsNone(yardstick_ns_cli.run_test(args, test_path))
103         os.chdir(cur_dir)
104
105     def test_terminate_if_less_options(self):
106         yardstick_ns_cli = YardstickNSCli()
107         args = {"vnf": False}
108         self.assertRaises(SystemExit,
109                           yardstick_ns_cli.terminate_if_less_options, args)
110
111     def test_validate_input(self):
112         yardstick_ns_cli = YardstickNSCli()
113         self.assertEqual(1, yardstick_ns_cli.validate_input("", 4))
114         NSBperf.input = lambda _: 'yes'
115         self.assertEqual(1, yardstick_ns_cli.validate_input(5, 4))
116         subprocess.call = mock.Mock(return_value=0)
117         self.assertEqual(0, yardstick_ns_cli.validate_input(2, 4))
118         subprocess.call = mock.Mock(return_value=0)