blob: b538e849de5bd2347f4bd5395f8f92fc5e8252c0 [file] [log] [blame]
Ash Wilsonfc1af5a2014-10-08 09:10:41 -04001package openstack
2
3import (
4 "strings"
5 "testing"
6
Jon Perritt27249f42016-02-18 10:35:59 -06007 "github.com/gophercloud/gophercloud"
8 tokens2 "github.com/gophercloud/gophercloud/openstack/identity/v2/tokens"
9 tokens3 "github.com/gophercloud/gophercloud/openstack/identity/v3/tokens"
10 th "github.com/gophercloud/gophercloud/testhelper"
Ash Wilsonfc1af5a2014-10-08 09:10:41 -040011)
12
13// Service catalog fixtures take too much vertical space!
14var catalog2 = tokens2.ServiceCatalog{
15 Entries: []tokens2.CatalogEntry{
16 tokens2.CatalogEntry{
17 Type: "same",
18 Name: "same",
19 Endpoints: []tokens2.Endpoint{
20 tokens2.Endpoint{
21 Region: "same",
22 PublicURL: "https://public.correct.com/",
23 InternalURL: "https://internal.correct.com/",
24 AdminURL: "https://admin.correct.com/",
25 },
26 tokens2.Endpoint{
27 Region: "different",
28 PublicURL: "https://badregion.com/",
29 },
30 },
31 },
32 tokens2.CatalogEntry{
33 Type: "same",
34 Name: "different",
35 Endpoints: []tokens2.Endpoint{
36 tokens2.Endpoint{
37 Region: "same",
38 PublicURL: "https://badname.com/",
39 },
40 tokens2.Endpoint{
41 Region: "different",
42 PublicURL: "https://badname.com/+badregion",
43 },
44 },
45 },
46 tokens2.CatalogEntry{
47 Type: "different",
48 Name: "different",
49 Endpoints: []tokens2.Endpoint{
50 tokens2.Endpoint{
51 Region: "same",
52 PublicURL: "https://badtype.com/+badname",
53 },
54 tokens2.Endpoint{
55 Region: "different",
56 PublicURL: "https://badtype.com/+badregion+badname",
57 },
58 },
59 },
60 },
61}
62
63func TestV2EndpointExact(t *testing.T) {
64 expectedURLs := map[gophercloud.Availability]string{
65 gophercloud.AvailabilityPublic: "https://public.correct.com/",
66 gophercloud.AvailabilityAdmin: "https://admin.correct.com/",
67 gophercloud.AvailabilityInternal: "https://internal.correct.com/",
68 }
69
70 for availability, expected := range expectedURLs {
71 actual, err := V2EndpointURL(&catalog2, gophercloud.EndpointOpts{
72 Type: "same",
73 Name: "same",
74 Region: "same",
75 Availability: availability,
76 })
77 th.AssertNoErr(t, err)
78 th.CheckEquals(t, expected, actual)
79 }
80}
81
82func TestV2EndpointNone(t *testing.T) {
Jon Perritteb015632016-02-21 19:56:53 -060083 _, actual := V2EndpointURL(&catalog2, gophercloud.EndpointOpts{
Ash Wilsonfc1af5a2014-10-08 09:10:41 -040084 Type: "nope",
85 Availability: gophercloud.AvailabilityPublic,
86 })
Jon Perritteb015632016-02-21 19:56:53 -060087 expected := &gophercloud.ErrEndpointNotFound{}
88 th.CheckEquals(t, expected.Error(), actual.Error())
Ash Wilsonfc1af5a2014-10-08 09:10:41 -040089}
90
91func TestV2EndpointMultiple(t *testing.T) {
92 _, err := V2EndpointURL(&catalog2, gophercloud.EndpointOpts{
93 Type: "same",
94 Region: "same",
95 Availability: gophercloud.AvailabilityPublic,
96 })
97 if !strings.HasPrefix(err.Error(), "Discovered 2 matching endpoints:") {
98 t.Errorf("Received unexpected error: %v", err)
99 }
100}
101
102func TestV2EndpointBadAvailability(t *testing.T) {
103 _, err := V2EndpointURL(&catalog2, gophercloud.EndpointOpts{
104 Type: "same",
105 Name: "same",
106 Region: "same",
107 Availability: "wat",
108 })
Guillaume Giamarchib2663b22015-04-01 01:23:29 +0200109 th.CheckEquals(t, "Unexpected availability in endpoint query: wat", err.Error())
Ash Wilsonfc1af5a2014-10-08 09:10:41 -0400110}
Ash Wilson61c49a52014-10-08 14:15:04 -0400111
Guillaume Giamarchib2663b22015-04-01 01:23:29 +0200112var catalog3 = tokens3.ServiceCatalog{
113 Entries: []tokens3.CatalogEntry{
114 tokens3.CatalogEntry{
115 Type: "same",
116 Name: "same",
117 Endpoints: []tokens3.Endpoint{
118 tokens3.Endpoint{
119 ID: "1",
120 Region: "same",
121 Interface: "public",
122 URL: "https://public.correct.com/",
Ash Wilson61c49a52014-10-08 14:15:04 -0400123 },
Guillaume Giamarchib2663b22015-04-01 01:23:29 +0200124 tokens3.Endpoint{
125 ID: "2",
126 Region: "same",
127 Interface: "admin",
128 URL: "https://admin.correct.com/",
129 },
130 tokens3.Endpoint{
131 ID: "3",
132 Region: "same",
133 Interface: "internal",
134 URL: "https://internal.correct.com/",
135 },
136 tokens3.Endpoint{
137 ID: "4",
138 Region: "different",
139 Interface: "public",
140 URL: "https://badregion.com/",
141 },
142 },
143 },
144 tokens3.CatalogEntry{
145 Type: "same",
146 Name: "different",
147 Endpoints: []tokens3.Endpoint{
148 tokens3.Endpoint{
149 ID: "5",
150 Region: "same",
151 Interface: "public",
152 URL: "https://badname.com/",
153 },
154 tokens3.Endpoint{
155 ID: "6",
156 Region: "different",
157 Interface: "public",
158 URL: "https://badname.com/+badregion",
159 },
160 },
161 },
162 tokens3.CatalogEntry{
163 Type: "different",
164 Name: "different",
165 Endpoints: []tokens3.Endpoint{
166 tokens3.Endpoint{
167 ID: "7",
168 Region: "same",
169 Interface: "public",
170 URL: "https://badtype.com/+badname",
171 },
172 tokens3.Endpoint{
173 ID: "8",
174 Region: "different",
175 Interface: "public",
176 URL: "https://badtype.com/+badregion+badname",
177 },
178 },
179 },
180 },
Ash Wilson61c49a52014-10-08 14:15:04 -0400181}
182
183func TestV3EndpointExact(t *testing.T) {
Guillaume Giamarchib2663b22015-04-01 01:23:29 +0200184 expectedURLs := map[gophercloud.Availability]string{
185 gophercloud.AvailabilityPublic: "https://public.correct.com/",
186 gophercloud.AvailabilityAdmin: "https://admin.correct.com/",
187 gophercloud.AvailabilityInternal: "https://internal.correct.com/",
188 }
Ash Wilson61c49a52014-10-08 14:15:04 -0400189
Guillaume Giamarchib2663b22015-04-01 01:23:29 +0200190 for availability, expected := range expectedURLs {
191 actual, err := V3EndpointURL(&catalog3, gophercloud.EndpointOpts{
192 Type: "same",
193 Name: "same",
194 Region: "same",
195 Availability: availability,
196 })
197 th.AssertNoErr(t, err)
198 th.CheckEquals(t, expected, actual)
199 }
200}
201
202func TestV3EndpointNone(t *testing.T) {
Jon Perritteb015632016-02-21 19:56:53 -0600203 _, actual := V3EndpointURL(&catalog3, gophercloud.EndpointOpts{
Guillaume Giamarchib2663b22015-04-01 01:23:29 +0200204 Type: "nope",
205 Availability: gophercloud.AvailabilityPublic,
206 })
Jon Perritteb015632016-02-21 19:56:53 -0600207 expected := &gophercloud.ErrEndpointNotFound{}
208 th.CheckEquals(t, expected.Error(), actual.Error())
Guillaume Giamarchib2663b22015-04-01 01:23:29 +0200209}
210
211func TestV3EndpointMultiple(t *testing.T) {
212 _, err := V3EndpointURL(&catalog3, gophercloud.EndpointOpts{
213 Type: "same",
214 Region: "same",
215 Availability: gophercloud.AvailabilityPublic,
216 })
217 if !strings.HasPrefix(err.Error(), "Discovered 2 matching endpoints:") {
218 t.Errorf("Received unexpected error: %v", err)
219 }
220}
221
222func TestV3EndpointBadAvailability(t *testing.T) {
223 _, err := V3EndpointURL(&catalog3, gophercloud.EndpointOpts{
Ash Wilson61c49a52014-10-08 14:15:04 -0400224 Type: "same",
225 Name: "same",
226 Region: "same",
Guillaume Giamarchib2663b22015-04-01 01:23:29 +0200227 Availability: "wat",
Ash Wilson61c49a52014-10-08 14:15:04 -0400228 })
Guillaume Giamarchib2663b22015-04-01 01:23:29 +0200229 th.CheckEquals(t, "Unexpected availability in endpoint query: wat", err.Error())
Ash Wilson61c49a52014-10-08 14:15:04 -0400230}