github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/cmd/modelcmd/files_test.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package modelcmd_test 5 6 import ( 7 "io/ioutil" 8 "os" 9 10 jc "github.com/juju/testing/checkers" 11 gc "gopkg.in/check.v1" 12 13 "github.com/juju/juju/cmd/modelcmd" 14 "github.com/juju/juju/testing" 15 ) 16 17 type filesSuite struct { 18 testing.FakeJujuXDGDataHomeSuite 19 } 20 21 var _ = gc.Suite(&filesSuite{}) 22 23 func (s *filesSuite) assertCurrentController(c *gc.C, controllerName string) { 24 current, err := modelcmd.ReadCurrentController() 25 c.Assert(err, jc.ErrorIsNil) 26 c.Assert(current, gc.Equals, controllerName) 27 } 28 29 func (s *filesSuite) TestReadCurrentControllerUnset(c *gc.C) { 30 s.assertCurrentController(c, "") 31 } 32 33 func (s *filesSuite) TestReadCurrentControllerSet(c *gc.C) { 34 err := modelcmd.WriteCurrentController("fubar") 35 c.Assert(err, jc.ErrorIsNil) 36 s.assertCurrentController(c, "fubar") 37 } 38 39 func (s *filesSuite) TestWriteControllerAddsNewline(c *gc.C) { 40 err := modelcmd.WriteCurrentController("fubar") 41 c.Assert(err, jc.ErrorIsNil) 42 current, err := ioutil.ReadFile(modelcmd.GetCurrentControllerFilePath()) 43 c.Assert(err, jc.ErrorIsNil) 44 c.Assert(string(current), gc.Equals, "fubar\n") 45 } 46 47 func (*filesSuite) TestErrorWritingCurrentController(c *gc.C) { 48 // Can't write a file over a directory. 49 os.MkdirAll(modelcmd.GetCurrentControllerFilePath(), 0777) 50 err := modelcmd.WriteCurrentController("fubar") 51 c.Assert(err, gc.ErrorMatches, "unable to write to the controller file: .*") 52 } 53 54 func (s *filesSuite) TestWriteCurrentControllerExistingController(c *gc.C) { 55 err := modelcmd.WriteCurrentController("fubar") 56 c.Assert(err, jc.ErrorIsNil) 57 err = modelcmd.WriteCurrentController("new-sys") 58 c.Assert(err, jc.ErrorIsNil) 59 s.assertCurrentController(c, "new-sys") 60 }