Skip to content

Commit

Permalink
Merge pull request #38 from digitalocean/change-kernel-regression
Browse files Browse the repository at this point in the history
Issues with listing droplet kernels
  • Loading branch information
bryanl committed Apr 6, 2016
2 parents b940856 + 8fd6a4f commit 925ed1b
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 7 deletions.
2 changes: 1 addition & 1 deletion commands/droplet_actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func DropletAction() *Command {

cmdDropletActionChangeKernel := CmdBuilder(cmd, RunDropletActionChangeKernel,
"change-kernel <droplet-id>", "change kernel", Writer,
docCategories("droplet"))
displayerType(&action{}), docCategories("droplet"))
AddIntFlag(cmdDropletActionChangeKernel, doit.ArgKernelID, 0, "Kernel ID", requiredOpt())
AddBoolFlag(cmdDropletActionChangeKernel, doit.ArgCommandWait, false, "Wait for action to complete")

Expand Down
10 changes: 7 additions & 3 deletions do/pagination.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ import (

const maxFetchPages = 5

var perPage = 200

var fetchFn = fetchPage

type paginatedList struct {
list []interface{}
mu sync.Mutex
Expand All @@ -41,7 +45,7 @@ type Generator func(*godo.ListOptions) ([]interface{}, *godo.Response, error)

// PaginateResp paginates a Response.
func PaginateResp(gen Generator) ([]interface{}, error) {
opt := &godo.ListOptions{Page: 1, PerPage: 200}
opt := &godo.ListOptions{Page: 1, PerPage: perPage}

l := paginatedList{}

Expand All @@ -52,7 +56,7 @@ func PaginateResp(gen Generator) ([]interface{}, error) {
wg.Add(1)
go func() {
for page := range fetchChan {
items, err := fetchPage(gen, page)
items, err := fetchFn(gen, page)
if err == nil {
l.append(items...)
}
Expand All @@ -76,7 +80,7 @@ func PaginateResp(gen Generator) ([]interface{}, error) {
}

// start with second page
for i := 2; i < lp; i++ {
for i := 1; i < lp; i++ {
fetchChan <- i
}
close(fetchChan)
Expand Down
72 changes: 69 additions & 3 deletions do/pagination_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

/*
Copyright 2016 The Doctl Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -14,7 +13,74 @@ limitations under the License.

package do

import "testing"
import (
"sync"
"testing"

"github.com/digitalocean/godo"
"github.com/stretchr/testify/assert"
)

func Test_PaginateResp(t *testing.T) {
var mu sync.Mutex
currentPage := 0
resp := &godo.Response{Links: &godo.Links{Pages: &godo.Pages{Last: "http://example.com/?page=5"}}}

gen := func(*godo.ListOptions) ([]interface{}, *godo.Response, error) {
mu.Lock()
defer mu.Unlock()
currentPage++
return []interface{}{currentPage}, resp, nil
}

list, err := PaginateResp(gen)
assert.NoError(t, err)

assert.Len(t, list, 5)
}

func Test_Pagination_fetchPage(t *testing.T) {
gen := func(opt *godo.ListOptions) ([]interface{}, *godo.Response, error) {
items := []interface{}{}
resp := &godo.Response{}

assert.Equal(t, 10, opt.Page)

return items, resp, nil
}

fetchPage(gen, 10)
}

func Test_Pagination_lastPage(t *testing.T) {
cases := []struct {
r *godo.Response
lastPage int
isValid bool
}{
{
r: &godo.Response{
Links: &godo.Links{
Pages: &godo.Pages{Last: "http://example.com/?page=1"},
},
},
lastPage: 1,
isValid: true,
},
{
r: &godo.Response{Links: &godo.Links{}},
lastPage: 1,
isValid: true,
},
}

func TestPaginateResp(t *testing.T) {
for _, c := range cases {
lp, err := lastPage(c.r)
if c.isValid {
assert.NoError(t, err)
assert.Equal(t, c.lastPage, lp)
} else {
assert.Error(t, err)
}
}
}

0 comments on commit 925ed1b

Please sign in to comment.