blob: b78f01fced9fb8a14252179d6fe82d44c98c5a42 [file] [log] [blame]
Samuel A. Falvo IIfca35b72013-07-02 18:30:28 -07001package gophercloud
2
3import (
Samuel A. Falvo IIb9670762014-01-10 14:26:39 -08004 "os"
Samuel A. Falvo IIfca35b72013-07-02 18:30:28 -07005 "testing"
6)
7
Samuel A. Falvo IIb9670762014-01-10 14:26:39 -08008// TestFFEBCViaEnvVariable exercises only those calls where a region
9// parameter is required, but is provided by an environment variable.
10func TestFFEBCViaEnvVariable(t *testing.T) {
11 changeRegion("RGN")
Samuel A. Falvo IIfca35b72013-07-02 18:30:28 -070012
Samuel A. Falvo IIb9670762014-01-10 14:26:39 -080013 endpoint := FindFirstEndpointByCriteria(
Justin Santa Barbaraa4db50a2013-09-01 10:45:07 -070014 catalog("test", "compute", "http://localhost", "", ""),
Samuel A. Falvo IIfca35b72013-07-02 18:30:28 -070015 ApiCriteria{Name: "test"},
16 )
Samuel A. Falvo IIb9670762014-01-10 14:26:39 -080017 if endpoint.PublicURL != "" {
18 t.Error("If provided, the Region qualifier must exclude endpoints with missing or mismatching regions.")
Samuel A. Falvo IIfca35b72013-07-02 18:30:28 -070019 return
20 }
21
22 endpoint = FindFirstEndpointByCriteria(
Samuel A. Falvo IIb9670762014-01-10 14:26:39 -080023 catalog("test", "compute", "http://localhost", "rgn", ""),
24 ApiCriteria{Name: "test"},
Justin Santa Barbaraa4db50a2013-09-01 10:45:07 -070025 )
26 if endpoint.PublicURL != "http://localhost" {
Samuel A. Falvo IIb9670762014-01-10 14:26:39 -080027 t.Error("Regions are case insensitive.")
Justin Santa Barbaraa4db50a2013-09-01 10:45:07 -070028 return
29 }
30
31 endpoint = FindFirstEndpointByCriteria(
Samuel A. Falvo IIb9670762014-01-10 14:26:39 -080032 catalog("test", "compute", "http://localhost", "rgn", ""),
33 ApiCriteria{Name: "test", VersionId: "2"},
Justin Santa Barbaraa4db50a2013-09-01 10:45:07 -070034 )
35 if endpoint.PublicURL != "" {
Samuel A. Falvo IIb9670762014-01-10 14:26:39 -080036 t.Error("Missing version ID means no match.")
Justin Santa Barbaraa4db50a2013-09-01 10:45:07 -070037 return
38 }
39
40 endpoint = FindFirstEndpointByCriteria(
Samuel A. Falvo IIb9670762014-01-10 14:26:39 -080041 catalog("test", "compute", "http://localhost", "rgn", "3"),
42 ApiCriteria{Name: "test", VersionId: "2"},
43 )
44 if endpoint.PublicURL != "" {
45 t.Error("Mismatched version ID means no match.")
46 return
47 }
48
49 endpoint = FindFirstEndpointByCriteria(
50 catalog("test", "compute", "http://localhost", "rgn", "2"),
51 ApiCriteria{Name: "test", VersionId: "2"},
52 )
53 if endpoint.PublicURL != "http://localhost" {
54 t.Error("All search criteria met; endpoint expected.")
55 return
56 }
57}
58
59// TestFFEBCViaRegionOption exercises only those calls where a region
60// parameter is specified explicitly. The region option overrides
61// any defined OS_REGION_NAME environment setting.
62func TestFFEBCViaRegionOption(t *testing.T) {
63 changeRegion("Starfleet Command")
64
65 endpoint := FindFirstEndpointByCriteria(
Justin Santa Barbaraa4db50a2013-09-01 10:45:07 -070066 catalog("test", "compute", "http://localhost", "", ""),
Samuel A. Falvo IIfca35b72013-07-02 18:30:28 -070067 ApiCriteria{Name: "test", Region: "RGN"},
68 )
69 if endpoint.PublicURL != "" {
70 t.Error("If provided, the Region qualifier must exclude endpoints with missing or mismatching regions.")
71 return
72 }
73
74 endpoint = FindFirstEndpointByCriteria(
Justin Santa Barbaraa4db50a2013-09-01 10:45:07 -070075 catalog("test", "compute", "http://localhost", "rgn", ""),
Samuel A. Falvo IIfca35b72013-07-02 18:30:28 -070076 ApiCriteria{Name: "test", Region: "RGN"},
77 )
78 if endpoint.PublicURL != "http://localhost" {
79 t.Error("Regions are case insensitive.")
80 return
81 }
82
83 endpoint = FindFirstEndpointByCriteria(
Justin Santa Barbaraa4db50a2013-09-01 10:45:07 -070084 catalog("test", "compute", "http://localhost", "rgn", ""),
Samuel A. Falvo IIfca35b72013-07-02 18:30:28 -070085 ApiCriteria{Name: "test", Region: "RGN", VersionId: "2"},
86 )
87 if endpoint.PublicURL != "" {
88 t.Error("Missing version ID means no match.")
89 return
90 }
91
92 endpoint = FindFirstEndpointByCriteria(
Justin Santa Barbaraa4db50a2013-09-01 10:45:07 -070093 catalog("test", "compute", "http://localhost", "rgn", "3"),
Samuel A. Falvo IIfca35b72013-07-02 18:30:28 -070094 ApiCriteria{Name: "test", Region: "RGN", VersionId: "2"},
95 )
96 if endpoint.PublicURL != "" {
97 t.Error("Mismatched version ID means no match.")
98 return
99 }
100
101 endpoint = FindFirstEndpointByCriteria(
Justin Santa Barbaraa4db50a2013-09-01 10:45:07 -0700102 catalog("test", "compute", "http://localhost", "rgn", "2"),
Samuel A. Falvo IIfca35b72013-07-02 18:30:28 -0700103 ApiCriteria{Name: "test", Region: "RGN", VersionId: "2"},
104 )
105 if endpoint.PublicURL != "http://localhost" {
106 t.Error("All search criteria met; endpoint expected.")
107 return
108 }
Samuel A. Falvo IIb9670762014-01-10 14:26:39 -0800109}
110
111// TestFFEBCWithoutRegion exercises only those calls where a region
112// is irrelevant. Just to make sure, though, we enforce Gophercloud
113// from paying any attention to OS_REGION_NAME if it happens to be set.
114func TestFindFirstEndpointByCriteria(t *testing.T) {
115 endpoint := FindFirstEndpointByCriteria([]CatalogEntry{}, ApiCriteria{Name: "test", IgnoreEnvVars: true})
116 if endpoint.PublicURL != "" {
117 t.Error("Not expecting to find anything in an empty service catalog.")
118 return
119 }
120
121 endpoint = FindFirstEndpointByCriteria(
122 []CatalogEntry{
123 {Name: "test"},
124 },
125 ApiCriteria{Name: "test", IgnoreEnvVars: true},
126 )
127 if endpoint.PublicURL != "" {
128 t.Error("Even though we have a matching entry, no endpoints exist")
129 return
130 }
131
132 endpoint = FindFirstEndpointByCriteria(
133 catalog("test", "compute", "http://localhost", "", ""),
134 ApiCriteria{Name: "test", IgnoreEnvVars: true},
135 )
136 if endpoint.PublicURL != "http://localhost" {
137 t.Error("Looking for an endpoint by name but without region or version ID should match first entry endpoint.")
138 return
139 }
140
141 endpoint = FindFirstEndpointByCriteria(
142 catalog("test", "compute", "http://localhost", "", ""),
143 ApiCriteria{Type: "compute", IgnoreEnvVars: true},
144 )
145 if endpoint.PublicURL != "http://localhost" {
146 t.Error("Looking for an endpoint by type but without region or version ID should match first entry endpoint.")
147 return
148 }
149
150 endpoint = FindFirstEndpointByCriteria(
151 catalog("test", "compute", "http://localhost", "", ""),
152 ApiCriteria{Type: "identity", IgnoreEnvVars: true},
153 )
154 if endpoint.PublicURL != "" {
155 t.Error("Returned mismatched type.")
156 return
157 }
Samuel A. Falvo IIfca35b72013-07-02 18:30:28 -0700158
159 endpoint = FindFirstEndpointByCriteria(
Justin Santa Barbaraa4db50a2013-09-01 10:45:07 -0700160 catalog("test", "compute", "http://localhost", "ord", "2"),
Samuel A. Falvo IIb9670762014-01-10 14:26:39 -0800161 ApiCriteria{Name: "test", VersionId: "2", IgnoreEnvVars: true},
Samuel A. Falvo IIfca35b72013-07-02 18:30:28 -0700162 )
163 if endpoint.PublicURL != "http://localhost" {
164 t.Error("Sometimes, you might not care what region your stuff is in.")
165 return
166 }
167}
168
Justin Santa Barbaraa4db50a2013-09-01 10:45:07 -0700169func catalog(name, entry_type, url, region, version string) []CatalogEntry {
Samuel A. Falvo IIfca35b72013-07-02 18:30:28 -0700170 return []CatalogEntry{
Samuel A. Falvo IIc0b07aa2013-08-19 17:13:06 -0700171 {
Samuel A. Falvo IIfca35b72013-07-02 18:30:28 -0700172 Name: name,
Justin Santa Barbaraa4db50a2013-09-01 10:45:07 -0700173 Type: entry_type,
Samuel A. Falvo IIfca35b72013-07-02 18:30:28 -0700174 Endpoints: []EntryEndpoint{
Samuel A. Falvo IIc0b07aa2013-08-19 17:13:06 -0700175 {
Samuel A. Falvo IIfca35b72013-07-02 18:30:28 -0700176 PublicURL: url,
177 Region: region,
178 VersionId: version,
179 },
180 },
181 },
182 }
183}
Samuel A. Falvo IIb9670762014-01-10 14:26:39 -0800184
185func changeRegion(r string) {
186 err := os.Setenv("OS_REGION_NAME", r)
187 if err != nil {
188 panic(err)
189 }
190}