More Unit Tests for utils module
[functest.git] / functest / tests / unit / opnfv_tests / vnf / ims / test_orchestrator_cloudify.py
1 #!/usr/bin/env python
2
3 # All rights reserved. This program and the accompanying materials
4 # are made available under the terms of the Apache License, Version 2.0
5 # which accompanies this distribution, and is available at
6 # http://www.apache.org/licenses/LICENSE-2.0
7
8 import logging
9 import unittest
10
11 import mock
12
13 from functest.opnfv_tests.vnf.ims import orchestrator_cloudify
14
15
16 class ImsVnfTesting(unittest.TestCase):
17
18     logging.disable(logging.CRITICAL)
19
20     def setUp(self):
21         self.orchestrator = orchestrator_cloudify.Orchestrator('test_dir')
22         self.bp = {'file_name': 'test_file',
23                    'destination_folder': 'test_folder',
24                    'url': 'test_url',
25                    'branch': 'test_branch'}
26
27     def test_download_manager_blueprint_download_blueprint_failed(self):
28         self.orchestrator.manager_blueprint = False
29         with mock.patch.object(self.orchestrator, '_download_blueprints',
30                                return_value=False), \
31             mock.patch('functest.opnfv_tests.vnf.ims.orchestrator_cloudify.'
32                        'exit') as mock_exit:
33             self.orchestrator.download_manager_blueprint('test_url',
34                                                          'test_branch')
35             mock_exit.assert_any_call(-1)
36
37     def test_download_manager_blueprint_download_blueprint_passed(self):
38         self.orchestrator.manager_blueprint = False
39         with mock.patch.object(self.orchestrator, '_download_blueprints',
40                                return_value=True):
41             self.orchestrator.download_manager_blueprint('test_url',
42                                                          'test_branch')
43             self.assertEqual(self.orchestrator.manager_blueprint,
44                              True)
45
46     def test_deploy_manager_failed(self):
47         self.orchestrator.manager_blueprint = True
48         with mock.patch('__builtin__.open', mock.mock_open()), \
49             mock.patch('functest.opnfv_tests.vnf.ims.orchestrator_cloudify.'
50                        'os.remove'), \
51             mock.patch('functest.opnfv_tests.vnf.ims.orchestrator_cloudify.'
52                        'execute_command', return_value='error'):
53             self.assertEqual(self.orchestrator.deploy_manager(),
54                              'error')
55             self.assertEqual(self.orchestrator.manager_up,
56                              False)
57
58     def test_deploy_manager_passed(self):
59         self.orchestrator.manager_blueprint = True
60         with mock.patch('__builtin__.open', mock.mock_open()), \
61             mock.patch('functest.opnfv_tests.vnf.ims.orchestrator_cloudify.'
62                        'os.remove'), \
63             mock.patch('functest.opnfv_tests.vnf.ims.orchestrator_cloudify.'
64                        'execute_command', return_value=''):
65             self.orchestrator.deploy_manager()
66             self.assertEqual(self.orchestrator.manager_up,
67                              True)
68
69     def test_undeploy_manager_passed(self):
70         with mock.patch('functest.opnfv_tests.vnf.ims.orchestrator_cloudify.'
71                         'execute_command', return_value=''):
72             self.orchestrator.deploy_manager()
73             self.assertEqual(self.orchestrator.manager_up,
74                              False)
75
76     def test_dwnld_upload_and_depl_blueprint_dwnld_blueprint_failed(self):
77         with mock.patch.object(self.orchestrator, '_download_blueprints',
78                                return_value=False), \
79             mock.patch('functest.opnfv_tests.vnf.ims.orchestrator_cloudify.'
80                        'exit', side_effect=Exception) as mock_exit, \
81                 self.assertRaises(Exception):
82             self.orchestrator.download_upload_and_deploy_blueprint(self.bp,
83                                                                    'cfig',
84                                                                    'bpn',
85                                                                    'dpn')
86             mock_exit.assert_any_call(-1)
87
88     def test_dwnld_upload_and_depl_blueprint_failed(self):
89         with mock.patch.object(self.orchestrator, '_download_blueprints',
90                                return_value=True), \
91             mock.patch('__builtin__.open', mock.mock_open()), \
92             mock.patch('functest.opnfv_tests.vnf.ims.orchestrator_cloudify.'
93                        'execute_command', return_value='error'):
94             r = self.orchestrator.download_upload_and_deploy_blueprint(self.bp,
95                                                                        'cfig',
96                                                                        'bpn',
97                                                                        'dpn')
98             self.assertEqual(r, 'error')
99
100     def test__download_blueprints_failed(self):
101         with mock.patch('functest.opnfv_tests.vnf.ims.orchestrator_cloudify.'
102                         'shutil.rmtree'), \
103             mock.patch('functest.opnfv_tests.vnf.ims.orchestrator_cloudify.'
104                        'Repo.clone_from', side_effect=Exception):
105             self.assertEqual(self.orchestrator._download_blueprints('bp_url',
106                                                                     'branch',
107                                                                     'dest'),
108                              False)
109
110     def test__download_blueprints_passed(self):
111         with mock.patch('functest.opnfv_tests.vnf.ims.orchestrator_cloudify.'
112                         'shutil.rmtree'), \
113             mock.patch('functest.opnfv_tests.vnf.ims.orchestrator_cloudify.'
114                        'Repo.clone_from'):
115             self.assertEqual(self.orchestrator._download_blueprints('bp_url',
116                                                                     'branch',
117                                                                     'dest'),
118                              True)
119
120
121 if __name__ == "__main__":
122     unittest.main(verbosity=2)