Skip to content

Commit

Permalink
google: add test if image cache works
Browse files Browse the repository at this point in the history
  • Loading branch information
mvo5 committed Oct 13, 2023
1 parent e23da80 commit c011683
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 1 deletion.
17 changes: 17 additions & 0 deletions spread/export_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package spread

import (
"net/http"
"time"

"golang.org/x/crypto/ssh"
Expand Down Expand Up @@ -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
6 changes: 5 additions & 1 deletion spread/google.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}
}

Expand All @@ -50,6 +52,8 @@ type googleProvider struct {
keyErr error

imagesCache map[string]*googleImagesCache

apiURL string
}

type googleServer struct {
Expand Down Expand Up @@ -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 {
Expand Down
57 changes: 57 additions & 0 deletions spread/google_test.go
Original file line number Diff line number Diff line change
@@ -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"}},
})

}

0 comments on commit c011683

Please sign in to comment.