summaryrefslogtreecommitdiff
path: root/trove/tests/unittests/common/test_remote.py
blob: e58562d81a5ff05ba32054699deffe06db4d1947 (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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
# Copyright 2013 OpenStack Foundation
# Copyright 2013 Rackspace Hosting
# Copyright 2013 Hewlett-Packard Development Company, L.P.
# 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.
#
import uuid

from mock import patch, MagicMock
import swiftclient.client
from testtools import ExpectedException, matchers

from trove.common import cfg
from trove.common.context import TroveContext
from trove.common import exception
from trove.common import glance_remote
from trove.common import remote
from trove.tests.fakes.swift import SwiftClientStub
from trove.tests.unittests import trove_testtools


class TestRemote(trove_testtools.TestCase):
    def setUp(self):
        super(TestRemote, self).setUp()

    def tearDown(self):
        super(TestRemote, self).tearDown()

    @patch.object(swiftclient.client.Connection, 'get_auth')
    def test_creation(self, get_auth_mock):
        self.assertIsNotNone(swiftclient.client.Connection())

    def test_create_swift_client(self):
        mock_resp = MagicMock()
        with patch.object(swiftclient.client.Connection, 'get_container',
                          MagicMock(return_value=["text", mock_resp])):
            service_catalog = [{'endpoints': [{'region': 'RegionOne',
                                               'publicURL': 'example.com'}],
                                'type': 'object-store'}]
            client = remote.create_swift_client(TroveContext(
                tenant=uuid.uuid4().hex,
                service_catalog=service_catalog))
            headers, container = client.get_container('bob')
            self.assertIs(headers, "text")
            self.assertIs(container, mock_resp)

    def test_empty_account(self):
        """
        this is an account with no containers and no objects
        """
        # setup expectation
        with SwiftClientStub() as swift_stub:
            swift_stub.with_account('123223')
            # interact
            conn = swiftclient.client.Connection()
            account_info = conn.get_account()
            self.assertThat(account_info, matchers.Not(matchers.Is(None)))
            self.assertThat(len(account_info), matchers.Is(2))
            self.assertThat(account_info, matchers.IsInstance(tuple))
            self.assertThat(account_info[0], matchers.IsInstance(dict))
            self.assertThat(
                account_info[0],
                matchers.KeysEqual('content-length', 'accept-ranges',
                                   'x-timestamp', 'x-trans-id', 'date',
                                   'x-account-bytes-used',
                                   'x-account-container-count',
                                   'content-type',
                                   'x-account-object-count'))
            self.assertThat(account_info[1], matchers.IsInstance(list))
            self.assertThat(len(account_info[1]), matchers.Is(0))

    def test_one_container(self):
        """
        tests to ensure behavior is normal with one container
        """
        # setup expectation
        with SwiftClientStub() as swift_stub:
            swift_stub.with_account('123223')
            cont_name = 'a-container-name'
            swift_stub.with_container(cont_name)
            # interact
            conn = swiftclient.client.Connection()
            conn.get_auth()
            conn.put_container(cont_name)
            # get headers plus container metadata
            self.assertThat(len(conn.get_account()), matchers.Is(2))
            # verify container details
            account_containers = conn.get_account()[1]
            self.assertThat(len(account_containers), matchers.Is(1))
            self.assertThat(account_containers[0],
                            matchers.KeysEqual('count', 'bytes', 'name'))
            self.assertThat(account_containers[0]['name'],
                            matchers.Is(cont_name))
            # get container details
            cont_info = conn.get_container(cont_name)
            self.assertIsNotNone(cont_info)
            self.assertThat(
                cont_info[0],
                matchers.KeysEqual('content-length',
                                   'x-container-object-count', 'accept-ranges',
                                   'x-container-bytes-used', 'x-timestamp',
                                   'x-trans-id', 'date', 'content-type'))
            self.assertThat(len(cont_info[1]), matchers.Equals(0))
            # remove container
            swift_stub.without_container(cont_name)
            with ExpectedException(swiftclient.ClientException):
                conn.get_container(cont_name)
                # ensure there are no more containers in account
            self.assertThat(len(conn.get_account()[1]), matchers.Is(0))

    def test_one_object(self):
        with SwiftClientStub() as swift_stub:
            swift_stub.with_account('123223')
            swift_stub.with_container('bob')
            swift_stub.with_object('bob', 'test', 'test_contents')
            # create connection
            conn = swiftclient.client.Connection()
            # test container lightly
            cont_info = conn.get_container('bob')
            self.assertIsNotNone(cont_info)
            self.assertThat(cont_info[0],
                            matchers.KeysEqual('content-length',
                                               'x-container-object-count',
                                               'accept-ranges',
                                               'x-container-bytes-used',
                                               'x-timestamp',
                                               'x-trans-id',
                                               'date',
                                               'content-type'))
            cont_objects = cont_info[1]
            self.assertThat(len(cont_objects), matchers.Equals(1))
            obj_1 = cont_objects[0]
            self.assertThat(obj_1, matchers.Equals(
                {'bytes': 13, 'last_modified': '2013-03-15T22:10:49.361950',
                 'hash': 'ccc55aefbf92aa66f42b638802c5e7f6', 'name': 'test',
                 'content_type': 'application/octet-stream',
                 'contents': 'test_contents'}))
            # test object api - not much to do here
            self.assertThat(conn.get_object('bob', 'test')[1],
                            matchers.Is('test_contents'))

            # test remove object
            swift_stub.without_object('bob', 'test')
            # interact
            with ExpectedException(swiftclient.ClientException):
                conn.delete_object('bob', 'test')
            self.assertThat(len(conn.get_container('bob')[1]), matchers.Is(0))

    def test_two_objects(self):
        with SwiftClientStub() as swift_stub:
            swift_stub.with_account('123223')
            swift_stub.with_container('bob')
            swift_stub.with_container('bob2')
            swift_stub.with_object('bob', 'test', 'test_contents')
            swift_stub.with_object('bob', 'test2', 'test_contents2')

            conn = swiftclient.client.Connection()

            self.assertIs(len(conn.get_account()), 2)
            cont_info = conn.get_container('bob')
            self.assertIsNotNone(cont_info)
            self.assertThat(cont_info[0],
                            matchers.KeysEqual('content-length',
                                               'x-container-object-count',
                                               'accept-ranges',
                                               'x-container-bytes-used',
                                               'x-timestamp',
                                               'x-trans-id',
                                               'date',
                                               'content-type'))
            self.assertThat(len(cont_info[1]), matchers.Equals(2))
            self.assertThat(cont_info[1][0], matchers.Equals(
                {'bytes': 13, 'last_modified': '2013-03-15T22:10:49.361950',
                 'hash': 'ccc55aefbf92aa66f42b638802c5e7f6', 'name': 'test',
                 'content_type': 'application/octet-stream',
                 'contents': 'test_contents'}))
            self.assertThat(conn.get_object('bob', 'test')[1],
                            matchers.Is('test_contents'))
            self.assertThat(conn.get_object('bob', 'test2')[1],
                            matchers.Is('test_contents2'))

            swift_stub.without_object('bob', 'test')
            with ExpectedException(swiftclient.ClientException):
                conn.delete_object('bob', 'test')
            self.assertThat(len(conn.get_container('bob')[1]), matchers.Is(1))

            swift_stub.without_container('bob')
            with ExpectedException(swiftclient.ClientException):
                conn.get_container('bob')

            self.assertThat(len(conn.get_account()), matchers.Is(2))

    def test_nonexisting_container(self):
        """
        when a container does not exist and is accessed then a 404 is returned
        """

        with SwiftClientStub() as swift_stub:
            swift_stub.with_account('123223')
            swift_stub.with_container('existing')

            conn = swiftclient.client.Connection()

            with ExpectedException(swiftclient.ClientException):
                conn.get_container('nonexisting')

    def test_replace_object(self):
        """
        Test to ensure that if an object is updated the container object
        count is the same and the contents of the object are updated
        """
        with SwiftClientStub() as swift_stub:
            swift_stub.with_account('1223df2')
            swift_stub.with_container('new-container')
            swift_stub.with_object('new-container', 'new-object',
                                   'new-object-contents')

            conn = swiftclient.client.Connection()

            conn.put_object('new-container', 'new-object',
                            'new-object-contents')
            obj_resp = conn.get_object('new-container', 'new-object')
            self.assertThat(obj_resp, matchers.Not(matchers.Is(None)))
            self.assertThat(len(obj_resp), matchers.Is(2))
            self.assertThat(obj_resp[1], matchers.Is('new-object-contents'))

            # set expected behavior - trivial here since it is the intended
            # behavior however keep in mind this is just to support testing of
            # trove components
            swift_stub.with_object('new-container', 'new-object',
                                   'updated-object-contents')

            conn.put_object('new-container', 'new-object',
                            'updated-object-contents')
            obj_resp = conn.get_object('new-container', 'new-object')
            self.assertThat(obj_resp, matchers.Not(matchers.Is(None)))
            self.assertThat(len(obj_resp), matchers.Is(2))
            self.assertThat(obj_resp[1], matchers.Is(
                'updated-object-contents'))
            # ensure object count has not increased
            self.assertThat(len(conn.get_container('new-container')[1]),
                            matchers.Is(1))


class TestCreateCinderClient(trove_testtools.TestCase):
    def setUp(self):
        super(TestCreateCinderClient, self).setUp()
        self.volumev2_public_url = 'http://publicURL/v2'
        self.volume_public_url_region_two = 'http://publicURL-r2/v1'
        self.service_catalog = [
            {
                'endpoints': [
                    {
                        'region': 'RegionOne',
                        'publicURL': self.volumev2_public_url,
                    }
                ],
                'type': 'volumev2'
            },
            {
                'endpoints': [
                    {
                        'region': 'RegionOne',
                        'publicURL': 'http://publicURL-r1/v1',
                    },
                    {
                        'region': 'RegionTwo',
                        'publicURL': self.volume_public_url_region_two,
                    }
                ],
                'type': 'volume'
            }
        ]

    def tearDown(self):
        super(TestCreateCinderClient, self).tearDown()
        cfg.CONF.clear_override('cinder_url')
        cfg.CONF.clear_override('cinder_service_type')
        cfg.CONF.clear_override('os_region_name')

    def test_create_with_no_conf_no_catalog(self):
        self.assertRaises(exception.EmptyCatalog,
                          remote.create_cinder_client,
                          TroveContext())

    def test_create_with_conf_override(self):
        cinder_url_from_conf = 'http://example.com'
        tenant_from_ctx = uuid.uuid4().hex
        cfg.CONF.set_override('cinder_url', cinder_url_from_conf,
                              enforce_type=True)

        client = remote.create_cinder_client(
            TroveContext(tenant=tenant_from_ctx))
        self.assertEqual('%s/%s' % (cinder_url_from_conf, tenant_from_ctx),
                         client.client.management_url)

    def test_create_with_conf_override_trailing_slash(self):
        cinder_url_from_conf = 'http://example.com/'
        tenant_from_ctx = uuid.uuid4().hex
        cfg.CONF.set_override('cinder_url', cinder_url_from_conf,
                              enforce_type=True)
        client = remote.create_cinder_client(
            TroveContext(tenant=tenant_from_ctx))
        self.assertEqual('%s%s' % (cinder_url_from_conf, tenant_from_ctx),
                         client.client.management_url)

    def test_create_with_catalog_and_default_service_type(self):
        client = remote.create_cinder_client(
            TroveContext(service_catalog=self.service_catalog))
        self.assertEqual(self.volumev2_public_url,
                         client.client.management_url)

    def test_create_with_catalog_all_opts(self):
        cfg.CONF.set_override('cinder_service_type', 'volume',
                              enforce_type=True)
        cfg.CONF.set_override('os_region_name', 'RegionTwo',
                              enforce_type=True)
        client = remote.create_cinder_client(
            TroveContext(service_catalog=self.service_catalog))
        self.assertEqual(self.volume_public_url_region_two,
                         client.client.management_url)


class TestCreateNovaClient(trove_testtools.TestCase):
    def setUp(self):
        super(TestCreateNovaClient, self).setUp()
        self.compute_public_url = 'http://publicURL/v2'
        self.computev3_public_url_region_two = 'http://publicURL-r2/v3'
        self.service_catalog = [
            {
                'endpoints': [
                    {
                        'region': 'RegionOne',
                        'publicURL': self.compute_public_url,
                    }
                ],
                'type': 'compute'
            },
            {
                'endpoints': [
                    {
                        'region': 'RegionOne',
                        'publicURL': 'http://publicURL-r1/v1',
                    },
                    {
                        'region': 'RegionTwo',
                        'publicURL': self.computev3_public_url_region_two,
                    }
                ],
                'type': 'computev3'
            }
        ]

    def tearDown(self):
        super(TestCreateNovaClient, self).tearDown()
        cfg.CONF.clear_override('nova_compute_url')
        cfg.CONF.clear_override('nova_compute_service_type')
        cfg.CONF.clear_override('os_region_name')

    def test_create_with_no_conf_no_catalog(self):
        self.assertRaises(exception.EmptyCatalog,
                          remote.create_nova_client,
                          TroveContext())

    def test_create_with_conf_override(self):
        nova_url_from_conf = 'http://example.com'
        tenant_from_ctx = uuid.uuid4().hex
        cfg.CONF.set_override('nova_compute_url', nova_url_from_conf,
                              enforce_type=True)

        client = remote.create_nova_client(
            TroveContext(tenant=tenant_from_ctx))
        self.assertEqual('%s/%s' % (nova_url_from_conf, tenant_from_ctx),
                         client.client.management_url)

    def test_create_with_conf_override_trailing_slash(self):
        nova_url_from_conf = 'http://example.com/'
        tenant_from_ctx = uuid.uuid4().hex
        cfg.CONF.set_override('nova_compute_url', nova_url_from_conf,
                              enforce_type=True)
        client = remote.create_nova_client(
            TroveContext(tenant=tenant_from_ctx))
        self.assertEqual('%s%s' % (nova_url_from_conf, tenant_from_ctx),
                         client.client.management_url)

    def test_create_with_catalog_and_default_service_type(self):
        client = remote.create_nova_client(
            TroveContext(service_catalog=self.service_catalog))
        self.assertEqual(self.compute_public_url,
                         client.client.management_url)

    def test_create_with_catalog_all_opts(self):
        cfg.CONF.set_override('nova_compute_service_type', 'computev3',
                              enforce_type=True)
        cfg.CONF.set_override('os_region_name', 'RegionTwo',
                              enforce_type=True)
        client = remote.create_nova_client(
            TroveContext(service_catalog=self.service_catalog))
        self.assertEqual(self.computev3_public_url_region_two,
                         client.client.management_url)

    def test_create_admin_client(self):
        nova_url_from_conf = 'http://adminexample.com/'
        cfg.CONF.set_override('nova_compute_url', nova_url_from_conf,
                              enforce_type=True)
        admin_user = 'admin1'
        admin_pass = 'adminpwd'
        admin_tenant_id = uuid.uuid4().hex
        admin_client = remote.create_admin_nova_client(
            TroveContext(user=admin_user,
                         auth_token=admin_pass,
                         tenant=admin_tenant_id))
        # self.assertEqual(admin_user, admin_client.client.user)
        # self.assertEqual(admin_pass, admin_client.client.password)
        self.assertEqual('%s%s' % (nova_url_from_conf, admin_tenant_id),
                         admin_client.client.management_url)


class TestCreateHeatClient(trove_testtools.TestCase):
    def setUp(self):
        super(TestCreateHeatClient, self).setUp()
        self.heat_public_url = 'http://publicURL/v2'
        self.heatv3_public_url_region_two = 'http://publicURL-r2/v3'
        self.service_catalog = [
            {
                'endpoints': [
                    {
                        'region': 'RegionOne',
                        'publicURL': self.heat_public_url,
                    }
                ],
                'type': 'orchestration'
            },
            {
                'endpoints': [
                    {
                        'region': 'RegionOne',
                        'publicURL': 'http://publicURL-r1/v1',
                    },
                    {
                        'region': 'RegionTwo',
                        'publicURL': self.heatv3_public_url_region_two,
                    }
                ],
                'type': 'orchestrationv3'
            }
        ]

    def tearDown(self):
        super(TestCreateHeatClient, self).tearDown()
        cfg.CONF.clear_override('heat_url')
        cfg.CONF.clear_override('heat_service_type')
        cfg.CONF.clear_override('os_region_name')

    def test_create_with_no_conf_no_catalog(self):
        self.assertRaises(exception.EmptyCatalog,
                          remote.create_heat_client,
                          TroveContext())

    def test_create_with_conf_override(self):
        heat_url_from_conf = 'http://example.com'
        tenant_from_ctx = uuid.uuid4().hex
        cfg.CONF.set_override('heat_url', heat_url_from_conf,
                              enforce_type=True)

        client = remote.create_heat_client(
            TroveContext(tenant=tenant_from_ctx))
        self.assertEqual('%s/%s' % (heat_url_from_conf, tenant_from_ctx),
                         client.http_client.endpoint)

    def test_create_with_conf_override_trailing_slash(self):
        heat_url_from_conf = 'http://example.com/'
        tenant_from_ctx = uuid.uuid4().hex
        cfg.CONF.set_override('heat_url', heat_url_from_conf,
                              enforce_type=True)
        client = remote.create_heat_client(
            TroveContext(tenant=tenant_from_ctx))
        self.assertEqual('%s%s' % (heat_url_from_conf, tenant_from_ctx),
                         client.http_client.endpoint)

    def test_create_with_catalog_and_default_service_type(self):
        client = remote.create_heat_client(
            TroveContext(service_catalog=self.service_catalog))
        self.assertEqual(self.heat_public_url,
                         client.http_client.endpoint)

    def test_create_with_catalog_all_opts(self):
        cfg.CONF.set_override('heat_service_type', 'orchestrationv3',
                              enforce_type=True)
        cfg.CONF.set_override('os_region_name', 'RegionTwo',
                              enforce_type=True)
        client = remote.create_heat_client(
            TroveContext(service_catalog=self.service_catalog))
        self.assertEqual(self.heatv3_public_url_region_two,
                         client.http_client.endpoint)


class TestCreateSwiftClient(trove_testtools.TestCase):
    def setUp(self):
        super(TestCreateSwiftClient, self).setUp()
        self.swift_public_url = 'http://publicURL/v2'
        self.swiftv3_public_url_region_two = 'http://publicURL-r2/v3'
        self.service_catalog = [
            {
                'endpoints': [
                    {
                        'region': 'RegionOne',
                        'publicURL': self.swift_public_url,
                    }
                ],
                'type': 'object-store'
            },
            {
                'endpoints': [
                    {
                        'region': 'RegionOne',
                        'publicURL': 'http://publicURL-r1/v1',
                    },
                    {
                        'region': 'RegionTwo',
                        'publicURL': self.swiftv3_public_url_region_two,
                    }
                ],
                'type': 'object-storev3'
            }
        ]

    def tearDown(self):
        super(TestCreateSwiftClient, self).tearDown()
        cfg.CONF.clear_override('swift_url')
        cfg.CONF.clear_override('swift_service_type')
        cfg.CONF.clear_override('os_region_name')

    def test_create_with_no_conf_no_catalog(self):
        self.assertRaises(exception.EmptyCatalog,
                          remote.create_swift_client,
                          TroveContext())

    def test_create_with_conf_override(self):
        swift_url_from_conf = 'http://example.com/AUTH_'
        tenant_from_ctx = uuid.uuid4().hex
        cfg.CONF.set_override('swift_url', swift_url_from_conf,
                              enforce_type=True)

        client = remote.create_swift_client(
            TroveContext(tenant=tenant_from_ctx))
        self.assertEqual('%s%s' % (swift_url_from_conf, tenant_from_ctx),
                         client.url)

    def test_create_with_catalog_and_default_service_type(self):
        client = remote.create_swift_client(
            TroveContext(service_catalog=self.service_catalog))
        self.assertEqual(self.swift_public_url,
                         client.url)

    def test_create_with_catalog_all_opts(self):
        cfg.CONF.set_override('swift_service_type', 'object-storev3',
                              enforce_type=True)
        cfg.CONF.set_override('os_region_name', 'RegionTwo',
                              enforce_type=True)
        client = remote.create_swift_client(
            TroveContext(service_catalog=self.service_catalog))
        self.assertEqual(self.swiftv3_public_url_region_two,
                         client.url)


class TestCreateGlanceClient(trove_testtools.TestCase):
    def setUp(self):
        super(TestCreateGlanceClient, self).setUp()
        self.glance_public_url = 'http://publicURL/v2'
        self.glancev3_public_url_region_two = 'http://publicURL-r2/v3'
        self.service_catalog = [
            {
                'endpoints': [
                    {
                        'region': 'RegionOne',
                        'publicURL': self.glance_public_url,
                    }
                ],
                'type': 'image'
            },
            {
                'endpoints': [
                    {
                        'region': 'RegionOne',
                        'publicURL': 'http://publicURL-r1/v1',
                    },
                    {
                        'region': 'RegionTwo',
                        'publicURL': self.glancev3_public_url_region_two,
                    }
                ],
                'type': 'imagev3'
            }
        ]

    def test_create_with_no_conf_no_catalog(self):
        self.assertRaises(exception.EmptyCatalog,
                          glance_remote.create_glance_client,
                          TroveContext())

    def test_create(self):
        client = glance_remote.create_glance_client(
            TroveContext(service_catalog=self.service_catalog))
        self.assertIsNotNone(client)


class TestEndpoints(trove_testtools.TestCase):
    """
    Copied from glance/tests/unit/test_auth.py.
    """
    def setUp(self):
        super(TestEndpoints, self).setUp()

        self.service_catalog = [
            {
                'endpoint_links': [],
                'endpoints': [
                    {
                        'adminURL': 'http://localhost:8080/',
                        'region': 'RegionOne',
                        'internalURL': 'http://internalURL/',
                        'publicURL': 'http://publicURL/',
                    },
                    {
                        'adminURL': 'http://localhost:8081/',
                        'region': 'RegionTwo',
                        'internalURL': 'http://internalURL2/',
                        'publicURL': 'http://publicURL2/',
                    },
                ],
                'type': 'object-store',
                'name': 'Object Storage Service',
            }
        ]

    def test_get_endpoint_empty_catalog(self):
        self.assertRaises(exception.EmptyCatalog,
                          remote.get_endpoint,
                          None)

    def test_get_endpoint_with_custom_server_type(self):
        endpoint = remote.get_endpoint(self.service_catalog,
                                       service_type='object-store',
                                       endpoint_region='RegionOne')
        self.assertEqual('http://publicURL/', endpoint)

    def test_get_endpoint_with_custom_endpoint_type(self):
        endpoint = remote.get_endpoint(self.service_catalog,
                                       service_type='object-store',
                                       endpoint_type='internalURL',
                                       endpoint_region='RegionOne')
        self.assertEqual('http://internalURL/', endpoint)

    def test_get_endpoint_raises_with_invalid_service_type(self):
        self.assertRaises(exception.NoServiceEndpoint,
                          remote.get_endpoint,
                          self.service_catalog,
                          service_type='foo')

    def test_get_endpoint_raises_with_invalid_endpoint_type(self):
        self.assertRaises(exception.NoServiceEndpoint,
                          remote.get_endpoint,
                          self.service_catalog,
                          service_type='object-store',
                          endpoint_type='foo',
                          endpoint_region='RegionOne')

    def test_get_endpoint_raises_with_invalid_endpoint_region(self):
        self.assertRaises(exception.NoServiceEndpoint,
                          remote.get_endpoint,
                          self.service_catalog,
                          service_type='object-store',
                          endpoint_region='foo',
                          endpoint_type='internalURL')

    def test_get_endpoint_ignores_missing_type(self):
        service_catalog = [
            {
                'name': 'Other Service',
            },
            {
                'endpoint_links': [],
                'endpoints': [
                    {
                        'adminURL': 'http://localhost:8080/',
                        'region': 'RegionOne',
                        'internalURL': 'http://internalURL/',
                        'publicURL': 'http://publicURL/',
                    },
                    {
                        'adminURL': 'http://localhost:8081/',
                        'region': 'RegionTwo',
                        'internalURL': 'http://internalURL2/',
                        'publicURL': 'http://publicURL2/',
                    },
                ],
                'type': 'object-store',
                'name': 'Object Storage Service',
            }
        ]
        endpoint = remote.get_endpoint(service_catalog,
                                       service_type='object-store',
                                       endpoint_region='RegionOne')
        self.assertEqual('http://publicURL/', endpoint)