github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/state/gui_test.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package state_test 5 6 import ( 7 "bytes" 8 9 "github.com/juju/errors" 10 jc "github.com/juju/testing/checkers" 11 "github.com/juju/version" 12 gc "gopkg.in/check.v1" 13 14 "github.com/juju/juju/state" 15 "github.com/juju/juju/state/binarystorage" 16 ) 17 18 type guiVersionSuite struct { 19 ConnSuite 20 } 21 22 var _ = gc.Suite(&guiVersionSuite{}) 23 24 func (s *guiVersionSuite) TestGUISetVersionNotFoundError(c *gc.C) { 25 err := s.State.GUISetVersion(version.MustParse("2.0.1")) 26 c.Assert(err, gc.ErrorMatches, `cannot find "2.0.1" GUI version in the storage: 2.0.1 binary metadata not found`) 27 c.Assert(err, jc.Satisfies, errors.IsNotFound) 28 } 29 30 func (s *guiVersionSuite) TestGUIVersionNotFoundError(c *gc.C) { 31 _, err := s.State.GUIVersion() 32 c.Assert(err, gc.ErrorMatches, "Juju GUI version not found") 33 c.Assert(err, jc.Satisfies, errors.IsNotFound) 34 } 35 36 func (s *guiVersionSuite) TestGUISetVersion(c *gc.C) { 37 vers := s.addArchive(c, "2.1.0") 38 err := s.State.GUISetVersion(vers) 39 c.Assert(err, jc.ErrorIsNil) 40 obtainedVers, err := s.State.GUIVersion() 41 c.Assert(err, jc.ErrorIsNil) 42 c.Assert(obtainedVers, gc.Equals, vers) 43 } 44 45 func (s *guiVersionSuite) TestGUISwitchVersion(c *gc.C) { 46 err := s.State.GUISetVersion(s.addArchive(c, "2.47.0")) 47 c.Assert(err, jc.ErrorIsNil) 48 49 vers := s.addArchive(c, "2.42.0") 50 err = s.State.GUISetVersion(vers) 51 c.Assert(err, jc.ErrorIsNil) 52 53 obtainedVers, err := s.State.GUIVersion() 54 c.Assert(err, jc.ErrorIsNil) 55 c.Assert(obtainedVers, gc.Equals, vers) 56 57 // The collection still only includes one document. 58 s.checkCount(c) 59 } 60 61 // addArchive adds a fake Juju GUI archive to the binary storage. 62 func (s *guiVersionSuite) addArchive(c *gc.C, vers string) version.Number { 63 storage, err := s.State.GUIStorage() 64 c.Assert(err, jc.ErrorIsNil) 65 defer storage.Close() 66 content := "content " + vers 67 err = storage.Add(bytes.NewReader([]byte(content)), binarystorage.Metadata{ 68 SHA256: "hash", 69 Size: int64(len(content)), 70 Version: vers, 71 }) 72 c.Assert(err, jc.ErrorIsNil) 73 return version.MustParse(vers) 74 } 75 76 // checkCount ensures that there is only one document in the GUI settings 77 // mongo collection. 78 func (s *guiVersionSuite) checkCount(c *gc.C) { 79 settings := s.State.MongoSession().DB("juju").C(state.GUISettingsC) 80 count, err := settings.Find(nil).Count() 81 c.Assert(err, jc.ErrorIsNil) 82 c.Assert(count, gc.Equals, 1) 83 }