blob: ea7bdd2bf0ff8e3241b8aed1e129ae7b3f239378 [file] [log] [blame]
jrperritt3d966162016-06-06 14:08:54 -05001package testing
Ash Wilsonfc1af5a2014-10-08 09:10:41 -04002
3import (
4 "strings"
5 "testing"
6
Jon Perritt27249f42016-02-18 10:35:59 -06007 "github.com/gophercloud/gophercloud"
jrperritt3d966162016-06-06 14:08:54 -05008 "github.com/gophercloud/gophercloud/openstack"
Jon Perritt27249f42016-02-18 10:35:59 -06009 tokens2 "github.com/gophercloud/gophercloud/openstack/identity/v2/tokens"
10 tokens3 "github.com/gophercloud/gophercloud/openstack/identity/v3/tokens"
11 th "github.com/gophercloud/gophercloud/testhelper"
Ash Wilsonfc1af5a2014-10-08 09:10:41 -040012)
13
14// Service catalog fixtures take too much vertical space!
15var catalog2 = tokens2.ServiceCatalog{
16 Entries: []tokens2.CatalogEntry{
17 tokens2.CatalogEntry{
18 Type: "same",
19 Name: "same",
20 Endpoints: []tokens2.Endpoint{
21 tokens2.Endpoint{
22 Region: "same",
23 PublicURL: "https://public.correct.com/",
24 InternalURL: "https://internal.correct.com/",
25 AdminURL: "https://admin.correct.com/",
26 },
27 tokens2.Endpoint{
28 Region: "different",
29 PublicURL: "https://badregion.com/",
30 },
31 },
32 },
33 tokens2.CatalogEntry{
34 Type: "same",
35 Name: "different",
36 Endpoints: []tokens2.Endpoint{
37 tokens2.Endpoint{
38 Region: "same",
39 PublicURL: "https://badname.com/",
40 },
41 tokens2.Endpoint{
42 Region: "different",
43 PublicURL: "https://badname.com/+badregion",
44 },
45 },
46 },
47 tokens2.CatalogEntry{
48 Type: "different",
49 Name: "different",
50 Endpoints: []tokens2.Endpoint{
51 tokens2.Endpoint{
52 Region: "same",
53 PublicURL: "https://badtype.com/+badname",
54 },
55 tokens2.Endpoint{
56 Region: "different",
57 PublicURL: "https://badtype.com/+badregion+badname",
58 },
59 },
60 },
61 },
62}
63
64func TestV2EndpointExact(t *testing.T) {
65 expectedURLs := map[gophercloud.Availability]string{
66 gophercloud.AvailabilityPublic: "https://public.correct.com/",
67 gophercloud.AvailabilityAdmin: "https://admin.correct.com/",
68 gophercloud.AvailabilityInternal: "https://internal.correct.com/",
69 }
70
71 for availability, expected := range expectedURLs {
jrperritt3d966162016-06-06 14:08:54 -050072 actual, err := openstack.V2EndpointURL(&catalog2, gophercloud.EndpointOpts{
Ash Wilsonfc1af5a2014-10-08 09:10:41 -040073 Type: "same",
74 Name: "same",
75 Region: "same",
76 Availability: availability,
77 })
78 th.AssertNoErr(t, err)
79 th.CheckEquals(t, expected, actual)
80 }
81}
82
83func TestV2EndpointNone(t *testing.T) {
jrperritt3d966162016-06-06 14:08:54 -050084 _, actual := openstack.V2EndpointURL(&catalog2, gophercloud.EndpointOpts{
Ash Wilsonfc1af5a2014-10-08 09:10:41 -040085 Type: "nope",
86 Availability: gophercloud.AvailabilityPublic,
87 })
Jon Perritteb015632016-02-21 19:56:53 -060088 expected := &gophercloud.ErrEndpointNotFound{}
89 th.CheckEquals(t, expected.Error(), actual.Error())
Ash Wilsonfc1af5a2014-10-08 09:10:41 -040090}
91
92func TestV2EndpointMultiple(t *testing.T) {
jrperritt3d966162016-06-06 14:08:54 -050093 _, err := openstack.V2EndpointURL(&catalog2, gophercloud.EndpointOpts{
Ash Wilsonfc1af5a2014-10-08 09:10:41 -040094 Type: "same",
95 Region: "same",
96 Availability: gophercloud.AvailabilityPublic,
97 })
98 if !strings.HasPrefix(err.Error(), "Discovered 2 matching endpoints:") {
99 t.Errorf("Received unexpected error: %v", err)
100 }
101}
102
103func TestV2EndpointBadAvailability(t *testing.T) {
jrperritt3d966162016-06-06 14:08:54 -0500104 _, err := openstack.V2EndpointURL(&catalog2, gophercloud.EndpointOpts{
Ash Wilsonfc1af5a2014-10-08 09:10:41 -0400105 Type: "same",
106 Name: "same",
107 Region: "same",
108 Availability: "wat",
109 })
Guillaume Giamarchib2663b22015-04-01 01:23:29 +0200110 th.CheckEquals(t, "Unexpected availability in endpoint query: wat", err.Error())
Ash Wilsonfc1af5a2014-10-08 09:10:41 -0400111}
Ash Wilson61c49a52014-10-08 14:15:04 -0400112
Guillaume Giamarchib2663b22015-04-01 01:23:29 +0200113var catalog3 = tokens3.ServiceCatalog{
114 Entries: []tokens3.CatalogEntry{
115 tokens3.CatalogEntry{
116 Type: "same",
117 Name: "same",
118 Endpoints: []tokens3.Endpoint{
119 tokens3.Endpoint{
120 ID: "1",
121 Region: "same",
122 Interface: "public",
123 URL: "https://public.correct.com/",
Ash Wilson61c49a52014-10-08 14:15:04 -0400124 },
Guillaume Giamarchib2663b22015-04-01 01:23:29 +0200125 tokens3.Endpoint{
126 ID: "2",
127 Region: "same",
128 Interface: "admin",
129 URL: "https://admin.correct.com/",
130 },
131 tokens3.Endpoint{
132 ID: "3",
133 Region: "same",
134 Interface: "internal",
135 URL: "https://internal.correct.com/",
136 },
137 tokens3.Endpoint{
138 ID: "4",
139 Region: "different",
140 Interface: "public",
141 URL: "https://badregion.com/",
142 },
143 },
144 },
145 tokens3.CatalogEntry{
146 Type: "same",
147 Name: "different",
148 Endpoints: []tokens3.Endpoint{
149 tokens3.Endpoint{
150 ID: "5",
151 Region: "same",
152 Interface: "public",
153 URL: "https://badname.com/",
154 },
155 tokens3.Endpoint{
156 ID: "6",
157 Region: "different",
158 Interface: "public",
159 URL: "https://badname.com/+badregion",
160 },
161 },
162 },
163 tokens3.CatalogEntry{
164 Type: "different",
165 Name: "different",
166 Endpoints: []tokens3.Endpoint{
167 tokens3.Endpoint{
168 ID: "7",
169 Region: "same",
170 Interface: "public",
171 URL: "https://badtype.com/+badname",
172 },
173 tokens3.Endpoint{
174 ID: "8",
175 Region: "different",
176 Interface: "public",
177 URL: "https://badtype.com/+badregion+badname",
178 },
179 },
180 },
181 },
Ash Wilson61c49a52014-10-08 14:15:04 -0400182}
183
184func TestV3EndpointExact(t *testing.T) {
Guillaume Giamarchib2663b22015-04-01 01:23:29 +0200185 expectedURLs := map[gophercloud.Availability]string{
186 gophercloud.AvailabilityPublic: "https://public.correct.com/",
187 gophercloud.AvailabilityAdmin: "https://admin.correct.com/",
188 gophercloud.AvailabilityInternal: "https://internal.correct.com/",
189 }
Ash Wilson61c49a52014-10-08 14:15:04 -0400190
Guillaume Giamarchib2663b22015-04-01 01:23:29 +0200191 for availability, expected := range expectedURLs {
jrperritt3d966162016-06-06 14:08:54 -0500192 actual, err := openstack.V3EndpointURL(&catalog3, gophercloud.EndpointOpts{
Guillaume Giamarchib2663b22015-04-01 01:23:29 +0200193 Type: "same",
194 Name: "same",
195 Region: "same",
196 Availability: availability,
197 })
198 th.AssertNoErr(t, err)
199 th.CheckEquals(t, expected, actual)
200 }
201}
202
203func TestV3EndpointNone(t *testing.T) {
jrperritt3d966162016-06-06 14:08:54 -0500204 _, actual := openstack.V3EndpointURL(&catalog3, gophercloud.EndpointOpts{
Guillaume Giamarchib2663b22015-04-01 01:23:29 +0200205 Type: "nope",
206 Availability: gophercloud.AvailabilityPublic,
207 })
Jon Perritteb015632016-02-21 19:56:53 -0600208 expected := &gophercloud.ErrEndpointNotFound{}
209 th.CheckEquals(t, expected.Error(), actual.Error())
Guillaume Giamarchib2663b22015-04-01 01:23:29 +0200210}
211
212func TestV3EndpointMultiple(t *testing.T) {
jrperritt3d966162016-06-06 14:08:54 -0500213 _, err := openstack.V3EndpointURL(&catalog3, gophercloud.EndpointOpts{
Guillaume Giamarchib2663b22015-04-01 01:23:29 +0200214 Type: "same",
215 Region: "same",
216 Availability: gophercloud.AvailabilityPublic,
217 })
218 if !strings.HasPrefix(err.Error(), "Discovered 2 matching endpoints:") {
219 t.Errorf("Received unexpected error: %v", err)
220 }
221}
222
223func TestV3EndpointBadAvailability(t *testing.T) {
jrperritt3d966162016-06-06 14:08:54 -0500224 _, err := openstack.V3EndpointURL(&catalog3, gophercloud.EndpointOpts{
Ash Wilson61c49a52014-10-08 14:15:04 -0400225 Type: "same",
226 Name: "same",
227 Region: "same",
Guillaume Giamarchib2663b22015-04-01 01:23:29 +0200228 Availability: "wat",
Ash Wilson61c49a52014-10-08 14:15:04 -0400229 })
Guillaume Giamarchib2663b22015-04-01 01:23:29 +0200230 th.CheckEquals(t, "Unexpected availability in endpoint query: wat", err.Error())
Ash Wilson61c49a52014-10-08 14:15:04 -0400231}