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