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

store: add download test with EOF in the middle #3158

Merged
merged 2 commits into from Apr 10, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 48 additions & 0 deletions store/store_test.go
Expand Up @@ -221,6 +221,10 @@ func (t *remoteRepoTestSuite) SetUpTest(c *C) {
dirs.SetRootDir(c.MkDir())
c.Assert(os.MkdirAll(dirs.SnapMountDir, 0755), IsNil)

oldSnapdDebug := os.Getenv("SNAPD_DEBUG")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIR we should not use setenv much as it is unreliable in multi threaded programs. Could we mock something else to control the effective value of "SNAPD_DEBUG" in tests?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That seems a bit far-fetched, as it'd imply Setenv can't ever be used in Go at all.

Unless there are more details including why they affect Go given the implementation currently in use, let's not take that kind of theory for granted.

os.Setenv("SNAPD_DEBUG", "1")
t.AddCleanup(func() { os.Setenv("SNAPD_DEBUG", oldSnapdDebug) })

t.logbuf = bytes.NewBuffer(nil)
l, err := logger.NewConsoleLog(t.logbuf, logger.DefaultFlags)
c.Assert(err, IsNil)
Expand Down Expand Up @@ -326,6 +330,50 @@ func (t *remoteRepoTestSuite) TestDownloadRangeRequest(c *C) {
c.Assert(string(content), Equals, partialContentStr+"was downloaded")
}

func (t *remoteRepoTestSuite) TestDownloadEOFHandlesResumeHashCorrectly(c *C) {
n := 0
var mockServer *httptest.Server

// our mock download content
buf := make([]byte, 50000)
for i := range buf {
buf[i] = 'x'
}
h := crypto.SHA3_384.New()
io.Copy(h, bytes.NewBuffer(buf))

// raise an EOF shortly before the end
mockServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
n++
if n < 2 {
w.Header().Add("Content-Length", fmt.Sprintf("%d", len(buf)))
w.Write(buf[0 : len(buf)-5])
mockServer.CloseClientConnections()
return
}
w.Write(buf[len(buf)-5:])
}))

c.Assert(mockServer, NotNil)
defer mockServer.Close()

snap := &snap.Info{}
snap.RealName = "foo"
snap.AnonDownloadURL = mockServer.URL
snap.DownloadURL = "AUTH-URL"
snap.Sha3_384 = fmt.Sprintf("%x", h.Sum(nil))

targetFn := filepath.Join(c.MkDir(), "foo_1.0_all.snap")
err := t.store.Download(context.TODO(), "foo", targetFn, &snap.DownloadInfo, nil, nil)
c.Assert(err, IsNil)

content, err := ioutil.ReadFile(targetFn)
c.Assert(err, IsNil)
c.Assert(content, DeepEquals, buf)

c.Assert(t.logbuf.String(), Matches, "(?s).*Retrying .* attempt 2, .*")
}

func (t *remoteRepoTestSuite) TestDownloadRangeRequestRetryOnHashError(c *C) {
partialContentStr := "partial content "

Expand Down