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 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 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 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 TestListURL(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() c := client.ServiceClient() th.CheckEquals(t, c.Endpoint+"os-keypairs", listURL(c)) }
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 TestCreateURL(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() c := client.ServiceClient() th.CheckEquals(t, c.Endpoint+"os-volumes_boot", createURL(c)) }
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 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 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 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 TestWarningEmittedForNonDirs(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() fs := flag.NewFlagSet("flags", 1) _, filename, _, _ := runtime.Caller(0) fs.String("container", "", "") fs.String("dir", "", "") fs.Set("container", "foo") fs.Set("dir", filename) cmd := newUpDirCmd(fs) cmd.Ctx.ServiceClient = client.ServiceClient() res := &handler.Resource{} cmd.HandleFlags(res) cmd.Execute(res) fmt.Println(filename) err := fmt.Errorf("%s is not a directory, ignoring", filename) th.AssertDeepEquals(t, err, res.Err) }
func tokenPost(t *testing.T, options gophercloud.AuthOptions, requestJSON string) os.CreateResult { th.SetupHTTP() defer th.TeardownHTTP() os.HandleTokenPost(t, requestJSON) return Create(client.ServiceClient(), WrapOptions(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 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 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 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 TestDeleteContainers(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() os.HandleDeleteContainerSuccessfully(t) res := Delete(fake.ServiceClient(), "testContainer") th.CheckNoErr(t, res.Err) }
func TestDeleteServer(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() HandleServerDeletionSuccessfully(t) res := Delete(client.ServiceClient(), "asdfasdfasdf") th.AssertNoErr(t, res.Err) }
func TestRebootServer(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() HandleRebootSuccessfully(t) res := Reboot(client.ServiceClient(), "1234asdf", SoftReboot) th.AssertNoErr(t, res.Err) }
func TestChangeServerAdminPassword(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() HandleAdminPasswordChangeSuccessfully(t) res := ChangeAdminPassword(client.ServiceClient(), "1234asdf", "new-password") th.AssertNoErr(t, res.Err) }
func tokenPostErr(t *testing.T, options gophercloud.AuthOptions, expectedErr error) { th.SetupHTTP() defer th.TeardownHTTP() HandleTokenPost(t, "") actualErr := Create(client.ServiceClient(), AuthOptions{options}).Err th.CheckDeepEquals(t, expectedErr, actualErr) }
func TestDeleteObject(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() os.HandleDeleteObjectSuccessfully(t) res := Delete(fake.ServiceClient(), "testContainer", "testObject", nil) th.AssertNoErr(t, res.Err) }
func TestGetContainers(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() os.HandleGetContainerSuccessfully(t) _, err := Get(fake.ServiceClient(), "testContainer").ExtractMetadata() th.CheckNoErr(t, err) }
func TestCreateServerImage(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() HandleCreateServerImageSuccessfully(t) _, err := CreateImage(client.ServiceClient(), "serverimage", CreateImageOpts{Name: "test"}).ExtractImageID() th.AssertNoErr(t, err) }
func TestDelete(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() os.HandleDeleteSuccessfully(t) err := Delete(client.ServiceClient(), "deletedkey").ExtractErr() th.AssertNoErr(t, err) }
func TestListURL(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() c := client.ServiceClient() serverId := "4d8c3732-a248-40ed-bebc-539a6ffd25c0" th.CheckEquals(t, c.Endpoint+"servers/"+serverId+"/os-volume_attachments", listURL(c, serverId)) }
func TestUpdateContainers(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() os.HandleUpdateContainerSuccessfully(t) options := &os.UpdateOpts{Metadata: map[string]string{"foo": "bar"}} res := Update(fake.ServiceClient(), "testContainer", options) th.CheckNoErr(t, res.Err) }
func TestDeleteURL(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() c := client.ServiceClient() serverId := "4d8c3732-a248-40ed-bebc-539a6ffd25c0" aId := "a26887c6-c47b-4654-abb5-dfadf7d3f804" th.CheckEquals(t, c.Endpoint+"servers/"+serverId+"/os-volume_attachments/"+aId, deleteURL(c, serverId, aId)) }