Ash Wilson | fc1af5a | 2014-10-08 09:10:41 -0400 | [diff] [blame] | 1 | package openstack |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 7 | "github.com/gophercloud/gophercloud" |
| 8 | tokens2 "github.com/gophercloud/gophercloud/openstack/identity/v2/tokens" |
| 9 | tokens3 "github.com/gophercloud/gophercloud/openstack/identity/v3/tokens" |
| 10 | th "github.com/gophercloud/gophercloud/testhelper" |
Ash Wilson | fc1af5a | 2014-10-08 09:10:41 -0400 | [diff] [blame] | 11 | ) |
| 12 | |
| 13 | // Service catalog fixtures take too much vertical space! |
| 14 | var catalog2 = tokens2.ServiceCatalog{ |
| 15 | Entries: []tokens2.CatalogEntry{ |
| 16 | tokens2.CatalogEntry{ |
| 17 | Type: "same", |
| 18 | Name: "same", |
| 19 | Endpoints: []tokens2.Endpoint{ |
| 20 | tokens2.Endpoint{ |
| 21 | Region: "same", |
| 22 | PublicURL: "https://public.correct.com/", |
| 23 | InternalURL: "https://internal.correct.com/", |
| 24 | AdminURL: "https://admin.correct.com/", |
| 25 | }, |
| 26 | tokens2.Endpoint{ |
| 27 | Region: "different", |
| 28 | PublicURL: "https://badregion.com/", |
| 29 | }, |
| 30 | }, |
| 31 | }, |
| 32 | tokens2.CatalogEntry{ |
| 33 | Type: "same", |
| 34 | Name: "different", |
| 35 | Endpoints: []tokens2.Endpoint{ |
| 36 | tokens2.Endpoint{ |
| 37 | Region: "same", |
| 38 | PublicURL: "https://badname.com/", |
| 39 | }, |
| 40 | tokens2.Endpoint{ |
| 41 | Region: "different", |
| 42 | PublicURL: "https://badname.com/+badregion", |
| 43 | }, |
| 44 | }, |
| 45 | }, |
| 46 | tokens2.CatalogEntry{ |
| 47 | Type: "different", |
| 48 | Name: "different", |
| 49 | Endpoints: []tokens2.Endpoint{ |
| 50 | tokens2.Endpoint{ |
| 51 | Region: "same", |
| 52 | PublicURL: "https://badtype.com/+badname", |
| 53 | }, |
| 54 | tokens2.Endpoint{ |
| 55 | Region: "different", |
| 56 | PublicURL: "https://badtype.com/+badregion+badname", |
| 57 | }, |
| 58 | }, |
| 59 | }, |
| 60 | }, |
| 61 | } |
| 62 | |
| 63 | func TestV2EndpointExact(t *testing.T) { |
| 64 | expectedURLs := map[gophercloud.Availability]string{ |
| 65 | gophercloud.AvailabilityPublic: "https://public.correct.com/", |
| 66 | gophercloud.AvailabilityAdmin: "https://admin.correct.com/", |
| 67 | gophercloud.AvailabilityInternal: "https://internal.correct.com/", |
| 68 | } |
| 69 | |
| 70 | for availability, expected := range expectedURLs { |
| 71 | actual, err := V2EndpointURL(&catalog2, gophercloud.EndpointOpts{ |
| 72 | Type: "same", |
| 73 | Name: "same", |
| 74 | Region: "same", |
| 75 | Availability: availability, |
| 76 | }) |
| 77 | th.AssertNoErr(t, err) |
| 78 | th.CheckEquals(t, expected, actual) |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | func TestV2EndpointNone(t *testing.T) { |
Jon Perritt | eb01563 | 2016-02-21 19:56:53 -0600 | [diff] [blame^] | 83 | _, actual := V2EndpointURL(&catalog2, gophercloud.EndpointOpts{ |
Ash Wilson | fc1af5a | 2014-10-08 09:10:41 -0400 | [diff] [blame] | 84 | Type: "nope", |
| 85 | Availability: gophercloud.AvailabilityPublic, |
| 86 | }) |
Jon Perritt | eb01563 | 2016-02-21 19:56:53 -0600 | [diff] [blame^] | 87 | expected := &gophercloud.ErrEndpointNotFound{} |
| 88 | th.CheckEquals(t, expected.Error(), actual.Error()) |
Ash Wilson | fc1af5a | 2014-10-08 09:10:41 -0400 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | func TestV2EndpointMultiple(t *testing.T) { |
| 92 | _, err := V2EndpointURL(&catalog2, gophercloud.EndpointOpts{ |
| 93 | Type: "same", |
| 94 | Region: "same", |
| 95 | Availability: gophercloud.AvailabilityPublic, |
| 96 | }) |
| 97 | if !strings.HasPrefix(err.Error(), "Discovered 2 matching endpoints:") { |
| 98 | t.Errorf("Received unexpected error: %v", err) |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | func TestV2EndpointBadAvailability(t *testing.T) { |
| 103 | _, err := V2EndpointURL(&catalog2, gophercloud.EndpointOpts{ |
| 104 | Type: "same", |
| 105 | Name: "same", |
| 106 | Region: "same", |
| 107 | Availability: "wat", |
| 108 | }) |
Guillaume Giamarchi | b2663b2 | 2015-04-01 01:23:29 +0200 | [diff] [blame] | 109 | th.CheckEquals(t, "Unexpected availability in endpoint query: wat", err.Error()) |
Ash Wilson | fc1af5a | 2014-10-08 09:10:41 -0400 | [diff] [blame] | 110 | } |
Ash Wilson | 61c49a5 | 2014-10-08 14:15:04 -0400 | [diff] [blame] | 111 | |
Guillaume Giamarchi | b2663b2 | 2015-04-01 01:23:29 +0200 | [diff] [blame] | 112 | var catalog3 = tokens3.ServiceCatalog{ |
| 113 | Entries: []tokens3.CatalogEntry{ |
| 114 | tokens3.CatalogEntry{ |
| 115 | Type: "same", |
| 116 | Name: "same", |
| 117 | Endpoints: []tokens3.Endpoint{ |
| 118 | tokens3.Endpoint{ |
| 119 | ID: "1", |
| 120 | Region: "same", |
| 121 | Interface: "public", |
| 122 | URL: "https://public.correct.com/", |
Ash Wilson | 61c49a5 | 2014-10-08 14:15:04 -0400 | [diff] [blame] | 123 | }, |
Guillaume Giamarchi | b2663b2 | 2015-04-01 01:23:29 +0200 | [diff] [blame] | 124 | tokens3.Endpoint{ |
| 125 | ID: "2", |
| 126 | Region: "same", |
| 127 | Interface: "admin", |
| 128 | URL: "https://admin.correct.com/", |
| 129 | }, |
| 130 | tokens3.Endpoint{ |
| 131 | ID: "3", |
| 132 | Region: "same", |
| 133 | Interface: "internal", |
| 134 | URL: "https://internal.correct.com/", |
| 135 | }, |
| 136 | tokens3.Endpoint{ |
| 137 | ID: "4", |
| 138 | Region: "different", |
| 139 | Interface: "public", |
| 140 | URL: "https://badregion.com/", |
| 141 | }, |
| 142 | }, |
| 143 | }, |
| 144 | tokens3.CatalogEntry{ |
| 145 | Type: "same", |
| 146 | Name: "different", |
| 147 | Endpoints: []tokens3.Endpoint{ |
| 148 | tokens3.Endpoint{ |
| 149 | ID: "5", |
| 150 | Region: "same", |
| 151 | Interface: "public", |
| 152 | URL: "https://badname.com/", |
| 153 | }, |
| 154 | tokens3.Endpoint{ |
| 155 | ID: "6", |
| 156 | Region: "different", |
| 157 | Interface: "public", |
| 158 | URL: "https://badname.com/+badregion", |
| 159 | }, |
| 160 | }, |
| 161 | }, |
| 162 | tokens3.CatalogEntry{ |
| 163 | Type: "different", |
| 164 | Name: "different", |
| 165 | Endpoints: []tokens3.Endpoint{ |
| 166 | tokens3.Endpoint{ |
| 167 | ID: "7", |
| 168 | Region: "same", |
| 169 | Interface: "public", |
| 170 | URL: "https://badtype.com/+badname", |
| 171 | }, |
| 172 | tokens3.Endpoint{ |
| 173 | ID: "8", |
| 174 | Region: "different", |
| 175 | Interface: "public", |
| 176 | URL: "https://badtype.com/+badregion+badname", |
| 177 | }, |
| 178 | }, |
| 179 | }, |
| 180 | }, |
Ash Wilson | 61c49a5 | 2014-10-08 14:15:04 -0400 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | func TestV3EndpointExact(t *testing.T) { |
Guillaume Giamarchi | b2663b2 | 2015-04-01 01:23:29 +0200 | [diff] [blame] | 184 | expectedURLs := map[gophercloud.Availability]string{ |
| 185 | gophercloud.AvailabilityPublic: "https://public.correct.com/", |
| 186 | gophercloud.AvailabilityAdmin: "https://admin.correct.com/", |
| 187 | gophercloud.AvailabilityInternal: "https://internal.correct.com/", |
| 188 | } |
Ash Wilson | 61c49a5 | 2014-10-08 14:15:04 -0400 | [diff] [blame] | 189 | |
Guillaume Giamarchi | b2663b2 | 2015-04-01 01:23:29 +0200 | [diff] [blame] | 190 | for availability, expected := range expectedURLs { |
| 191 | actual, err := V3EndpointURL(&catalog3, gophercloud.EndpointOpts{ |
| 192 | Type: "same", |
| 193 | Name: "same", |
| 194 | Region: "same", |
| 195 | Availability: availability, |
| 196 | }) |
| 197 | th.AssertNoErr(t, err) |
| 198 | th.CheckEquals(t, expected, actual) |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | func TestV3EndpointNone(t *testing.T) { |
Jon Perritt | eb01563 | 2016-02-21 19:56:53 -0600 | [diff] [blame^] | 203 | _, actual := V3EndpointURL(&catalog3, gophercloud.EndpointOpts{ |
Guillaume Giamarchi | b2663b2 | 2015-04-01 01:23:29 +0200 | [diff] [blame] | 204 | Type: "nope", |
| 205 | Availability: gophercloud.AvailabilityPublic, |
| 206 | }) |
Jon Perritt | eb01563 | 2016-02-21 19:56:53 -0600 | [diff] [blame^] | 207 | expected := &gophercloud.ErrEndpointNotFound{} |
| 208 | th.CheckEquals(t, expected.Error(), actual.Error()) |
Guillaume Giamarchi | b2663b2 | 2015-04-01 01:23:29 +0200 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | func TestV3EndpointMultiple(t *testing.T) { |
| 212 | _, err := V3EndpointURL(&catalog3, gophercloud.EndpointOpts{ |
| 213 | Type: "same", |
| 214 | Region: "same", |
| 215 | Availability: gophercloud.AvailabilityPublic, |
| 216 | }) |
| 217 | if !strings.HasPrefix(err.Error(), "Discovered 2 matching endpoints:") { |
| 218 | t.Errorf("Received unexpected error: %v", err) |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | func TestV3EndpointBadAvailability(t *testing.T) { |
| 223 | _, err := V3EndpointURL(&catalog3, gophercloud.EndpointOpts{ |
Ash Wilson | 61c49a5 | 2014-10-08 14:15:04 -0400 | [diff] [blame] | 224 | Type: "same", |
| 225 | Name: "same", |
| 226 | Region: "same", |
Guillaume Giamarchi | b2663b2 | 2015-04-01 01:23:29 +0200 | [diff] [blame] | 227 | Availability: "wat", |
Ash Wilson | 61c49a5 | 2014-10-08 14:15:04 -0400 | [diff] [blame] | 228 | }) |
Guillaume Giamarchi | b2663b2 | 2015-04-01 01:23:29 +0200 | [diff] [blame] | 229 | th.CheckEquals(t, "Unexpected availability in endpoint query: wat", err.Error()) |
Ash Wilson | 61c49a5 | 2014-10-08 14:15:04 -0400 | [diff] [blame] | 230 | } |