func TestWaitForStatus(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() th.Mux.HandleFunc("/volumes/1234", func(w http.ResponseWriter, r *http.Request) { time.Sleep(3 * time.Second) w.Header().Add("Content-Type", "application/json") w.WriteHeader(http.StatusOK) fmt.Fprintf(w, ` { "volume": { "display_name": "vol-001", "id": "1234", "status":"available" } }`) }) err := WaitForStatus(client.ServiceClient(), "1234", "available", 0) if err == nil { t.Errorf("Expected error: 'Time Out in WaitFor'") } err = WaitForStatus(client.ServiceClient(), "1234", "available", 6) th.CheckNoErr(t, err) }
func TestWaitForStatus(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() th.Mux.HandleFunc("/servers/4321", func(w http.ResponseWriter, r *http.Request) { time.Sleep(2 * time.Second) w.Header().Add("Content-Type", "application/json") w.WriteHeader(http.StatusOK) fmt.Fprintf(w, ` { "server": { "name": "the-server", "id": "4321", "status": "ACTIVE" } }`) }) err := WaitForStatus(client.ServiceClient(), "4321", "ACTIVE", 0) if err == nil { t.Errorf("Expected error: 'Time Out in WaitFor'") } err = WaitForStatus(client.ServiceClient(), "4321", "ACTIVE", 3) th.CheckNoErr(t, err) }
func TestSizeRange(t *testing.T) { _, err := Create(fake.ServiceClient(), CreateOpts{volumes.CreateOpts{Size: 1}}).Extract() if err == nil { t.Fatalf("Expected error, got none") } _, err = Create(fake.ServiceClient(), CreateOpts{volumes.CreateOpts{Size: 2000}}).Extract() if err == nil { t.Fatalf("Expected error, got 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 TestDeleteURL(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() c := client.ServiceClient() th.CheckEquals(t, c.Endpoint+"os-keypairs/wat", deleteURL(c, "wat")) }
func TestList(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() os.MockListResponse(t) count := 0 err := List(fake.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) { count++ actual, err := ExtractVolumes(page) if err != nil { t.Errorf("Failed to extract volumes: %v", err) return false, err } expected := []Volume{ Volume{ ID: "289da7f8-6440-407c-9fb4-7db01ec49164", Name: "vol-001", }, Volume{ ID: "96c3bda7-c82a-4f50-be73-ca7621794835", Name: "vol-002", }, } th.CheckDeepEquals(t, expected, actual) return true, nil }) th.AssertEquals(t, 1, count) th.AssertNoErr(t, err) }
func TestListCerts(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() mockListCertsResponse(t, lbID) count := 0 err := ListCerts(client.ServiceClient(), lbID).EachPage(func(page pagination.Page) (bool, error) { count++ actual, err := ExtractCerts(page) th.AssertNoErr(t, err) expected := []Certificate{ Certificate{ID: 123, HostName: "rackspace.com"}, Certificate{ID: 124, HostName: "*.rackspace.com"}, } th.CheckDeepEquals(t, expected, actual) return true, nil }) th.AssertNoErr(t, err) th.AssertEquals(t, 1, count) }
func TestCreate(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() HandleCreate(t) opts := CreateOpts{ Name: "json_rack_instance", FlavorRef: "1", Databases: db.BatchCreateOpts{ db.CreateOpts{CharSet: "utf8", Collate: "utf8_general_ci", Name: "sampledb"}, db.CreateOpts{Name: "nextround"}, }, Users: users.BatchCreateOpts{ users.CreateOpts{ Name: "demouser", Password: "******", Databases: db.BatchCreateOpts{ db.CreateOpts{Name: "sampledb"}, }, }, }, Size: 2, } instance, err := Create(fake.ServiceClient(), opts).Extract() th.AssertNoErr(t, err) th.AssertDeepEquals(t, &expectedInstance, instance) }
func TestGet(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() th.Mux.HandleFunc("/extensions/agent", 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") 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(client.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 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 TestGetHomeDocument(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() os.HandleGetSuccessfully(t) actual, err := Get(fake.ServiceClient()).Extract() th.CheckNoErr(t, err) expected := os.HomeDocument{ "rel/cdn": map[string]interface{}{ "href-template": "services{?marker,limit}", "href-vars": map[string]interface{}{ "marker": "param/marker", "limit": "param/limit", }, "hints": map[string]interface{}{ "allow": []string{"GET"}, "formats": map[string]interface{}{ "application/json": map[string]interface{}{}, }, }, }, } th.CheckDeepEquals(t, expected, *actual) }
func TestList(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() mockListResponse(t, lbID) count := 0 err := List(client.ServiceClient(), lbID).EachPage(func(page pagination.Page) (bool, error) { count++ actual, err := ExtractVIPs(page) th.AssertNoErr(t, err) expected := []VIP{ VIP{ID: 1000, Address: "206.10.10.210", Type: "PUBLIC"}, } th.CheckDeepEquals(t, expected, actual) return true, nil }) th.AssertNoErr(t, err) th.AssertEquals(t, 1, count) }
func tokenPost(t *testing.T, options gophercloud.AuthOptions, requestJSON string) CreateResult { th.SetupHTTP() defer th.TeardownHTTP() HandleTokenPost(t, requestJSON) return Create(client.ServiceClient(), AuthOptions{options}) }
func TestCreateErr(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() mockCreateErrResponse(t, lbID) opts := CreateOpts{ CreateOpt{ Address: "10.2.2.3", Port: 80, Condition: ENABLED, Type: PRIMARY, }, CreateOpt{ Address: "10.2.2.4", Port: 81, Condition: ENABLED, Type: SECONDARY, }, } page := Create(client.ServiceClient(), lbID, opts) actual, err := page.ExtractNodes() if err == nil { t.Fatal("Did not receive expected error from ExtractNodes") } if actual != nil { t.Fatalf("Received non-nil result from failed ExtractNodes: %#v", actual) } }
func TestCreate(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() mockCreateRuleResponse(t) opts := CreateOpts{ IPProtocol: "TCP", FromPort: 80, ToPort: 80, CIDR: "10.10.12.0/24", } group, err := Create(client.ServiceClient(), opts).Extract() th.AssertNoErr(t, err) expected := &DefaultRule{ ID: ruleID, FromPort: 80, ToPort: 80, IPProtocol: "TCP", IPRange: secgroups.IPRange{CIDR: "10.10.12.0/24"}, } th.AssertDeepEquals(t, expected, group) }
func TestList(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() mockListRulesResponse(t) count := 0 err := List(client.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) { count++ actual, err := ExtractDefaultRules(page) th.AssertNoErr(t, err) expected := []DefaultRule{ DefaultRule{ FromPort: 80, ID: ruleID, IPProtocol: "TCP", IPRange: secgroups.IPRange{CIDR: "10.10.10.0/24"}, ToPort: 80, }, } th.CheckDeepEquals(t, expected, actual) return true, nil }) th.AssertNoErr(t, err) th.AssertEquals(t, 1, count) }
func TestListURL(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() c := client.ServiceClient() th.CheckEquals(t, c.Endpoint+"os-keypairs", listURL(c)) }
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) } }
func TestGenerateExecute(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() th.Mux.HandleFunc("/os-keypairs", func(w http.ResponseWriter, r *http.Request) { w.Header().Add("Content-Type", "application/json") fmt.Fprintf(w, `{"keypair":{}}`) }) app := cli.NewApp() flagset := flag.NewFlagSet("flags", 1) flagset.Bool("json", false, "") c := cli.NewContext(app, flagset, nil) cmd := &commandGenerate{ Ctx: &handler.Context{ CLIContext: c, ServiceClient: client.ServiceClient(), }, } actual := &handler.Resource{ Params: ¶msGenerate{ opts: &osKeypairs.CreateOpts{ Name: "keypair1Name", }, }, } cmd.Execute(actual) th.AssertNoErr(t, actual.Err) }
func TestGet(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() mockGetGroupsResponse(t, groupID) group, err := Get(client.ServiceClient(), groupID).Extract() th.AssertNoErr(t, err) expected := &SecurityGroup{ ID: groupID, Description: "default", Name: "default", TenantID: "openstack", Rules: []Rule{ Rule{ FromPort: 80, ToPort: 85, IPProtocol: "TCP", IPRange: IPRange{CIDR: "0.0.0.0"}, Group: Group{TenantID: "openstack", Name: "default"}, ParentGroupID: groupID, ID: ruleID, }, }, } th.AssertDeepEquals(t, expected, group) }
func TestList(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() HandlePolicyListSuccessfully(t) pages := 0 pager := List(client.ServiceClient(), "60b15dad-5ea1-43fa-9a12-a1d737b4da07") err := pager.EachPage(func(page pagination.Page) (bool, error) { pages++ policies, err := ExtractPolicies(page) if err != nil { return false, err } if len(policies) != 3 { t.Fatalf("Expected 3 policies, got %d", len(policies)) } th.CheckDeepEquals(t, WebhookPolicy, policies[0]) th.CheckDeepEquals(t, OneTimePolicy, policies[1]) th.CheckDeepEquals(t, SundayAfternoonPolicy, policies[2]) return true, nil }) th.AssertNoErr(t, err) if pages != 1 { t.Errorf("Expected 1 page, saw %d", pages) } }
func TestAddRuleICMPZero(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() mockAddRuleResponseICMPZero(t) opts := CreateRuleOpts{ ParentGroupID: groupID, FromPort: 0, ToPort: 0, IPProtocol: "ICMP", CIDR: "0.0.0.0/0", } rule, err := CreateRule(client.ServiceClient(), opts).Extract() th.AssertNoErr(t, err) expected := &Rule{ FromPort: 0, ToPort: 0, Group: Group{}, IPProtocol: "ICMP", ParentGroupID: groupID, IPRange: IPRange{CIDR: "0.0.0.0/0"}, ID: ruleID, } th.AssertDeepEquals(t, expected, rule) }
func TestList(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() mockListResponse(t, lbID) count := 0 err := List(client.ServiceClient(), lbID).EachPage(func(page pagination.Page) (bool, error) { count++ actual, err := ExtractAccessList(page) th.AssertNoErr(t, err) expected := AccessList{ NetworkItem{Address: "206.160.163.21", ID: 21, Type: DENY}, NetworkItem{Address: "206.160.163.22", ID: 22, Type: DENY}, NetworkItem{Address: "206.160.163.23", ID: 23, Type: DENY}, NetworkItem{Address: "206.160.163.24", ID: 24, Type: DENY}, } th.CheckDeepEquals(t, expected, actual) return true, nil }) th.AssertNoErr(t, err) th.AssertEquals(t, 1, count) }
func TestResizeHandleSingle(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() th.Mux.HandleFunc("/servers/detail", func(w http.ResponseWriter, r *http.Request) { w.Header().Add("Content-Type", "application/json") fmt.Fprintf(w, `{"servers":[{"ID":"server1","Name":"server1Name"}]}`) }) app := cli.NewApp() flagset := flag.NewFlagSet("flags", 1) flagset.String("id", "", "") flagset.Set("id", "server1") c := cli.NewContext(app, flagset, nil) cmd := &commandResize{ Ctx: &handler.Context{ CLIContext: c, ServiceClient: client.ServiceClient(), }, } expected := &handler.Resource{ Params: ¶msResize{ serverID: "server1", }, } actual := &handler.Resource{ Params: ¶msResize{}, } err := cmd.HandleSingle(actual) th.AssertNoErr(t, err) th.AssertEquals(t, expected.Params.(*paramsResize).serverID, actual.Params.(*paramsResize).serverID) }
func TestList(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() HandleList(t) expectedDBs := []Database{ Database{Name: "anotherexampledb"}, Database{Name: "exampledb"}, Database{Name: "nextround"}, Database{Name: "sampledb"}, Database{Name: "testingdb"}, } pages := 0 err := List(fake.ServiceClient(), instanceID).EachPage(func(page pagination.Page) (bool, error) { pages++ actual, err := ExtractDBs(page) if err != nil { return false, err } th.CheckDeepEquals(t, expectedDBs, actual) return true, nil }) th.AssertNoErr(t, err) if pages != 1 { t.Errorf("Expected 1 page, saw %d", pages) } }
func TestRebuildExecute(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() th.Mux.HandleFunc("/servers/server1/action", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusAccepted) w.Header().Add("Content-Type", "application/json") fmt.Fprintf(w, `{"server":{}}`) }) cmd := &commandRebuild{ Ctx: &handler.Context{ ServiceClient: client.ServiceClient(), }, } actual := &handler.Resource{ Params: ¶msRebuild{ serverID: "server1", opts: &servers.RebuildOpts{ Name: "server1Rename", ImageID: "123456789", AdminPass: "******", DiskConfig: diskconfig.Auto, }, }, } cmd.Execute(actual) th.AssertNoErr(t, actual.Err) }
func TestListByServer(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() mockListGroupsByServerResponse(t, serverID) count := 0 err := ListByServer(client.ServiceClient(), serverID).EachPage(func(page pagination.Page) (bool, error) { count++ actual, err := ExtractSecurityGroups(page) if err != nil { t.Errorf("Failed to extract users: %v", err) return false, err } expected := []SecurityGroup{ SecurityGroup{ ID: groupID, Description: "default", Name: "default", Rules: []Rule{}, TenantID: "openstack", }, } th.CheckDeepEquals(t, expected, actual) return true, nil }) th.AssertNoErr(t, err) th.AssertEquals(t, 1, count) }
func TestListServers(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() HandleServerListSuccessfully(t) pages := 0 err := List(client.ServiceClient(), ListOpts{}).EachPage(func(page pagination.Page) (bool, error) { pages++ actual, err := ExtractServers(page) if err != nil { return false, err } if len(actual) != 2 { t.Fatalf("Expected 2 servers, got %d", len(actual)) } th.CheckDeepEquals(t, ServerHerp, actual[0]) th.CheckDeepEquals(t, ServerDerp, actual[1]) return true, nil }) th.AssertNoErr(t, err) if pages != 1 { t.Errorf("Expected 1 page, saw %d", pages) } }
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 TestRole(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() MockListRoleResponse(t) count := 0 err := List(client.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) { count++ actual, err := os.ExtractRoles(page) if err != nil { t.Errorf("Failed to extract users: %v", err) return false, err } expected := []os.Role{ os.Role{ ID: "123", Name: "compute:admin", Description: "Nova Administrator", ServiceID: "cke5372ebabeeabb70a0e702a4626977x4406e5", }, } th.CheckDeepEquals(t, expected, actual) return true, nil }) th.AssertNoErr(t, err) th.AssertEquals(t, 1, count) }