Fixed document for Grafana Port usage
[yardstick.git] / yardstick / tests / unit / benchmark / scenarios / parser / test_parser.py
1 #!/usr/bin/env python
2
3 ##############################################################################
4 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and other.
5 #
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Apache License, Version 2.0
8 # which accompanies this distribution, and is available at
9 # http://www.apache.org/licenses/LICENSE-2.0
10 ##############################################################################
11
12 import subprocess
13
14 import unittest
15 import mock
16
17 from oslo_serialization import jsonutils
18
19 from yardstick.benchmark.scenarios.parser import parser
20
21
22 class ParserTestCase(unittest.TestCase):
23
24     def setUp(self):
25         args = {
26             'options': {'yangfile': '/root/yardstick/samples/yang.yaml',
27                         'toscafile': '/root/yardstick/samples/tosca.yaml'},
28         }
29         self.scenario = parser.Parser(scenario_cfg=args, context_cfg={})
30
31         self._mock_popen = mock.patch.object(subprocess, 'Popen')
32         self.mock_popen = self._mock_popen.start()
33         self._mock_call = mock.patch.object(subprocess, 'call')
34         self.mock_call = self._mock_call.start()
35
36         self.addCleanup(self._stop_mock)
37
38     def _stop_mock(self):
39         self._mock_popen.stop()
40         self._mock_call.stop()
41
42     def test_setup_successful(self):
43
44         self.mock_call.return_value = 0
45         self.scenario.setup()
46         self.assertTrue(self.scenario.setup_done)
47
48     def test_run_successful(self):
49
50         result = {}
51
52         self.mock_popen().returncode = 0
53
54         expected_result = jsonutils.loads('{"yangtotosca": "success"}')
55
56         self.scenario.run(result)
57         self.assertEqual(result, expected_result)
58
59     def test_run_fail(self):
60         result = {}
61
62         self.mock_popen().returncode = 1
63         expected_result = jsonutils.loads('{"yangtotosca": "fail"}')
64
65         self.scenario.run(result)
66         self.assertEqual(result, expected_result)
67
68     def test_teardown_successful(self):
69
70         self.mock_call.return_value = 0
71         self.scenario.teardown()
72         self.assertTrue(self.scenario.teardown_done)
73
74
75 def main():
76     unittest.main()
77
78 if __name__ == '__main__':
79     main()