From 3cb6ff612f0ea432b84b249bcf353e8949cb4b07 Mon Sep 17 00:00:00 2001 From: Zhenguo Niu Date: Tue, 30 Jul 2019 16:36:02 +0800 Subject: [PATCH 1/3] Add wwn to volume v3 --- openstack/evs/v3/requests.go | 12 ++++++++++++ openstack/evs/v3/results.go | 34 ++++++++++++++++++++++++++++++++++ openstack/evs/v3/urls.go | 7 +++++++ 3 files changed, 53 insertions(+) create mode 100644 openstack/evs/v3/requests.go create mode 100644 openstack/evs/v3/results.go create mode 100644 openstack/evs/v3/urls.go diff --git a/openstack/evs/v3/requests.go b/openstack/evs/v3/requests.go new file mode 100644 index 000000000..11b39ee9b --- /dev/null +++ b/openstack/evs/v3/requests.go @@ -0,0 +1,12 @@ +package volumes + +import ( + "github.com/huaweicloud/golangsdk" +) + +// Get retrieves the Volume with the provided ID. To extract the Volume object +// from the response, call the Extract method on the GetResult. +func Get(client *golangsdk.ServiceClient, id string) (r GetResult) { + _, r.Err = client.Get(getURL(client, id), &r.Body, nil) + return +} diff --git a/openstack/evs/v3/results.go b/openstack/evs/v3/results.go new file mode 100644 index 000000000..58c13d2db --- /dev/null +++ b/openstack/evs/v3/results.go @@ -0,0 +1,34 @@ +package volumes + +import ( + "github.com/huaweicloud/golangsdk" +) + +// Volume contains all the information associated with a Volume. +type Volume struct { + // Unique identifier for the volume. + ID string `json:"id"` + // wwn of the volume. + WWN string `json:"wwn"` +} + +type commonResult struct { + golangsdk.Result +} + +// Extract will get the Volume object out of the commonResult object. +func (r commonResult) Extract() (*Volume, error) { + var s Volume + err := r.ExtractInto(&s) + return &s, err +} + +// ExtractInto converts our response data into a volume struct +func (r commonResult) ExtractInto(v interface{}) error { + return r.Result.ExtractIntoStructPtr(v, "volume") +} + +// GetResult contains the response body and error from a Get request. +type GetResult struct { + commonResult +} diff --git a/openstack/evs/v3/urls.go b/openstack/evs/v3/urls.go new file mode 100644 index 000000000..a12728728 --- /dev/null +++ b/openstack/evs/v3/urls.go @@ -0,0 +1,7 @@ +package volumes + +import "github.com/huaweicloud/golangsdk" + +func getURL(c *golangsdk.ServiceClient, id string) string { + return c.ServiceURL("os-vendor-volumes", id) +} From e1d00e6d3695e5e753a21bc9c62361b73e9bb444 Mon Sep 17 00:00:00 2001 From: Zhenguo Niu Date: Tue, 30 Jul 2019 19:21:59 +0800 Subject: [PATCH 2/3] Remove unneeded testing --- .../objectstorage/v1/accounts/testing/doc.go | 2 - .../v1/accounts/testing/fixtures.go | 57 ---- .../v1/accounts/testing/requests_test.go | 82 ----- .../v1/containers/testing/doc.go | 2 - .../v1/containers/testing/fixtures.go | 155 --------- .../v1/containers/testing/requests_test.go | 148 --------- .../objectstorage/v1/objects/testing/doc.go | 2 - .../v1/objects/testing/fixtures.go | 250 -------------- .../v1/objects/testing/requests_test.go | 312 ------------------ .../objectstorage/v1/swauth/testing/doc.go | 2 - .../v1/swauth/testing/fixtures.go | 29 -- .../v1/swauth/testing/requests_test.go | 35 -- 12 files changed, 1076 deletions(-) delete mode 100644 openstack/objectstorage/v1/accounts/testing/doc.go delete mode 100644 openstack/objectstorage/v1/accounts/testing/fixtures.go delete mode 100755 openstack/objectstorage/v1/accounts/testing/requests_test.go delete mode 100644 openstack/objectstorage/v1/containers/testing/doc.go delete mode 100644 openstack/objectstorage/v1/containers/testing/fixtures.go delete mode 100644 openstack/objectstorage/v1/containers/testing/requests_test.go delete mode 100644 openstack/objectstorage/v1/objects/testing/doc.go delete mode 100644 openstack/objectstorage/v1/objects/testing/fixtures.go delete mode 100644 openstack/objectstorage/v1/objects/testing/requests_test.go delete mode 100644 openstack/objectstorage/v1/swauth/testing/doc.go delete mode 100644 openstack/objectstorage/v1/swauth/testing/fixtures.go delete mode 100755 openstack/objectstorage/v1/swauth/testing/requests_test.go diff --git a/openstack/objectstorage/v1/accounts/testing/doc.go b/openstack/objectstorage/v1/accounts/testing/doc.go deleted file mode 100644 index d6ad0afdd..000000000 --- a/openstack/objectstorage/v1/accounts/testing/doc.go +++ /dev/null @@ -1,2 +0,0 @@ -// accounts unit tests -package testing diff --git a/openstack/objectstorage/v1/accounts/testing/fixtures.go b/openstack/objectstorage/v1/accounts/testing/fixtures.go deleted file mode 100644 index 5aee403f8..000000000 --- a/openstack/objectstorage/v1/accounts/testing/fixtures.go +++ /dev/null @@ -1,57 +0,0 @@ -package testing - -import ( - "net/http" - "testing" - - th "github.com/huaweicloud/golangsdk/testhelper" - fake "github.com/huaweicloud/golangsdk/testhelper/client" -) - -// HandleGetAccountSuccessfully creates an HTTP handler at `/` on the test handler mux that -// responds with a `Get` response. -func HandleGetAccountSuccessfully(t *testing.T) { - th.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - th.TestMethod(t, r, "HEAD") - th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) - - w.Header().Set("X-Account-Container-Count", "2") - w.Header().Set("X-Account-Object-Count", "5") - w.Header().Set("X-Account-Meta-Quota-Bytes", "42") - w.Header().Set("X-Account-Bytes-Used", "14") - w.Header().Set("X-Account-Meta-Subject", "books") - w.Header().Set("Date", "Fri, 17 Jan 2014 16:09:56 GMT") - - w.WriteHeader(http.StatusNoContent) - }) -} - -// HandleGetAccountNoQuotaSuccessfully creates an HTTP handler at `/` on the -// test handler mux that responds with a `Get` response. -func HandleGetAccountNoQuotaSuccessfully(t *testing.T) { - th.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - th.TestMethod(t, r, "HEAD") - th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) - - w.Header().Set("X-Account-Container-Count", "2") - w.Header().Set("X-Account-Object-Count", "5") - w.Header().Set("X-Account-Bytes-Used", "14") - w.Header().Set("X-Account-Meta-Subject", "books") - w.Header().Set("Date", "Fri, 17 Jan 2014 16:09:56 GMT") - - w.WriteHeader(http.StatusNoContent) - }) -} - -// HandleUpdateAccountSuccessfully creates an HTTP handler at `/` on the test handler mux that -// responds with a `Update` response. -func HandleUpdateAccountSuccessfully(t *testing.T) { - th.Mux.HandleFunc("/", 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, "X-Account-Meta-Gophercloud-Test", "accounts") - - w.Header().Set("Date", "Fri, 17 Jan 2014 16:09:56 GMT") - w.WriteHeader(http.StatusNoContent) - }) -} diff --git a/openstack/objectstorage/v1/accounts/testing/requests_test.go b/openstack/objectstorage/v1/accounts/testing/requests_test.go deleted file mode 100755 index 0c34a3a2e..000000000 --- a/openstack/objectstorage/v1/accounts/testing/requests_test.go +++ /dev/null @@ -1,82 +0,0 @@ -package testing - -import ( - "testing" - "time" - - "github.com/huaweicloud/golangsdk/openstack/objectstorage/v1/accounts" - th "github.com/huaweicloud/golangsdk/testhelper" - fake "github.com/huaweicloud/golangsdk/testhelper/client" -) - -var ( - loc, _ = time.LoadLocation("GMT") -) - -//func TestUpdateAccount(t *testing.T) { -// th.SetupHTTP() -// defer th.TeardownHTTP() -// HandleUpdateAccountSuccessfully(t) - -// options := &accounts.UpdateOpts{Metadata: map[string]string{"golangsdk-test": "accounts"}} -// res := accounts.Update(fake.ServiceClient(), options) -// th.AssertNoErr(t, res.Err) - -// expected := &accounts.UpdateHeader{ -// Date: time.Date(2014, time.January, 17, 16, 9, 56, 0, loc), // Fri, 17 Jan 2014 16:09:56 GMT -// } -// actual, err := res.Extract() -// th.AssertNoErr(t, err) -// th.CheckDeepEquals(t, expected, actual) -//} - -func TestGetAccount(t *testing.T) { - th.SetupHTTP() - defer th.TeardownHTTP() - HandleGetAccountSuccessfully(t) - - expectedMetadata := map[string]string{"Subject": "books", "Quota-Bytes": "42"} - res := accounts.Get(fake.ServiceClient(), &accounts.GetOpts{}) - th.AssertNoErr(t, res.Err) - actualMetadata, _ := res.ExtractMetadata() - th.CheckDeepEquals(t, expectedMetadata, actualMetadata) - _, err := res.Extract() - th.AssertNoErr(t, err) - - var quotaBytes int64 = 42 - expected := &accounts.GetHeader{ - QuotaBytes: "aBytes, - ContainerCount: 2, - ObjectCount: 5, - BytesUsed: 14, - Date: time.Date(2014, time.January, 17, 16, 9, 56, 0, loc), // Fri, 17 Jan 2014 16:09:56 GMT - } - actual, err := res.Extract() - th.AssertNoErr(t, err) - th.CheckDeepEquals(t, expected, actual) -} - -func TestGetAccountNoQuota(t *testing.T) { - th.SetupHTTP() - defer th.TeardownHTTP() - HandleGetAccountNoQuotaSuccessfully(t) - - expectedMetadata := map[string]string{"Subject": "books"} - res := accounts.Get(fake.ServiceClient(), &accounts.GetOpts{}) - th.AssertNoErr(t, res.Err) - actualMetadata, _ := res.ExtractMetadata() - th.CheckDeepEquals(t, expectedMetadata, actualMetadata) - _, err := res.Extract() - th.AssertNoErr(t, err) - - expected := &accounts.GetHeader{ - QuotaBytes: nil, - ContainerCount: 2, - ObjectCount: 5, - BytesUsed: 14, - Date: time.Date(2014, time.January, 17, 16, 9, 56, 0, loc), // Fri, 17 Jan 2014 16:09:56 GMT - } - actual, err := res.Extract() - th.AssertNoErr(t, err) - th.CheckDeepEquals(t, expected, actual) -} diff --git a/openstack/objectstorage/v1/containers/testing/doc.go b/openstack/objectstorage/v1/containers/testing/doc.go deleted file mode 100644 index a39f42b41..000000000 --- a/openstack/objectstorage/v1/containers/testing/doc.go +++ /dev/null @@ -1,2 +0,0 @@ -// containers unit tests -package testing diff --git a/openstack/objectstorage/v1/containers/testing/fixtures.go b/openstack/objectstorage/v1/containers/testing/fixtures.go deleted file mode 100644 index 01dff1e08..000000000 --- a/openstack/objectstorage/v1/containers/testing/fixtures.go +++ /dev/null @@ -1,155 +0,0 @@ -package testing - -import ( - "fmt" - "net/http" - "testing" - - "github.com/huaweicloud/golangsdk/openstack/objectstorage/v1/containers" - th "github.com/huaweicloud/golangsdk/testhelper" - fake "github.com/huaweicloud/golangsdk/testhelper/client" -) - -// ExpectedListInfo is the result expected from a call to `List` when full -// info is requested. -var ExpectedListInfo = []containers.Container{ - { - Count: 0, - Bytes: 0, - Name: "janeausten", - }, - { - Count: 1, - Bytes: 14, - Name: "marktwain", - }, -} - -// ExpectedListNames is the result expected from a call to `List` when just -// container names are requested. -var ExpectedListNames = []string{"janeausten", "marktwain"} - -// HandleListContainerInfoSuccessfully creates an HTTP handler at `/` on the test handler mux that -// responds with a `List` response when full info is requested. -func HandleListContainerInfoSuccessfully(t *testing.T) { - th.Mux.HandleFunc("/", 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, `[ - { - "count": 0, - "bytes": 0, - "name": "janeausten" - }, - { - "count": 1, - "bytes": 14, - "name": "marktwain" - } - ]`) - case "janeausten": - fmt.Fprintf(w, `[ - { - "count": 1, - "bytes": 14, - "name": "marktwain" - } - ]`) - case "marktwain": - fmt.Fprintf(w, `[]`) - default: - t.Fatalf("Unexpected marker: [%s]", marker) - } - }) -} - -// HandleListContainerNamesSuccessfully creates an HTTP handler at `/` on the test handler mux that -// responds with a `ListNames` response when only container names are requested. -func HandleListContainerNamesSuccessfully(t *testing.T) { - th.Mux.HandleFunc("/", 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", "text/plain") - - w.Header().Set("Content-Type", "text/plain") - r.ParseForm() - marker := r.Form.Get("marker") - switch marker { - case "": - fmt.Fprintf(w, "janeausten\nmarktwain\n") - case "janeausten": - fmt.Fprintf(w, "marktwain\n") - case "marktwain": - fmt.Fprintf(w, ``) - default: - t.Fatalf("Unexpected marker: [%s]", marker) - } - }) -} - -// HandleCreateContainerSuccessfully creates an HTTP handler at `/testContainer` on the test handler mux that -// responds with a `Create` response. -func HandleCreateContainerSuccessfully(t *testing.T) { - th.Mux.HandleFunc("/testContainer", 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, "Accept", "application/json") - - w.Header().Add("X-Container-Meta-Foo", "bar") - w.Header().Set("Content-Length", "0") - w.Header().Set("Content-Type", "text/html; charset=UTF-8") - w.Header().Set("Date", "Wed, 17 Aug 2016 19:25:43 GMT") - w.Header().Set("X-Trans-Id", "tx554ed59667a64c61866f1-0058b4ba37") - w.WriteHeader(http.StatusNoContent) - }) -} - -// HandleDeleteContainerSuccessfully creates an HTTP handler at `/testContainer` on the test handler mux that -// responds with a `Delete` response. -func HandleDeleteContainerSuccessfully(t *testing.T) { - th.Mux.HandleFunc("/testContainer", func(w http.ResponseWriter, r *http.Request) { - th.TestMethod(t, r, "DELETE") - th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) - th.TestHeader(t, r, "Accept", "application/json") - w.WriteHeader(http.StatusNoContent) - }) -} - -// HandleUpdateContainerSuccessfully creates an HTTP handler at `/testContainer` on the test handler mux that -// responds with a `Update` response. -func HandleUpdateContainerSuccessfully(t *testing.T) { - th.Mux.HandleFunc("/testContainer", 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") - w.WriteHeader(http.StatusNoContent) - }) -} - -// HandleGetContainerSuccessfully creates an HTTP handler at `/testContainer` on the test handler mux that -// responds with a `Get` response. -func HandleGetContainerSuccessfully(t *testing.T) { - th.Mux.HandleFunc("/testContainer", func(w http.ResponseWriter, r *http.Request) { - th.TestMethod(t, r, "HEAD") - th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) - th.TestHeader(t, r, "Accept", "application/json") - w.Header().Set("Accept-Ranges", "bytes") - w.Header().Set("Content-Type", "application/json; charset=utf-8") - w.Header().Set("Date", "Wed, 17 Aug 2016 19:25:43 GMT") - w.Header().Set("X-Container-Bytes-Used", "100") - w.Header().Set("X-Container-Object-Count", "4") - w.Header().Set("X-Container-Read", "test") - w.Header().Set("X-Container-Write", "test2,user4") - w.Header().Set("X-Timestamp", "1471298837.95721") - w.Header().Set("X-Trans-Id", "tx554ed59667a64c61866f1-0057b4ba37") - w.Header().Set("X-Storage-Policy", "test_policy") - w.WriteHeader(http.StatusNoContent) - }) -} diff --git a/openstack/objectstorage/v1/containers/testing/requests_test.go b/openstack/objectstorage/v1/containers/testing/requests_test.go deleted file mode 100644 index 292fe371a..000000000 --- a/openstack/objectstorage/v1/containers/testing/requests_test.go +++ /dev/null @@ -1,148 +0,0 @@ -package testing - -import ( - "testing" - "time" - - "github.com/huaweicloud/golangsdk/openstack/objectstorage/v1/containers" - "github.com/huaweicloud/golangsdk/pagination" - th "github.com/huaweicloud/golangsdk/testhelper" - fake "github.com/huaweicloud/golangsdk/testhelper/client" -) - -var ( - metadata = map[string]string{"golangsdk-test": "containers"} - loc, _ = time.LoadLocation("GMT") -) - -func TestListContainerInfo(t *testing.T) { - th.SetupHTTP() - defer th.TeardownHTTP() - HandleListContainerInfoSuccessfully(t) - - count := 0 - err := containers.List(fake.ServiceClient(), &containers.ListOpts{Full: true}).EachPage(func(page pagination.Page) (bool, error) { - count++ - actual, err := containers.ExtractInfo(page) - th.AssertNoErr(t, err) - - th.CheckDeepEquals(t, ExpectedListInfo, actual) - - return true, nil - }) - th.AssertNoErr(t, err) - th.CheckEquals(t, count, 1) -} - -func TestListAllContainerInfo(t *testing.T) { - th.SetupHTTP() - defer th.TeardownHTTP() - HandleListContainerInfoSuccessfully(t) - - allPages, err := containers.List(fake.ServiceClient(), &containers.ListOpts{Full: true}).AllPages() - th.AssertNoErr(t, err) - actual, err := containers.ExtractInfo(allPages) - th.AssertNoErr(t, err) - th.CheckDeepEquals(t, ExpectedListInfo, actual) -} - -func TestListContainerNames(t *testing.T) { - th.SetupHTTP() - defer th.TeardownHTTP() - HandleListContainerNamesSuccessfully(t) - - count := 0 - err := containers.List(fake.ServiceClient(), &containers.ListOpts{Full: false}).EachPage(func(page pagination.Page) (bool, error) { - count++ - actual, err := containers.ExtractNames(page) - if err != nil { - t.Errorf("Failed to extract container names: %v", err) - return false, err - } - - th.CheckDeepEquals(t, ExpectedListNames, actual) - - return true, nil - }) - th.AssertNoErr(t, err) - th.CheckEquals(t, count, 1) -} - -func TestListAllContainerNames(t *testing.T) { - th.SetupHTTP() - defer th.TeardownHTTP() - HandleListContainerNamesSuccessfully(t) - - allPages, err := containers.List(fake.ServiceClient(), &containers.ListOpts{Full: false}).AllPages() - th.AssertNoErr(t, err) - actual, err := containers.ExtractNames(allPages) - th.AssertNoErr(t, err) - th.CheckDeepEquals(t, ExpectedListNames, actual) -} - -func TestCreateContainer(t *testing.T) { - th.SetupHTTP() - defer th.TeardownHTTP() - HandleCreateContainerSuccessfully(t) - - options := containers.CreateOpts{ContentType: "application/json", Metadata: map[string]string{"foo": "bar"}} - res := containers.Create(fake.ServiceClient(), "testContainer", options) - th.CheckEquals(t, "bar", res.Header["X-Container-Meta-Foo"][0]) - - expected := &containers.CreateHeader{ - ContentLength: 0, - ContentType: "text/html; charset=UTF-8", - Date: time.Date(2016, time.August, 17, 19, 25, 43, 0, loc), //Wed, 17 Aug 2016 19:25:43 GMT - TransID: "tx554ed59667a64c61866f1-0058b4ba37", - } - actual, err := res.Extract() - th.CheckNoErr(t, err) - th.AssertDeepEquals(t, expected, actual) -} - -func TestDeleteContainer(t *testing.T) { - th.SetupHTTP() - defer th.TeardownHTTP() - HandleDeleteContainerSuccessfully(t) - - res := containers.Delete(fake.ServiceClient(), "testContainer") - th.CheckNoErr(t, res.Err) -} - -func TestUpdateContainer(t *testing.T) { - th.SetupHTTP() - defer th.TeardownHTTP() - HandleUpdateContainerSuccessfully(t) - - options := &containers.UpdateOpts{Metadata: map[string]string{"foo": "bar"}} - res := containers.Update(fake.ServiceClient(), "testContainer", options) - th.CheckNoErr(t, res.Err) -} - -func TestGetContainer(t *testing.T) { - th.SetupHTTP() - defer th.TeardownHTTP() - HandleGetContainerSuccessfully(t) - - getOpts := containers.GetOpts{ - Newest: true, - } - res := containers.Get(fake.ServiceClient(), "testContainer", getOpts) - _, err := res.ExtractMetadata() - th.CheckNoErr(t, err) - - expected := &containers.GetHeader{ - AcceptRanges: "bytes", - BytesUsed: 100, - ContentType: "application/json; charset=utf-8", - Date: time.Date(2016, time.August, 17, 19, 25, 43, 0, loc), //Wed, 17 Aug 2016 19:25:43 GMT - ObjectCount: 4, - Read: []string{"test"}, - TransID: "tx554ed59667a64c61866f1-0057b4ba37", - Write: []string{"test2", "user4"}, - StoragePolicy: "test_policy", - } - actual, err := res.Extract() - th.CheckNoErr(t, err) - th.AssertDeepEquals(t, expected, actual) -} diff --git a/openstack/objectstorage/v1/objects/testing/doc.go b/openstack/objectstorage/v1/objects/testing/doc.go deleted file mode 100644 index 9ca1d8a11..000000000 --- a/openstack/objectstorage/v1/objects/testing/doc.go +++ /dev/null @@ -1,2 +0,0 @@ -// objects unit tests -package testing diff --git a/openstack/objectstorage/v1/objects/testing/fixtures.go b/openstack/objectstorage/v1/objects/testing/fixtures.go deleted file mode 100644 index c7867f4fa..000000000 --- a/openstack/objectstorage/v1/objects/testing/fixtures.go +++ /dev/null @@ -1,250 +0,0 @@ -package testing - -import ( - "crypto/md5" - "fmt" - "io" - "net/http" - "testing" - "time" - - "github.com/huaweicloud/golangsdk/openstack/objectstorage/v1/objects" - th "github.com/huaweicloud/golangsdk/testhelper" - fake "github.com/huaweicloud/golangsdk/testhelper/client" -) - -// HandleDownloadObjectSuccessfully creates an HTTP handler at `/testContainer/testObject` on the test handler mux that -// responds with a `Download` response. -func HandleDownloadObjectSuccessfully(t *testing.T) { - th.Mux.HandleFunc("/testContainer/testObject", 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("Date", "Wed, 10 Nov 2009 23:00:00 GMT") - w.Header().Set("X-Static-Large-Object", "True") - w.WriteHeader(http.StatusOK) - fmt.Fprintf(w, "Successful download with Gophercloud") - }) -} - -// ExpectedListInfo is the result expected from a call to `List` when full -// info is requested. -var ExpectedListInfo = []objects.Object{ - { - Hash: "451e372e48e0f6b1114fa0724aa79fa1", - LastModified: time.Date(2016, time.August, 17, 22, 11, 58, 602650000, time.UTC), //"2016-08-17T22:11:58.602650" - Bytes: 14, - Name: "goodbye", - ContentType: "application/octet-stream", - }, - { - Hash: "451e372e48e0f6b1114fa0724aa79fa1", - LastModified: time.Date(2016, time.August, 17, 22, 11, 58, 602650000, time.UTC), - Bytes: 14, - Name: "hello", - ContentType: "application/octet-stream", - }, -} - -// ExpectedListSubdir is the result expected from a call to `List` when full -// info is requested. -var ExpectedListSubdir = []objects.Object{ - { - Subdir: "directory/", - }, -} - -// ExpectedListNames is the result expected from a call to `List` when just -// object names are requested. -var ExpectedListNames = []string{"hello", "goodbye"} - -// 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": "2016-08-17T22:11:58.602650", - "bytes": 14, - "name": "goodbye", - "content_type": "application/octet-stream" - }, - { - "hash": "451e372e48e0f6b1114fa0724aa79fa1", - "last_modified": "2016-08-17T22:11:58.602650", - "bytes": 14, - "name": "hello", - "content_type": "application/octet-stream" - } - ]`) - case "hello": - fmt.Fprintf(w, `[]`) - default: - t.Fatalf("Unexpected marker: [%s]", marker) - } - }) -} - -// HandleListSubdirSuccessfully creates an HTTP handler at `/testContainer` on the test handler mux that -// responds with a `List` response when full info is requested. -func HandleListSubdirSuccessfully(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, `[ - { - "subdir": "directory/" - } - ]`) - case "directory/": - fmt.Fprintf(w, `[]`) - default: - t.Fatalf("Unexpected marker: [%s]", marker) - } - }) -} - -// HandleListObjectNamesSuccessfully creates an HTTP handler at `/testContainer` on the test handler mux that -// responds with a `List` response when only object names are requested. -func HandleListObjectNamesSuccessfully(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", "text/plain") - - w.Header().Set("Content-Type", "text/plain") - r.ParseForm() - marker := r.Form.Get("marker") - switch marker { - case "": - fmt.Fprintf(w, "hello\ngoodbye\n") - case "goodbye": - fmt.Fprintf(w, "") - default: - t.Fatalf("Unexpected marker: [%s]", marker) - } - }) -} - -// HandleCreateTextObjectSuccessfully creates an HTTP handler at `/testContainer/testObject` on the test handler mux -// that responds with a `Create` response. A Content-Type of "text/plain" is expected. -func HandleCreateTextObjectSuccessfully(t *testing.T, content string) { - th.Mux.HandleFunc("/testContainer/testObject", 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", "text/plain") - th.TestHeader(t, r, "Accept", "application/json") - - hash := md5.New() - io.WriteString(hash, content) - localChecksum := hash.Sum(nil) - - w.Header().Set("ETag", fmt.Sprintf("%x", localChecksum)) - w.WriteHeader(http.StatusCreated) - }) -} - -// HandleCreateTextWithCacheControlSuccessfully creates an HTTP handler at `/testContainer/testObject` on the test handler -// mux that responds with a `Create` response. A Cache-Control of `max-age="3600", public` is expected. -func HandleCreateTextWithCacheControlSuccessfully(t *testing.T, content string) { - th.Mux.HandleFunc("/testContainer/testObject", 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, "Cache-Control", `max-age="3600", public`) - th.TestHeader(t, r, "Accept", "application/json") - - hash := md5.New() - io.WriteString(hash, content) - localChecksum := hash.Sum(nil) - - w.Header().Set("ETag", fmt.Sprintf("%x", localChecksum)) - w.WriteHeader(http.StatusCreated) - }) -} - -// HandleCreateTypelessObjectSuccessfully creates an HTTP handler at `/testContainer/testObject` on the test handler -// mux that responds with a `Create` response. No Content-Type header may be present in the request, so that server- -// side content-type detection will be triggered properly. -func HandleCreateTypelessObjectSuccessfully(t *testing.T, content string) { - th.Mux.HandleFunc("/testContainer/testObject", 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, "Accept", "application/json") - - if contentType, present := r.Header["Content-Type"]; present { - t.Errorf("Expected Content-Type header to be omitted, but was %#v", contentType) - } - - hash := md5.New() - io.WriteString(hash, content) - localChecksum := hash.Sum(nil) - - w.Header().Set("ETag", fmt.Sprintf("%x", localChecksum)) - w.WriteHeader(http.StatusCreated) - }) -} - -// HandleCopyObjectSuccessfully creates an HTTP handler at `/testContainer/testObject` on the test handler mux that -// responds with a `Copy` response. -func HandleCopyObjectSuccessfully(t *testing.T) { - th.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) { - th.TestMethod(t, r, "COPY") - th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) - th.TestHeader(t, r, "Accept", "application/json") - th.TestHeader(t, r, "Destination", "/newTestContainer/newTestObject") - w.WriteHeader(http.StatusCreated) - }) -} - -// HandleDeleteObjectSuccessfully creates an HTTP handler at `/testContainer/testObject` on the test handler mux that -// responds with a `Delete` response. -func HandleDeleteObjectSuccessfully(t *testing.T) { - th.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) { - th.TestMethod(t, r, "DELETE") - th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) - th.TestHeader(t, r, "Accept", "application/json") - w.WriteHeader(http.StatusNoContent) - }) -} - -// HandleUpdateObjectSuccessfully creates an HTTP handler at `/testContainer/testObject` on the test handler mux that -// responds with a `Update` response. -func HandleUpdateObjectSuccessfully(t *testing.T) { - th.Mux.HandleFunc("/testContainer/testObject", 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.TestHeader(t, r, "X-Object-Meta-Gophercloud-Test", "objects") - w.WriteHeader(http.StatusAccepted) - }) -} - -// HandleGetObjectSuccessfully creates an HTTP handler at `/testContainer/testObject` on the test handler mux that -// responds with a `Get` response. -func HandleGetObjectSuccessfully(t *testing.T) { - th.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) { - th.TestMethod(t, r, "HEAD") - th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) - th.TestHeader(t, r, "Accept", "application/json") - w.Header().Add("X-Object-Meta-Gophercloud-Test", "objects") - w.Header().Add("X-Static-Large-Object", "true") - w.WriteHeader(http.StatusNoContent) - }) -} diff --git a/openstack/objectstorage/v1/objects/testing/requests_test.go b/openstack/objectstorage/v1/objects/testing/requests_test.go deleted file mode 100644 index 4242e2cf3..000000000 --- a/openstack/objectstorage/v1/objects/testing/requests_test.go +++ /dev/null @@ -1,312 +0,0 @@ -package testing - -import ( - "bytes" - "crypto/md5" - "fmt" - "io" - "io/ioutil" - "strings" - "testing" - "time" - - "github.com/huaweicloud/golangsdk/openstack/objectstorage/v1/objects" - "github.com/huaweicloud/golangsdk/pagination" - th "github.com/huaweicloud/golangsdk/testhelper" - fake "github.com/huaweicloud/golangsdk/testhelper/client" -) - -var ( - loc, _ = time.LoadLocation("GMT") -) - -func TestDownloadReader(t *testing.T) { - th.SetupHTTP() - defer th.TeardownHTTP() - HandleDownloadObjectSuccessfully(t) - - response := objects.Download(fake.ServiceClient(), "testContainer", "testObject", nil) - defer response.Body.Close() - - // Check reader - buf := bytes.NewBuffer(make([]byte, 0)) - io.CopyN(buf, response.Body, 10) - th.CheckEquals(t, "Successful", string(buf.Bytes())) -} - -func TestDownloadExtraction(t *testing.T) { - th.SetupHTTP() - defer th.TeardownHTTP() - HandleDownloadObjectSuccessfully(t) - - response := objects.Download(fake.ServiceClient(), "testContainer", "testObject", nil) - - // Check []byte extraction - bytes, err := response.ExtractContent() - th.AssertNoErr(t, err) - th.CheckEquals(t, "Successful download with Gophercloud", string(bytes)) - - expected := &objects.DownloadHeader{ - ContentLength: 36, - ContentType: "text/plain; charset=utf-8", - Date: time.Date(2009, time.November, 10, 23, 0, 0, 0, loc), - StaticLargeObject: true, - } - actual, err := response.Extract() - th.AssertNoErr(t, err) - th.CheckDeepEquals(t, expected, actual) -} - -func TestListObjectInfo(t *testing.T) { - th.SetupHTTP() - defer th.TeardownHTTP() - HandleListObjectsInfoSuccessfully(t) - - count := 0 - options := &objects.ListOpts{Full: true} - err := objects.List(fake.ServiceClient(), "testContainer", options).EachPage(func(page pagination.Page) (bool, error) { - count++ - actual, err := objects.ExtractInfo(page) - th.AssertNoErr(t, err) - - th.CheckDeepEquals(t, ExpectedListInfo, actual) - - return true, nil - }) - th.AssertNoErr(t, err) - th.CheckEquals(t, count, 1) -} - -func TestListObjectSubdir(t *testing.T) { - th.SetupHTTP() - defer th.TeardownHTTP() - HandleListSubdirSuccessfully(t) - - count := 0 - options := &objects.ListOpts{Full: true, Prefix: "", Delimiter: "/"} - err := objects.List(fake.ServiceClient(), "testContainer", options).EachPage(func(page pagination.Page) (bool, error) { - count++ - actual, err := objects.ExtractInfo(page) - th.AssertNoErr(t, err) - - th.CheckDeepEquals(t, ExpectedListSubdir, actual) - - return true, nil - }) - th.AssertNoErr(t, err) - th.CheckEquals(t, count, 1) -} - -func TestListObjectNames(t *testing.T) { - th.SetupHTTP() - defer th.TeardownHTTP() - HandleListObjectNamesSuccessfully(t) - - // Check without delimiter. - count := 0 - options := &objects.ListOpts{Full: false} - err := objects.List(fake.ServiceClient(), "testContainer", options).EachPage(func(page pagination.Page) (bool, error) { - count++ - actual, err := objects.ExtractNames(page) - if err != nil { - t.Errorf("Failed to extract container names: %v", err) - return false, err - } - - th.CheckDeepEquals(t, ExpectedListNames, actual) - - return true, nil - }) - th.AssertNoErr(t, err) - th.CheckEquals(t, count, 1) - - // Check with delimiter. - count = 0 - options = &objects.ListOpts{Full: false, Delimiter: "/"} - err = objects.List(fake.ServiceClient(), "testContainer", options).EachPage(func(page pagination.Page) (bool, error) { - count++ - actual, err := objects.ExtractNames(page) - if err != nil { - t.Errorf("Failed to extract container names: %v", err) - return false, err - } - - th.CheckDeepEquals(t, ExpectedListNames, actual) - - return true, nil - }) - th.AssertNoErr(t, err) - th.CheckEquals(t, count, 1) -} - -func TestCreateObject(t *testing.T) { - th.SetupHTTP() - defer th.TeardownHTTP() - - content := "Did gyre and gimble in the wabe" - - HandleCreateTextObjectSuccessfully(t, content) - - options := &objects.CreateOpts{ContentType: "text/plain", Content: strings.NewReader(content)} - res := objects.Create(fake.ServiceClient(), "testContainer", "testObject", options) - th.AssertNoErr(t, res.Err) -} - -func TestCreateObjectWithCacheControl(t *testing.T) { - th.SetupHTTP() - defer th.TeardownHTTP() - - content := "All mimsy were the borogoves" - - HandleCreateTextWithCacheControlSuccessfully(t, content) - - options := &objects.CreateOpts{ - CacheControl: `max-age="3600", public`, - Content: strings.NewReader(content), - } - res := objects.Create(fake.ServiceClient(), "testContainer", "testObject", options) - th.AssertNoErr(t, res.Err) -} - -func TestCreateObjectWithoutContentType(t *testing.T) { - th.SetupHTTP() - defer th.TeardownHTTP() - - content := "The sky was the color of television, tuned to a dead channel." - - HandleCreateTypelessObjectSuccessfully(t, content) - - res := objects.Create(fake.ServiceClient(), "testContainer", "testObject", &objects.CreateOpts{Content: strings.NewReader(content)}) - th.AssertNoErr(t, res.Err) -} - -/* -func TestErrorIsRaisedForChecksumMismatch(t *testing.T) { - th.SetupHTTP() - defer th.TeardownHTTP() - - th.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) { - w.Header().Set("ETag", "acbd18db4cc2f85cedef654fccc4a4d8") - w.WriteHeader(http.StatusCreated) - }) - - content := strings.NewReader("The sky was the color of television, tuned to a dead channel.") - res := Create(fake.ServiceClient(), "testContainer", "testObject", &CreateOpts{Content: content}) - - err := fmt.Errorf("Local checksum does not match API ETag header") - th.AssertDeepEquals(t, err, res.Err) -} -*/ - -func TestCopyObject(t *testing.T) { - th.SetupHTTP() - defer th.TeardownHTTP() - HandleCopyObjectSuccessfully(t) - - options := &objects.CopyOpts{Destination: "/newTestContainer/newTestObject"} - res := objects.Copy(fake.ServiceClient(), "testContainer", "testObject", options) - th.AssertNoErr(t, res.Err) -} - -func TestDeleteObject(t *testing.T) { - th.SetupHTTP() - defer th.TeardownHTTP() - HandleDeleteObjectSuccessfully(t) - - res := objects.Delete(fake.ServiceClient(), "testContainer", "testObject", nil) - th.AssertNoErr(t, res.Err) -} - -func TestUpateObjectMetadata(t *testing.T) { - th.SetupHTTP() - defer th.TeardownHTTP() - HandleUpdateObjectSuccessfully(t) - - options := &objects.UpdateOpts{Metadata: map[string]string{"Gophercloud-Test": "objects"}} - res := objects.Update(fake.ServiceClient(), "testContainer", "testObject", options) - th.AssertNoErr(t, res.Err) -} - -func TestGetObject(t *testing.T) { - th.SetupHTTP() - defer th.TeardownHTTP() - HandleGetObjectSuccessfully(t) - - expected := map[string]string{"Gophercloud-Test": "objects"} - actual, err := objects.Get(fake.ServiceClient(), "testContainer", "testObject", nil).ExtractMetadata() - th.AssertNoErr(t, err) - th.CheckDeepEquals(t, expected, actual) - - getOpts := objects.GetOpts{ - Newest: true, - } - actualHeaders, err := objects.Get(fake.ServiceClient(), "testContainer", "testObject", getOpts).Extract() - th.AssertNoErr(t, err) - th.AssertEquals(t, actualHeaders.StaticLargeObject, true) -} - -func TestETag(t *testing.T) { - content := "some example object" - createOpts := objects.CreateOpts{ - Content: strings.NewReader(content), - NoETag: true, - } - - _, headers, _, err := createOpts.ToObjectCreateParams() - th.AssertNoErr(t, err) - _, ok := headers["ETag"] - th.AssertEquals(t, ok, false) - - hash := md5.New() - io.WriteString(hash, content) - localChecksum := fmt.Sprintf("%x", hash.Sum(nil)) - - createOpts = objects.CreateOpts{ - Content: strings.NewReader(content), - ETag: localChecksum, - } - - _, headers, _, err = createOpts.ToObjectCreateParams() - th.AssertNoErr(t, err) - th.AssertEquals(t, headers["ETag"], localChecksum) -} - -func TestObjectCreateParamsWithoutSeek(t *testing.T) { - content := "I do not implement Seek()" - buf := bytes.NewBuffer([]byte(content)) - - createOpts := objects.CreateOpts{Content: buf} - reader, headers, _, err := createOpts.ToObjectCreateParams() - - th.AssertNoErr(t, err) - - _, ok := reader.(io.ReadSeeker) - th.AssertEquals(t, ok, true) - - c, err := ioutil.ReadAll(reader) - th.AssertNoErr(t, err) - - th.AssertEquals(t, content, string(c)) - - _, ok = headers["ETag"] - th.AssertEquals(t, true, ok) -} - -func TestObjectCreateParamsWithSeek(t *testing.T) { - content := "I implement Seek()" - createOpts := objects.CreateOpts{Content: strings.NewReader(content)} - reader, headers, _, err := createOpts.ToObjectCreateParams() - - th.AssertNoErr(t, err) - - _, ok := reader.(io.ReadSeeker) - th.AssertEquals(t, ok, true) - - c, err := ioutil.ReadAll(reader) - th.AssertNoErr(t, err) - - th.AssertEquals(t, content, string(c)) - - _, ok = headers["ETag"] - th.AssertEquals(t, true, ok) -} diff --git a/openstack/objectstorage/v1/swauth/testing/doc.go b/openstack/objectstorage/v1/swauth/testing/doc.go deleted file mode 100644 index 2388e8d40..000000000 --- a/openstack/objectstorage/v1/swauth/testing/doc.go +++ /dev/null @@ -1,2 +0,0 @@ -// swauth unit tests -package testing diff --git a/openstack/objectstorage/v1/swauth/testing/fixtures.go b/openstack/objectstorage/v1/swauth/testing/fixtures.go deleted file mode 100644 index ef069a67c..000000000 --- a/openstack/objectstorage/v1/swauth/testing/fixtures.go +++ /dev/null @@ -1,29 +0,0 @@ -package testing - -import ( - "fmt" - "net/http" - "testing" - - "github.com/huaweicloud/golangsdk/openstack/objectstorage/v1/swauth" - th "github.com/huaweicloud/golangsdk/testhelper" -) - -// AuthResult is the expected result of AuthOutput -var AuthResult = swauth.AuthResult{ - Token: "AUTH_tk6223e6071f8f4299aa334b48015484a1", - StorageURL: "http://127.0.0.1:8080/v1/AUTH_test/", -} - -// HandleAuthSuccessfully configures the test server to respond to an Auth request. -func HandleAuthSuccessfully(t *testing.T, authOpts swauth.AuthOpts) { - th.Mux.HandleFunc("/auth/v1.0", func(w http.ResponseWriter, r *http.Request) { - th.TestMethod(t, r, "GET") - th.TestHeader(t, r, "X-Auth-User", authOpts.User) - th.TestHeader(t, r, "X-Auth-Key", authOpts.Key) - - w.Header().Add("X-Auth-Token", AuthResult.Token) - w.Header().Add("X-Storage-Url", AuthResult.StorageURL) - fmt.Fprintf(w, "") - }) -} diff --git a/openstack/objectstorage/v1/swauth/testing/requests_test.go b/openstack/objectstorage/v1/swauth/testing/requests_test.go deleted file mode 100755 index 1149dfdbc..000000000 --- a/openstack/objectstorage/v1/swauth/testing/requests_test.go +++ /dev/null @@ -1,35 +0,0 @@ -package testing - -import ( - "testing" - - "github.com/huaweicloud/golangsdk/openstack" - "github.com/huaweicloud/golangsdk/openstack/objectstorage/v1/swauth" - th "github.com/huaweicloud/golangsdk/testhelper" -) - -func TestAuth(t *testing.T) { - authOpts := swauth.AuthOpts{ - User: "test:tester", - Key: "testing", - } - - th.SetupHTTP() - defer th.TeardownHTTP() - HandleAuthSuccessfully(t, authOpts) - - providerClient, err := openstack.NewClient(th.Endpoint()) - th.AssertNoErr(t, err) - - swiftClient, err := swauth.NewObjectStorageV1(providerClient, authOpts) - th.AssertNoErr(t, err) - th.AssertEquals(t, swiftClient.TokenID, AuthResult.Token) -} - -//func TestBadAuth(t *testing.T) { -// authOpts := swauth.AuthOpts{} -// _, err := authOpts.ToAuthOptsMap() -// if err == nil { -// t.Fatalf("Expected an error due to missing auth options") -// } -//} From 0ffb45702e62231aabab8857103cd3a2151351c9 Mon Sep 17 00:00:00 2001 From: Zhenguo Niu Date: Wed, 31 Jul 2019 08:59:06 +0800 Subject: [PATCH 3/3] use evs/v3/volumes --- openstack/evs/v3/{ => volumes}/requests.go | 0 openstack/evs/v3/{ => volumes}/results.go | 0 openstack/evs/v3/{ => volumes}/urls.go | 0 3 files changed, 0 insertions(+), 0 deletions(-) rename openstack/evs/v3/{ => volumes}/requests.go (100%) rename openstack/evs/v3/{ => volumes}/results.go (100%) rename openstack/evs/v3/{ => volumes}/urls.go (100%) diff --git a/openstack/evs/v3/requests.go b/openstack/evs/v3/volumes/requests.go similarity index 100% rename from openstack/evs/v3/requests.go rename to openstack/evs/v3/volumes/requests.go diff --git a/openstack/evs/v3/results.go b/openstack/evs/v3/volumes/results.go similarity index 100% rename from openstack/evs/v3/results.go rename to openstack/evs/v3/volumes/results.go diff --git a/openstack/evs/v3/urls.go b/openstack/evs/v3/volumes/urls.go similarity index 100% rename from openstack/evs/v3/urls.go rename to openstack/evs/v3/volumes/urls.go