func mockCreateRuleResponse(t *testing.T) { th.Mux.HandleFunc(rootPath, func(w http.ResponseWriter, r *http.Request) { th.TestMethod(t, r, "POST") th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) th.TestJSONRequest(t, r, ` { "security_group_default_rule": { "ip_protocol": "TCP", "from_port": 80, "to_port": 80, "cidr": "10.10.12.0/24" } } `) w.Header().Add("Content-Type", "application/json") w.WriteHeader(http.StatusOK) fmt.Fprintf(w, ` { "security_group_default_rule": { "from_port": 80, "id": "{ruleID}", "ip_protocol": "TCP", "ip_range": { "cidr": "10.10.12.0/24" }, "to_port": 80 } } `) }) }
func TestBulkDelete(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() th.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { th.TestMethod(t, r, "DELETE") th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) th.AssertEquals(t, r.URL.RawQuery, "bulk-delete") w.WriteHeader(http.StatusOK) fmt.Fprintf(w, ` { "Number Not Found": 1, "Response Status": "200 OK", "Errors": [], "Number Deleted": 1, "Response Body": "" } `) }) options := DeleteOpts{"gophercloud-testcontainer1", "gophercloud-testcontainer2"} actual, err := Delete(fake.ServiceClient(), options).ExtractBody() th.AssertNoErr(t, err) th.AssertEquals(t, actual.NumberDeleted, 1) }
func MockDeleteUserRoleResponse(t *testing.T) { th.Mux.HandleFunc("/users/{user_id}/roles/OS-KSADM/{role_id}", func(w http.ResponseWriter, r *http.Request) { th.TestMethod(t, r, "DELETE") th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) w.WriteHeader(http.StatusNoContent) }) }
// HandleDeleteCDNAssetSuccessfully creates an HTTP handler at `/services/{id}/assets` on the test handler mux // that responds with a `Delete` response. func HandleDeleteCDNAssetSuccessfully(t *testing.T) { th.Mux.HandleFunc("/services/96737ae3-cfc1-4c72-be88-5d0e7cc9a3f0/assets", func(w http.ResponseWriter, r *http.Request) { th.TestMethod(t, r, "DELETE") th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) w.WriteHeader(http.StatusAccepted) }) }
// HandleImageMemberList happy path setup func HandleImageMemberList(t *testing.T) { th.Mux.HandleFunc("/images/da3b75d9-3f4a-40e7-8a2c-bfab23927dea/members", func(w http.ResponseWriter, r *http.Request) { th.TestMethod(t, r, "GET") th.TestHeader(t, r, "X-Auth-Token", fakeclient.TokenID) w.WriteHeader(http.StatusOK) fmt.Fprintf(w, `{ "members": [ { "created_at": "2013-10-07T17:58:03Z", "image_id": "da3b75d9-3f4a-40e7-8a2c-bfab23927dea", "member_id": "123456789", "schema": "/v2/schemas/member", "status": "pending", "updated_at": "2013-10-07T17:58:03Z" }, { "created_at": "2013-10-07T17:58:55Z", "image_id": "da3b75d9-3f4a-40e7-8a2c-bfab23927dea", "member_id": "987654321", "schema": "/v2/schemas/member", "status": "accepted", "updated_at": "2013-10-08T12:08:55Z" } ], "schema": "/v2/schemas/members" }`) }) }
func mockAddRuleResponseICMPZero(t *testing.T) { th.Mux.HandleFunc("/os-security-group-rules", func(w http.ResponseWriter, r *http.Request) { th.TestMethod(t, r, "POST") th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) th.TestJSONRequest(t, r, ` { "security_group_rule": { "from_port": 0, "ip_protocol": "ICMP", "to_port": 0, "parent_group_id": "{groupID}", "cidr": "0.0.0.0/0" } } `) w.Header().Add("Content-Type", "application/json") w.WriteHeader(http.StatusOK) fmt.Fprintf(w, ` { "security_group_rule": { "from_port": 0, "group": {}, "ip_protocol": "ICMP", "to_port": 0, "parent_group_id": "{groupID}", "ip_range": { "cidr": "0.0.0.0/0" }, "id": "{ruleID}" } }`) }) }
func mockCreateResponse(t *testing.T, lbID int) { th.Mux.HandleFunc(_rootURL(lbID), func(w http.ResponseWriter, r *http.Request) { th.TestMethod(t, r, "POST") th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) th.TestJSONRequest(t, r, ` { "type":"PUBLIC", "ipVersion":"IPV6" } `) w.Header().Add("Content-Type", "application/json") w.WriteHeader(http.StatusAccepted) fmt.Fprintf(w, ` { "address":"fd24:f480:ce44:91bc:1af2:15ff:0000:0002", "id":9000134, "type":"PUBLIC", "ipVersion":"IPV6" } `) }) }
// HandleListObjectsInfoSuccessfully creates an HTTP handler at `/testContainer` on the test handler mux that // responds with a `List` response when full info is requested. func HandleListObjectsInfoSuccessfully(t *testing.T) { th.Mux.HandleFunc("/testContainer", func(w http.ResponseWriter, r *http.Request) { th.TestMethod(t, r, "GET") th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) th.TestHeader(t, r, "Accept", "application/json") w.Header().Set("Content-Type", "application/json") r.ParseForm() marker := r.Form.Get("marker") switch marker { case "": fmt.Fprintf(w, `[ { "hash": "451e372e48e0f6b1114fa0724aa79fa1", "last_modified": "2009-11-10 23:00:00 +0000 UTC", "bytes": 14, "name": "goodbye", "content_type": "application/octet-stream" }, { "hash": "451e372e48e0f6b1114fa0724aa79fa1", "last_modified": "2009-11-10 23:00:00 +0000 UTC", "bytes": 14, "name": "hello", "content_type": "application/octet-stream" } ]`) case "hello": fmt.Fprintf(w, `[]`) default: t.Fatalf("Unexpected marker: [%s]", marker) } }) }
func MockGetResponse(t *testing.T) { th.Mux.HandleFunc("/volumes/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) { th.TestMethod(t, r, "GET") th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) w.Header().Add("Content-Type", "application/json") w.WriteHeader(http.StatusOK) fmt.Fprintf(w, ` { "volume": { "display_name": "vol-001", "id": "d32019d3-bc6e-4319-9c1d-6722fc136a22", "attachments": [ { "device": "/dev/vde", "server_id": "a740d24b-dc5b-4d59-ac75-53971c2920ba", "id": "d6da11e5-2ed3-413e-88d8-b772ba62193d", "volume_id": "d6da11e5-2ed3-413e-88d8-b772ba62193d" } ] } } `) }) }
func mockGetStatsResponse(t *testing.T, id int) { th.Mux.HandleFunc("/loadbalancers/"+strconv.Itoa(id)+"/stats", func(w http.ResponseWriter, r *http.Request) { th.TestMethod(t, r, "GET") th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) w.Header().Add("Content-Type", "application/json") w.WriteHeader(http.StatusOK) fmt.Fprintf(w, ` { "connectTimeOut": 10, "connectError": 20, "connectFailure": 30, "dataTimedOut": 40, "keepAliveTimedOut": 50, "maxConn": 60, "currentConn": 40, "connectTimeOutSsl": 10, "connectErrorSsl": 20, "connectFailureSsl": 30, "dataTimedOutSsl": 40, "keepAliveTimedOutSsl": 50, "maxConnSsl": 60, "currentConnSsl": 40 } `) }) }
func TestAssociateHealthMonitor(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() th.Mux.HandleFunc("/v2.0/lb/pools/332abe93-f488-41ba-870b-2ac66be7f853/health_monitors", func(w http.ResponseWriter, r *http.Request) { th.TestMethod(t, r, "POST") th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) th.TestHeader(t, r, "Content-Type", "application/json") th.TestHeader(t, r, "Accept", "application/json") th.TestJSONRequest(t, r, ` { "health_monitor":{ "id":"b624decf-d5d3-4c66-9a3d-f047e7786181" } } `) w.Header().Add("Content-Type", "application/json") w.WriteHeader(http.StatusCreated) fmt.Fprintf(w, `{}`) }) _, err := AssociateMonitor(fake.ServiceClient(), "332abe93-f488-41ba-870b-2ac66be7f853", "b624decf-d5d3-4c66-9a3d-f047e7786181").Extract() th.AssertNoErr(t, err) }
func mockDeleteErrorPageResponse(t *testing.T, id int) { th.Mux.HandleFunc("/loadbalancers/"+strconv.Itoa(id)+"/errorpage", func(w http.ResponseWriter, r *http.Request) { th.TestMethod(t, r, "DELETE") th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) w.WriteHeader(http.StatusOK) }) }
func mockListAlgorithmsResponse(t *testing.T) { th.Mux.HandleFunc("/loadbalancers/algorithms", func(w http.ResponseWriter, r *http.Request) { th.TestMethod(t, r, "GET") th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) w.Header().Add("Content-Type", "application/json") w.WriteHeader(http.StatusOK) fmt.Fprintf(w, ` { "algorithms": [ { "name": "LEAST_CONNECTIONS" }, { "name": "RANDOM" }, { "name": "ROUND_ROBIN" }, { "name": "WEIGHTED_LEAST_CONNECTIONS" }, { "name": "WEIGHTED_ROUND_ROBIN" } ] } `) }) }
// HandleListExtensionsSuccessfully creates an HTTP handler that returns ListOutput for a List // call. func HandleListExtensionsSuccessfully(t *testing.T) { th.Mux.HandleFunc("/extensions", func(w http.ResponseWriter, r *http.Request) { th.TestMethod(t, r, "GET") th.TestHeader(t, r, "X-Auth-Token", client.TokenID) w.Header().Add("Content-Type", "application/json") fmt.Fprintf(w, ` { "extensions": { "values": [ { "updated": "2013-01-20T00:00:00-00:00", "name": "Neutron Service Type Management", "links": [], "namespace": "http://docs.openstack.org/ext/neutron/service-type/api/v1.0", "alias": "service-type", "description": "API for retrieving service providers for Neutron advanced services" } ] } } `) }) }
func mockDisableResponse(t *testing.T, lbID int) { th.Mux.HandleFunc(_rootURL(lbID), func(w http.ResponseWriter, r *http.Request) { th.TestMethod(t, r, "DELETE") th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) w.WriteHeader(http.StatusAccepted) }) }
func registerTenants(s *CommonSuite) { th.Mux.HandleFunc("/v2.0/tenants", func(w http.ResponseWriter, r *http.Request) { th.TestMethod(s.T(), r, "GET") th.TestHeader(s.T(), r, "X-Auth-Token", s.Token) w.Header().Add("Content-Type", "application/json") w.WriteHeader(http.StatusOK) fmt.Fprintf(w, ` { "tenants": [ { "description": "Test tenat", "enabled": true, "id": "%s", "name": "%s" }, { "description": "admin tenant", "enabled": true, "id": "%s", "name": "%s" } ], "tenants_links": [] } `, s.Tenant1ID, s.Tenant1Name, s.Tenant2ID, s.Tenant2Name) }) }
// HandleAddressListSuccessfully sets up the test server to respond to a ListAddresses request. func HandleAddressListSuccessfully(t *testing.T) { th.Mux.HandleFunc("/servers/asdfasdfasdf/ips", func(w http.ResponseWriter, r *http.Request) { th.TestMethod(t, r, "GET") th.TestHeader(t, r, "X-Auth-Token", client.TokenID) w.Header().Add("Content-Type", "application/json") fmt.Fprintf(w, `{ "addresses": { "public": [ { "version": 4, "addr": "50.56.176.35" }, { "version": 6, "addr": "2001:4800:780e:510:be76:4eff:fe04:84a8" } ], "private": [ { "version": 4, "addr": "10.180.3.155" } ] } }`) }) }
func TestListImageDetails(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() th.Mux.HandleFunc("/images/detail", func(w http.ResponseWriter, r *http.Request) { th.TestMethod(t, r, "GET") th.TestHeader(t, r, "X-Auth-Token", client.TokenID) w.Header().Add("Content-Type", "application/json") r.ParseForm() marker := r.Form.Get("marker") switch marker { case "": fmt.Fprintf(w, ListOutput) case "e19a734c-c7e6-443a-830c-242209c4d65d": fmt.Fprintf(w, `{ "images": [] }`) default: t.Fatalf("Unexpected marker: [%s]", marker) } }) count := 0 err := ListDetail(client.ServiceClient(), nil).EachPage(func(page pagination.Page) (bool, error) { count++ actual, err := ExtractImages(page) th.AssertNoErr(t, err) th.CheckDeepEquals(t, ExpectedImageSlice, actual) return true, nil }) th.AssertNoErr(t, err) th.CheckEquals(t, 1, count) }
func mockUpdateGroupResponse(t *testing.T, groupID string) { url := fmt.Sprintf("%s/%s", rootPath, groupID) th.Mux.HandleFunc(url, func(w http.ResponseWriter, r *http.Request) { th.TestMethod(t, r, "PUT") th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) th.TestJSONRequest(t, r, ` { "security_group": { "name": "new_name", "description": "new_desc" } } `) w.Header().Add("Content-Type", "application/json") w.WriteHeader(http.StatusOK) fmt.Fprintf(w, ` { "security_group": { "description": "something", "id": "{groupID}", "name": "new_name", "rules": [], "tenant_id": "openstack" } } `) }) }
func MockListResponse(t *testing.T) { th.Mux.HandleFunc("/types", func(w http.ResponseWriter, r *http.Request) { th.TestMethod(t, r, "GET") th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) w.Header().Add("Content-Type", "application/json") w.WriteHeader(http.StatusOK) fmt.Fprintf(w, ` { "volume_types": [ { "id": "289da7f8-6440-407c-9fb4-7db01ec49164", "name": "vol-type-001", "extra_specs": { "capabilities": "gpu" } }, { "id": "96c3bda7-c82a-4f50-be73-ca7621794835", "name": "vol-type-002", "extra_specs": {} } ] } `) }) }
func MockTerminateConnectionResponse(t *testing.T) { th.Mux.HandleFunc("/volumes/cd281d77-8217-4830-be95-9528227c105c/action", func(w http.ResponseWriter, r *http.Request) { th.TestMethod(t, r, "POST") th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) th.TestHeader(t, r, "Content-Type", "application/json") th.TestHeader(t, r, "Accept", "application/json") th.TestJSONRequest(t, r, ` { "os-terminate_connection": { "connector": { "ip":"127.0.0.1", "host":"stack", "initiator":"iqn.1994-05.com.redhat:17cf566367d2", "multipath": false, "platform": "x86_64", "os_type": "linux2" } } } `) w.Header().Add("Content-Type", "application/json") w.WriteHeader(http.StatusAccepted) fmt.Fprintf(w, `{}`) }) }
func TestUpdateSuccessful(t *testing.T) { testhelper.SetupHTTP() defer testhelper.TeardownHTTP() testhelper.Mux.HandleFunc("/services/12345", func(w http.ResponseWriter, r *http.Request) { testhelper.TestMethod(t, r, "PATCH") testhelper.TestHeader(t, r, "X-Auth-Token", client.TokenID) testhelper.TestJSONRequest(t, r, `{ "type": "lasermagic" }`) w.Header().Add("Content-Type", "application/json") fmt.Fprintf(w, ` { "service": { "id": "12345", "type": "lasermagic" } } `) }) result, err := Update(client.ServiceClient(), "12345", "lasermagic").Extract() if err != nil { t.Fatalf("Unable to update service: %v", err) } if result.ID != "12345" { t.Fatalf("Expected ID 12345, was %s", result.ID) } }
// HandleImageGetSuccessfully test setup func HandleImageGetSuccessfully(t *testing.T) { th.Mux.HandleFunc("/images/1bea47ed-f6a9-463b-b423-14b9cca9ad27", func(w http.ResponseWriter, r *http.Request) { th.TestMethod(t, r, "GET") th.TestHeader(t, r, "X-Auth-Token", fakeclient.TokenID) w.WriteHeader(http.StatusOK) w.Header().Add("Content-Type", "application/json") fmt.Fprintf(w, `{ "status": "active", "name": "cirros-0.3.2-x86_64-disk", "tags": [], "container_format": "bare", "created_at": "2014-05-05T17:15:10Z", "disk_format": "qcow2", "updated_at": "2014-05-05T17:15:11Z", "visibility": "public", "self": "/v2/images/1bea47ed-f6a9-463b-b423-14b9cca9ad27", "min_disk": 0, "protected": false, "id": "1bea47ed-f6a9-463b-b423-14b9cca9ad27", "file": "/v2/images/1bea47ed-f6a9-463b-b423-14b9cca9ad27/file", "checksum": "64d7c1cd2b6f60c92c14662941cb7913", "owner": "5ef70662f8b34079a6eddb8da9d75fe8", "size": 13167616, "min_ram": 0, "schema": "/v2/schemas/image", "virtual_size": "None" }`) }) }
func TestSendEvent(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() th.Mux.HandleFunc("/events", func(w http.ResponseWriter, r *http.Request) { th.TestMethod(t, r, "POST") th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) th.TestHeader(t, r, "Accept", "application/json") th.TestJSONRequest(t, r, `{ "what": "Test Event", "when": 1441831996000, "tags": "Restart", "data": "Test Data" }`) w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) fmt.Fprintf(w, ``) }) event := Event{ What: "Test Event", When: 1441831996000, Tags: "Restart", Data: "Test Data", } SendEvent(fake.ServiceClient(), event) }
func TestGet(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() th.Mux.HandleFunc("/v2.0/extensions/agent", func(w http.ResponseWriter, r *http.Request) { th.TestMethod(t, r, "GET") th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) w.Header().Add("Content-Type", "application/json") w.WriteHeader(http.StatusOK) fmt.Fprintf(w, ` { "extension": { "updated": "2013-02-03T10:00:00-00:00", "name": "agent", "links": [], "namespace": "http://docs.openstack.org/ext/agent/api/v2.0", "alias": "agent", "description": "The agent management extension." } } `) }) ext, err := Get(fake.ServiceClient(), "agent").Extract() th.AssertNoErr(t, err) th.AssertEquals(t, ext.Updated, "2013-02-03T10:00:00-00:00") th.AssertEquals(t, ext.Name, "agent") th.AssertEquals(t, ext.Namespace, "http://docs.openstack.org/ext/agent/api/v2.0") th.AssertEquals(t, ext.Alias, "agent") th.AssertEquals(t, ext.Description, "The agent management extension.") }
func MockDeleteResponse(t *testing.T) { th.Mux.HandleFunc("/snapshots/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) { th.TestMethod(t, r, "DELETE") th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) w.WriteHeader(http.StatusNoContent) }) }
func MockAddUserRoleResponse(t *testing.T) { th.Mux.HandleFunc("/users/{user_id}/roles/OS-KSADM/{role_id}", func(w http.ResponseWriter, r *http.Request) { th.TestMethod(t, r, "PUT") th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) w.WriteHeader(http.StatusCreated) }) }
func MockCreateResponse(t *testing.T) { th.Mux.HandleFunc("/snapshots", func(w http.ResponseWriter, r *http.Request) { th.TestMethod(t, r, "POST") th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) th.TestHeader(t, r, "Content-Type", "application/json") th.TestHeader(t, r, "Accept", "application/json") th.TestJSONRequest(t, r, ` { "snapshot": { "volume_id": "1234", "display_name": "snapshot-001" } } `) w.Header().Add("Content-Type", "application/json") w.WriteHeader(http.StatusCreated) fmt.Fprintf(w, ` { "snapshot": { "volume_id": "1234", "display_name": "snapshot-001", "id": "d32019d3-bc6e-4319-9c1d-6722fc136a22" } } `) }) }
func TestCreateWithOptionalFields(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() th.Mux.HandleFunc("/v2.0/networks", func(w http.ResponseWriter, r *http.Request) { th.TestMethod(t, r, "POST") th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) th.TestHeader(t, r, "Content-Type", "application/json") th.TestHeader(t, r, "Accept", "application/json") th.TestJSONRequest(t, r, ` { "network": { "name": "sample_network", "admin_state_up": true, "shared": true, "tenant_id": "12345" } } `) w.WriteHeader(http.StatusCreated) fmt.Fprintf(w, `{}`) }) iTrue := true options := CreateOpts{Name: "sample_network", AdminStateUp: &iTrue, Shared: &iTrue, TenantID: "12345"} _, err := Create(fake.ServiceClient(), options).Extract() th.AssertNoErr(t, err) }
// HandlePingSuccessfully creates an HTTP handler at `/ping` on the test handler // mux that responds with a `Ping` response. func HandlePingSuccessfully(t *testing.T) { th.Mux.HandleFunc("/ping", func(w http.ResponseWriter, r *http.Request) { th.TestMethod(t, r, "GET") th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) w.WriteHeader(http.StatusNoContent) }) }