File: test_services.py

package info (click to toggle)
python-tempestconf 3.5.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 964 kB
  • sloc: python: 4,530; makefile: 18; sh: 9
file content (171 lines) | stat: -rw-r--r-- 7,009 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# Copyright 2018 Red Hat, Inc.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

from config_tempest.services.services import Services
from config_tempest.tests.base import BaseConfigTempestTest
from unittest import mock


class TestServices(BaseConfigTempestTest):

    FAKE_ENTRY = {
        'endpoints': [
            {
                'region': 'my_region',
                'interface': 'public'
            },
            {
                'region': 'other_region',
                'interface': 'private'
            }
        ]
    }

    def setUp(self):
        super(TestServices, self).setUp()

    @mock.patch('config_tempest.services.services.Services.discover')
    @mock.patch('config_tempest.services.services.Services.'
                'set_catalog_and_url')
    @mock.patch('config_tempest.services.services.Services.'
                'get_available_services')
    def _create_services_instance(self, mock_avail, mock_catalog,
                                  mock_discover, v2=False):
        mock_avail.return_value = [{'name': 'my_service', 'type': 'my_type'}]
        conf = self._get_conf('v2', 'v3')
        creds = self._get_creds(conf, v2=v2)
        clients = mock.Mock()
        services = Services(clients, conf, creds)
        return services

    def test_get_endpoints_api_2(self):
        services = self._create_services_instance(v2=True)
        services._region = 'my_region'
        resp = services.get_endpoints(self.FAKE_ENTRY)
        self.assertEqual(resp, self.FAKE_ENTRY['endpoints'][0])
        services._region = 'doesnt_exist_region'
        resp = services.get_endpoints(self.FAKE_ENTRY)
        self.assertEqual(resp, self.FAKE_ENTRY['endpoints'][0])
        services._region = 'other_region'
        resp = services.get_endpoints(self.FAKE_ENTRY)
        self.assertEqual(resp, self.FAKE_ENTRY['endpoints'][1])

    def test_get_endpoints_api_3(self):
        services = self._create_services_instance()
        services._creds.api_version = 3
        services._region = 'my_region'
        resp = services.get_endpoints(self.FAKE_ENTRY)
        self.assertEqual(resp, self.FAKE_ENTRY['endpoints'][0])
        services._region = 'other_region'
        resp = services.get_endpoints(self.FAKE_ENTRY)
        self.assertEqual(resp, self.FAKE_ENTRY['endpoints'][0])

    def test_get_endpoints_no_endpoints(self):
        services = self._create_services_instance()
        resp = services.get_endpoints({'endpoints': []})
        self.assertEqual(resp, [])

    def test_parse_endpoints_empty(self):
        services = self._create_services_instance()
        services.public_url = "url"
        ep = []
        url = services.parse_endpoints(ep, "ServiceName")
        self.assertEqual("", url)

    def test_parse_endpoints(self):
        services = self._create_services_instance()
        services.public_url = "url"
        ep = {
            'url': 'http://10.0.0.107:8386/v1.1/96409a589d',
            'interface': 'public',
            'region': 'regioneOne',
        }
        url = services.parse_endpoints(ep, "ServiceName")
        self.assertEqual('http://10.0.0.107:8386/v1.1/96409a589d', url)
        ep['url'] = 'https://10.0.0.101/identity'
        auth_url = 'https://10.0.0.101:13000/v3.0'
        services._clients.auth_provider.auth_url = auth_url
        url = services.parse_endpoints(ep, 'ServiceName')
        self.assertEqual('https://10.0.0.101:13000/identity/v3', url)

    def test_parse_endpoints_not_ip_hostname(self):
        services = self._create_services_instance()
        services.public_url = "url"
        url = "https://identity-my.cloud.com:35456/v2.0"
        ep = {
            'url': url,
            'interface': 'public',
            'region': 'regioneOne',
        }
        services._clients.auth_provider.auth_url = "35456"
        url_resp = services.parse_endpoints(ep, "ServiceName")
        self.assertEqual(url, url_resp)

    def test_edit_identity_url_v3(self):
        services = self._create_services_instance()
        url_port = 'https://10.0.0.101:13000/v3.0'
        identity_url = 'https://10.0.0.101/identity'
        url_no_port = 'https://10.0.0.101/v333'
        services._clients.auth_provider.auth_url = url_port
        url = services.edit_identity_url(url_port)
        self.assertEqual(url_port, url)
        url = services.edit_identity_url(identity_url)
        self.assertEqual("https://10.0.0.101:13000/identity/v3", url)
        url = services.edit_identity_url(url_no_port)
        self.assertEqual(url_no_port, url)
        services._clients.auth_provider.auth_url = url_no_port
        url = services.edit_identity_url(identity_url)
        self.assertEqual(identity_url + "/v3", url)

    def test_edit_identity_url_v2(self):
        services = self._create_services_instance(v2=True)
        url_port = 'https://10.0.0.101:13000/v2.0'
        identity_url = 'https://10.0.0.101/identity'
        url_no_port = 'https://10.0.0.101/v2.0'
        services._clients.auth_provider.auth_url = url_port
        url = services.edit_identity_url(url_port)
        self.assertEqual(url_port, url)
        url = services.edit_identity_url(identity_url)
        self.assertEqual("https://10.0.0.101:13000/identity/v2", url)
        url = services.edit_identity_url(url_no_port)
        self.assertEqual(url_no_port, url)
        services._clients.auth_provider.auth_url = url_no_port
        url = services.edit_identity_url(identity_url)
        self.assertEqual(identity_url + "/v2", url)

    def test_get_service(self):
        services = self._create_services_instance()
        exp_resp = mock.Mock()
        exp_resp.s_type = 'my_service_type'
        services._services = [exp_resp]
        resp = services.get_service('my_service_type')
        self.assertEqual(resp, exp_resp)
        resp = services.get_service('my')
        self.assertEqual(resp, None)

    def test_is_service(self):
        services = self._create_services_instance()
        service = mock.Mock()
        service.name = 'my_service'
        service.s_type = 'my_type'
        services._services = [service]
        resp = services.is_service(name='my_service')
        self.assertEqual(resp, True)
        resp = services.is_service(name='other_service')
        self.assertEqual(resp, False)
        resp = services.is_service(**{'type': 'my_type'})
        self.assertEqual(resp, True)
        resp = services.is_service(**{'type': 'other_type'})
        self.assertEqual(resp, False)