blob: 52e238837554f125562fc0e428b3220e956c6bcc [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{
Samuel A. Falvo IIc0b07aa2013-08-19 17:13:06 -070016 {Name: "test"},
Samuel A. Falvo IIfca35b72013-07-02 18:30:28 -070017 },
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(
Justin Santa Barbaraa4db50a2013-09-01 10:45:07 -070026 catalog("test", "compute", "http://localhost", "", ""),
Samuel A. Falvo IIfca35b72013-07-02 18:30:28 -070027 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(
Justin Santa Barbaraa4db50a2013-09-01 10:45:07 -070035 catalog("test", "compute", "http://localhost", "", ""),
36 ApiCriteria{Type: "compute"},
37 )
38 if endpoint.PublicURL != "http://localhost" {
39 t.Error("Looking for an endpoint by type but without region or version ID should match first entry endpoint.")
40 return
41 }
42
43 endpoint = FindFirstEndpointByCriteria(
44 catalog("test", "compute", "http://localhost", "", ""),
45 ApiCriteria{Type: "identity"},
46 )
47 if endpoint.PublicURL != "" {
48 t.Error("Returned mismatched type.")
49 return
50 }
51
52 endpoint = FindFirstEndpointByCriteria(
53 catalog("test", "compute", "http://localhost", "", ""),
Samuel A. Falvo IIfca35b72013-07-02 18:30:28 -070054 ApiCriteria{Name: "test", Region: "RGN"},
55 )
56 if endpoint.PublicURL != "" {
57 t.Error("If provided, the Region qualifier must exclude endpoints with missing or mismatching regions.")
58 return
59 }
60
61 endpoint = FindFirstEndpointByCriteria(
Justin Santa Barbaraa4db50a2013-09-01 10:45:07 -070062 catalog("test", "compute", "http://localhost", "rgn", ""),
Samuel A. Falvo IIfca35b72013-07-02 18:30:28 -070063 ApiCriteria{Name: "test", Region: "RGN"},
64 )
65 if endpoint.PublicURL != "http://localhost" {
66 t.Error("Regions are case insensitive.")
67 return
68 }
69
70 endpoint = FindFirstEndpointByCriteria(
Justin Santa Barbaraa4db50a2013-09-01 10:45:07 -070071 catalog("test", "compute", "http://localhost", "rgn", ""),
Samuel A. Falvo IIfca35b72013-07-02 18:30:28 -070072 ApiCriteria{Name: "test", Region: "RGN", VersionId: "2"},
73 )
74 if endpoint.PublicURL != "" {
75 t.Error("Missing version ID means no match.")
76 return
77 }
78
79 endpoint = FindFirstEndpointByCriteria(
Justin Santa Barbaraa4db50a2013-09-01 10:45:07 -070080 catalog("test", "compute", "http://localhost", "rgn", "3"),
Samuel A. Falvo IIfca35b72013-07-02 18:30:28 -070081 ApiCriteria{Name: "test", Region: "RGN", VersionId: "2"},
82 )
83 if endpoint.PublicURL != "" {
84 t.Error("Mismatched version ID means no match.")
85 return
86 }
87
88 endpoint = FindFirstEndpointByCriteria(
Justin Santa Barbaraa4db50a2013-09-01 10:45:07 -070089 catalog("test", "compute", "http://localhost", "rgn", "2"),
Samuel A. Falvo IIfca35b72013-07-02 18:30:28 -070090 ApiCriteria{Name: "test", Region: "RGN", VersionId: "2"},
91 )
92 if endpoint.PublicURL != "http://localhost" {
93 t.Error("All search criteria met; endpoint expected.")
94 return
95 }
96
97 endpoint = FindFirstEndpointByCriteria(
Justin Santa Barbaraa4db50a2013-09-01 10:45:07 -070098 catalog("test", "compute", "http://localhost", "ord", "2"),
Samuel A. Falvo IIfca35b72013-07-02 18:30:28 -070099 ApiCriteria{Name: "test", VersionId: "2"},
100 )
101 if endpoint.PublicURL != "http://localhost" {
102 t.Error("Sometimes, you might not care what region your stuff is in.")
103 return
104 }
105}
106
Justin Santa Barbaraa4db50a2013-09-01 10:45:07 -0700107func catalog(name, entry_type, url, region, version string) []CatalogEntry {
Samuel A. Falvo IIfca35b72013-07-02 18:30:28 -0700108 return []CatalogEntry{
Samuel A. Falvo IIc0b07aa2013-08-19 17:13:06 -0700109 {
Samuel A. Falvo IIfca35b72013-07-02 18:30:28 -0700110 Name: name,
Justin Santa Barbaraa4db50a2013-09-01 10:45:07 -0700111 Type: entry_type,
Samuel A. Falvo IIfca35b72013-07-02 18:30:28 -0700112 Endpoints: []EntryEndpoint{
Samuel A. Falvo IIc0b07aa2013-08-19 17:13:06 -0700113 {
Samuel A. Falvo IIfca35b72013-07-02 18:30:28 -0700114 PublicURL: url,
115 Region: region,
116 VersionId: version,
117 },
118 },
119 },
120 }
121}