~juju-qa/ubuntu/xenial/juju/2.0-rc2

« back to all changes in this revision

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

  • Committer: Nicholas Skaggs
  • Date: 2016-09-30 14:39:30 UTC
  • mfrom: (1.8.1)
  • Revision ID: nicholas.skaggs@canonical.com-20160930143930-vwwhrefh6ftckccy
import upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
package downloader_test
5
5
 
6
6
import (
7
 
        "io/ioutil"
8
7
        "net/url"
9
 
        "os"
10
8
        "path/filepath"
11
 
        "time"
12
9
 
13
10
        gitjujutesting "github.com/juju/testing"
14
11
        jc "github.com/juju/testing/checkers"
53
50
        return URL
54
51
}
55
52
 
56
 
func (s *DownloaderSuite) testDownload(c *gc.C, hostnameVerification utils.SSLHostnameVerification) {
 
53
func (s *DownloaderSuite) testStart(c *gc.C, hostnameVerification utils.SSLHostnameVerification) {
57
54
        tmp := c.MkDir()
58
55
        gitjujutesting.Server.Response(200, nil, []byte("archive"))
59
56
        dlr := downloader.New(downloader.NewArgs{
64
61
                TargetDir: tmp,
65
62
        })
66
63
        status := <-dl.Done()
67
 
        defer os.Remove(status.File.Name())
68
 
        defer status.File.Close()
69
64
        c.Assert(status.Err, gc.IsNil)
70
 
        c.Assert(status.File, gc.NotNil)
71
 
 
72
 
        dir, _ := filepath.Split(status.File.Name())
 
65
        dir, _ := filepath.Split(status.Filename)
73
66
        c.Assert(filepath.Clean(dir), gc.Equals, tmp)
74
 
        assertFileContents(c, status.File, "archive")
 
67
        assertFileContents(c, status.Filename, "archive")
75
68
}
76
69
 
77
70
func (s *DownloaderSuite) TestDownloadWithoutDisablingSSLHostnameVerification(c *gc.C) {
78
 
        s.testDownload(c, utils.VerifySSLHostnames)
 
71
        s.testStart(c, utils.VerifySSLHostnames)
79
72
}
80
73
 
81
74
func (s *DownloaderSuite) TestDownloadWithDisablingSSLHostnameVerification(c *gc.C) {
82
 
        s.testDownload(c, utils.NoVerifySSLHostnames)
 
75
        s.testStart(c, utils.NoVerifySSLHostnames)
83
76
}
84
77
 
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{
 
78
func (s *DownloaderSuite) TestDownload(c *gc.C) {
 
79
        tmp := c.MkDir()
 
80
        gitjujutesting.Server.Response(200, nil, []byte("archive"))
 
81
        dlr := downloader.New(downloader.NewArgs{})
 
82
        filename, err := dlr.Download(downloader.Request{
91
83
                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
84
                TargetDir: tmp,
107
85
        })
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
86
        c.Assert(err, jc.ErrorIsNil)
116
 
        c.Assert(infos, gc.HasLen, 0)
 
87
        dir, _ := filepath.Split(filename)
 
88
        c.Assert(filepath.Clean(dir), gc.Equals, tmp)
 
89
        assertFileContents(c, filename, "archive")
117
90
}