1
// Copyright 2016 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
4
package downloader_test
13
gitjujutesting "github.com/juju/testing"
14
jc "github.com/juju/testing/checkers"
15
"github.com/juju/utils"
16
gc "gopkg.in/check.v1"
18
"github.com/juju/juju/downloader"
19
"github.com/juju/juju/testing"
22
type DownloaderSuite struct {
24
gitjujutesting.HTTPSuite
27
func (s *DownloaderSuite) SetUpSuite(c *gc.C) {
28
s.BaseSuite.SetUpSuite(c)
29
s.HTTPSuite.SetUpSuite(c)
32
func (s *DownloaderSuite) TearDownSuite(c *gc.C) {
33
s.HTTPSuite.TearDownSuite(c)
34
s.BaseSuite.TearDownSuite(c)
37
func (s *DownloaderSuite) SetUpTest(c *gc.C) {
38
s.BaseSuite.SetUpTest(c)
39
s.HTTPSuite.SetUpTest(c)
42
func (s *DownloaderSuite) TearDownTest(c *gc.C) {
43
s.HTTPSuite.TearDownTest(c)
44
s.BaseSuite.TearDownTest(c)
47
var _ = gc.Suite(&DownloaderSuite{})
49
func (s *DownloaderSuite) URL(c *gc.C, path string) *url.URL {
50
urlStr := s.HTTPSuite.URL(path)
51
URL, err := url.Parse(urlStr)
52
c.Assert(err, jc.ErrorIsNil)
56
func (s *DownloaderSuite) testDownload(c *gc.C, hostnameVerification utils.SSLHostnameVerification) {
58
gitjujutesting.Server.Response(200, nil, []byte("archive"))
59
dlr := downloader.New(downloader.NewArgs{
60
HostnameVerification: hostnameVerification,
62
dl := dlr.Start(downloader.Request{
63
URL: s.URL(c, "/archive.tgz"),
67
defer os.Remove(status.File.Name())
68
defer status.File.Close()
69
c.Assert(status.Err, gc.IsNil)
70
c.Assert(status.File, gc.NotNil)
72
dir, _ := filepath.Split(status.File.Name())
73
c.Assert(filepath.Clean(dir), gc.Equals, tmp)
74
assertFileContents(c, status.File, "archive")
77
func (s *DownloaderSuite) TestDownloadWithoutDisablingSSLHostnameVerification(c *gc.C) {
78
s.testDownload(c, utils.VerifySSLHostnames)
81
func (s *DownloaderSuite) TestDownloadWithDisablingSSLHostnameVerification(c *gc.C) {
82
s.testDownload(c, utils.NoVerifySSLHostnames)
85
func (s *DownloaderSuite) TestDownloadError(c *gc.C) {
86
gitjujutesting.Server.Response(404, nil, nil)
87
dlr := downloader.New(downloader.NewArgs{
88
HostnameVerification: utils.VerifySSLHostnames,
90
dl := dlr.Start(downloader.Request{
91
URL: s.URL(c, "/archive.tgz"),
95
c.Assert(status.File, gc.IsNil)
96
c.Assert(status.Err, gc.ErrorMatches, `cannot download ".*": bad http response: 404 Not Found`)
99
func (s *DownloaderSuite) TestStopDownload(c *gc.C) {
101
dlr := downloader.New(downloader.NewArgs{
102
HostnameVerification: utils.VerifySSLHostnames,
104
dl := dlr.Start(downloader.Request{
105
URL: s.URL(c, "/x.tgz"),
110
case status := <-dl.Done():
111
c.Fatalf("received status %#v after stop", status)
112
case <-time.After(testing.ShortWait):
114
infos, err := ioutil.ReadDir(tmp)
115
c.Assert(err, jc.ErrorIsNil)
116
c.Assert(infos, gc.HasLen, 0)