Skip to content

Commit

Permalink
storage: list volumes should handle only name or only region params
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewsykim committed Nov 26, 2017
1 parent 3484038 commit 2594011
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 5 deletions.
4 changes: 4 additions & 0 deletions storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ func (svc *StorageServiceOp) ListVolumes(ctx context.Context, params *ListVolume
if params != nil {
if params.Region != "" && params.Name != "" {
path = fmt.Sprintf("%s?name=%s&region=%s", path, params.Name, params.Region)
} else if params.Region != "" {
path = fmt.Sprintf("%s?region=%s", path, params.Region)
} else if params.Name != "" {
path = fmt.Sprintf("%s?name=%s", path, params.Name)
}

if params.ListOptions != nil {
Expand Down
122 changes: 117 additions & 5 deletions storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,24 +163,136 @@ func TestStorageVolumes_ListVolumesByName(t *testing.T) {
}

mux.HandleFunc("/v2/volumes", func(w http.ResponseWriter, r *http.Request) {
if r.URL.Query().Get("name") != "myvolume" || r.URL.Query().Get("region") != "nyc3" {
t.Errorf("Storage.GetVolumeByName did not request the correct name or region")
if r.URL.Query().Get("name") != "myvolume" || r.URL.Query().Get("region") != "" {
t.Errorf("Storage.ListVolumeByName did not request the correct name or region")
}
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, jBlob)
})

options := &ListVolumeParams{
Name: "myvolume",
}
volumes, _, err := client.Storage.ListVolumes(ctx, options)
if err != nil {
t.Errorf("Storage.ListVolumeByName returned error: %v", err)
}

if !reflect.DeepEqual(volumes, expected) {
t.Errorf("Storage.ListVolumeByName returned %+v, expected %+v", volumes, expected)
}
}

func TestStorageVolumes_ListVolumesByRegion(t *testing.T) {
setup()
defer teardown()

jBlob :=
`{
"volumes": [
{
"region": {"slug": "nyc3"},
"id": "80d414c6-295e-4e3a-ac58-eb9456c1e1d1",
"name": "myvolume",
"description": "my description",
"size_gigabytes": 100,
"droplet_ids": [10],
"created_at": "2002-10-02T15:00:00.05Z"
}
],
"links": {},
"meta": {
"total": 1
}
}`

expected := []Volume{
{
Region: &Region{Slug: "nyc3"},
ID: "80d414c6-295e-4e3a-ac58-eb9456c1e1d1",
Name: "myvolume",
Description: "my description",
SizeGigaBytes: 100,
DropletIDs: []int{10},
CreatedAt: time.Date(2002, 10, 02, 15, 00, 00, 50000000, time.UTC),
},
}

mux.HandleFunc("/v2/volumes", func(w http.ResponseWriter, r *http.Request) {
if r.URL.Query().Get("region") != "nyc3" || r.URL.Query().Get("name") != "" {
t.Errorf("Storage.ListVolumeByName did not request the correct name or region")
}
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, jBlob)
})

options := &ListVolumeParams{
Name: "myvolume",
Region: "nyc3",
}
volumes, _, err := client.Storage.ListVolumes(ctx, options)
if err != nil {
t.Errorf("Storage.GetVolumeByName returned error: %v", err)
t.Errorf("Storage.ListVolumeByName returned error: %v", err)
}

if !reflect.DeepEqual(volumes, expected) {
t.Errorf("Storage.ListVolumeByName returned %+v, expected %+v", volumes, expected)
}
}

func TestStorageVolumes_ListVolumesByNameAndRegion(t *testing.T) {
setup()
defer teardown()

jBlob :=
`{
"volumes": [
{
"region": {"slug": "nyc3"},
"id": "80d414c6-295e-4e3a-ac58-eb9456c1e1d1",
"name": "myvolume",
"description": "my description",
"size_gigabytes": 100,
"droplet_ids": [10],
"created_at": "2002-10-02T15:00:00.05Z"
}
],
"links": {},
"meta": {
"total": 1
}
}`

expected := []Volume{
{
Region: &Region{Slug: "nyc3"},
ID: "80d414c6-295e-4e3a-ac58-eb9456c1e1d1",
Name: "myvolume",
Description: "my description",
SizeGigaBytes: 100,
DropletIDs: []int{10},
CreatedAt: time.Date(2002, 10, 02, 15, 00, 00, 50000000, time.UTC),
},
}

mux.HandleFunc("/v2/volumes", func(w http.ResponseWriter, r *http.Request) {
if r.URL.Query().Get("region") != "nyc3" || r.URL.Query().Get("name") != "myvolume" {
t.Errorf("Storage.ListVolumeByName did not request the correct name or region")
}
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, jBlob)
})

options := &ListVolumeParams{
Region: "nyc3",
Name: "myvolume",
}
volumes, _, err := client.Storage.ListVolumes(ctx, options)
if err != nil {
t.Errorf("Storage.ListVolumeByName returned error: %v", err)
}

if !reflect.DeepEqual(volumes, expected) {
t.Errorf("Storage.GetVolumeByName returned %+v, expected %+v", volumes, expected)
t.Errorf("Storage.ListVolumeByName returned %+v, expected %+v", volumes, expected)
}
}

Expand Down

0 comments on commit 2594011

Please sign in to comment.