Samuel A. Falvo II | fca35b7 | 2013-07-02 18:30:28 -0700 | [diff] [blame] | 1 | package gophercloud |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | ) |
| 6 | |
Samuel A. Falvo II | fca35b7 | 2013-07-02 18:30:28 -0700 | [diff] [blame] | 7 | func 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 II | c0b07aa | 2013-08-19 17:13:06 -0700 | [diff] [blame] | 16 | {Name: "test"}, |
Samuel A. Falvo II | fca35b7 | 2013-07-02 18:30:28 -0700 | [diff] [blame] | 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( |
Justin Santa Barbara | a4db50a | 2013-09-01 10:45:07 -0700 | [diff] [blame^] | 26 | catalog("test", "compute", "http://localhost", "", ""), |
Samuel A. Falvo II | fca35b7 | 2013-07-02 18:30:28 -0700 | [diff] [blame] | 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( |
Justin Santa Barbara | a4db50a | 2013-09-01 10:45:07 -0700 | [diff] [blame^] | 35 | 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 II | fca35b7 | 2013-07-02 18:30:28 -0700 | [diff] [blame] | 54 | 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 Barbara | a4db50a | 2013-09-01 10:45:07 -0700 | [diff] [blame^] | 62 | catalog("test", "compute", "http://localhost", "rgn", ""), |
Samuel A. Falvo II | fca35b7 | 2013-07-02 18:30:28 -0700 | [diff] [blame] | 63 | 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 Barbara | a4db50a | 2013-09-01 10:45:07 -0700 | [diff] [blame^] | 71 | catalog("test", "compute", "http://localhost", "rgn", ""), |
Samuel A. Falvo II | fca35b7 | 2013-07-02 18:30:28 -0700 | [diff] [blame] | 72 | 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 Barbara | a4db50a | 2013-09-01 10:45:07 -0700 | [diff] [blame^] | 80 | catalog("test", "compute", "http://localhost", "rgn", "3"), |
Samuel A. Falvo II | fca35b7 | 2013-07-02 18:30:28 -0700 | [diff] [blame] | 81 | 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 Barbara | a4db50a | 2013-09-01 10:45:07 -0700 | [diff] [blame^] | 89 | catalog("test", "compute", "http://localhost", "rgn", "2"), |
Samuel A. Falvo II | fca35b7 | 2013-07-02 18:30:28 -0700 | [diff] [blame] | 90 | 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 Barbara | a4db50a | 2013-09-01 10:45:07 -0700 | [diff] [blame^] | 98 | catalog("test", "compute", "http://localhost", "ord", "2"), |
Samuel A. Falvo II | fca35b7 | 2013-07-02 18:30:28 -0700 | [diff] [blame] | 99 | 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 Barbara | a4db50a | 2013-09-01 10:45:07 -0700 | [diff] [blame^] | 107 | func catalog(name, entry_type, url, region, version string) []CatalogEntry { |
Samuel A. Falvo II | fca35b7 | 2013-07-02 18:30:28 -0700 | [diff] [blame] | 108 | return []CatalogEntry{ |
Samuel A. Falvo II | c0b07aa | 2013-08-19 17:13:06 -0700 | [diff] [blame] | 109 | { |
Samuel A. Falvo II | fca35b7 | 2013-07-02 18:30:28 -0700 | [diff] [blame] | 110 | Name: name, |
Justin Santa Barbara | a4db50a | 2013-09-01 10:45:07 -0700 | [diff] [blame^] | 111 | Type: entry_type, |
Samuel A. Falvo II | fca35b7 | 2013-07-02 18:30:28 -0700 | [diff] [blame] | 112 | Endpoints: []EntryEndpoint{ |
Samuel A. Falvo II | c0b07aa | 2013-08-19 17:13:06 -0700 | [diff] [blame] | 113 | { |
Samuel A. Falvo II | fca35b7 | 2013-07-02 18:30:28 -0700 | [diff] [blame] | 114 | PublicURL: url, |
| 115 | Region: region, |
| 116 | VersionId: version, |
| 117 | }, |
| 118 | }, |
| 119 | }, |
| 120 | } |
| 121 | } |