Debian Codegraph @

config_tempest/tests/services/test_services.py

raw symbols SCIP
1 # Copyright 2018 Red Hat, Inc.
2 # All Rights Reserved.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
14 # under the License.
15
16 from config_tempest.services.services import Services
17 from config_tempest.tests.base import BaseConfigTempestTest
18 from unittest import mock
19
20
21 class TestServices(BaseConfigTempestTest):
22
23 FAKE_ENTRY = {
24 'endpoints': [
25 {
26 'region': 'my_region',
27 'interface': 'public'
28 },
29 {
30 'region': 'other_region',
31 'interface': 'private'
32 }
33 ]
34 }
35
36 def setUp(self):
37 super(TestServices, self).setUp()
38
39 @mock.patch('config_tempest.services.services.Services.discover')
40 @mock.patch('config_tempest.services.services.Services.'
41 'set_catalog_and_url')
42 @mock.patch('config_tempest.services.services.Services.'
43 'get_available_services')
44 def _create_services_instance(self, mock_avail, mock_catalog,
45 mock_discover, v2=False):
46 mock_avail.return_value = [{'name': 'my_service', 'type': 'my_type'}]
47 conf = self._get_conf('v2', 'v3')
48 creds = self._get_creds(conf, v2=v2)
49 clients = mock.Mock()
50 services = Services(clients, conf, creds)
51 return services
52
53 def test_get_endpoints_api_2(self):
54 services = self._create_services_instance(v2=True)
55 services._region = 'my_region'
56 resp = services.get_endpoints(self.FAKE_ENTRY)
57 self.assertEqual(resp, self.FAKE_ENTRY['endpoints'][0])
58 services._region = 'doesnt_exist_region'
59 resp = services.get_endpoints(self.FAKE_ENTRY)
60 self.assertEqual(resp, self.FAKE_ENTRY['endpoints'][0])
61 services._region = 'other_region'
62 resp = services.get_endpoints(self.FAKE_ENTRY)
63 self.assertEqual(resp, self.FAKE_ENTRY['endpoints'][1])
64
65 def test_get_endpoints_api_3(self):
66 services = self._create_services_instance()
67 services._creds.api_version = 3
68 services._region = 'my_region'
69 resp = services.get_endpoints(self.FAKE_ENTRY)
70 self.assertEqual(resp, self.FAKE_ENTRY['endpoints'][0])
71 services._region = 'other_region'
72 resp = services.get_endpoints(self.FAKE_ENTRY)
73 self.assertEqual(resp, self.FAKE_ENTRY['endpoints'][0])
74
75 def test_get_endpoints_no_endpoints(self):
76 services = self._create_services_instance()
77 resp = services.get_endpoints({'endpoints': []})
78 self.assertEqual(resp, [])
79
80 def test_parse_endpoints_empty(self):
81 services = self._create_services_instance()
82 services.public_url = "url"
83 ep = []
84 url = services.parse_endpoints(ep, "ServiceName")
85 self.assertEqual("", url)
86
87 def test_parse_endpoints(self):
88 services = self._create_services_instance()
89 services.public_url = "url"
90 ep = {
91 'url': 'http://10.0.0.107:8386/v1.1/96409a589d',
92 'interface': 'public',
93 'region': 'regioneOne',
94 }
95 url = services.parse_endpoints(ep, "ServiceName")
96 self.assertEqual('http://10.0.0.107:8386/v1.1/96409a589d', url)
97 ep['url'] = 'https://10.0.0.101/identity'
98 auth_url = 'https://10.0.0.101:13000/v3.0'
99 services._clients.auth_provider.auth_url = auth_url
100 url = services.parse_endpoints(ep, 'ServiceName')
101 self.assertEqual('https://10.0.0.101:13000/identity/v3', url)
102
103 def test_parse_endpoints_not_ip_hostname(self):
104 services = self._create_services_instance()
105 services.public_url = "url"
106 url = "https://identity-my.cloud.com:35456/v2.0"
107 ep = {
108 'url': url,
109 'interface': 'public',
110 'region': 'regioneOne',
111 }
112 services._clients.auth_provider.auth_url = "35456"
113 url_resp = services.parse_endpoints(ep, "ServiceName")
114 self.assertEqual(url, url_resp)
115
116 def test_edit_identity_url_v3(self):
117 services = self._create_services_instance()
118 url_port = 'https://10.0.0.101:13000/v3.0'
119 identity_url = 'https://10.0.0.101/identity'
120 url_no_port = 'https://10.0.0.101/v333'
121 services._clients.auth_provider.auth_url = url_port
122 url = services.edit_identity_url(url_port)
123 self.assertEqual(url_port, url)
124 url = services.edit_identity_url(identity_url)
125 self.assertEqual("https://10.0.0.101:13000/identity/v3", url)
126 url = services.edit_identity_url(url_no_port)
127 self.assertEqual(url_no_port, url)
128 services._clients.auth_provider.auth_url = url_no_port
129 url = services.edit_identity_url(identity_url)
130 self.assertEqual(identity_url + "/v3", url)
131
132 def test_edit_identity_url_v2(self):
133 services = self._create_services_instance(v2=True)
134 url_port = 'https://10.0.0.101:13000/v2.0'
135 identity_url = 'https://10.0.0.101/identity'
136 url_no_port = 'https://10.0.0.101/v2.0'
137 services._clients.auth_provider.auth_url = url_port
138 url = services.edit_identity_url(url_port)
139 self.assertEqual(url_port, url)
140 url = services.edit_identity_url(identity_url)
141 self.assertEqual("https://10.0.0.101:13000/identity/v2", url)
142 url = services.edit_identity_url(url_no_port)
143 self.assertEqual(url_no_port, url)
144 services._clients.auth_provider.auth_url = url_no_port
145 url = services.edit_identity_url(identity_url)
146 self.assertEqual(identity_url + "/v2", url)
147
148 def test_get_service(self):
149 services = self._create_services_instance()
150 exp_resp = mock.Mock()
151 exp_resp.s_type = 'my_service_type'
152 services._services = [exp_resp]
153 resp = services.get_service('my_service_type')
154 self.assertEqual(resp, exp_resp)
155 resp = services.get_service('my')
156 self.assertEqual(resp, None)
157
158 def test_is_service(self):
159 services = self._create_services_instance()
160 service = mock.Mock()
161 service.name = 'my_service'
162 service.s_type = 'my_type'
163 services._services = [service]
164 resp = services.is_service(name='my_service')
165 self.assertEqual(resp, True)
166 resp = services.is_service(name='other_service')
167 self.assertEqual(resp, False)
168 resp = services.is_service(**{'type': 'my_type'})
169 self.assertEqual(resp, True)
170 resp = services.is_service(**{'type': 'other_type'})
171 self.assertEqual(resp, False)