Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

google: mark cache as ready after it got populated #175

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
7 changes: 6 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 @@ -369,6 +373,7 @@ func (p *googleProvider) projectImages(project string) ([]googleImage, error) {
Terms: toTerms(item.Description),
})
}
cache.ready = true

return cache.images, err
}
Expand Down Expand Up @@ -898,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
69 changes: 69 additions & 0 deletions spread/google_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
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) TestImagesCache(c *C) {
n := 0
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch n {
case 0:
c.Check(r.URL.Path, Equals, "/compute/v1/projects/snapd/global/images")
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"}]
}
`))
case 1:
c.Check(r.URL.Path, Equals, "/compute/v1/projects/other-project/global/images")
w.Write([]byte(`
{
"items": [{"status":"READY","name":"ubuntu-2004-64-v20190826", "description":"ubuntu-19.10-64"}]
}
`))
default:
c.Fatalf("unexpected number of requests")
}
n++
}))
defer mockServer.Close()

g := spread.NewGoogleProviderForTesting(mockServer.URL, nil, nil, nil)
c.Assert(g, NotNil)

// Request the project images
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"}},
})
c.Check(n, Equals, 1)

// 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"}},
})
c.Check(n, Equals, 1)

// again, this time for another project
images, err = g.ProjectImages("other-project")
c.Assert(err, IsNil)
c.Assert(images, HasLen, 1)
c.Check(n, Equals, 2)
}
Loading