github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/cmd/jujud/agent/agentconf_test.go (about) 1 // Copyright 2014 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package agent 5 6 import ( 7 "github.com/juju/errors" 8 jc "github.com/juju/testing/checkers" 9 gc "gopkg.in/check.v1" 10 11 "github.com/juju/juju/agent" 12 coretesting "github.com/juju/juju/testing" 13 ) 14 15 var _ = gc.Suite(&agentConfSuite{}) 16 17 type agentConfSuite struct { 18 coretesting.BaseSuite 19 } 20 21 func (s *agentConfSuite) TestChangeConfigSuccess(c *gc.C) { 22 mcsw := &mockConfigSetterWriter{} 23 24 conf := agentConf{ 25 dataDir: c.MkDir(), 26 _config: mcsw, 27 } 28 29 err := conf.ChangeConfig(func(agent.ConfigSetter) error { 30 return nil 31 }) 32 33 c.Assert(err, jc.ErrorIsNil) 34 c.Assert(mcsw.WriteCalled, jc.IsTrue) 35 } 36 37 func (s *agentConfSuite) TestChangeConfigMutateFailure(c *gc.C) { 38 mcsw := &mockConfigSetterWriter{} 39 40 conf := agentConf{ 41 dataDir: c.MkDir(), 42 _config: mcsw, 43 } 44 45 err := conf.ChangeConfig(func(agent.ConfigSetter) error { 46 return errors.New("blam") 47 }) 48 49 c.Assert(err, gc.ErrorMatches, "blam") 50 c.Assert(mcsw.WriteCalled, jc.IsFalse) 51 } 52 53 func (s *agentConfSuite) TestChangeConfigWriteFailure(c *gc.C) { 54 mcsw := &mockConfigSetterWriter{ 55 WriteError: errors.New("boom"), 56 } 57 58 conf := agentConf{ 59 dataDir: c.MkDir(), 60 _config: mcsw, 61 } 62 63 err := conf.ChangeConfig(func(agent.ConfigSetter) error { 64 return nil 65 }) 66 67 c.Assert(err, gc.ErrorMatches, "cannot write agent configuration: boom") 68 c.Assert(mcsw.WriteCalled, jc.IsTrue) 69 } 70 71 type mockConfigSetterWriter struct { 72 agent.ConfigSetterWriter 73 WriteError error 74 WriteCalled bool 75 } 76 77 func (c *mockConfigSetterWriter) Write() error { 78 c.WriteCalled = true 79 return c.WriteError 80 }