diff --git a/spread/export_test.go b/spread/export_test.go index d20b407b..c27fca27 100644 --- a/spread/export_test.go +++ b/spread/export_test.go @@ -1,6 +1,7 @@ package spread import ( + "net/http" "time" "golang.org/x/crypto/ssh" @@ -38,3 +39,19 @@ func MockSshDial(f func(network, addr string, config *ssh.ClientConfig) (*ssh.Cl } var QemuCmd = qemuCmd + +func NewGoogleProviderForTesting(mockApiURL string, p *Project, b *Backend, o *Options) *googleProvider { + provider := Google(p, b, o) + ggl := provider.(*googleProvider) + ggl.apiURL = mockApiURL + ggl.keyChecked = true + ggl.client = &http.Client{} + + return ggl +} + +func (p *googleProvider) ProjectImages(project string) ([]googleImage, error) { + return p.projectImages(project) +} + +type GoogleImage = googleImage diff --git a/spread/google.go b/spread/google.go index fe6633f9..9e82835c 100644 --- a/spread/google.go +++ b/spread/google.go @@ -31,6 +31,8 @@ func Google(p *Project, b *Backend, o *Options) Provider { options: o, imagesCache: make(map[string]*googleImagesCache), + + apiURL: "https://www.googleapis.com", } } @@ -50,6 +52,8 @@ type googleProvider struct { keyErr error imagesCache map[string]*googleImagesCache + + apiURL string } type googleServer struct { @@ -899,7 +903,7 @@ func (p *googleProvider) dofl(method, subpath string, params interface{}, result <-googleThrottle - url := "https://www.googleapis.com" + url := p.apiURL if flags&noPathPrefix == 0 { url += "/compute/v1/projects/" + p.gproject() + subpath } else { diff --git a/spread/google_test.go b/spread/google_test.go new file mode 100644 index 00000000..36bf6d23 --- /dev/null +++ b/spread/google_test.go @@ -0,0 +1,57 @@ +package spread_test + +import ( + "net/http" + "net/http/httptest" + + "github.com/snapcore/spread/spread" + + . "gopkg.in/check.v1" +) + +type googleSuite struct{} + +var _ = Suite(&googleSuite{}) + +func (s *googleSuite) TestPagination(c *C) { + n := 0 + mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch n { + case 0: + c.Check(r.ParseForm(), IsNil) + c.Check(r.Form.Get("pageToken"), Equals, "") + w.Write([]byte(` + { + "items": [{"status":"READY","name":"ubuntu-1910-64-v20190826", "description":"ubuntu-19.10-64"}, {"status":"READY","name":"ubuntu-1904-64-v20190726", "description":"ubuntu-19.04-64"}], + "nextpagetoken": "token-1" + } + `)) + case 1: + c.Fatalf("cache not working") + } + n++ + })) + defer mockServer.Close() + + // hit the google API + g := spread.NewGoogleProviderForTesting(mockServer.URL, nil, nil, nil) + c.Assert(g, NotNil) + + images, err := g.ProjectImages("snapd") + c.Assert(err, IsNil) + c.Assert(images, HasLen, 2) + c.Check(images, DeepEquals, []spread.GoogleImage{ + {"snapd", "ubuntu-1910-64-v20190826", "", []string{"ubuntu", "19.10", "64"}}, + {"snapd", "ubuntu-1904-64-v20190726", "", []string{"ubuntu", "19.04", "64"}}, + }) + + // do it again, now it comes from the cache + images, err = g.ProjectImages("snapd") + c.Assert(err, IsNil) + c.Assert(images, HasLen, 2) + c.Check(images, DeepEquals, []spread.GoogleImage{ + {"snapd", "ubuntu-1910-64-v20190826", "", []string{"ubuntu", "19.10", "64"}}, + {"snapd", "ubuntu-1904-64-v20190726", "", []string{"ubuntu", "19.04", "64"}}, + }) + +}