github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/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 type stubDownload struct { 44 *internalStub 45 internal.ContentSource 46 47 ReturnResolve []string 48 } 49 50 func (s *stubDownload) Close() error { 51 s.Stub.AddCall("Close") 52 if err := s.Stub.NextErr(); err != nil { 53 return errors.Trace(err) 54 } 55 56 return nil 57 } 58 59 func (s *stubDownload) Initialize() (internal.DownloadDirectory, error) { 60 s.Stub.AddCall("Initialize") 61 if err := s.Stub.NextErr(); err != nil { 62 return nil, errors.Trace(err) 63 } 64 65 return s, nil 66 } 67 68 func (s *stubDownload) Write(source internal.ContentSource) error { 69 s.Stub.AddCall("Write", source) 70 if err := s.Stub.NextErr(); err != nil { 71 return errors.Trace(err) 72 } 73 74 return nil 75 } 76 77 func (s *stubDownload) Resolve(path ...string) string { 78 s.Stub.AddCall("Resolve", path) 79 s.Stub.NextErr() // Pop one off. 80 81 resolved := s.ReturnResolve[0] 82 s.ReturnResolve = s.ReturnResolve[1:] 83 return resolved 84 }