Samuel A. Falvo II | fca35b7 | 2013-07-02 18:30:28 -0700 | [diff] [blame] | 1 | package gophercloud |
| 2 | |
| 3 | import ( |
Samuel A. Falvo II | b967076 | 2014-01-10 14:26:39 -0800 | [diff] [blame] | 4 | "os" |
Samuel A. Falvo II | fca35b7 | 2013-07-02 18:30:28 -0700 | [diff] [blame] | 5 | "testing" |
| 6 | ) |
| 7 | |
Samuel A. Falvo II | b967076 | 2014-01-10 14:26:39 -0800 | [diff] [blame] | 8 | // TestFFEBCViaEnvVariable exercises only those calls where a region |
| 9 | // parameter is required, but is provided by an environment variable. |
| 10 | func TestFFEBCViaEnvVariable(t *testing.T) { |
| 11 | changeRegion("RGN") |
Samuel A. Falvo II | fca35b7 | 2013-07-02 18:30:28 -0700 | [diff] [blame] | 12 | |
Samuel A. Falvo II | b967076 | 2014-01-10 14:26:39 -0800 | [diff] [blame] | 13 | endpoint := FindFirstEndpointByCriteria( |
Justin Santa Barbara | a4db50a | 2013-09-01 10:45:07 -0700 | [diff] [blame] | 14 | catalog("test", "compute", "http://localhost", "", ""), |
Samuel A. Falvo II | fca35b7 | 2013-07-02 18:30:28 -0700 | [diff] [blame] | 15 | ApiCriteria{Name: "test"}, |
| 16 | ) |
Samuel A. Falvo II | b967076 | 2014-01-10 14:26:39 -0800 | [diff] [blame] | 17 | if endpoint.PublicURL != "" { |
| 18 | t.Error("If provided, the Region qualifier must exclude endpoints with missing or mismatching regions.") |
Samuel A. Falvo II | fca35b7 | 2013-07-02 18:30:28 -0700 | [diff] [blame] | 19 | return |
| 20 | } |
| 21 | |
| 22 | endpoint = FindFirstEndpointByCriteria( |
Samuel A. Falvo II | b967076 | 2014-01-10 14:26:39 -0800 | [diff] [blame] | 23 | catalog("test", "compute", "http://localhost", "rgn", ""), |
| 24 | ApiCriteria{Name: "test"}, |
Justin Santa Barbara | a4db50a | 2013-09-01 10:45:07 -0700 | [diff] [blame] | 25 | ) |
| 26 | if endpoint.PublicURL != "http://localhost" { |
Samuel A. Falvo II | b967076 | 2014-01-10 14:26:39 -0800 | [diff] [blame] | 27 | t.Error("Regions are case insensitive.") |
Justin Santa Barbara | a4db50a | 2013-09-01 10:45:07 -0700 | [diff] [blame] | 28 | return |
| 29 | } |
| 30 | |
| 31 | endpoint = FindFirstEndpointByCriteria( |
Samuel A. Falvo II | b967076 | 2014-01-10 14:26:39 -0800 | [diff] [blame] | 32 | catalog("test", "compute", "http://localhost", "rgn", ""), |
| 33 | ApiCriteria{Name: "test", VersionId: "2"}, |
Justin Santa Barbara | a4db50a | 2013-09-01 10:45:07 -0700 | [diff] [blame] | 34 | ) |
| 35 | if endpoint.PublicURL != "" { |
Samuel A. Falvo II | b967076 | 2014-01-10 14:26:39 -0800 | [diff] [blame] | 36 | t.Error("Missing version ID means no match.") |
Justin Santa Barbara | a4db50a | 2013-09-01 10:45:07 -0700 | [diff] [blame] | 37 | return |
| 38 | } |
| 39 | |
| 40 | endpoint = FindFirstEndpointByCriteria( |
Samuel A. Falvo II | b967076 | 2014-01-10 14:26:39 -0800 | [diff] [blame] | 41 | 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. |
| 62 | func TestFFEBCViaRegionOption(t *testing.T) { |
| 63 | changeRegion("Starfleet Command") |
| 64 | |
| 65 | endpoint := FindFirstEndpointByCriteria( |
Justin Santa Barbara | a4db50a | 2013-09-01 10:45:07 -0700 | [diff] [blame] | 66 | catalog("test", "compute", "http://localhost", "", ""), |
Samuel A. Falvo II | fca35b7 | 2013-07-02 18:30:28 -0700 | [diff] [blame] | 67 | 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 Barbara | a4db50a | 2013-09-01 10:45:07 -0700 | [diff] [blame] | 75 | catalog("test", "compute", "http://localhost", "rgn", ""), |
Samuel A. Falvo II | fca35b7 | 2013-07-02 18:30:28 -0700 | [diff] [blame] | 76 | 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 Barbara | a4db50a | 2013-09-01 10:45:07 -0700 | [diff] [blame] | 84 | catalog("test", "compute", "http://localhost", "rgn", ""), |
Samuel A. Falvo II | fca35b7 | 2013-07-02 18:30:28 -0700 | [diff] [blame] | 85 | 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 Barbara | a4db50a | 2013-09-01 10:45:07 -0700 | [diff] [blame] | 93 | catalog("test", "compute", "http://localhost", "rgn", "3"), |
Samuel A. Falvo II | fca35b7 | 2013-07-02 18:30:28 -0700 | [diff] [blame] | 94 | 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 Barbara | a4db50a | 2013-09-01 10:45:07 -0700 | [diff] [blame] | 102 | catalog("test", "compute", "http://localhost", "rgn", "2"), |
Samuel A. Falvo II | fca35b7 | 2013-07-02 18:30:28 -0700 | [diff] [blame] | 103 | 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 II | b967076 | 2014-01-10 14:26:39 -0800 | [diff] [blame] | 109 | } |
| 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. |
| 114 | func 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 II | fca35b7 | 2013-07-02 18:30:28 -0700 | [diff] [blame] | 158 | |
| 159 | endpoint = FindFirstEndpointByCriteria( |
Justin Santa Barbara | a4db50a | 2013-09-01 10:45:07 -0700 | [diff] [blame] | 160 | catalog("test", "compute", "http://localhost", "ord", "2"), |
Samuel A. Falvo II | b967076 | 2014-01-10 14:26:39 -0800 | [diff] [blame] | 161 | ApiCriteria{Name: "test", VersionId: "2", IgnoreEnvVars: true}, |
Samuel A. Falvo II | fca35b7 | 2013-07-02 18:30:28 -0700 | [diff] [blame] | 162 | ) |
| 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 Barbara | a4db50a | 2013-09-01 10:45:07 -0700 | [diff] [blame] | 169 | func catalog(name, entry_type, url, region, version string) []CatalogEntry { |
Samuel A. Falvo II | fca35b7 | 2013-07-02 18:30:28 -0700 | [diff] [blame] | 170 | return []CatalogEntry{ |
Samuel A. Falvo II | c0b07aa | 2013-08-19 17:13:06 -0700 | [diff] [blame] | 171 | { |
Samuel A. Falvo II | fca35b7 | 2013-07-02 18:30:28 -0700 | [diff] [blame] | 172 | Name: name, |
Justin Santa Barbara | a4db50a | 2013-09-01 10:45:07 -0700 | [diff] [blame] | 173 | Type: entry_type, |
Samuel A. Falvo II | fca35b7 | 2013-07-02 18:30:28 -0700 | [diff] [blame] | 174 | Endpoints: []EntryEndpoint{ |
Samuel A. Falvo II | c0b07aa | 2013-08-19 17:13:06 -0700 | [diff] [blame] | 175 | { |
Samuel A. Falvo II | fca35b7 | 2013-07-02 18:30:28 -0700 | [diff] [blame] | 176 | PublicURL: url, |
| 177 | Region: region, |
| 178 | VersionId: version, |
| 179 | }, |
| 180 | }, |
| 181 | }, |
| 182 | } |
| 183 | } |
Samuel A. Falvo II | b967076 | 2014-01-10 14:26:39 -0800 | [diff] [blame] | 184 | |
| 185 | func changeRegion(r string) { |
| 186 | err := os.Setenv("OS_REGION_NAME", r) |
| 187 | if err != nil { |
| 188 | panic(err) |
| 189 | } |
| 190 | } |