| Ash Wilson | fc1af5a | 2014-10-08 09:10:41 -0400 | [diff] [blame] | 1 | package openstack | 
 | 2 |  | 
 | 3 | import ( | 
| Ash Wilson | 61c49a5 | 2014-10-08 14:15:04 -0400 | [diff] [blame] | 4 | 	"fmt" | 
 | 5 | 	"net/http" | 
| Ash Wilson | fc1af5a | 2014-10-08 09:10:41 -0400 | [diff] [blame] | 6 | 	"strings" | 
 | 7 | 	"testing" | 
 | 8 |  | 
 | 9 | 	"github.com/rackspace/gophercloud" | 
 | 10 | 	tokens2 "github.com/rackspace/gophercloud/openstack/identity/v2/tokens" | 
 | 11 | 	th "github.com/rackspace/gophercloud/testhelper" | 
| Ash Wilson | 61c49a5 | 2014-10-08 14:15:04 -0400 | [diff] [blame] | 12 | 	fake "github.com/rackspace/gophercloud/testhelper/client" | 
| Ash Wilson | fc1af5a | 2014-10-08 09:10:41 -0400 | [diff] [blame] | 13 | ) | 
 | 14 |  | 
 | 15 | // Service catalog fixtures take too much vertical space! | 
 | 16 | var catalog2 = tokens2.ServiceCatalog{ | 
 | 17 | 	Entries: []tokens2.CatalogEntry{ | 
 | 18 | 		tokens2.CatalogEntry{ | 
 | 19 | 			Type: "same", | 
 | 20 | 			Name: "same", | 
 | 21 | 			Endpoints: []tokens2.Endpoint{ | 
 | 22 | 				tokens2.Endpoint{ | 
 | 23 | 					Region:      "same", | 
 | 24 | 					PublicURL:   "https://public.correct.com/", | 
 | 25 | 					InternalURL: "https://internal.correct.com/", | 
 | 26 | 					AdminURL:    "https://admin.correct.com/", | 
 | 27 | 				}, | 
 | 28 | 				tokens2.Endpoint{ | 
 | 29 | 					Region:    "different", | 
 | 30 | 					PublicURL: "https://badregion.com/", | 
 | 31 | 				}, | 
 | 32 | 			}, | 
 | 33 | 		}, | 
 | 34 | 		tokens2.CatalogEntry{ | 
 | 35 | 			Type: "same", | 
 | 36 | 			Name: "different", | 
 | 37 | 			Endpoints: []tokens2.Endpoint{ | 
 | 38 | 				tokens2.Endpoint{ | 
 | 39 | 					Region:    "same", | 
 | 40 | 					PublicURL: "https://badname.com/", | 
 | 41 | 				}, | 
 | 42 | 				tokens2.Endpoint{ | 
 | 43 | 					Region:    "different", | 
 | 44 | 					PublicURL: "https://badname.com/+badregion", | 
 | 45 | 				}, | 
 | 46 | 			}, | 
 | 47 | 		}, | 
 | 48 | 		tokens2.CatalogEntry{ | 
 | 49 | 			Type: "different", | 
 | 50 | 			Name: "different", | 
 | 51 | 			Endpoints: []tokens2.Endpoint{ | 
 | 52 | 				tokens2.Endpoint{ | 
 | 53 | 					Region:    "same", | 
 | 54 | 					PublicURL: "https://badtype.com/+badname", | 
 | 55 | 				}, | 
 | 56 | 				tokens2.Endpoint{ | 
 | 57 | 					Region:    "different", | 
 | 58 | 					PublicURL: "https://badtype.com/+badregion+badname", | 
 | 59 | 				}, | 
 | 60 | 			}, | 
 | 61 | 		}, | 
 | 62 | 	}, | 
 | 63 | } | 
 | 64 |  | 
 | 65 | func TestV2EndpointExact(t *testing.T) { | 
 | 66 | 	expectedURLs := map[gophercloud.Availability]string{ | 
 | 67 | 		gophercloud.AvailabilityPublic:   "https://public.correct.com/", | 
 | 68 | 		gophercloud.AvailabilityAdmin:    "https://admin.correct.com/", | 
 | 69 | 		gophercloud.AvailabilityInternal: "https://internal.correct.com/", | 
 | 70 | 	} | 
 | 71 |  | 
 | 72 | 	for availability, expected := range expectedURLs { | 
 | 73 | 		actual, err := V2EndpointURL(&catalog2, gophercloud.EndpointOpts{ | 
 | 74 | 			Type:         "same", | 
 | 75 | 			Name:         "same", | 
 | 76 | 			Region:       "same", | 
 | 77 | 			Availability: availability, | 
 | 78 | 		}) | 
 | 79 | 		th.AssertNoErr(t, err) | 
 | 80 | 		th.CheckEquals(t, expected, actual) | 
 | 81 | 	} | 
 | 82 | } | 
 | 83 |  | 
 | 84 | func TestV2EndpointNone(t *testing.T) { | 
 | 85 | 	_, err := V2EndpointURL(&catalog2, gophercloud.EndpointOpts{ | 
 | 86 | 		Type:         "nope", | 
 | 87 | 		Availability: gophercloud.AvailabilityPublic, | 
 | 88 | 	}) | 
 | 89 | 	th.CheckEquals(t, gophercloud.ErrEndpointNotFound, err) | 
 | 90 | } | 
 | 91 |  | 
 | 92 | func TestV2EndpointMultiple(t *testing.T) { | 
 | 93 | 	_, err := V2EndpointURL(&catalog2, gophercloud.EndpointOpts{ | 
 | 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) { | 
 | 104 | 	_, err := V2EndpointURL(&catalog2, gophercloud.EndpointOpts{ | 
 | 105 | 		Type:         "same", | 
 | 106 | 		Name:         "same", | 
 | 107 | 		Region:       "same", | 
 | 108 | 		Availability: "wat", | 
 | 109 | 	}) | 
 | 110 | 	th.CheckEquals(t, err.Error(), "Unexpected availability in endpoint query: wat") | 
 | 111 | } | 
| Ash Wilson | 61c49a5 | 2014-10-08 14:15:04 -0400 | [diff] [blame] | 112 |  | 
 | 113 | func setupV3Responses(t *testing.T) { | 
 | 114 | 	// Mock the service query. | 
 | 115 | 	th.Mux.HandleFunc("/services", func(w http.ResponseWriter, r *http.Request) { | 
 | 116 | 		th.TestMethod(t, r, "GET") | 
 | 117 | 		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) | 
 | 118 |  | 
 | 119 | 		w.Header().Add("Content-Type", "application/json") | 
 | 120 | 		fmt.Fprintf(w, ` | 
 | 121 | 			{ | 
 | 122 | 				"links": { | 
 | 123 | 					"next": null, | 
 | 124 | 					"previous": null | 
 | 125 | 				}, | 
 | 126 | 				"services": [ | 
 | 127 | 					{ | 
 | 128 | 						"description": "Correct", | 
 | 129 | 						"id": "1234", | 
 | 130 | 						"name": "same", | 
 | 131 | 						"type": "same" | 
 | 132 | 					}, | 
 | 133 | 					{ | 
 | 134 | 						"description": "Bad Name", | 
 | 135 | 						"id": "9876", | 
 | 136 | 						"name": "different", | 
 | 137 | 						"type": "same" | 
 | 138 | 					} | 
 | 139 | 				] | 
 | 140 | 			} | 
 | 141 | 		`) | 
 | 142 | 	}) | 
 | 143 |  | 
 | 144 | 	// Mock the endpoint query. | 
 | 145 | 	th.Mux.HandleFunc("/endpoints", func(w http.ResponseWriter, r *http.Request) { | 
 | 146 | 		th.TestMethod(t, r, "GET") | 
 | 147 | 		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) | 
 | 148 | 		th.TestFormValues(t, r, map[string]string{ | 
 | 149 | 			"service_id": "1234", | 
 | 150 | 			"interface":  "public", | 
 | 151 | 		}) | 
 | 152 |  | 
 | 153 | 		w.Header().Add("Content-Type", "application/json") | 
 | 154 | 		fmt.Fprintf(w, ` | 
 | 155 | 			{ | 
 | 156 | 				"endpoints": [ | 
 | 157 | 					{ | 
 | 158 | 						"id": "12", | 
 | 159 | 						"interface": "public", | 
 | 160 | 						"name": "the-right-one", | 
 | 161 | 						"region": "same", | 
 | 162 | 						"service_id": "1234", | 
 | 163 | 						"url": "https://correct:9000/" | 
 | 164 | 					}, | 
 | 165 | 					{ | 
 | 166 | 						"id": "14", | 
 | 167 | 						"interface": "public", | 
 | 168 | 						"name": "bad-region", | 
 | 169 | 						"region": "different", | 
 | 170 | 						"service_id": "1234", | 
 | 171 | 						"url": "https://bad-region:9001/" | 
 | 172 | 					} | 
 | 173 | 				], | 
 | 174 | 				"links": { | 
 | 175 | 					"next": null, | 
 | 176 | 					"previous": null | 
 | 177 | 				} | 
 | 178 | 			} | 
 | 179 |     `) | 
 | 180 | 	}) | 
 | 181 | } | 
 | 182 |  | 
 | 183 | func TestV3EndpointExact(t *testing.T) { | 
 | 184 | 	th.SetupHTTP() | 
 | 185 | 	defer th.TeardownHTTP() | 
 | 186 | 	setupV3Responses(t) | 
 | 187 |  | 
 | 188 | 	actual, err := V3EndpointURL(fake.ServiceClient(), gophercloud.EndpointOpts{ | 
 | 189 | 		Type:         "same", | 
 | 190 | 		Name:         "same", | 
 | 191 | 		Region:       "same", | 
 | 192 | 		Availability: gophercloud.AvailabilityPublic, | 
 | 193 | 	}) | 
 | 194 | 	th.AssertNoErr(t, err) | 
 | 195 | 	th.CheckEquals(t, actual, "https://correct:9000/") | 
 | 196 | } | 
 | 197 |  | 
 | 198 | func TestV3EndpointNoService(t *testing.T) { | 
 | 199 | 	th.SetupHTTP() | 
 | 200 | 	defer th.TeardownHTTP() | 
 | 201 |  | 
 | 202 | 	th.Mux.HandleFunc("/services", func(w http.ResponseWriter, r *http.Request) { | 
 | 203 | 		th.TestMethod(t, r, "GET") | 
 | 204 | 		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) | 
 | 205 |  | 
 | 206 | 		w.Header().Add("Content-Type", "application/json") | 
 | 207 | 		fmt.Fprintf(w, ` | 
 | 208 |       { | 
 | 209 |         "links": { | 
 | 210 |           "next": null, | 
 | 211 |           "previous": null | 
 | 212 |         }, | 
 | 213 |         "services": [] | 
 | 214 |       } | 
 | 215 |     `) | 
 | 216 | 	}) | 
 | 217 |  | 
 | 218 | 	_, err := V3EndpointURL(fake.ServiceClient(), gophercloud.EndpointOpts{ | 
 | 219 | 		Type:         "nope", | 
 | 220 | 		Name:         "same", | 
 | 221 | 		Region:       "same", | 
 | 222 | 		Availability: gophercloud.AvailabilityPublic, | 
 | 223 | 	}) | 
 | 224 | 	th.CheckEquals(t, gophercloud.ErrServiceNotFound, err) | 
 | 225 | } |