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