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 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 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 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 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 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 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 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 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 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 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 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 TestGet(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() HandleGet(t) n, err := Get(fake.ServiceClient(), "46d4bfb9-b26e-41f3-bd2e-e6dcc1ccedb2").Extract() th.AssertNoErr(t, err) th.AssertEquals(t, n.Status, "ACTIVE") th.AssertEquals(t, n.Name, "") th.AssertEquals(t, n.AdminStateUp, true) th.AssertEquals(t, n.NetworkID, "a87cc70a-3e15-4acf-8205-9b711a3531b7") th.AssertEquals(t, n.TenantID, "7e02058126cc4950b75f9970368ba177") th.AssertEquals(t, n.DeviceOwner, "network:router_interface") th.AssertEquals(t, n.MACAddress, "fa:16:3e:23:fd:d7") th.AssertDeepEquals(t, n.FixedIPs, []ports.IP{ {SubnetID: "a0304c3a-4f08-4c43-88af-d796509c97d2", IPAddress: "10.0.0.1"}, }) th.AssertEquals(t, n.ID, "46d4bfb9-b26e-41f3-bd2e-e6dcc1ccedb2") th.AssertDeepEquals(t, n.SecurityGroups, []string{}) th.AssertEquals(t, n.DeviceID, "5e3898d7-11be-483e-9732-b2f5eccd2b2e") th.AssertEquals(t, n.HostID, "devstack") th.AssertEquals(t, n.VNICType, "normal") th.AssertEquals(t, n.VIFType, "ovs") th.AssertDeepEquals(t, n.VIFDetails, map[string]interface{}{"port_filter": true, "ovs_hybrid_plug": true}) }
func TestUpdate(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() HandleUpdate(t) options := UpdateOpts{ UpdateOptsBuilder: ports.UpdateOpts{ Name: "new_port_name", FixedIPs: []ports.IP{ {SubnetID: "a0304c3a-4f08-4c43-88af-d796509c97d2", IPAddress: "10.0.0.3"}, }, SecurityGroups: []string{"f0ac4394-7e4a-4409-9701-ba8be283dbc3"}, }, HostID: "HOST1", VNICType: "normal", } s, err := Update(fake.ServiceClient(), "65c0ee9f-d634-4522-8954-51021b570b0d", options).Extract() th.AssertNoErr(t, err) th.AssertEquals(t, s.Name, "new_port_name") th.AssertDeepEquals(t, s.FixedIPs, []ports.IP{ {SubnetID: "a0304c3a-4f08-4c43-88af-d796509c97d2", IPAddress: "10.0.0.3"}, }) th.AssertDeepEquals(t, s.SecurityGroups, []string{"f0ac4394-7e4a-4409-9701-ba8be283dbc3"}) th.AssertEquals(t, s.HostID, "HOST1") th.AssertEquals(t, s.VNICType, "normal") }
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 TestEnumerateMarker(t *testing.T) { pager := createMarkerPaged(t) defer testhelper.TeardownHTTP() callCount := 0 err := pager.EachPage(func(page Page) (bool, error) { actual, err := ExtractMarkerStrings(page) if err != nil { return false, err } t.Logf("Handler invoked with %v", actual) var expected []string switch callCount { case 0: expected = []string{"aaa", "bbb", "ccc"} case 1: expected = []string{"ddd", "eee", "fff"} case 2: expected = []string{"ggg", "hhh", "iii"} default: t.Fatalf("Unexpected call count: %d", callCount) return false, nil } testhelper.CheckDeepEquals(t, expected, actual) callCount++ return true, nil }) testhelper.AssertNoErr(t, err) testhelper.AssertEquals(t, callCount, 3) }
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 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 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 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 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 TestURLs(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() th.AssertEquals(t, th.Endpoint()+"v2.0/security-groups", rootURL(fake.ServiceClient())) th.AssertEquals(t, th.Endpoint()+"v2.0/security-groups/foo", resourceURL(fake.ServiceClient(), "foo")) }
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 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 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) }
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 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 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) }
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) }