github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/resource/cmd/upload_test.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package cmd 5 6 import ( 7 jujucmd "github.com/juju/cmd" 8 "github.com/juju/errors" 9 "github.com/juju/testing" 10 jc "github.com/juju/testing/checkers" 11 gc "gopkg.in/check.v1" 12 ) 13 14 var _ = gc.Suite(&UploadSuite{}) 15 16 type UploadSuite struct { 17 testing.IsolationSuite 18 19 stub *testing.Stub 20 stubDeps *stubUploadDeps 21 } 22 23 func (s *UploadSuite) SetUpTest(c *gc.C) { 24 s.IsolationSuite.SetUpTest(c) 25 26 s.stub = &testing.Stub{} 27 s.stubDeps = &stubUploadDeps{ 28 stub: s.stub, 29 client: &stubAPIClient{stub: s.stub}, 30 } 31 } 32 33 func (*UploadSuite) TestInitEmpty(c *gc.C) { 34 var u UploadCommand 35 36 err := u.Init([]string{}) 37 c.Assert(err, jc.Satisfies, errors.IsBadRequest) 38 } 39 40 func (*UploadSuite) TestInitOneArg(c *gc.C) { 41 var u UploadCommand 42 err := u.Init([]string{"foo"}) 43 c.Assert(err, jc.Satisfies, errors.IsBadRequest) 44 } 45 46 func (*UploadSuite) TestInitJustName(c *gc.C) { 47 var u UploadCommand 48 49 err := u.Init([]string{"foo", "bar"}) 50 c.Assert(err, jc.Satisfies, errors.IsNotValid) 51 } 52 53 func (*UploadSuite) TestInitNoName(c *gc.C) { 54 var u UploadCommand 55 56 err := u.Init([]string{"foo", "=foobar"}) 57 c.Assert(errors.Cause(err), jc.Satisfies, errors.IsNotValid) 58 } 59 60 func (*UploadSuite) TestInitNoPath(c *gc.C) { 61 var u UploadCommand 62 63 err := u.Init([]string{"foo", "foobar="}) 64 c.Assert(errors.Cause(err), jc.Satisfies, errors.IsNotValid) 65 } 66 67 func (*UploadSuite) TestInitGood(c *gc.C) { 68 var u UploadCommand 69 70 err := u.Init([]string{"foo", "bar=baz"}) 71 c.Assert(err, jc.ErrorIsNil) 72 c.Assert(u.resourceFile, gc.DeepEquals, resourceFile{ 73 service: "foo", 74 name: "bar", 75 filename: "baz", 76 }) 77 c.Assert(u.service, gc.Equals, "foo") 78 } 79 80 func (*UploadSuite) TestInitTwoResources(c *gc.C) { 81 var u UploadCommand 82 83 err := u.Init([]string{"foo", "bar=baz", "fizz=buzz"}) 84 c.Assert(err, jc.Satisfies, errors.IsBadRequest) 85 } 86 87 func (s *UploadSuite) TestInfo(c *gc.C) { 88 var command UploadCommand 89 info := command.Info() 90 91 c.Check(info, jc.DeepEquals, &jujucmd.Info{ 92 Name: "attach", 93 Args: "application name=file", 94 Purpose: "upload a file as a resource for an application", 95 Doc: ` 96 This command uploads a file from your local disk to the juju controller to be 97 used as a resource for an application. 98 `, 99 }) 100 } 101 102 func (s *UploadSuite) TestRun(c *gc.C) { 103 file := &stubFile{stub: s.stub} 104 s.stubDeps.file = file 105 u := UploadCommand{ 106 deps: UploadDeps{ 107 NewClient: s.stubDeps.NewClient, 108 OpenResource: s.stubDeps.OpenResource, 109 }, 110 resourceFile: resourceFile{ 111 service: "svc", 112 name: "foo", 113 filename: "bar", 114 }, 115 service: "svc", 116 } 117 118 err := u.Run(nil) 119 c.Assert(err, jc.ErrorIsNil) 120 121 s.stub.CheckCallNames(c, 122 "NewClient", 123 "OpenResource", 124 "Upload", 125 "FileClose", 126 "Close", 127 ) 128 s.stub.CheckCall(c, 1, "OpenResource", "bar") 129 s.stub.CheckCall(c, 2, "Upload", "svc", "foo", "bar", file) 130 } 131 132 type stubUploadDeps struct { 133 stub *testing.Stub 134 file ReadSeekCloser 135 client UploadClient 136 } 137 138 func (s *stubUploadDeps) NewClient(c *UploadCommand) (UploadClient, error) { 139 s.stub.AddCall("NewClient", c) 140 if err := s.stub.NextErr(); err != nil { 141 return nil, errors.Trace(err) 142 } 143 144 return s.client, nil 145 } 146 147 func (s *stubUploadDeps) OpenResource(path string) (ReadSeekCloser, error) { 148 s.stub.AddCall("OpenResource", path) 149 if err := s.stub.NextErr(); err != nil { 150 return nil, errors.Trace(err) 151 } 152 153 return s.file, nil 154 }