github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/downloader/downloader_test.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package downloader_test
     5  
     6  import (
     7  	"net/url"
     8  	"path/filepath"
     9  
    10  	gitjujutesting "github.com/juju/testing"
    11  	jc "github.com/juju/testing/checkers"
    12  	"github.com/juju/utils"
    13  	gc "gopkg.in/check.v1"
    14  
    15  	"github.com/juju/juju/downloader"
    16  	"github.com/juju/juju/testing"
    17  )
    18  
    19  type DownloaderSuite struct {
    20  	testing.BaseSuite
    21  	gitjujutesting.HTTPSuite
    22  }
    23  
    24  func (s *DownloaderSuite) SetUpSuite(c *gc.C) {
    25  	s.BaseSuite.SetUpSuite(c)
    26  	s.HTTPSuite.SetUpSuite(c)
    27  }
    28  
    29  func (s *DownloaderSuite) TearDownSuite(c *gc.C) {
    30  	s.HTTPSuite.TearDownSuite(c)
    31  	s.BaseSuite.TearDownSuite(c)
    32  }
    33  
    34  func (s *DownloaderSuite) SetUpTest(c *gc.C) {
    35  	s.BaseSuite.SetUpTest(c)
    36  	s.HTTPSuite.SetUpTest(c)
    37  }
    38  
    39  func (s *DownloaderSuite) TearDownTest(c *gc.C) {
    40  	s.HTTPSuite.TearDownTest(c)
    41  	s.BaseSuite.TearDownTest(c)
    42  }
    43  
    44  var _ = gc.Suite(&DownloaderSuite{})
    45  
    46  func (s *DownloaderSuite) URL(c *gc.C, path string) *url.URL {
    47  	urlStr := s.HTTPSuite.URL(path)
    48  	URL, err := url.Parse(urlStr)
    49  	c.Assert(err, jc.ErrorIsNil)
    50  	return URL
    51  }
    52  
    53  func (s *DownloaderSuite) testStart(c *gc.C, hostnameVerification utils.SSLHostnameVerification) {
    54  	tmp := c.MkDir()
    55  	gitjujutesting.Server.Response(200, nil, []byte("archive"))
    56  	dlr := downloader.New(downloader.NewArgs{
    57  		HostnameVerification: hostnameVerification,
    58  	})
    59  	dl := dlr.Start(downloader.Request{
    60  		URL:       s.URL(c, "/archive.tgz"),
    61  		TargetDir: tmp,
    62  	})
    63  	status := <-dl.Done()
    64  	c.Assert(status.Err, gc.IsNil)
    65  	dir, _ := filepath.Split(status.Filename)
    66  	c.Assert(filepath.Clean(dir), gc.Equals, tmp)
    67  	assertFileContents(c, status.Filename, "archive")
    68  }
    69  
    70  func (s *DownloaderSuite) TestDownloadWithoutDisablingSSLHostnameVerification(c *gc.C) {
    71  	s.testStart(c, utils.VerifySSLHostnames)
    72  }
    73  
    74  func (s *DownloaderSuite) TestDownloadWithDisablingSSLHostnameVerification(c *gc.C) {
    75  	s.testStart(c, utils.NoVerifySSLHostnames)
    76  }
    77  
    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{
    83  		URL:       s.URL(c, "/archive.tgz"),
    84  		TargetDir: tmp,
    85  	})
    86  	c.Assert(err, jc.ErrorIsNil)
    87  	dir, _ := filepath.Split(filename)
    88  	c.Assert(filepath.Clean(dir), gc.Equals, tmp)
    89  	assertFileContents(c, filename, "archive")
    90  }