44package application_test
55
66import (
7+ "fmt"
8+
79 "github.com/golang/mock/gomock"
810 gc "gopkg.in/check.v1"
911 charm "gopkg.in/juju/charm.v6"
@@ -15,6 +17,7 @@ import (
1517 "github.com/juju/juju/apiserver/facades/client/application"
1618 "github.com/juju/juju/apiserver/facades/client/application/mocks"
1719 "github.com/juju/juju/apiserver/params"
20+ "github.com/juju/juju/state"
1821 "github.com/juju/juju/state/storage"
1922 "github.com/juju/juju/testcharms"
2023)
@@ -204,3 +207,66 @@ func (s *CharmStoreSuite) TestAddCharmWithAuthorizationWithInvalidLXDProfileAndF
204207 })
205208 c .Assert (err , gc .IsNil )
206209}
210+
211+ func (s * CharmStoreSuite ) TestAddVersionedCharmWithAuthorization (c * gc.C ) {
212+ ctrl := gomock .NewController (c )
213+ defer ctrl .Finish ()
214+
215+ cacheDir := c .MkDir ()
216+ s .PatchValue (& charmrepo .CacheDir , cacheDir )
217+
218+ url := "cs:~juju-qa/bionic/versioned-0"
219+ charmURL , err := charm .ParseURL (url )
220+ c .Assert (err , gc .IsNil )
221+
222+ mockState := mocks .NewMockState (ctrl )
223+ mockStateCharm := mocks .NewMockStateCharm (ctrl )
224+ mockStorage := mocks .NewMockStorage (ctrl )
225+ mockInterface := mocks .NewMockInterface (ctrl )
226+
227+ charm := testcharms .Repo .CharmArchive (cacheDir , "versioned" )
228+
229+ // inject the mock as a back handed dependency
230+ s .PatchValue (application .NewStateStorage , func (uuid string , session * mgo.Session ) storage.Storage {
231+ return mockStorage
232+ })
233+
234+ sExp := mockState .EXPECT ()
235+ sExp .PrepareStoreCharmUpload (charmURL ).Return (mockStateCharm , nil )
236+ sExp .ModelUUID ().Return ("model-id" )
237+ sExp .MongoSession ().Return (& mgo.Session {})
238+ sExp .UpdateUploadedCharm (charmVersionMatcher {"929903d" }).Return (nil , nil )
239+
240+ cExp := mockStateCharm .EXPECT ()
241+ cExp .IsUploaded ().Return (false )
242+
243+ stExp := mockStorage .EXPECT ()
244+ stExp .Put (gomock .Any (), gomock .Any (), gomock .Any ()).Return (nil )
245+
246+ iExp := mockInterface .EXPECT ()
247+ iExp .Get (charmURL ).Return (charm , nil )
248+
249+ err = application .AddCharmWithAuthorizationAndRepo (mockState , params.AddCharmWithAuthorization {
250+ URL : url ,
251+ }, func () (charmrepo.Interface , error ) {
252+ return mockInterface , nil
253+ })
254+ c .Assert (err , gc .IsNil )
255+ }
256+
257+ type charmVersionMatcher struct {
258+ expVersion string
259+ }
260+
261+ func (m charmVersionMatcher ) Matches (x interface {}) bool {
262+ info , ok := x .(state.CharmInfo )
263+ if ! ok {
264+ return false
265+ }
266+
267+ return info .Version == m .expVersion
268+ }
269+
270+ func (m charmVersionMatcher ) String () string {
271+ return fmt .Sprintf ("state.CharmInfo.Version == %q" , m .expVersion )
272+ }
0 commit comments