Merge "Remove tacker library from functest"
[functest.git] / functest / tests / unit / vnf / router / test_cloudify_vrouter.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2017 Okinawa Open Laboratory and others.
4 #
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9
10 import logging
11 import unittest
12
13 import mock
14
15 from functest.core import vnf
16 from functest.opnfv_tests.vnf.router import cloudify_vrouter
17
18 from snaps.openstack.os_credentials import OSCreds
19
20
21 class CloudifyVrouterTesting(unittest.TestCase):
22
23     def setUp(self):
24
25         self.tenant = 'cloudify_vrouter'
26         self.creds = {'username': 'user',
27                       'password': 'pwd'}
28         self.orchestrator = {'name': 'cloudify',
29                              'version': '4.0',
30                              'object': 'foo',
31                              'requirements': {'flavor': {'name': 'm1.medium',
32                                                          'ram_min': 4096},
33                                               'os_image': 'manager_4.0'}}
34
35         self.vnf = {'name': 'vrouter',
36                     'descriptor': {'version': '100',
37                                    'file_name': 'function-test-' +
38                                                 'openstack-blueprint.yaml',
39                                    'name': 'vrouter-opnfv',
40                                    'url': 'https://foo',
41                                    'requirements': {'flavor':
42                                                     {'name': 'm1.medium',
43                                                      'ram_min': 2048}}}}
44
45         with mock.patch('functest.opnfv_tests.vnf.router.cloudify_vrouter.'
46                         'os.makedirs'), \
47             mock.patch('functest.opnfv_tests.vnf.router.cloudify_vrouter.'
48                        'get_config', return_value={
49                            'tenant_images': 'foo',
50                            'orchestrator': self.orchestrator,
51                            'vnf': self.vnf,
52                            'vnf_test_suite': '',
53                            'version': 'whatever'}):
54
55             self.router_vnf = cloudify_vrouter.CloudifyVrouter()
56
57         self.images = {'image1': 'url1',
58                        'image2': 'url2'}
59         self.details = {'orchestrator': {'status': 'PASS', 'duration': 120},
60                         'vnf': {},
61                         'test_vnf':  {}}
62
63     @mock.patch('functest.core.vnf.os_utils.get_keystone_client',
64                 return_value='test')
65     @mock.patch('functest.core.vnf.os_utils.get_or_create_tenant_for_vnf',
66                 return_value=True)
67     @mock.patch('functest.core.vnf.os_utils.get_or_create_user_for_vnf',
68                 return_value=True)
69     @mock.patch('functest.core.vnf.os_utils.get_credentials',
70                 return_value={'auth_url': 'test/v1'})
71     @mock.patch('snaps.openstack.create_image.OpenStackImage.create')
72     def test_prepare_default(self, *args):
73         self.assertIsNone(self.router_vnf.prepare())
74         args[4].assert_called_once_with()
75
76     @mock.patch('functest.core.vnf.os_utils.get_keystone_client',
77                 return_value='test')
78     @mock.patch('functest.core.vnf.os_utils.get_or_create_tenant_for_vnf',
79                 return_value=True)
80     @mock.patch('functest.core.vnf.os_utils.get_or_create_user_for_vnf',
81                 return_value=True)
82     @mock.patch('functest.core.vnf.os_utils.get_credentials',
83                 return_value={'auth_url': 'test/no_v'})
84     @mock.patch('snaps.openstack.create_image.OpenStackImage.create')
85     def test_prepare_bad_auth_url(self, *args):
86         with self.assertRaises(Exception):
87             self.router_vnf.image_creator(
88                 OSCreds(username='user', password='pass', auth_url='url',
89                         project_name='project', identity_api_version=3),
90                 mock.Mock())
91             args[0].assert_not_called()
92
93     def test_prepare_missing_param(self):
94         with self.assertRaises(vnf.VnfPreparationException):
95             self.router_vnf.prepare()
96
97     @mock.patch('functest.core.vnf.os_utils.get_keystone_client',
98                 side_effect=Exception)
99     def test_prepare_keystone_exception(self, *args):
100         with self.assertRaises(vnf.VnfPreparationException):
101             self.router_vnf.prepare()
102         args[0].assert_called_once_with()
103
104     @mock.patch('functest.core.vnf.os_utils.get_keystone_client',
105                 return_value='test')
106     @mock.patch('functest.core.vnf.os_utils.get_or_create_tenant_for_vnf',
107                 side_effect=Exception)
108     def test_prepare_tenant_exception(self, *args):
109         with self.assertRaises(vnf.VnfPreparationException):
110             self.router_vnf.prepare()
111         args[1].assert_called_once_with()
112
113     @mock.patch('functest.core.vnf.os_utils.get_keystone_client',
114                 return_value='test')
115     @mock.patch('functest.core.vnf.os_utils.get_or_create_tenant_for_vnf',
116                 return_value=True)
117     @mock.patch('functest.core.vnf.os_utils.get_or_create_user_for_vnf',
118                 side_effect=Exception)
119     def test_prepare_user_exception(self, *args):
120         with self.assertRaises(vnf.VnfPreparationException):
121             self.router_vnf.prepare()
122         args[2].assert_called_once_with()
123
124     @mock.patch('functest.core.vnf.os_utils.get_keystone_client',
125                 return_value='test')
126     @mock.patch('functest.core.vnf.os_utils.get_or_create_tenant_for_vnf',
127                 return_value=True)
128     @mock.patch('functest.core.vnf.os_utils.get_or_create_user_for_vnf',
129                 return_value=True)
130     @mock.patch('functest.core.vnf.os_utils.get_credentials',
131                 side_effect=Exception)
132     def test_prepare_credentials_exception(self, *args):
133         with self.assertRaises(vnf.VnfPreparationException):
134             self.router_vnf.prepare()
135         args[0].assert_called_once_with()
136
137
138 if __name__ == "__main__":
139     logging.disable(logging.CRITICAL)
140     unittest.main(verbosity=2)