github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/downloader/download_test.go (about)

     1  // Copyright 2012, 2013 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  
    12  	"github.com/juju/errors"
    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 DownloadSuite struct {
    23  	testing.BaseSuite
    24  	gitjujutesting.HTTPSuite
    25  }
    26  
    27  func (s *DownloadSuite) SetUpSuite(c *gc.C) {
    28  	s.BaseSuite.SetUpSuite(c)
    29  	s.HTTPSuite.SetUpSuite(c)
    30  }
    31  
    32  func (s *DownloadSuite) TearDownSuite(c *gc.C) {
    33  	s.HTTPSuite.TearDownSuite(c)
    34  	s.BaseSuite.TearDownSuite(c)
    35  }
    36  
    37  func (s *DownloadSuite) SetUpTest(c *gc.C) {
    38  	s.BaseSuite.SetUpTest(c)
    39  	s.HTTPSuite.SetUpTest(c)
    40  }
    41  
    42  func (s *DownloadSuite) TearDownTest(c *gc.C) {
    43  	s.HTTPSuite.TearDownTest(c)
    44  	s.BaseSuite.TearDownTest(c)
    45  }
    46  
    47  var _ = gc.Suite(&DownloadSuite{})
    48  
    49  func (s *DownloadSuite) 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 *DownloadSuite) testDownload(c *gc.C, hostnameVerification utils.SSLHostnameVerification) {
    57  	tmp := c.MkDir()
    58  	gitjujutesting.Server.Response(200, nil, []byte("archive"))
    59  	d := downloader.StartDownload(
    60  		downloader.Request{
    61  			URL:       s.URL(c, "/archive.tgz"),
    62  			TargetDir: tmp,
    63  		},
    64  		downloader.NewHTTPBlobOpener(hostnameVerification),
    65  	)
    66  	status := <-d.Done()
    67  	c.Assert(status.Err, gc.IsNil)
    68  
    69  	dir, _ := filepath.Split(status.Filename)
    70  	c.Assert(filepath.Clean(dir), gc.Equals, tmp)
    71  	assertFileContents(c, status.Filename, "archive")
    72  }
    73  
    74  func (s *DownloadSuite) TestDownloadWithoutDisablingSSLHostnameVerification(c *gc.C) {
    75  	s.testDownload(c, utils.VerifySSLHostnames)
    76  }
    77  
    78  func (s *DownloadSuite) TestDownloadWithDisablingSSLHostnameVerification(c *gc.C) {
    79  	s.testDownload(c, utils.NoVerifySSLHostnames)
    80  }
    81  
    82  func (s *DownloadSuite) TestDownloadError(c *gc.C) {
    83  	gitjujutesting.Server.Response(404, nil, nil)
    84  	tmp := c.MkDir()
    85  	d := downloader.StartDownload(
    86  		downloader.Request{
    87  			URL:       s.URL(c, "/archive.tgz"),
    88  			TargetDir: tmp,
    89  		},
    90  		downloader.NewHTTPBlobOpener(utils.VerifySSLHostnames),
    91  	)
    92  	filename, err := d.Wait()
    93  	c.Assert(filename, gc.Equals, "")
    94  	c.Assert(err, gc.ErrorMatches, `bad http response: 404 Not Found`)
    95  	checkDirEmpty(c, tmp)
    96  }
    97  
    98  func (s *DownloadSuite) TestVerifyValid(c *gc.C) {
    99  	stub := &gitjujutesting.Stub{}
   100  	tmp := c.MkDir()
   101  	gitjujutesting.Server.Response(200, nil, []byte("archive"))
   102  	dl := downloader.StartDownload(
   103  		downloader.Request{
   104  			URL:       s.URL(c, "/archive.tgz"),
   105  			TargetDir: tmp,
   106  			Verify: func(f *os.File) error {
   107  				stub.AddCall("Verify", f)
   108  				return nil
   109  			},
   110  		},
   111  		downloader.NewHTTPBlobOpener(utils.VerifySSLHostnames),
   112  	)
   113  	filename, err := dl.Wait()
   114  	c.Assert(err, jc.ErrorIsNil)
   115  	c.Check(filename, gc.Not(gc.Equals), "")
   116  	stub.CheckCallNames(c, "Verify")
   117  }
   118  
   119  func (s *DownloadSuite) TestVerifyInvalid(c *gc.C) {
   120  	stub := &gitjujutesting.Stub{}
   121  	tmp := c.MkDir()
   122  	gitjujutesting.Server.Response(200, nil, []byte("archive"))
   123  	invalid := errors.NotValidf("oops")
   124  	dl := downloader.StartDownload(
   125  		downloader.Request{
   126  			URL:       s.URL(c, "/archive.tgz"),
   127  			TargetDir: tmp,
   128  			Verify: func(f *os.File) error {
   129  				stub.AddCall("Verify", f)
   130  				return invalid
   131  			},
   132  		},
   133  		downloader.NewHTTPBlobOpener(utils.VerifySSLHostnames),
   134  	)
   135  	filename, err := dl.Wait()
   136  	c.Check(filename, gc.Equals, "")
   137  	c.Check(errors.Cause(err), gc.Equals, invalid)
   138  	stub.CheckCallNames(c, "Verify")
   139  	checkDirEmpty(c, tmp)
   140  }
   141  
   142  func (s *DownloadSuite) TestAbort(c *gc.C) {
   143  	tmp := c.MkDir()
   144  	gitjujutesting.Server.Response(200, nil, []byte("archive"))
   145  	abort := make(chan struct{})
   146  	close(abort)
   147  	dl := downloader.StartDownload(
   148  		downloader.Request{
   149  			URL:       s.URL(c, "/archive.tgz"),
   150  			TargetDir: tmp,
   151  			Abort:     abort,
   152  		},
   153  		downloader.NewHTTPBlobOpener(utils.VerifySSLHostnames),
   154  	)
   155  	filename, err := dl.Wait()
   156  	c.Check(filename, gc.Equals, "")
   157  	c.Check(err, gc.ErrorMatches, "download aborted")
   158  	checkDirEmpty(c, tmp)
   159  }
   160  
   161  func assertFileContents(c *gc.C, filename, expect string) {
   162  	got, err := ioutil.ReadFile(filename)
   163  	c.Assert(err, jc.ErrorIsNil)
   164  	c.Check(string(got), gc.Equals, expect)
   165  }
   166  
   167  func checkDirEmpty(c *gc.C, dir string) {
   168  	files, err := ioutil.ReadDir(dir)
   169  	c.Assert(err, jc.ErrorIsNil)
   170  	c.Check(files, gc.HasLen, 0)
   171  }