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
9 import subprocess32 as subprocess
14 from functest.opnfv_tests.vnf.ims import orchestrator_cloudify
17 class ImsVnfTesting(unittest.TestCase):
20 self.orchestrator = orchestrator_cloudify.Orchestrator('test_dir')
21 self.bp = {'file_name': 'test_file',
22 'destination_folder': 'test_folder',
24 'branch': 'test_branch'}
26 def test_download_manager_blueprint_download_blueprint_failed(self):
27 self.orchestrator.manager_blueprint = False
28 with mock.patch.object(self.orchestrator, '_download_blueprints',
29 return_value=False), \
30 mock.patch('functest.opnfv_tests.vnf.ims.orchestrator_cloudify.'
32 self.orchestrator.download_manager_blueprint('test_url',
34 mock_exit.assert_any_call(-1)
36 def test_download_manager_blueprint_download_blueprint_passed(self):
37 self.orchestrator.manager_blueprint = False
38 with mock.patch.object(self.orchestrator, '_download_blueprints',
40 self.orchestrator.download_manager_blueprint('test_url',
42 self.assertEqual(self.orchestrator.manager_blueprint,
45 def test_deploy_manager_failed(self):
46 self.orchestrator.manager_blueprint = True
47 with mock.patch('__builtin__.open', mock.mock_open()), \
48 mock.patch('functest.opnfv_tests.vnf.ims.orchestrator_cloudify.'
50 mock.patch('functest.opnfv_tests.vnf.ims.orchestrator_cloudify.'
51 'execute_command', return_value='error'):
52 self.assertEqual(self.orchestrator.deploy_manager(),
54 self.assertEqual(self.orchestrator.manager_up,
57 def test_deploy_manager_passed(self):
58 self.orchestrator.manager_blueprint = True
59 with mock.patch('__builtin__.open', mock.mock_open()), \
60 mock.patch('functest.opnfv_tests.vnf.ims.orchestrator_cloudify.'
62 mock.patch('functest.opnfv_tests.vnf.ims.orchestrator_cloudify.'
63 'execute_command', return_value=''):
64 self.orchestrator.deploy_manager()
65 self.assertEqual(self.orchestrator.manager_up,
68 def test_undeploy_manager_passed(self):
69 with mock.patch('functest.opnfv_tests.vnf.ims.orchestrator_cloudify.'
70 'execute_command', return_value=''):
71 self.orchestrator.deploy_manager()
72 self.assertEqual(self.orchestrator.manager_up,
75 def test_dwnld_upload_and_depl_blueprint_dwnld_blueprint_failed(self):
76 with mock.patch.object(self.orchestrator, '_download_blueprints',
77 return_value=False), \
78 mock.patch('functest.opnfv_tests.vnf.ims.orchestrator_cloudify.'
79 'exit', side_effect=Exception) as mock_exit, \
80 self.assertRaises(Exception):
81 self.orchestrator.download_upload_and_deploy_blueprint(self.bp,
85 mock_exit.assert_any_call(-1)
87 def test_dwnld_upload_and_depl_blueprint_failed(self):
88 with mock.patch.object(self.orchestrator, '_download_blueprints',
90 mock.patch('__builtin__.open', mock.mock_open()), \
91 mock.patch('functest.opnfv_tests.vnf.ims.orchestrator_cloudify.'
92 'execute_command', return_value='error'):
93 r = self.orchestrator.download_upload_and_deploy_blueprint(self.bp,
97 self.assertEqual(r, 'error')
99 def test__download_blueprints_failed(self):
100 with mock.patch('functest.opnfv_tests.vnf.ims.orchestrator_cloudify.'
102 mock.patch('functest.opnfv_tests.vnf.ims.orchestrator_cloudify.'
103 'Repo.clone_from', side_effect=Exception):
104 self.assertEqual(self.orchestrator._download_blueprints('bp_url',
109 def test__download_blueprints_passed(self):
110 with mock.patch('functest.opnfv_tests.vnf.ims.orchestrator_cloudify.'
112 mock.patch('functest.opnfv_tests.vnf.ims.orchestrator_cloudify.'
114 self.assertEqual(self.orchestrator._download_blueprints('bp_url',
119 def test_execute_command_failed(self):
120 with mock.patch('__builtin__.open',
121 mock.mock_open(read_data='test_data\n')):
122 subprocess.call = mock.create_autospec(subprocess.call,
124 mock_log = mock.Mock()
125 cmd = 'test_cmd -e test_env bash_script'
126 ret = orchestrator_cloudify.execute_command(cmd, mock_log,
128 self.assertEqual(ret, False)
130 def test_execute_command_default(self):
131 with mock.patch('__builtin__.open',
132 mock.mock_open(read_data='test_data\n')):
133 subprocess.call = mock. \
134 create_autospec(subprocess.call,
135 return_value=subprocess.TimeoutExpired)
136 mock_log = mock.Mock()
137 cmd = 'test_cmd -e test_env bash_script'
138 ret = orchestrator_cloudify.execute_command(cmd, mock_log,
140 self.assertEqual(ret, ['test_data\n'])
142 def test_set_methods(self):
143 self.orchestrator.set_credentials('test_username', 'test_password',
144 'test_tenant_name', 'test_auth_url')
145 self.assertTrue(self.orchestrator.config['keystone_username'],
147 self.assertTrue(self.orchestrator.config['keystone_password'],
149 self.assertTrue(self.orchestrator.config['keystone_url'],
151 self.assertTrue(self.orchestrator.config['keystone_tenant_name'],
153 self.orchestrator.set_flavor_id('test_flavor_id')
154 self.assertTrue(self.orchestrator.config['flavor_id'],
156 self.orchestrator.set_image_id('test_image_id')
157 self.assertTrue(self.orchestrator.config['image_id'], 'test_image_id')
158 self.orchestrator.set_external_network_name('test_network')
159 self.assertTrue(self.orchestrator.config['external_network_name'],
161 self.orchestrator.set_ssh_user('test_user')
162 self.assertTrue(self.orchestrator.config['ssh_user'],
164 self.orchestrator.set_nova_url('test_nova_url')
165 self.assertTrue(self.orchestrator.config['nova_url'],
167 self.orchestrator.set_neutron_url('test_neutron_url')
168 self.assertTrue(self.orchestrator.config['neutron_url'],
170 self.orchestrator.set_nameservers(['test_subnet'])
171 self.assertTrue(self.orchestrator.config['dns_subnet_1'],
174 if __name__ == "__main__":
175 logging.disable(logging.CRITICAL)
176 unittest.main(verbosity=2)