launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/downloader/downloader_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 "os" 9 "path/filepath" 10 stdtesting "testing" 11 "time" 12 13 gc "launchpad.net/gocheck" 14 15 "launchpad.net/juju-core/downloader" 16 "launchpad.net/juju-core/testing" 17 "launchpad.net/juju-core/testing/testbase" 18 ) 19 20 type suite struct { 21 testbase.LoggingSuite 22 testing.HTTPSuite 23 } 24 25 func (s *suite) SetUpSuite(c *gc.C) { 26 s.LoggingSuite.SetUpSuite(c) 27 s.HTTPSuite.SetUpSuite(c) 28 } 29 30 func (s *suite) TearDownSuite(c *gc.C) { 31 s.HTTPSuite.TearDownSuite(c) 32 s.LoggingSuite.TearDownSuite(c) 33 } 34 35 func (s *suite) SetUpTest(c *gc.C) { 36 s.LoggingSuite.SetUpTest(c) 37 s.HTTPSuite.SetUpTest(c) 38 } 39 40 func (s *suite) TearDownTest(c *gc.C) { 41 s.HTTPSuite.TearDownTest(c) 42 s.LoggingSuite.TearDownTest(c) 43 } 44 45 var _ = gc.Suite(&suite{}) 46 47 func Test(t *stdtesting.T) { 48 gc.TestingT(t) 49 } 50 51 func (s *suite) testDownload(c *gc.C, disableSSLHostnameVerification bool) { 52 tmp := c.MkDir() 53 testing.Server.Response(200, nil, []byte("archive")) 54 d := downloader.New(s.URL("/archive.tgz"), tmp, disableSSLHostnameVerification) 55 status := <-d.Done() 56 c.Assert(status.Err, gc.IsNil) 57 c.Assert(status.File, gc.NotNil) 58 defer os.Remove(status.File.Name()) 59 defer status.File.Close() 60 61 dir, _ := filepath.Split(status.File.Name()) 62 c.Assert(filepath.Clean(dir), gc.Equals, tmp) 63 assertFileContents(c, status.File, "archive") 64 } 65 66 func (s *suite) TestDownloadWithoutDisablingSSLHostnameVerification(c *gc.C) { 67 s.testDownload(c, false) 68 } 69 70 func (s *suite) TestDownloadWithDisablingSSLHostnameVerification(c *gc.C) { 71 s.testDownload(c, true) 72 } 73 74 func (s *suite) TestDownloadError(c *gc.C) { 75 testing.Server.Response(404, nil, nil) 76 d := downloader.New(s.URL("/archive.tgz"), c.MkDir(), false) 77 status := <-d.Done() 78 c.Assert(status.File, gc.IsNil) 79 c.Assert(status.Err, gc.ErrorMatches, `cannot download ".*": bad http response: 404 Not Found`) 80 } 81 82 func (s *suite) TestStopDownload(c *gc.C) { 83 tmp := c.MkDir() 84 d := downloader.New(s.URL("/x.tgz"), tmp, false) 85 d.Stop() 86 select { 87 case status := <-d.Done(): 88 c.Fatalf("received status %#v after stop", status) 89 case <-time.After(testing.ShortWait): 90 } 91 infos, err := ioutil.ReadDir(tmp) 92 c.Assert(err, gc.IsNil) 93 c.Assert(infos, gc.HasLen, 0) 94 } 95 96 func assertFileContents(c *gc.C, f *os.File, expect string) { 97 got, err := ioutil.ReadAll(f) 98 c.Assert(err, gc.IsNil) 99 if !c.Check(string(got), gc.Equals, expect) { 100 info, err := f.Stat() 101 c.Assert(err, gc.IsNil) 102 c.Logf("info %#v", info) 103 } 104 }