| Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 1 | package endpoints | 
 | 2 |  | 
 | 3 | import ( | 
 | 4 | 	"fmt" | 
 | 5 | 	"net/http" | 
 | 6 | 	"reflect" | 
 | 7 | 	"testing" | 
 | 8 |  | 
 | 9 | 	"github.com/rackspace/gophercloud" | 
| Ash Wilson | 3c8cc77 | 2014-09-16 11:40:49 -0400 | [diff] [blame] | 10 | 	"github.com/rackspace/gophercloud/pagination" | 
| Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 11 | 	"github.com/rackspace/gophercloud/testhelper" | 
 | 12 | ) | 
 | 13 |  | 
 | 14 | const tokenID = "abcabcabcabc" | 
 | 15 |  | 
 | 16 | func serviceClient() *gophercloud.ServiceClient { | 
 | 17 | 	return &gophercloud.ServiceClient{ | 
 | 18 | 		Provider: &gophercloud.ProviderClient{TokenID: tokenID}, | 
 | 19 | 		Endpoint: testhelper.Endpoint(), | 
 | 20 | 	} | 
 | 21 | } | 
 | 22 |  | 
 | 23 | func TestCreateSuccessful(t *testing.T) { | 
 | 24 | 	testhelper.SetupHTTP() | 
 | 25 | 	defer testhelper.TeardownHTTP() | 
 | 26 |  | 
 | 27 | 	testhelper.Mux.HandleFunc("/endpoints", func(w http.ResponseWriter, r *http.Request) { | 
 | 28 | 		testhelper.TestMethod(t, r, "POST") | 
 | 29 | 		testhelper.TestHeader(t, r, "X-Auth-Token", tokenID) | 
 | 30 | 		testhelper.TestJSONRequest(t, r, ` | 
 | 31 |       { | 
 | 32 |         "endpoint": { | 
 | 33 |           "interface": "public", | 
 | 34 |           "name": "the-endiest-of-points", | 
 | 35 |           "region": "underground", | 
 | 36 |           "url": "https://1.2.3.4:9000/", | 
 | 37 |           "service_id": "asdfasdfasdfasdf" | 
 | 38 |         } | 
 | 39 |       } | 
 | 40 |     `) | 
 | 41 |  | 
| Ash Wilson | 989ce54 | 2014-09-04 10:52:49 -0400 | [diff] [blame] | 42 | 		w.WriteHeader(http.StatusCreated) | 
| Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 43 | 		fmt.Fprintf(w, ` | 
 | 44 |       { | 
 | 45 |         "endpoint": { | 
 | 46 |           "id": "12", | 
 | 47 |           "interface": "public", | 
 | 48 |           "links": { | 
 | 49 |             "self": "https://localhost:5000/v3/endpoints/12" | 
 | 50 |           }, | 
 | 51 |           "name": "the-endiest-of-points", | 
 | 52 |           "region": "underground", | 
 | 53 |           "service_id": "asdfasdfasdfasdf", | 
 | 54 |           "url": "https://1.2.3.4:9000/" | 
 | 55 |         } | 
 | 56 |       } | 
 | 57 |     `) | 
 | 58 | 	}) | 
 | 59 |  | 
 | 60 | 	client := serviceClient() | 
 | 61 |  | 
| Ash Wilson | 3f59ade | 2014-10-02 09:22:23 -0400 | [diff] [blame] | 62 | 	actual, err := Create(client, EndpointOpts{ | 
| Ash Wilson | efac18b | 2014-09-10 14:44:42 -0400 | [diff] [blame] | 63 | 		Availability: gophercloud.AvailabilityPublic, | 
 | 64 | 		Name:         "the-endiest-of-points", | 
 | 65 | 		Region:       "underground", | 
 | 66 | 		URL:          "https://1.2.3.4:9000/", | 
 | 67 | 		ServiceID:    "asdfasdfasdfasdf", | 
| Ash Wilson | 3f59ade | 2014-10-02 09:22:23 -0400 | [diff] [blame] | 68 | 	}).Extract() | 
| Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 69 | 	if err != nil { | 
 | 70 | 		t.Fatalf("Unable to create an endpoint: %v", err) | 
 | 71 | 	} | 
 | 72 |  | 
| Ash Wilson | 989ce54 | 2014-09-04 10:52:49 -0400 | [diff] [blame] | 73 | 	expected := &Endpoint{ | 
| Ash Wilson | efac18b | 2014-09-10 14:44:42 -0400 | [diff] [blame] | 74 | 		ID:           "12", | 
 | 75 | 		Availability: gophercloud.AvailabilityPublic, | 
 | 76 | 		Name:         "the-endiest-of-points", | 
 | 77 | 		Region:       "underground", | 
 | 78 | 		ServiceID:    "asdfasdfasdfasdf", | 
 | 79 | 		URL:          "https://1.2.3.4:9000/", | 
| Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 80 | 	} | 
 | 81 |  | 
| Ash Wilson | 3f59ade | 2014-10-02 09:22:23 -0400 | [diff] [blame] | 82 | 	if !reflect.DeepEqual(actual, expected) { | 
 | 83 | 		t.Errorf("Expected %#v, was %#v", expected, actual) | 
| Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 84 | 	} | 
 | 85 | } | 
 | 86 |  | 
 | 87 | func TestListEndpoints(t *testing.T) { | 
 | 88 | 	testhelper.SetupHTTP() | 
 | 89 | 	defer testhelper.TeardownHTTP() | 
 | 90 |  | 
 | 91 | 	testhelper.Mux.HandleFunc("/endpoints", func(w http.ResponseWriter, r *http.Request) { | 
 | 92 | 		testhelper.TestMethod(t, r, "GET") | 
 | 93 | 		testhelper.TestHeader(t, r, "X-Auth-Token", tokenID) | 
 | 94 |  | 
| Ash Wilson | ab6be61 | 2014-09-15 15:51:22 -0400 | [diff] [blame] | 95 | 		w.Header().Add("Content-Type", "application/json") | 
| Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 96 | 		fmt.Fprintf(w, ` | 
| Ash Wilson | 8df23c8 | 2014-09-05 14:18:20 -0400 | [diff] [blame] | 97 | 			{ | 
 | 98 | 				"endpoints": [ | 
 | 99 | 					{ | 
 | 100 | 						"id": "12", | 
 | 101 | 						"interface": "public", | 
 | 102 | 						"links": { | 
 | 103 | 							"self": "https://localhost:5000/v3/endpoints/12" | 
 | 104 | 						}, | 
 | 105 | 						"name": "the-endiest-of-points", | 
 | 106 | 						"region": "underground", | 
 | 107 | 						"service_id": "asdfasdfasdfasdf", | 
 | 108 | 						"url": "https://1.2.3.4:9000/" | 
| Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 109 | 					}, | 
| Ash Wilson | 8df23c8 | 2014-09-05 14:18:20 -0400 | [diff] [blame] | 110 | 					{ | 
 | 111 | 						"id": "13", | 
 | 112 | 						"interface": "internal", | 
 | 113 | 						"links": { | 
 | 114 | 							"self": "https://localhost:5000/v3/endpoints/13" | 
 | 115 | 						}, | 
 | 116 | 						"name": "shhhh", | 
 | 117 | 						"region": "underground", | 
 | 118 | 						"service_id": "asdfasdfasdfasdf", | 
 | 119 | 						"url": "https://1.2.3.4:9001/" | 
 | 120 | 					} | 
 | 121 | 				], | 
 | 122 | 				"links": { | 
 | 123 | 					"next": null, | 
 | 124 | 					"previous": null | 
| Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 125 | 				} | 
| Ash Wilson | 8df23c8 | 2014-09-05 14:18:20 -0400 | [diff] [blame] | 126 | 			} | 
| Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 127 | 		`) | 
 | 128 | 	}) | 
 | 129 |  | 
 | 130 | 	client := serviceClient() | 
 | 131 |  | 
| Ash Wilson | 6269f25 | 2014-09-12 14:33:56 -0400 | [diff] [blame] | 132 | 	count := 0 | 
| Ash Wilson | 3c8cc77 | 2014-09-16 11:40:49 -0400 | [diff] [blame] | 133 | 	List(client, ListOpts{}).EachPage(func(page pagination.Page) (bool, error) { | 
| Ash Wilson | 6269f25 | 2014-09-12 14:33:56 -0400 | [diff] [blame] | 134 | 		count++ | 
 | 135 | 		actual, err := ExtractEndpoints(page) | 
 | 136 | 		if err != nil { | 
 | 137 | 			t.Errorf("Failed to extract endpoints: %v", err) | 
| Ash Wilson | 6b35e50 | 2014-09-12 15:15:23 -0400 | [diff] [blame] | 138 | 			return false, err | 
| Ash Wilson | 6269f25 | 2014-09-12 14:33:56 -0400 | [diff] [blame] | 139 | 		} | 
| Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 140 |  | 
| Ash Wilson | 6269f25 | 2014-09-12 14:33:56 -0400 | [diff] [blame] | 141 | 		expected := []Endpoint{ | 
| Ash Wilson | 32c0e8d | 2014-09-04 10:53:08 -0400 | [diff] [blame] | 142 | 			Endpoint{ | 
| Ash Wilson | efac18b | 2014-09-10 14:44:42 -0400 | [diff] [blame] | 143 | 				ID:           "12", | 
 | 144 | 				Availability: gophercloud.AvailabilityPublic, | 
 | 145 | 				Name:         "the-endiest-of-points", | 
 | 146 | 				Region:       "underground", | 
 | 147 | 				ServiceID:    "asdfasdfasdfasdf", | 
 | 148 | 				URL:          "https://1.2.3.4:9000/", | 
| Ash Wilson | 32c0e8d | 2014-09-04 10:53:08 -0400 | [diff] [blame] | 149 | 			}, | 
 | 150 | 			Endpoint{ | 
| Ash Wilson | efac18b | 2014-09-10 14:44:42 -0400 | [diff] [blame] | 151 | 				ID:           "13", | 
 | 152 | 				Availability: gophercloud.AvailabilityInternal, | 
 | 153 | 				Name:         "shhhh", | 
 | 154 | 				Region:       "underground", | 
 | 155 | 				ServiceID:    "asdfasdfasdfasdf", | 
 | 156 | 				URL:          "https://1.2.3.4:9001/", | 
| Ash Wilson | 32c0e8d | 2014-09-04 10:53:08 -0400 | [diff] [blame] | 157 | 			}, | 
| Ash Wilson | 6269f25 | 2014-09-12 14:33:56 -0400 | [diff] [blame] | 158 | 		} | 
| Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 159 |  | 
| Ash Wilson | 6269f25 | 2014-09-12 14:33:56 -0400 | [diff] [blame] | 160 | 		if !reflect.DeepEqual(expected, actual) { | 
 | 161 | 			t.Errorf("Expected %#v, got %#v", expected, actual) | 
 | 162 | 		} | 
 | 163 |  | 
| Ash Wilson | 6b35e50 | 2014-09-12 15:15:23 -0400 | [diff] [blame] | 164 | 		return true, nil | 
| Ash Wilson | 6269f25 | 2014-09-12 14:33:56 -0400 | [diff] [blame] | 165 | 	}) | 
 | 166 | 	if count != 1 { | 
 | 167 | 		t.Errorf("Expected 1 page, got %d", count) | 
| Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 168 | 	} | 
 | 169 | } | 
 | 170 |  | 
 | 171 | func TestUpdateEndpoint(t *testing.T) { | 
 | 172 | 	testhelper.SetupHTTP() | 
 | 173 | 	defer testhelper.TeardownHTTP() | 
 | 174 |  | 
 | 175 | 	testhelper.Mux.HandleFunc("/endpoints/12", func(w http.ResponseWriter, r *http.Request) { | 
| Ash Wilson | f04a74c | 2014-09-04 11:16:20 -0400 | [diff] [blame] | 176 | 		testhelper.TestMethod(t, r, "PATCH") | 
| Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 177 | 		testhelper.TestHeader(t, r, "X-Auth-Token", tokenID) | 
 | 178 | 		testhelper.TestJSONRequest(t, r, ` | 
 | 179 | 		{ | 
 | 180 | 	    "endpoint": { | 
 | 181 | 	      "name": "renamed", | 
 | 182 | 				"region": "somewhere-else" | 
 | 183 | 	    } | 
 | 184 | 		} | 
 | 185 | 	`) | 
 | 186 |  | 
 | 187 | 		fmt.Fprintf(w, ` | 
 | 188 | 		{ | 
 | 189 | 			"endpoint": { | 
 | 190 | 				"id": "12", | 
 | 191 | 				"interface": "public", | 
 | 192 | 				"links": { | 
 | 193 | 					"self": "https://localhost:5000/v3/endpoints/12" | 
 | 194 | 				}, | 
 | 195 | 				"name": "renamed", | 
 | 196 | 				"region": "somewhere-else", | 
 | 197 | 				"service_id": "asdfasdfasdfasdf", | 
 | 198 | 				"url": "https://1.2.3.4:9000/" | 
 | 199 | 			} | 
 | 200 | 		} | 
 | 201 | 	`) | 
 | 202 | 	}) | 
 | 203 |  | 
 | 204 | 	client := serviceClient() | 
 | 205 | 	actual, err := Update(client, "12", EndpointOpts{ | 
 | 206 | 		Name:   "renamed", | 
 | 207 | 		Region: "somewhere-else", | 
| Ash Wilson | 3f59ade | 2014-10-02 09:22:23 -0400 | [diff] [blame] | 208 | 	}).Extract() | 
| Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 209 | 	if err != nil { | 
 | 210 | 		t.Fatalf("Unexpected error from Update: %v", err) | 
 | 211 | 	} | 
 | 212 |  | 
| Ash Wilson | f04a74c | 2014-09-04 11:16:20 -0400 | [diff] [blame] | 213 | 	expected := &Endpoint{ | 
| Ash Wilson | efac18b | 2014-09-10 14:44:42 -0400 | [diff] [blame] | 214 | 		ID:           "12", | 
 | 215 | 		Availability: gophercloud.AvailabilityPublic, | 
 | 216 | 		Name:         "renamed", | 
 | 217 | 		Region:       "somewhere-else", | 
 | 218 | 		ServiceID:    "asdfasdfasdfasdf", | 
 | 219 | 		URL:          "https://1.2.3.4:9000/", | 
| Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 220 | 	} | 
 | 221 | 	if !reflect.DeepEqual(expected, actual) { | 
 | 222 | 		t.Errorf("Expected %#v, was %#v", expected, actual) | 
 | 223 | 	} | 
 | 224 | } | 
 | 225 |  | 
 | 226 | func TestDeleteEndpoint(t *testing.T) { | 
 | 227 | 	testhelper.SetupHTTP() | 
 | 228 | 	defer testhelper.TeardownHTTP() | 
 | 229 |  | 
 | 230 | 	testhelper.Mux.HandleFunc("/endpoints/34", func(w http.ResponseWriter, r *http.Request) { | 
 | 231 | 		testhelper.TestMethod(t, r, "DELETE") | 
 | 232 | 		testhelper.TestHeader(t, r, "X-Auth-Token", tokenID) | 
 | 233 |  | 
 | 234 | 		w.WriteHeader(http.StatusNoContent) | 
 | 235 | 	}) | 
 | 236 |  | 
 | 237 | 	client := serviceClient() | 
 | 238 |  | 
 | 239 | 	err := Delete(client, "34") | 
 | 240 | 	if err != nil { | 
 | 241 | 		t.Fatalf("Unexpected error from Delete: %v", err) | 
 | 242 | 	} | 
 | 243 | } |