~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/downloader/downloader_test.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2016 Canonical Ltd.
 
2
// Licensed under the AGPLv3, see LICENCE file for details.
 
3
 
 
4
package downloader_test
 
5
 
 
6
import (
 
7
        "io/ioutil"
 
8
        "net/url"
 
9
        "os"
 
10
        "path/filepath"
 
11
        "time"
 
12
 
 
13
        gitjujutesting "github.com/juju/testing"
 
14
        jc "github.com/juju/testing/checkers"
 
15
        "github.com/juju/utils"
 
16
        gc "gopkg.in/check.v1"
 
17
 
 
18
        "github.com/juju/juju/downloader"
 
19
        "github.com/juju/juju/testing"
 
20
)
 
21
 
 
22
type DownloaderSuite struct {
 
23
        testing.BaseSuite
 
24
        gitjujutesting.HTTPSuite
 
25
}
 
26
 
 
27
func (s *DownloaderSuite) SetUpSuite(c *gc.C) {
 
28
        s.BaseSuite.SetUpSuite(c)
 
29
        s.HTTPSuite.SetUpSuite(c)
 
30
}
 
31
 
 
32
func (s *DownloaderSuite) TearDownSuite(c *gc.C) {
 
33
        s.HTTPSuite.TearDownSuite(c)
 
34
        s.BaseSuite.TearDownSuite(c)
 
35
}
 
36
 
 
37
func (s *DownloaderSuite) SetUpTest(c *gc.C) {
 
38
        s.BaseSuite.SetUpTest(c)
 
39
        s.HTTPSuite.SetUpTest(c)
 
40
}
 
41
 
 
42
func (s *DownloaderSuite) TearDownTest(c *gc.C) {
 
43
        s.HTTPSuite.TearDownTest(c)
 
44
        s.BaseSuite.TearDownTest(c)
 
45
}
 
46
 
 
47
var _ = gc.Suite(&DownloaderSuite{})
 
48
 
 
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)
 
53
        return URL
 
54
}
 
55
 
 
56
func (s *DownloaderSuite) testDownload(c *gc.C, hostnameVerification utils.SSLHostnameVerification) {
 
57
        tmp := c.MkDir()
 
58
        gitjujutesting.Server.Response(200, nil, []byte("archive"))
 
59
        dlr := downloader.New(downloader.NewArgs{
 
60
                HostnameVerification: hostnameVerification,
 
61
        })
 
62
        dl := dlr.Start(downloader.Request{
 
63
                URL:       s.URL(c, "/archive.tgz"),
 
64
                TargetDir: tmp,
 
65
        })
 
66
        status := <-dl.Done()
 
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)
 
71
 
 
72
        dir, _ := filepath.Split(status.File.Name())
 
73
        c.Assert(filepath.Clean(dir), gc.Equals, tmp)
 
74
        assertFileContents(c, status.File, "archive")
 
75
}
 
76
 
 
77
func (s *DownloaderSuite) TestDownloadWithoutDisablingSSLHostnameVerification(c *gc.C) {
 
78
        s.testDownload(c, utils.VerifySSLHostnames)
 
79
}
 
80
 
 
81
func (s *DownloaderSuite) TestDownloadWithDisablingSSLHostnameVerification(c *gc.C) {
 
82
        s.testDownload(c, utils.NoVerifySSLHostnames)
 
83
}
 
84
 
 
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,
 
89
        })
 
90
        dl := dlr.Start(downloader.Request{
 
91
                URL:       s.URL(c, "/archive.tgz"),
 
92
                TargetDir: c.MkDir(),
 
93
        })
 
94
        status := <-dl.Done()
 
95
        c.Assert(status.File, gc.IsNil)
 
96
        c.Assert(status.Err, gc.ErrorMatches, `cannot download ".*": bad http response: 404 Not Found`)
 
97
}
 
98
 
 
99
func (s *DownloaderSuite) TestStopDownload(c *gc.C) {
 
100
        tmp := c.MkDir()
 
101
        dlr := downloader.New(downloader.NewArgs{
 
102
                HostnameVerification: utils.VerifySSLHostnames,
 
103
        })
 
104
        dl := dlr.Start(downloader.Request{
 
105
                URL:       s.URL(c, "/x.tgz"),
 
106
                TargetDir: tmp,
 
107
        })
 
108
        dl.Stop()
 
109
        select {
 
110
        case status := <-dl.Done():
 
111
                c.Fatalf("received status %#v after stop", status)
 
112
        case <-time.After(testing.ShortWait):
 
113
        }
 
114
        infos, err := ioutil.ReadDir(tmp)
 
115
        c.Assert(err, jc.ErrorIsNil)
 
116
        c.Assert(infos, gc.HasLen, 0)
 
117
}