blob: 4420366eac4c2b9e7b92f6927fb13bac56cf7474 [file] [log] [blame]
Samuel A. Falvo IIfca35b72013-07-02 18:30:28 -07001package gophercloud
2
3import (
4 "testing"
5)
6
Samuel A. Falvo IIfca35b72013-07-02 18:30:28 -07007func TestFindFirstEndpointByCriteria(t *testing.T) {
8 endpoint := FindFirstEndpointByCriteria([]CatalogEntry{}, ApiCriteria{Name: "test"})
9 if endpoint.PublicURL != "" {
10 t.Error("Not expecting to find anything in an empty service catalog.")
11 return
12 }
13
14 endpoint = FindFirstEndpointByCriteria(
15 []CatalogEntry{
16 CatalogEntry{Name: "test"},
17 },
18 ApiCriteria{Name: "test"},
19 )
20 if endpoint.PublicURL != "" {
21 t.Error("Even though we have a matching entry, no endpoints exist")
22 return
23 }
24
25 endpoint = FindFirstEndpointByCriteria(
26 catalog("test", "http://localhost", "", ""),
27 ApiCriteria{Name: "test"},
28 )
29 if endpoint.PublicURL != "http://localhost" {
30 t.Error("Looking for an endpoint by name but without region or version ID should match first entry endpoint.")
31 return
32 }
33
34 endpoint = FindFirstEndpointByCriteria(
35 catalog("test", "http://localhost", "", ""),
36 ApiCriteria{Name: "test", Region: "RGN"},
37 )
38 if endpoint.PublicURL != "" {
39 t.Error("If provided, the Region qualifier must exclude endpoints with missing or mismatching regions.")
40 return
41 }
42
43 endpoint = FindFirstEndpointByCriteria(
44 catalog("test", "http://localhost", "rgn", ""),
45 ApiCriteria{Name: "test", Region: "RGN"},
46 )
47 if endpoint.PublicURL != "http://localhost" {
48 t.Error("Regions are case insensitive.")
49 return
50 }
51
52 endpoint = FindFirstEndpointByCriteria(
53 catalog("test", "http://localhost", "rgn", ""),
54 ApiCriteria{Name: "test", Region: "RGN", VersionId: "2"},
55 )
56 if endpoint.PublicURL != "" {
57 t.Error("Missing version ID means no match.")
58 return
59 }
60
61 endpoint = FindFirstEndpointByCriteria(
62 catalog("test", "http://localhost", "rgn", "3"),
63 ApiCriteria{Name: "test", Region: "RGN", VersionId: "2"},
64 )
65 if endpoint.PublicURL != "" {
66 t.Error("Mismatched version ID means no match.")
67 return
68 }
69
70 endpoint = FindFirstEndpointByCriteria(
71 catalog("test", "http://localhost", "rgn", "2"),
72 ApiCriteria{Name: "test", Region: "RGN", VersionId: "2"},
73 )
74 if endpoint.PublicURL != "http://localhost" {
75 t.Error("All search criteria met; endpoint expected.")
76 return
77 }
78
79 endpoint = FindFirstEndpointByCriteria(
80 catalog("test", "http://localhost", "ord", "2"),
81 ApiCriteria{Name: "test", VersionId: "2"},
82 )
83 if endpoint.PublicURL != "http://localhost" {
84 t.Error("Sometimes, you might not care what region your stuff is in.")
85 return
86 }
87}
88
89func catalog(name, url, region, version string) []CatalogEntry {
90 return []CatalogEntry{
91 CatalogEntry{
92 Name: name,
93 Endpoints: []EntryEndpoint{
94 EntryEndpoint{
95 PublicURL: url,
96 Region: region,
97 VersionId: version,
98 },
99 },
100 },
101 }
102}