fix for hanging unit tests
diff --git a/openstack/storage/v1/containers/requests.go b/openstack/storage/v1/containers/requests.go
index 3a6a265..35bc1af 100644
--- a/openstack/storage/v1/containers/requests.go
+++ b/openstack/storage/v1/containers/requests.go
@@ -80,7 +80,7 @@
OkCodes: []int{201, 204},
})
if err == nil {
- container = Container{"name": containerName}
+ container = Container{Name: containerName}
}
return container, err
}
diff --git a/openstack/storage/v1/containers/requests_test.go b/openstack/storage/v1/containers/requests_test.go
index 3b59e00..93747be 100644
--- a/openstack/storage/v1/containers/requests_test.go
+++ b/openstack/storage/v1/containers/requests_test.go
@@ -33,14 +33,29 @@
testhelper.TestHeader(t, r, "Accept", "application/json")
w.Header().Add("Content-Type", "application/json")
- w.WriteHeader(http.StatusOK)
- fmt.Fprintf(w, `[{'count': 0,'bytes': 0,'name': 'janeausten'},{'count': 1,'bytes': 14,'name': 'marktwain'}]`)
+ 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"
+ }
+ ]`)
+ default:
+ t.Fatalf("Unexpected marker: [%s]", marker)
+ }
})
client := serviceClient()
- count := 0
List(client, ListOpts{Full: true}).EachPage(func(page pagination.Page) (bool, error) {
- count++
actual, err := ExtractInfo(page)
if err != nil {
t.Errorf("Failed to extract container info: %v", err)
@@ -49,14 +64,14 @@
expected := []Container{
Container{
- "count": 0,
- "bytes": 0,
- "name": "janeausten",
+ Count: 0,
+ Bytes: 0,
+ Name: "janeausten",
},
Container{
- "count": 1,
- "bytes": 14,
- "name": "marktwain",
+ Count: 1,
+ Bytes: 14,
+ Name: "marktwain",
},
}
@@ -64,10 +79,6 @@
return true, nil
})
-
- if count != 1 {
- t.Errorf("Expected 1 page, got %d", count)
- }
}
func TestListContainerNames(t *testing.T) {
@@ -85,9 +96,7 @@
})
client := serviceClient()
- count := 0
List(client, ListOpts{Full: false}).EachPage(func(page pagination.Page) (bool, error) {
- count++
actual, err := ExtractNames(page)
if err != nil {
t.Errorf("Failed to extract container names: %v", err)
@@ -100,10 +109,6 @@
return true, nil
})
-
- if count != 0 {
- t.Fatalf("Expected 0 pages, got %d", count)
- }
}
func TestCreateContainer(t *testing.T) {
diff --git a/openstack/storage/v1/containers/results.go b/openstack/storage/v1/containers/results.go
index 6a93b4d..283e44a 100644
--- a/openstack/storage/v1/containers/results.go
+++ b/openstack/storage/v1/containers/results.go
@@ -9,7 +9,11 @@
"strings"
)
-type Container map[string]interface{}
+type Container struct {
+ Bytes int
+ Count int
+ Name string
+}
type commonResult struct {
gophercloud.CommonResult
@@ -79,7 +83,11 @@
untyped := page.(ContainerPage).Body.([]interface{})
results := make([]Container, len(untyped))
for index, each := range untyped {
- results[index] = Container(each.(map[string]interface{}))
+ container := each.(map[string]interface{})
+ err := mapstructure.Decode(container, results[index])
+ if err != nil {
+ return results, err
+ }
}
return results, nil
}
@@ -98,7 +106,7 @@
names := make([]string, 0, len(parsed))
for _, container := range parsed {
- names = append(names, container["name"].(string))
+ names = append(names, container.Name)
}
return names, nil
case strings.HasPrefix(ct, "text/plain"):
diff --git a/openstack/storage/v1/objects/requests_test.go b/openstack/storage/v1/objects/requests_test.go
index c3047c6..6fe4d30 100644
--- a/openstack/storage/v1/objects/requests_test.go
+++ b/openstack/storage/v1/objects/requests_test.go
@@ -5,6 +5,7 @@
"fmt"
"net/http"
"testing"
+ "time"
"github.com/rackspace/gophercloud"
"github.com/rackspace/gophercloud/pagination"
@@ -15,7 +16,7 @@
tokenId = "abcabcabcabc"
)
-var metadata = map[string]string{"gophercloud-test": "objects"}
+var metadata = map[string]string{"Gophercloud-Test": "objects"}
func serviceClient() *gophercloud.ServiceClient {
return &gophercloud.ServiceClient{
@@ -55,14 +56,18 @@
testhelper.TestHeader(t, r, "X-Auth-Token", tokenId)
w.Header().Set("Content-Type", "application/json")
- w.WriteHeader(http.StatusOK)
- fmt.Fprintf(w, `[{'hash': '451e372e48e0f6b1114fa0724aa79fa1','last_modified': '2014-01-15T16:41:49.390270','bytes': 14,'name': 'goodbye','content_type': 'application/octet-stream'}]`)
+ r.ParseForm()
+ marker := r.Form.Get("marker")
+ switch marker {
+ case "":
+ fmt.Fprintf(w, `[{'hash': '451e372e48e0f6b1114fa0724aa79fa1','last_modified': '2009-11-10 23:00:00 +0000 UTC','bytes': 14,'name': 'goodbye','content_type': 'application/octet-stream'}]`)
+ default:
+ t.Fatalf("Unexpected marker: [%s]", marker)
+ }
})
client := serviceClient()
- count := 0
List(client, "testContainer", ListOpts{Full: true}).EachPage(func(page pagination.Page) (bool, error) {
- count++
actual, err := ExtractInfo(page)
if err != nil {
t.Errorf("Failed to extract object info: %v", err)
@@ -71,11 +76,11 @@
expected := []Object{
Object{
- "hash": "451e372e48e0f6b1114fa0724aa79fa1",
- "last_modified": "2014-01-15T16:41:49.390270",
- "bytes": 14,
- "name": "goodbye",
- "content_type": "application/octet-stream",
+ Hash: "451e372e48e0f6b1114fa0724aa79fa1",
+ LastModified: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC),
+ Bytes: 14,
+ Name: "goodbye",
+ ContentType: "application/octet-stream",
},
}
@@ -83,10 +88,6 @@
return true, nil
})
-
- if count != 1 {
- t.Errorf("Expected 1 page, got %d", count)
- }
}
func TestListObjectNames(t *testing.T) {
@@ -104,9 +105,7 @@
})
client := serviceClient()
- count := 0
List(client, "testContainer", ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
- count++
actual, err := ExtractNames(page)
if err != nil {
t.Errorf("Failed to extract object names: %v", err)
@@ -119,11 +118,8 @@
return true, nil
})
-
- if count != 0 {
- t.Fatalf("Expected 0 pages, got %d", count)
- }
}
+
func TestCreateObject(t *testing.T) {
testhelper.SetupHTTP()
defer testhelper.TeardownHTTP()
@@ -207,6 +203,7 @@
testhelper.TestMethod(t, r, "HEAD")
testhelper.TestHeader(t, r, "X-Auth-Token", tokenId)
testhelper.TestHeader(t, r, "Accept", "application/json")
+ w.Header().Add("X-Object-Meta-Gophercloud-Test", "objects")
w.WriteHeader(http.StatusNoContent)
})
diff --git a/openstack/storage/v1/objects/results.go b/openstack/storage/v1/objects/results.go
index 8808d03..529bbd9 100644
--- a/openstack/storage/v1/objects/results.go
+++ b/openstack/storage/v1/objects/results.go
@@ -5,12 +5,20 @@
"io/ioutil"
"net/http"
"strings"
+ "time"
+ "github.com/mitchellh/mapstructure"
"github.com/rackspace/gophercloud/pagination"
)
// Object is a structure that holds information related to a storage object.
-type Object map[string]interface{}
+type Object struct {
+ Bytes int `json:"bytes"`
+ ContentType string `json:"content_type"`
+ Hash string `json:"hash"`
+ LastModified time.Time `json:"last_modified"`
+ Name string `json:"name"`
+}
// ListResult is a single page of objects that is returned from a call to the List function.
type ObjectPage struct {
@@ -55,7 +63,12 @@
untyped := page.(ObjectPage).Body.([]interface{})
results := make([]Object, len(untyped))
for index, each := range untyped {
- results[index] = Object(each.(map[string]interface{}))
+ object := each.(map[string]interface{})
+ err := mapstructure.Decode(object, results[index])
+ if err != nil {
+ return results, err
+ }
+
}
return results, nil
}
@@ -74,7 +87,7 @@
names := make([]string, 0, len(parsed))
for _, object := range parsed {
- names = append(names, object["name"].(string))
+ names = append(names, object.Name)
}
return names, nil
case strings.HasPrefix(ct, "text/plain"):