use generic parameter building functions; pagination in unit tests
diff --git a/acceptance/openstack/storage/v1/containers_test.go b/acceptance/openstack/storage/v1/containers_test.go
index 29f6312..3cc367b 100644
--- a/acceptance/openstack/storage/v1/containers_test.go
+++ b/acceptance/openstack/storage/v1/containers_test.go
@@ -8,6 +8,7 @@
"github.com/rackspace/gophercloud/acceptance/tools"
"github.com/rackspace/gophercloud/openstack/storage/v1/containers"
+ "github.com/rackspace/gophercloud/pagination"
)
// numContainers is the number of containers to create for testing.
@@ -18,7 +19,6 @@
client, err := newClient()
if err != nil {
t.Error(err)
- return
}
// Create a slice of random container names.
@@ -29,80 +29,65 @@
// Create numContainers containers.
for i := 0; i < len(cNames); i++ {
- _, err := containers.Create(client, containers.CreateOpts{
- Name: cNames[i],
- })
+ _, err := containers.Create(client, cNames[i], containers.CreateOpts{})
if err != nil {
t.Error(err)
- return
}
}
// Delete the numContainers containers after function completion.
defer func() {
for i := 0; i < len(cNames); i++ {
- err = containers.Delete(client, containers.DeleteOpts{
- Name: cNames[i],
- })
+ err = containers.Delete(client, cNames[i])
if err != nil {
t.Error(err)
- return
}
}
}()
// List the numContainer names that were just created. To just list those,
// the 'prefix' parameter is used.
- lr, err := containers.List(client, containers.ListOpts{
- Full: false,
- Params: map[string]string{
- "prefix": "gophercloud-test-container-",
- },
+ pager := containers.List(client, containers.ListOpts{Full: true, Prefix: "gophercloud-test-container-"})
+ if pager.Err != nil {
+ t.Error(err)
+ return
+ }
+ err = pager.EachPage(func(page pagination.Page) (bool, error) {
+ containerList, err := containers.ExtractInfo(page)
+ if err != nil {
+ t.Error(err)
+ }
+ for _, n := range containerList {
+ t.Logf("Container: Name [%s] Count [%d] Bytes [%d]",
+ n["name"], int(n["count"].(float64)), int(n["bytes"].(float64)))
+ }
+
+ return true, nil
})
if err != nil {
t.Error(err)
- return
- }
- // Extract the names from the 'List' response.
- cns, err := containers.ExtractNames(lr)
- if err != nil {
- t.Error(err)
- return
- }
- if len(cns) != len(cNames) {
- t.Errorf("Expected %d names and got %d:\nExpected:%v\nActual:%v", len(cNames), len(cns), cNames, cns)
- return
}
// List the info for the numContainer containers that were created.
- lr, err = containers.List(client, containers.ListOpts{
- Full: true,
- Params: map[string]string{
- "prefix": "gophercloud-test-container-",
- },
+ pager = containers.List(client, containers.ListOpts{Full: false, Prefix: "gophercloud-test-container-"})
+ err = pager.EachPage(func(page pagination.Page) (bool, error) {
+ containerList, err := containers.ExtractNames(page)
+ if err != nil {
+ return false, err
+ }
+ for _, n := range containerList {
+ t.Logf("Container: Name [%s]", n)
+ }
+
+ return true, nil
})
if err != nil {
t.Error(err)
- return
- }
- // Extract the info from the 'List' response.
- cis, err := containers.ExtractInfo(lr)
- if err != nil {
- t.Error(err)
- return
- }
- if len(cis) != len(cNames) {
- t.Errorf("Expected %d containers and got %d", len(cNames), len(cis))
- return
}
// Update one of the numContainer container metadata.
- err = containers.Update(client, containers.UpdateOpts{
- Name: cNames[0],
- Metadata: metadata,
- })
+ err = containers.Update(client, cNames[0], containers.UpdateOpts{Metadata: metadata})
if err != nil {
t.Error(err)
- return
}
// After the tests are done, delete the metadata that was set.
defer func() {
@@ -110,30 +95,20 @@
for k := range metadata {
tempMap[k] = ""
}
- err = containers.Update(client, containers.UpdateOpts{
- Name: cNames[0],
- Metadata: tempMap,
- })
+ err = containers.Update(client, cNames[0], containers.UpdateOpts{Metadata: tempMap})
if err != nil {
t.Error(err)
- return
}
}()
// Retrieve a container's metadata.
- gr, err := containers.Get(client, containers.GetOpts{
- Name: cNames[0],
- })
+ cm, err := containers.Get(client, cNames[0]).ExtractMetadata()
if err != nil {
t.Error(err)
- return
}
- // Extract the metadata from the 'Get' response.
- cm := containers.ExtractMetadata(gr)
for k := range metadata {
if cm[k] != metadata[strings.Title(k)] {
t.Errorf("Expected custom metadata with key: %s", k)
- return
}
}
}
diff --git a/acceptance/openstack/storage/v1/objects_test.go b/acceptance/openstack/storage/v1/objects_test.go
index 066a223..239b4d5 100644
--- a/acceptance/openstack/storage/v1/objects_test.go
+++ b/acceptance/openstack/storage/v1/objects_test.go
@@ -33,18 +33,14 @@
// Create a container to hold the test objects.
cName := tools.RandomString("test-container-", 8)
- _, err = containers.Create(client, containers.CreateOpts{
- Name: cName,
- })
+ _, err = containers.Create(client, cName, containers.CreateOpts{})
if err != nil {
t.Error(err)
return
}
// Defer deletion of the container until after testing.
defer func() {
- err = containers.Delete(client, containers.DeleteOpts{
- Name: cName,
- })
+ err = containers.Delete(client, cName)
if err != nil {
t.Error(err)
return
@@ -55,11 +51,7 @@
oContents := make([]*bytes.Buffer, numObjects)
for i := 0; i < numObjects; i++ {
oContents[i] = bytes.NewBuffer([]byte(tools.RandomString("", 10)))
- err = objects.Create(client, objects.CreateOpts{
- Container: cName,
- Name: oNames[i],
- Content: oContents[i],
- })
+ err = objects.Create(client, cName, oNames[i], oContents[i], objects.CreateOpts{})
if err != nil {
t.Error(err)
return
@@ -68,14 +60,11 @@
// Delete the objects after testing.
defer func() {
for i := 0; i < numObjects; i++ {
- err = objects.Delete(client, objects.DeleteOpts{
- Container: cName,
- Name: oNames[i],
- })
+ err = objects.Delete(client, cName, oNames[i], objects.DeleteOpts{})
}
}()
- pager := objects.List(client, objects.ListOpts{Full: false, Container: cName})
+ pager := objects.List(client, cName, objects.ListOpts{Full: false})
ons := make([]string, 0, len(oNames))
err = pager.EachPage(func(page pagination.Page) (bool, error) {
names, err := objects.ExtractNames(page)
@@ -95,7 +84,7 @@
return
}
- pager = objects.List(client, objects.ListOpts{Full: true, Container: cName})
+ pager = objects.List(client, cName, objects.ListOpts{Full: true})
ois := make([]objects.Object, 0, len(oNames))
err = pager.EachPage(func(page pagination.Page) (bool, error) {
info, err := objects.ExtractInfo(page)
@@ -117,42 +106,20 @@
}
// Copy the contents of one object to another.
- err = objects.Copy(client, objects.CopyOpts{
- Container: cName,
- Name: oNames[0],
- NewContainer: cName,
- NewName: oNames[1],
- })
+ err = objects.Copy(client, cName, oNames[0], objects.CopyOpts{Destination: cName + "/" + oNames[1]})
if err != nil {
t.Error(err)
return
}
// Download one of the objects that was created above.
- dr, err := objects.Download(client, objects.DownloadOpts{
- Container: cName,
- Name: oNames[1],
- })
+ o1Content, err := objects.Download(client, cName, oNames[0], objects.DownloadOpts{}).ExtractContent()
if err != nil {
t.Error(err)
return
}
- // Extract the content from the 'Download' response of object.
- o2Content, err := objects.ExtractContent(dr)
- if err != nil {
- t.Error(err)
- }
// Download the another object that was create above.
- dr, err = objects.Download(client, objects.DownloadOpts{
- Container: cName,
- Name: oNames[0],
- })
- if err != nil {
- t.Error(err)
- return
- }
- // Extract the content from the 'Download' response of other object.
- o1Content, err := objects.ExtractContent(dr)
+ o2Content, err := objects.Download(client, cName, oNames[1], objects.DownloadOpts{}).ExtractContent()
if err != nil {
t.Error(err)
return
@@ -164,11 +131,7 @@
}
// Update an object's metadata.
- err = objects.Update(client, objects.UpdateOpts{
- Container: cName,
- Name: oNames[0],
- Metadata: metadata,
- })
+ err = objects.Update(client, cName, oNames[0], objects.UpdateOpts{Metadata: metadata})
if err != nil {
t.Error(err)
return
@@ -179,11 +142,7 @@
for k := range metadata {
tempMap[k] = ""
}
- err = objects.Update(client, objects.UpdateOpts{
- Container: cName,
- Name: oNames[0],
- Metadata: tempMap,
- })
+ err = objects.Update(client, cName, oNames[0], objects.UpdateOpts{Metadata: tempMap})
if err != nil {
t.Error(err)
return
@@ -191,13 +150,11 @@
}()
// Retrieve an object's metadata.
- gr, err := objects.Get(client, objects.GetOpts{})
+ om, err := objects.Get(client, cName, oNames[0], objects.GetOpts{}).ExtractMetadata()
if err != nil {
t.Error(err)
return
}
- // Extract the custom metadata from the 'Get' response.
- om := objects.ExtractMetadata(gr)
for k := range metadata {
if om[k] != metadata[strings.Title(k)] {
t.Errorf("Expected custom metadata with key: %s", k)