func TestListAll(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() MockListResponse(t) allPages, err := List(client.ServiceClient(), &ListOpts{}).AllPages() th.AssertNoErr(t, err) actual, err := ExtractVolumes(allPages) th.AssertNoErr(t, 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) }
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) }
// IsSuccessful ensures that a CreateResult was successful and contains the correct token and // service catalog. func IsSuccessful(t *testing.T, result CreateResult) { token, err := result.ExtractToken() th.AssertNoErr(t, err) th.CheckDeepEquals(t, ExpectedToken, token) serviceCatalog, err := result.ExtractServiceCatalog() th.AssertNoErr(t, err) th.CheckDeepEquals(t, ExpectedServiceCatalog, serviceCatalog) }
func TestListAllContainerNames(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() HandleListContainerNamesSuccessfully(t) allPages, err := List(fake.ServiceClient(), &ListOpts{Full: false}).AllPages() th.AssertNoErr(t, err) actual, err := ExtractNames(allPages) th.AssertNoErr(t, err) th.CheckDeepEquals(t, ExpectedListNames, actual) }
func TestListAllServers(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() HandleServerListSuccessfully(t) allPages, err := List(client.ServiceClient(), ListOpts{}).AllPages() th.AssertNoErr(t, err) actual, err := ExtractServers(allPages) th.AssertNoErr(t, err) th.CheckDeepEquals(t, ServerHerp, actual[0]) th.CheckDeepEquals(t, ServerDerp, actual[1]) }
func TestAllPagesSingle(t *testing.T) { pager := setupSinglePaged() defer testhelper.TeardownHTTP() page, err := pager.AllPages() testhelper.AssertNoErr(t, err) expected := []int{1, 2, 3} actual, err := ExtractSingleInts(page) testhelper.AssertNoErr(t, err) testhelper.CheckDeepEquals(t, expected, actual) }
func TestAllPagesLinked(t *testing.T) { pager := createLinked(t) defer testhelper.TeardownHTTP() page, err := pager.AllPages() testhelper.AssertNoErr(t, err) expected := []int{1, 2, 3, 4, 5, 6, 7, 8, 9} actual, err := ExtractLinkedInts(page) testhelper.AssertNoErr(t, err) testhelper.CheckDeepEquals(t, expected, actual) }
func TestAllPagesMarker(t *testing.T) { pager := createMarkerPaged(t) defer testhelper.TeardownHTTP() page, err := pager.AllPages() testhelper.AssertNoErr(t, err) expected := []string{"aaa", "bbb", "ccc", "ddd", "eee", "fff", "ggg", "hhh", "iii"} actual, err := ExtractMarkerStrings(page) testhelper.AssertNoErr(t, err) testhelper.CheckDeepEquals(t, expected, actual) }
func TestCreateHandleFlags(t *testing.T) { app := cli.NewApp() flagset := flag.NewFlagSet("flags", 1) flagset.String("metadata", "", "") flagset.String("container-read", "", "") flagset.String("container-write", "", "") flagset.Set("metadata", "key=val,foo=bar") c := cli.NewContext(app, flagset, nil) cmd := &commandCreate{ Ctx: &handler.Context{ CLIContext: c, }, } expected := &handler.Resource{ Params: ¶msCreate{ opts: containers.CreateOpts{ Metadata: map[string]string{ "key": "val", "foo": "bar", }, }, }, } actual := &handler.Resource{} err := cmd.HandleFlags(actual) th.AssertNoErr(t, err) th.AssertDeepEquals(t, expected.Params.(*paramsCreate).opts, actual.Params.(*paramsCreate).opts) }
func TestCreateExecute(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() th.Mux.HandleFunc("/container1", func(w http.ResponseWriter, r *http.Request) { w.Header().Add("X-Container-Meta-Foo", "bar") w.Header().Add("X-Container-Meta-Key", "val") w.Header().Add("X-Trans-Id", "1234567") w.WriteHeader(http.StatusNoContent) }) cmd := &commandCreate{ Ctx: &handler.Context{ ServiceClient: client.ServiceClient(), }, } actual := &handler.Resource{ Params: ¶msCreate{ opts: containers.CreateOpts{ Metadata: map[string]string{ "key": "val", "foo": "bar", }, }, container: "container1", }, } cmd.Execute(actual) th.AssertNoErr(t, actual.Err) }
func TestGetHandleSingle(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("name", "", "") flagset.Set("name", "server1Name") c := cli.NewContext(app, flagset, nil) cmd := &commandGet{ Ctx: &handler.Context{ CLIContext: c, ServiceClient: client.ServiceClient(), }, } expected := &handler.Resource{ Params: ¶msGet{ server: "server1", }, } actual := &handler.Resource{ Params: ¶msGet{}, } err := cmd.HandleSingle(actual) th.AssertNoErr(t, err) th.AssertEquals(t, expected.Params.(*paramsGet).server, actual.Params.(*paramsGet).server) }
func TestListHandleSingle(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() setupList(t) fs := flag.NewFlagSet("flags", 1) fs.String("container", "", "") fs.Set("container", "testContainer") cmd := newListCmd(fs) cmd.Ctx.ServiceClient = client.ServiceClient() expected := &handler.Resource{ Params: ¶msList{ container: "testContainer", }, } actual := &handler.Resource{ Params: ¶msList{}, } err := cmd.HandleSingle(actual) th.AssertNoErr(t, err) th.AssertEquals(t, expected.Params.(*paramsList).container, actual.Params.(*paramsList).container) }
func TestDeleteHandleSingle(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() th.Mux.HandleFunc("/foo/bar", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(204) }) fs := flag.NewFlagSet("flags", 1) fs.String("container", "", "") fs.String("name", "", "") fs.Set("container", "foo") fs.Set("name", "bar") cmd := newDelCmd(fs) cmd.Ctx.ServiceClient = client.ServiceClient() expected := &handler.Resource{ Params: ¶msDelete{ object: "bar", }, } actual := &handler.Resource{ Params: ¶msDelete{}, } err := cmd.HandleSingle(actual) th.AssertNoErr(t, err) th.AssertEquals(t, expected.Params.(*paramsDelete).object, actual.Params.(*paramsDelete).object) }
func TestUploadExecute(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() th.Mux.HandleFunc("/foo/bar", func(w http.ResponseWriter, r *http.Request) { th.TestMethod(t, r, "PUT") w.Header().Add("Content-Type", "text/plain") hash := md5.New() io.WriteString(hash, "hodor") localChecksum := hash.Sum(nil) w.Header().Set("ETag", fmt.Sprintf("%x", localChecksum)) w.WriteHeader(201) fmt.Fprintf(w, `hodor`) }) fs := flag.NewFlagSet("flags", 1) cmd := newUpCmd(fs) cmd.Ctx.ServiceClient = client.ServiceClient() res := &handler.Resource{ Params: ¶msUpload{ container: "foo", object: "bar", stream: strings.NewReader("hodor"), opts: osObjects.CreateOpts{}, }, } cmd.Execute(res) th.AssertNoErr(t, res.Err) th.AssertEquals(t, "Successfully uploaded object [bar] to container [foo]\n", res.Result) }
func TestRebuildOpts(t *testing.T) { base := servers.RebuildOpts{ Name: "rebuiltserver", AdminPass: "******", ImageID: "asdfasdfasdf", } ext := RebuildOptsExt{ RebuildOptsBuilder: base, DiskConfig: Auto, } actual, err := ext.ToServerRebuildMap() th.AssertNoErr(t, err) expected := ` { "rebuild": { "name": "rebuiltserver", "imageRef": "asdfasdfasdf", "adminPass": "******", "OS-DCF:diskConfig": "AUTO" } } ` th.CheckJSONEquals(t, expected, actual) }
func TestAuthenticatedClientV2(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() th.Mux.HandleFunc("/v2.0/tokens", func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, ` { "access": { "token": { "id": "01234567890", "expires": "2014-10-01T10:00:00.000000Z" }, "serviceCatalog": [] } } `) }) options := gophercloud.AuthOptions{ Username: "******", APIKey: "09876543210", IdentityEndpoint: th.Endpoint() + "v2.0/", } client, err := AuthenticatedClient(options) th.AssertNoErr(t, err) th.CheckEquals(t, "01234567890", client.TokenID) }
func TestUploadHandleFlags(t *testing.T) { app := cli.NewApp() flagset := flag.NewFlagSet("flags", 1) flagset.String("name", "", "") flagset.String("file", "", "") flagset.String("public-key", "", "") flagset.Set("name", "keypair1Name") flagset.Set("public-key", "ssh public key data here") c := cli.NewContext(app, flagset, nil) cmd := &commandUpload{ Ctx: &handler.Context{ CLIContext: c, }, } expected := &handler.Resource{ Params: ¶msUpload{ opts: &osKeypairs.CreateOpts{ Name: "keypair1Name", PublicKey: "ssh public key data here", }, }, } actual := &handler.Resource{} err := cmd.HandleFlags(actual) th.AssertNoErr(t, err) th.AssertDeepEquals(t, *expected.Params.(*paramsUpload).opts, *actual.Params.(*paramsUpload).opts) }
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 TestListHandleFlags(t *testing.T) { app := cli.NewApp() flagset := flag.NewFlagSet("flags", 1) flagset.String("image", "", "") flagset.String("flavor", "", "") flagset.String("name", "", "") flagset.String("status", "", "") flagset.String("marker", "", "") flagset.Set("image", "13ba-75c0-4483-acf9") flagset.Set("flavor", "1234-b95f-ac5b-cd23") flagset.Set("name", "server*") flagset.Set("status", "AVAILABLE") flagset.Set("marker", "1fd3-4f9f-44df-1b5c") c := cli.NewContext(app, flagset, nil) cmd := &commandList{ Ctx: &handler.Context{ CLIContext: c, }, } expected := &handler.Resource{ Params: ¶msList{ opts: &osServers.ListOpts{ Image: "13ba-75c0-4483-acf9", Flavor: "1234-b95f-ac5b-cd23", Name: "server*", Status: "AVAILABLE", Marker: "1fd3-4f9f-44df-1b5c", }, }, } actual := &handler.Resource{} err := cmd.HandleFlags(actual) th.AssertNoErr(t, err) th.AssertDeepEquals(t, *expected.Params.(*paramsList).opts, *actual.Params.(*paramsList).opts) }
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 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 TestDeleteExecute(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() th.Mux.HandleFunc("/foo/bar", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(204) }) fs := flag.NewFlagSet("flags", 1) fs.String("container", "", "") fs.String("name", "", "") fs.Set("container", "foo") fs.Set("name", "bar") cmd := newDelCmd(fs) cmd.Ctx.ServiceClient = client.ServiceClient() res := &handler.Resource{ Params: ¶msDelete{container: "foo", object: "bar"}, } cmd.Execute(res) th.AssertNoErr(t, res.Err) }
func TestGenerateHandleSingle(t *testing.T) { app := cli.NewApp() flagset := flag.NewFlagSet("flags", 1) flagset.String("name", "", "") flagset.Set("name", "keypair1Name") c := cli.NewContext(app, flagset, nil) cmd := &commandGenerate{ Ctx: &handler.Context{ CLIContext: c, }, } expected := &handler.Resource{ Params: ¶msGenerate{ opts: &osKeypairs.CreateOpts{ Name: "keypair1Name", }, }, } actual := &handler.Resource{ Params: ¶msGenerate{ opts: &osKeypairs.CreateOpts{}, }, } err := cmd.HandleSingle(actual) th.AssertNoErr(t, err) th.AssertEquals(t, *expected.Params.(*paramsGenerate).opts, *actual.Params.(*paramsGenerate).opts) }
func TestUploadExecute(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() th.Mux.HandleFunc("/foo/bar", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(201) th.TestMethod(t, r, "PUT") w.Header().Add("Content-Type", "text/plain") fmt.Fprintf(w, `hodor`) }) fs := flag.NewFlagSet("flags", 1) fs.String("container", "", "") fs.String("name", "", "") fs.Set("container", "foo") fs.Set("name", "bar") cmd := newUpCmd(fs) cmd.Ctx.ServiceClient = client.ServiceClient() res := &handler.Resource{ Params: ¶msUpload{container: "foo", object: "bar"}, } cmd.Execute(res) th.AssertNoErr(t, res.Err) th.AssertEquals(t, "Successfully uploaded object [bar] to container [foo]\n", res.Result) }
func TestCreateOpts(t *testing.T) { opts := CreateOpts{ Name: "createdserver", ImageRef: "image-id", FlavorRef: "flavor-id", KeyPair: "mykey", DiskConfig: diskconfig.Manual, } expected := ` { "server": { "name": "createdserver", "imageRef": "image-id", "flavorRef": "flavor-id", "flavorName": "", "imageName": "", "key_name": "mykey", "OS-DCF:diskConfig": "MANUAL" } } ` actual, err := opts.ToServerCreateMap() th.AssertNoErr(t, err) th.CheckJSONEquals(t, expected, actual) }
func TestCreateOpts(t *testing.T) { base := servers.CreateOpts{ Name: "createdserver", ImageRef: "asdfasdfasdf", FlavorRef: "performance1-1", } ext := CreateOptsExt{ CreateOptsBuilder: base, DiskConfig: Manual, } expected := ` { "server": { "name": "createdserver", "imageRef": "asdfasdfasdf", "flavorRef": "performance1-1", "flavorName": "", "imageName": "", "OS-DCF:diskConfig": "MANUAL" } } ` actual, err := ext.ToServerCreateMap() th.AssertNoErr(t, err) th.CheckJSONEquals(t, expected, actual) }
func TestDeleteExecute(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() th.Mux.HandleFunc("/container1", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNoContent) }) app := cli.NewApp() flagset := flag.NewFlagSet("flags", 1) flagset.String("name", "", "") flagset.Set("name", "container1") c := cli.NewContext(app, flagset, nil) cmd := &commandDelete{ Ctx: &handler.Context{ ServiceClient: client.ServiceClient(), CLIContext: c, }, } actual := &handler.Resource{ Params: ¶msDelete{ container: "container1", }, } cmd.Execute(actual) th.AssertNoErr(t, actual.Err) }
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 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) }) iTrue := true options := CreateOpts{Name: "sample_network", AdminStateUp: &iTrue, Shared: &iTrue, TenantID: "12345"} _, err := Create(fake.ServiceClient(), options).Extract() th.AssertNoErr(t, err) }
func TestUpdate(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() th.Mux.HandleFunc("/v2.0/subnets/08eae331-0402-425a-923c-34f7cfe39c1b", func(w http.ResponseWriter, r *http.Request) { th.TestMethod(t, r, "PUT") 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, ` { "subnet": { "name": "my_new_subnet", "dns_nameservers": ["foo"], "host_routes": [{"destination":"","nexthop": "bar"}] } } `) w.Header().Add("Content-Type", "application/json") w.WriteHeader(http.StatusCreated) fmt.Fprintf(w, ` { "subnet": { "name": "my_new_subnet", "enable_dhcp": true, "network_id": "db193ab3-96e3-4cb3-8fc5-05f4296d0324", "tenant_id": "26a7980765d0414dbc1fc1f88cdb7e6e", "dns_nameservers": [], "allocation_pools": [ { "start": "10.0.0.2", "end": "10.0.0.254" } ], "host_routes": [], "ip_version": 4, "gateway_ip": "10.0.0.1", "cidr": "10.0.0.0/24", "id": "08eae331-0402-425a-923c-34f7cfe39c1b" } } `) }) opts := UpdateOpts{ Name: "my_new_subnet", DNSNameservers: []string{"foo"}, HostRoutes: []HostRoute{ HostRoute{NextHop: "bar"}, }, } s, err := Update(fake.ServiceClient(), "08eae331-0402-425a-923c-34f7cfe39c1b", opts).Extract() th.AssertNoErr(t, err) th.AssertEquals(t, s.Name, "my_new_subnet") th.AssertEquals(t, s.ID, "08eae331-0402-425a-923c-34f7cfe39c1b") }