Example #1
0
func (s *DownloadSuite) TestDownloadError(c *gc.C) {
	gitjujutesting.Server.Response(404, nil, nil)
	d := downloader.StartDownload(
		downloader.Request{
			URL:       s.URL(c, "/archive.tgz"),
			TargetDir: c.MkDir(),
		},
		downloader.NewHTTPBlobOpener(utils.VerifySSLHostnames),
	)
	status := <-d.Done()
	c.Assert(status.File, gc.IsNil)
	c.Assert(status.Err, gc.ErrorMatches, `cannot download ".*": bad http response: 404 Not Found`)
}
Example #2
0
func (s *DownloadSuite) TestDownloadError(c *gc.C) {
	gitjujutesting.Server.Response(404, nil, nil)
	tmp := c.MkDir()
	d := downloader.StartDownload(
		downloader.Request{
			URL:       s.URL(c, "/archive.tgz"),
			TargetDir: tmp,
		},
		downloader.NewHTTPBlobOpener(utils.VerifySSLHostnames),
	)
	filename, err := d.Wait()
	c.Assert(filename, gc.Equals, "")
	c.Assert(err, gc.ErrorMatches, `bad http response: 404 Not Found`)
	checkDirEmpty(c, tmp)
}
Example #3
0
func (s *DownloadSuite) testDownload(c *gc.C, hostnameVerification utils.SSLHostnameVerification) {
	tmp := c.MkDir()
	gitjujutesting.Server.Response(200, nil, []byte("archive"))
	d := downloader.StartDownload(
		downloader.Request{
			URL:       s.URL(c, "/archive.tgz"),
			TargetDir: tmp,
		},
		downloader.NewHTTPBlobOpener(hostnameVerification),
	)
	status := <-d.Done()
	c.Assert(status.Err, gc.IsNil)

	dir, _ := filepath.Split(status.Filename)
	c.Assert(filepath.Clean(dir), gc.Equals, tmp)
	assertFileContents(c, status.Filename, "archive")
}
Example #4
0
func (s *DownloadSuite) TestAbort(c *gc.C) {
	tmp := c.MkDir()
	gitjujutesting.Server.Response(200, nil, []byte("archive"))
	abort := make(chan struct{})
	close(abort)
	dl := downloader.StartDownload(
		downloader.Request{
			URL:       s.URL(c, "/archive.tgz"),
			TargetDir: tmp,
			Abort:     abort,
		},
		downloader.NewHTTPBlobOpener(utils.VerifySSLHostnames),
	)
	filename, err := dl.Wait()
	c.Check(filename, gc.Equals, "")
	c.Check(err, gc.ErrorMatches, "download aborted")
	checkDirEmpty(c, tmp)
}
Example #5
0
func (s *DownloadSuite) TestStop(c *gc.C) {
	tmp := c.MkDir()
	d := downloader.StartDownload(
		downloader.Request{
			URL:       s.URL(c, "/x.tgz"),
			TargetDir: tmp,
		},
		downloader.NewHTTPBlobOpener(utils.VerifySSLHostnames),
	)
	d.Stop()
	select {
	case status := <-d.Done():
		c.Fatalf("received status %#v after stop", status)
	case <-time.After(testing.ShortWait):
	}
	infos, err := ioutil.ReadDir(tmp)
	c.Assert(err, jc.ErrorIsNil)
	c.Assert(infos, gc.HasLen, 0)
}
Example #6
0
func (s *DownloadSuite) TestVerifyValid(c *gc.C) {
	stub := &gitjujutesting.Stub{}
	tmp := c.MkDir()
	gitjujutesting.Server.Response(200, nil, []byte("archive"))
	dl := downloader.StartDownload(
		downloader.Request{
			URL:       s.URL(c, "/archive.tgz"),
			TargetDir: tmp,
			Verify: func(f *os.File) error {
				stub.AddCall("Verify", f)
				return nil
			},
		},
		downloader.NewHTTPBlobOpener(utils.VerifySSLHostnames),
	)
	filename, err := dl.Wait()
	c.Assert(err, jc.ErrorIsNil)
	c.Check(filename, gc.Not(gc.Equals), "")
	stub.CheckCallNames(c, "Verify")
}
Example #7
0
func (s *DownloadSuite) TestVerifyInvalid(c *gc.C) {
	stub := &gitjujutesting.Stub{}
	tmp := c.MkDir()
	gitjujutesting.Server.Response(200, nil, []byte("archive"))
	invalid := errors.NotValidf("oops")
	dl := downloader.StartDownload(
		downloader.Request{
			URL:       s.URL(c, "/archive.tgz"),
			TargetDir: tmp,
			Verify: func(f *os.File) error {
				stub.AddCall("Verify", f)
				return invalid
			},
		},
		downloader.NewHTTPBlobOpener(utils.VerifySSLHostnames),
	)
	status := <-dl.Done()

	c.Check(errors.Cause(status.Err), gc.Equals, invalid)
	stub.CheckCallNames(c, "Verify")
	stub.CheckCall(c, 0, "Verify", status.File)
}