blob: 8e65918abfe7ef2f0020109fc68f35ac1d20c942 [file] [log] [blame]
Ash Wilsonfc1af5a2014-10-08 09:10:41 -04001package openstack
2
3import (
4 "strings"
5 "testing"
6
7 "github.com/rackspace/gophercloud"
8 tokens2 "github.com/rackspace/gophercloud/openstack/identity/v2/tokens"
Guillaume Giamarchib2663b22015-04-01 01:23:29 +02009 tokens3 "github.com/rackspace/gophercloud/openstack/identity/v3/tokens"
Ash Wilsonfc1af5a2014-10-08 09:10:41 -040010 th "github.com/rackspace/gophercloud/testhelper"
11)
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) {
83 _, err := V2EndpointURL(&catalog2, gophercloud.EndpointOpts{
84 Type: "nope",
85 Availability: gophercloud.AvailabilityPublic,
86 })
87 th.CheckEquals(t, gophercloud.ErrEndpointNotFound, err)
88}
89
90func TestV2EndpointMultiple(t *testing.T) {
91 _, err := V2EndpointURL(&catalog2, gophercloud.EndpointOpts{
92 Type: "same",
93 Region: "same",
94 Availability: gophercloud.AvailabilityPublic,
95 })
96 if !strings.HasPrefix(err.Error(), "Discovered 2 matching endpoints:") {
97 t.Errorf("Received unexpected error: %v", err)
98 }
99}
100
101func TestV2EndpointBadAvailability(t *testing.T) {
102 _, err := V2EndpointURL(&catalog2, gophercloud.EndpointOpts{
103 Type: "same",
104 Name: "same",
105 Region: "same",
106 Availability: "wat",
107 })
Guillaume Giamarchib2663b22015-04-01 01:23:29 +0200108 th.CheckEquals(t, "Unexpected availability in endpoint query: wat", err.Error())
Ash Wilsonfc1af5a2014-10-08 09:10:41 -0400109}
Ash Wilson61c49a52014-10-08 14:15:04 -0400110
Guillaume Giamarchib2663b22015-04-01 01:23:29 +0200111var catalog3 = tokens3.ServiceCatalog{
112 Entries: []tokens3.CatalogEntry{
113 tokens3.CatalogEntry{
114 Type: "same",
115 Name: "same",
116 Endpoints: []tokens3.Endpoint{
117 tokens3.Endpoint{
118 ID: "1",
119 Region: "same",
120 Interface: "public",
121 URL: "https://public.correct.com/",
Ash Wilson61c49a52014-10-08 14:15:04 -0400122 },
Guillaume Giamarchib2663b22015-04-01 01:23:29 +0200123 tokens3.Endpoint{
124 ID: "2",
125 Region: "same",
126 Interface: "admin",
127 URL: "https://admin.correct.com/",
128 },
129 tokens3.Endpoint{
130 ID: "3",
131 Region: "same",
132 Interface: "internal",
133 URL: "https://internal.correct.com/",
134 },
135 tokens3.Endpoint{
136 ID: "4",
137 Region: "different",
138 Interface: "public",
139 URL: "https://badregion.com/",
140 },
141 },
142 },
143 tokens3.CatalogEntry{
144 Type: "same",
145 Name: "different",
146 Endpoints: []tokens3.Endpoint{
147 tokens3.Endpoint{
148 ID: "5",
149 Region: "same",
150 Interface: "public",
151 URL: "https://badname.com/",
152 },
153 tokens3.Endpoint{
154 ID: "6",
155 Region: "different",
156 Interface: "public",
157 URL: "https://badname.com/+badregion",
158 },
159 },
160 },
161 tokens3.CatalogEntry{
162 Type: "different",
163 Name: "different",
164 Endpoints: []tokens3.Endpoint{
165 tokens3.Endpoint{
166 ID: "7",
167 Region: "same",
168 Interface: "public",
169 URL: "https://badtype.com/+badname",
170 },
171 tokens3.Endpoint{
172 ID: "8",
173 Region: "different",
174 Interface: "public",
175 URL: "https://badtype.com/+badregion+badname",
176 },
177 },
178 },
179 },
Ash Wilson61c49a52014-10-08 14:15:04 -0400180}
181
182func TestV3EndpointExact(t *testing.T) {
Guillaume Giamarchib2663b22015-04-01 01:23:29 +0200183 expectedURLs := map[gophercloud.Availability]string{
184 gophercloud.AvailabilityPublic: "https://public.correct.com/",
185 gophercloud.AvailabilityAdmin: "https://admin.correct.com/",
186 gophercloud.AvailabilityInternal: "https://internal.correct.com/",
187 }
Ash Wilson61c49a52014-10-08 14:15:04 -0400188
Guillaume Giamarchib2663b22015-04-01 01:23:29 +0200189 for availability, expected := range expectedURLs {
190 actual, err := V3EndpointURL(&catalog3, gophercloud.EndpointOpts{
191 Type: "same",
192 Name: "same",
193 Region: "same",
194 Availability: availability,
195 })
196 th.AssertNoErr(t, err)
197 th.CheckEquals(t, expected, actual)
198 }
199}
200
201func TestV3EndpointNone(t *testing.T) {
202 _, err := V3EndpointURL(&catalog3, gophercloud.EndpointOpts{
203 Type: "nope",
204 Availability: gophercloud.AvailabilityPublic,
205 })
206 th.CheckEquals(t, gophercloud.ErrEndpointNotFound, err)
207}
208
209func TestV3EndpointMultiple(t *testing.T) {
210 _, err := V3EndpointURL(&catalog3, gophercloud.EndpointOpts{
211 Type: "same",
212 Region: "same",
213 Availability: gophercloud.AvailabilityPublic,
214 })
215 if !strings.HasPrefix(err.Error(), "Discovered 2 matching endpoints:") {
216 t.Errorf("Received unexpected error: %v", err)
217 }
218}
219
220func TestV3EndpointBadAvailability(t *testing.T) {
221 _, err := V3EndpointURL(&catalog3, gophercloud.EndpointOpts{
Ash Wilson61c49a52014-10-08 14:15:04 -0400222 Type: "same",
223 Name: "same",
224 Region: "same",
Guillaume Giamarchib2663b22015-04-01 01:23:29 +0200225 Availability: "wat",
Ash Wilson61c49a52014-10-08 14:15:04 -0400226 })
Guillaume Giamarchib2663b22015-04-01 01:23:29 +0200227 th.CheckEquals(t, "Unexpected availability in endpoint query: wat", err.Error())
Ash Wilson61c49a52014-10-08 14:15:04 -0400228}