summaryrefslogtreecommitdiff
path: root/tests/test_keystone_service.py
blob: c3e09c556896dd2fa796ac85763c805d61969d77 (plain)
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
import keystone_service
import mock
from nose.tools import assert_equal, assert_list_equal, assert_is_none
from nose import SkipTest


def setup():
    keystone = mock.MagicMock()
    service = mock.Mock(id="b6a7ff03f2574cd9b5c7c61186e0d781",
                        type="identity",
                        description="Keystone Identity Service")
    # Can't set <name> field in mock in initializer
    service.name = "keystone"
    keystone.services.list = mock.Mock(return_value=[service])
    endpoint = mock.Mock(id="600759628a214eb7b3acde39b1e85180",
                         service_id="b6a7ff03f2574cd9b5c7c61186e0d781",
                         publicurl="http://192.168.206.130:5000/v2.0",
                         internalurl="http://192.168.206.130:5000/v2.0",
                         adminurl="http://192.168.206.130:35357/v2.0",
                         region="RegionOne")
    keystone.endpoints.list = mock.Mock(return_value=[endpoint])
    return keystone


@mock.patch('keystone_service.ensure_endpoint_absent')
@mock.patch('keystone_service.ensure_service_absent')
@mock.patch('keystone_service.ensure_endpoint_present')
@mock.patch('keystone_service.ensure_service_present')
def test_dispatch_service_present(mock_ensure_service_present,
                                  mock_ensure_endpoint_present,
                                  mock_ensure_service_absent,
                                  mock_ensure_endpoint_absent):
    """ Dispatch: service present """
    # Setup
    mock_ensure_service_present.return_value = (True, None)
    mock_ensure_endpoint_present.return_value = (True, None)
    manager = mock.MagicMock()
    manager.attach_mock(mock_ensure_service_present, 'ensure_service_present')
    manager.attach_mock(mock_ensure_service_absent, 'ensure_service_absent')
    manager.attach_mock(mock_ensure_endpoint_present,
                        'ensure_endpoint_present')
    manager.attach_mock(mock_ensure_endpoint_absent,
                        'ensure_endpoint_absent')

    keystone = setup()
    name = "keystone"
    service_type = "identity"
    description = "Keystone Identity Service"
    state = "present"
    public_url = "http://192.168.206.130:5000/v2.0"
    internal_url = "http://192.168.206.130:5000/v2.0"
    admin_url = "http://192.168.206.130:35357/v2.0"
    region = "RegionOne"
    check_mode = False

    # Code under test
    keystone_service.dispatch(keystone, name, service_type, description,
            public_url, internal_url, admin_url, region, state, check_mode)

    expected_calls = [mock.call.ensure_service_present(keystone, name,
                                                       service_type,
                                                       description,
                                                       check_mode),
                      mock.call.ensure_endpoint_present(keystone, name,
                                                         public_url,
                                                         internal_url,
                                                         admin_url,
                                                         region,
                                                         check_mode)]

    assert_equal(manager.mock_calls, expected_calls)


@mock.patch('keystone_service.ensure_endpoint_absent')
@mock.patch('keystone_service.ensure_service_absent')
@mock.patch('keystone_service.ensure_endpoint_present')
@mock.patch('keystone_service.ensure_service_present')
def test_dispatch_service_absent(mock_ensure_service_present,
                                  mock_ensure_endpoint_present,
                                  mock_ensure_service_absent,
                                  mock_ensure_endpoint_absent):
    """ Dispatch: service absent """
    # Setup
    mock_ensure_service_absent.return_value = True
    mock_ensure_endpoint_absent.return_value = True
    manager = mock.MagicMock()
    manager.attach_mock(mock_ensure_service_present, 'ensure_service_present')
    manager.attach_mock(mock_ensure_service_absent, 'ensure_service_absent')
    manager.attach_mock(mock_ensure_endpoint_present,
                        'ensure_endpoint_present')
    manager.attach_mock(mock_ensure_endpoint_absent,
                        'ensure_endpoint_absent')

    keystone = setup()
    name = "keystone"
    service_type = "identity"
    description = "Keystone Identity Service"
    region = "RegionOne"
    state = "absent"
    public_url = "http://192.168.206.130:5000/v2.0"
    internal_url = "http://192.168.206.130:5000/v2.0"
    admin_url = "http://192.168.206.130:35357/v2.0"
    check_mode = False

    # Code under test
    keystone_service.dispatch(keystone, name, service_type, description,
            public_url, internal_url, admin_url, region, state, check_mode)

    expected_calls = [
        mock.call.ensure_endpoint_absent(keystone, name, check_mode),
        mock.call.ensure_service_absent(keystone, name, check_mode)
    ]

    assert_list_equal(manager.mock_calls, expected_calls)


def test_ensure_service_present_when_present():
    """ ensure_services_present when the service is present"""
    # Setup
    keystone = setup()
    name = "keystone"
    service_type = "identity"
    description = "Keystone Identity Service"
    check_mode = False

    # Code under test
    (changed, id) = keystone_service.ensure_service_present(keystone, name,
                        service_type, description, check_mode)

    # Assertions
    assert not changed
    assert_equal(id, "b6a7ff03f2574cd9b5c7c61186e0d781")

def test_ensure_service_present_when_present_check():
    """ ensure_services_present when the service is present, check mode"""
    # Setup
    keystone = setup()
    name = "keystone"
    service_type = "identity"
    description = "Keystone Identity Service"
    check_mode = True

    # Code under test
    (changed, id) = keystone_service.ensure_service_present(keystone, name,
                        service_type, description, check_mode)

    # Assertions
    assert not changed
    assert_equal(id, "b6a7ff03f2574cd9b5c7c61186e0d781")


def test_ensure_service_present_when_absent():
    """ ensure_services_present when the service is absent"""
    # Setup
    keystone = setup()
    service = mock.Mock(id="a7ebed35051147d4abbe2ee049eeb346")
    keystone.services.create = mock.Mock(return_value=service)
    name = "nova"
    service_type = "compute"
    description = "Compute Service"
    check_mode = False

    # Code under test
    (changed, id) = keystone_service.ensure_service_present(keystone, name,
                        service_type, description, check_mode)

    # Assertions
    assert changed
    assert_equal(id, "a7ebed35051147d4abbe2ee049eeb346")
    keystone.services.create.assert_called_with(name=name,
                                                service_type=service_type,
                                                description=description)


def test_ensure_service_present_when_absent_check():
    """ ensure_services_present when the service is absent, check mode"""
    # Setup
    keystone = setup()
    service = mock.Mock(id="a7ebed35051147d4abbe2ee049eeb346")
    keystone.services.create = mock.Mock(return_value=service)
    name = "nova"
    service_type = "compute"
    description = "Compute Service"
    check_mode = True

    # Code under test
    (changed, id) = keystone_service.ensure_service_present(keystone, name,
                        service_type, description, check_mode)

    # Assertions
    assert changed
    assert_equal(id, None)
    assert not keystone.services.create.called


def test_get_endpoint_present():
    """ get_endpoint when endpoint is present """
    keystone = setup()

    endpoint = keystone_service.get_endpoint(keystone, "keystone")

    assert_equal(endpoint.id, "600759628a214eb7b3acde39b1e85180")


def test_ensure_endpoint_present_when_present():
    """ ensure_endpoint_present when the endpoint is present """
    # Setup
    keystone = setup()
    name = "keystone"
    public_url = "http://192.168.206.130:5000/v2.0"
    internal_url = "http://192.168.206.130:5000/v2.0"
    admin_url = "http://192.168.206.130:35357/v2.0"
    region = "RegionOne"
    check_mode = False

    # Code under test
    (changed, id) = keystone_service.ensure_endpoint_present(keystone, name,
                        public_url, internal_url, admin_url, region,
                        check_mode)

    # Assertions
    assert not changed
    assert_equal(id, "600759628a214eb7b3acde39b1e85180")


def test_ensure_endpoint_present_when_present_check():
    """ ensure_endpoint_present when the endpoint is present, check mode"""
    # Setup
    keystone = setup()
    name = "keystone"
    public_url = "http://192.168.206.130:5000/v2.0"
    internal_url = "http://192.168.206.130:5000/v2.0"
    admin_url = "http://192.168.206.130:35357/v2.0"
    region = "RegionOne"
    check_mode = True

    # Code under test
    (changed, id) = keystone_service.ensure_endpoint_present(keystone, name,
                        public_url, internal_url, admin_url, region, check_mode)

    # Assertions
    assert not changed
    assert_equal(id, "600759628a214eb7b3acde39b1e85180")


def test_ensure_endpoint_present_when_absent():
    """ ensure_endpoint_present when the endpoint is absent """
    # Setup
    keystone = setup()
    # Mock out the endpoints create
    endpoint = mock.Mock(id="622386d836b14fd986d9cec7504d208a",
                 publicurl="http://192.168.206.130:8774/v2/%(tenant_id)s",
                 internalurl="http://192.168.206.130:8774/v2/%(tenant_id)s",
                 adminurl="http://192.168.206.130:8774/v2/%(tenant_id)s",
                 region="RegionOne")

    keystone.endpoints.create = mock.Mock(return_value=endpoint)

    # We need to add a service, but not an endpoint
    service = mock.Mock(id="0ad62de6cfe044c7a77ad3a7f2851b5d",
                        type="compute",
                        description="Compute Service")
    service.name = "nova"
    keystone.services.list.return_value.append(service)

    name = "nova"
    public_url = "http://192.168.206.130:8774/v2/%(tenant_id)s"
    internal_url = "http://192.168.206.130:8774/v2/%(tenant_id)s"
    admin_url = "http://192.168.206.130:8774/v2/%(tenant_id)s"
    region = "RegionOne"
    check_mode = False

    # Code under test
    (changed, id) = keystone_service.ensure_endpoint_present(keystone, name,
                        public_url, internal_url, admin_url, region,
                        check_mode)

    # Assertions
    assert changed
    assert_equal(id, "622386d836b14fd986d9cec7504d208a")
    keystone.endpoints.create.assert_called_with(
        service_id="0ad62de6cfe044c7a77ad3a7f2851b5d",
         publicurl="http://192.168.206.130:8774/v2/%(tenant_id)s",
         internalurl="http://192.168.206.130:8774/v2/%(tenant_id)s",
         adminurl="http://192.168.206.130:8774/v2/%(tenant_id)s",
        region="RegionOne")


def test_ensure_endpoint_present_when_absent_check():
    """ ensure_endpoint_present when the endpoint is absent, check mode"""
    # Setup
    keystone = setup()
    # We need to add a service, but not an endpoint
    service = mock.Mock(id="0ad62de6cfe044c7a77ad3a7f2851b5d",
                        type="compute",
                        description="Compute Service")
    service.name = "nova"
    keystone.services.list.return_value.append(service)


    name = "nova"
    public_url = "http://192.168.206.130:8774/v2/%(tenant_id)s"
    internal_url = "http://192.168.206.130:8774/v2/%(tenant_id)s"
    admin_url = "http://192.168.206.130:8774/v2/%(tenant_id)s"
    region = "RegionOne"
    check_mode = True

    # Code under test
    (changed, id) = keystone_service.ensure_endpoint_present(keystone, name,
                        public_url, internal_url, admin_url, region, check_mode)

    # Assertions
    assert changed
    assert_is_none(id)
    assert not keystone.endpoints.create.called