github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/resource/context/internal/download_test.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package internal_test 5 6 import ( 7 "github.com/juju/errors" 8 "github.com/juju/testing" 9 jc "github.com/juju/testing/checkers" 10 gc "gopkg.in/check.v1" 11 12 "github.com/juju/juju/resource/context/internal" 13 ) 14 15 var _ = gc.Suite(&DownloadSuite{}) 16 17 type DownloadSuite struct { 18 testing.IsolationSuite 19 20 stub *internalStub 21 } 22 23 func (s *DownloadSuite) SetUpTest(c *gc.C) { 24 s.IsolationSuite.SetUpTest(c) 25 26 s.stub = newInternalStub() 27 } 28 29 func (s *DownloadSuite) TestDownload(c *gc.C) { 30 stub := &stubDownload{ 31 internalStub: s.stub, 32 } 33 target := stub 34 remote := stub 35 36 err := internal.Download(target, remote) 37 c.Assert(err, jc.ErrorIsNil) 38 39 s.stub.CheckCallNames(c, "Initialize", "Write") 40 s.stub.CheckCall(c, 1, "Write", remote) 41 } 42 43 func (s *DownloadSuite) TestDownloadIndirectOkay(c *gc.C) { 44 stub := &stubDownload{ 45 internalStub: s.stub, 46 } 47 stub.ReturnNewTempDirSpec = stub 48 stub.ReturnResolve = []string{ 49 "/tmp/xyz/eggs", 50 "/var/lib/juju/agents/unit-spam-1/resources/eggs", 51 } 52 target := stub 53 remote := stub 54 deps := stub 55 56 err := internal.DownloadIndirect(target, remote, deps) 57 c.Assert(err, jc.ErrorIsNil) 58 59 s.stub.CheckCallNames(c, 60 "NewTempDirSpec", 61 "DownloadDirect", 62 "Initialize", 63 "Resolve", 64 "Resolve", 65 "ReplaceDirectory", 66 "CloseAndLog", 67 ) 68 s.stub.CheckCall(c, 1, "DownloadDirect", stub, remote) 69 } 70 71 func (s *DownloadSuite) TestDownloadIndirectTempDirFailure(c *gc.C) { 72 stub := &stubDownload{ 73 internalStub: s.stub, 74 } 75 stub.ReturnNewTempDirSpec = stub 76 failure := errors.New("<failure>") 77 stub.SetErrors(failure) 78 target := stub 79 remote := stub 80 deps := stub 81 82 err := internal.DownloadIndirect(target, remote, deps) 83 84 c.Check(errors.Cause(err), gc.Equals, failure) 85 s.stub.CheckCallNames(c, 86 "NewTempDirSpec", 87 "CloseAndLog", 88 ) 89 } 90 91 type stubDownload struct { 92 *internalStub 93 internal.ContentSource 94 95 ReturnResolve []string 96 } 97 98 func (s *stubDownload) Close() error { 99 s.Stub.AddCall("Close") 100 if err := s.Stub.NextErr(); err != nil { 101 return errors.Trace(err) 102 } 103 104 return nil 105 } 106 107 func (s *stubDownload) Initialize() (internal.DownloadDirectory, error) { 108 s.Stub.AddCall("Initialize") 109 if err := s.Stub.NextErr(); err != nil { 110 return nil, errors.Trace(err) 111 } 112 113 return s, nil 114 } 115 116 func (s *stubDownload) Write(source internal.ContentSource) error { 117 s.Stub.AddCall("Write", source) 118 if err := s.Stub.NextErr(); err != nil { 119 return errors.Trace(err) 120 } 121 122 return nil 123 } 124 125 func (s *stubDownload) Resolve(path ...string) string { 126 s.Stub.AddCall("Resolve", path) 127 s.Stub.NextErr() // Pop one off. 128 129 resolved := s.ReturnResolve[0] 130 s.ReturnResolve = s.ReturnResolve[1:] 131 return resolved 132 }