-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
72e8cd1
commit 04f5eef
Showing
4 changed files
with
187 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright 2016 Canonical Ltd. | ||
// Licensed under the AGPLv3, see LICENCE file for details. | ||
|
||
package downloader | ||
|
||
import ( | ||
"io" | ||
"net/url" | ||
|
||
"github.com/juju/loggo" | ||
"github.com/juju/utils" | ||
) | ||
|
||
var logger = loggo.GetLogger("juju.downloader") | ||
|
||
// Downloader provides the functionality for downloading files. | ||
type Downloader struct { | ||
// OpenBlob is the func used to gain access to the blob, whether | ||
// through an HTTP request or some other means. | ||
OpenBlob func(url *url.URL) (io.ReadCloser, error) | ||
} | ||
|
||
// NewArgs holds the arguments to New(). | ||
type NewArgs struct { | ||
// HostnameVerification is that which should be used for the client. | ||
// If it is disableSSLHostnameVerification then a non-validating | ||
// client will be used. | ||
HostnameVerification utils.SSLHostnameVerification | ||
} | ||
|
||
// New returns a new Downloader for the given args. | ||
func New(args NewArgs) *Downloader { | ||
return &Downloader{ | ||
OpenBlob: NewHTTPBlobOpener(args.HostnameVerification), | ||
} | ||
} | ||
|
||
// Start starts a new download and returns it. | ||
func (dlr Downloader) Start(req Request) *Download { | ||
dl := StartDownload(req, dlr.OpenBlob) | ||
return dl | ||
} | ||
|
||
// Download starts a new download, waits for it to complete, and | ||
// returns the result. | ||
func (dlr Downloader) Download(req Request, abort <-chan struct{}) (Status, error) { | ||
dl := dlr.Start(req) | ||
return dl.Wait(abort) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
// Copyright 2016 Canonical Ltd. | ||
// Licensed under the AGPLv3, see LICENCE file for details. | ||
|
||
package downloader_test | ||
|
||
import ( | ||
"io/ioutil" | ||
"net/url" | ||
"os" | ||
"path/filepath" | ||
"time" | ||
|
||
gitjujutesting "github.com/juju/testing" | ||
jc "github.com/juju/testing/checkers" | ||
"github.com/juju/utils" | ||
gc "gopkg.in/check.v1" | ||
|
||
"github.com/juju/juju/downloader" | ||
"github.com/juju/juju/testing" | ||
) | ||
|
||
type DownloaderSuite struct { | ||
testing.BaseSuite | ||
gitjujutesting.HTTPSuite | ||
} | ||
|
||
func (s *DownloaderSuite) SetUpSuite(c *gc.C) { | ||
s.BaseSuite.SetUpSuite(c) | ||
s.HTTPSuite.SetUpSuite(c) | ||
} | ||
|
||
func (s *DownloaderSuite) TearDownSuite(c *gc.C) { | ||
s.HTTPSuite.TearDownSuite(c) | ||
s.BaseSuite.TearDownSuite(c) | ||
} | ||
|
||
func (s *DownloaderSuite) SetUpTest(c *gc.C) { | ||
s.BaseSuite.SetUpTest(c) | ||
s.HTTPSuite.SetUpTest(c) | ||
} | ||
|
||
func (s *DownloaderSuite) TearDownTest(c *gc.C) { | ||
s.HTTPSuite.TearDownTest(c) | ||
s.BaseSuite.TearDownTest(c) | ||
} | ||
|
||
var _ = gc.Suite(&DownloaderSuite{}) | ||
|
||
func (s *DownloaderSuite) URL(c *gc.C, path string) *url.URL { | ||
urlStr := s.HTTPSuite.URL(path) | ||
URL, err := url.Parse(urlStr) | ||
c.Assert(err, jc.ErrorIsNil) | ||
return URL | ||
} | ||
|
||
func (s *DownloaderSuite) 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) | ||
c.Assert(status.File, gc.NotNil) | ||
defer os.Remove(status.File.Name()) | ||
defer status.File.Close() | ||
|
||
dir, _ := filepath.Split(status.File.Name()) | ||
c.Assert(filepath.Clean(dir), gc.Equals, tmp) | ||
assertFileContents(c, status.File, "archive") | ||
} | ||
|
||
func (s *DownloaderSuite) TestDownloadWithoutDisablingSSLHostnameVerification(c *gc.C) { | ||
s.testDownload(c, utils.VerifySSLHostnames) | ||
} | ||
|
||
func (s *DownloaderSuite) TestDownloadWithDisablingSSLHostnameVerification(c *gc.C) { | ||
s.testDownload(c, utils.NoVerifySSLHostnames) | ||
} | ||
|
||
func (s *DownloaderSuite) 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`) | ||
} | ||
|
||
func (s *DownloaderSuite) TestStopDownload(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) | ||
} |