github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/cmd/juju/unset_test.go (about) 1 // Copyright 2012, 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package main 5 6 import ( 7 "bytes" 8 "strings" 9 10 "github.com/juju/cmd" 11 jc "github.com/juju/testing/checkers" 12 gc "gopkg.in/check.v1" 13 "gopkg.in/juju/charm.v4" 14 15 "github.com/juju/juju/cmd/envcmd" 16 "github.com/juju/juju/juju/testing" 17 "github.com/juju/juju/state" 18 coretesting "github.com/juju/juju/testing" 19 ) 20 21 type UnsetSuite struct { 22 testing.JujuConnSuite 23 svc *state.Service 24 dir string 25 } 26 27 var _ = gc.Suite(&UnsetSuite{}) 28 29 func (s *UnsetSuite) SetUpTest(c *gc.C) { 30 s.JujuConnSuite.SetUpTest(c) 31 ch := s.AddTestingCharm(c, "dummy") 32 svc := s.AddTestingService(c, "dummy-service", ch) 33 s.svc = svc 34 s.dir = c.MkDir() 35 setupConfigFile(c, s.dir) 36 } 37 38 func (s *UnsetSuite) TestUnsetOptionOneByOneSuccess(c *gc.C) { 39 // Set options as preparation. 40 assertSetSuccess(c, s.dir, s.svc, []string{ 41 "username=hello", 42 "outlook=hello@world.tld", 43 }, charm.Settings{ 44 "username": "hello", 45 "outlook": "hello@world.tld", 46 }) 47 48 // Unset one by one. 49 assertUnsetSuccess(c, s.dir, s.svc, []string{"username"}, charm.Settings{ 50 "outlook": "hello@world.tld", 51 }) 52 assertUnsetSuccess(c, s.dir, s.svc, []string{"outlook"}, charm.Settings{}) 53 } 54 55 func (s *UnsetSuite) TestBlockUnset(c *gc.C) { 56 // Set options as preparation. 57 assertSetSuccess(c, s.dir, s.svc, []string{ 58 "username=hello", 59 "outlook=hello@world.tld", 60 }, charm.Settings{ 61 "username": "hello", 62 "outlook": "hello@world.tld", 63 }) 64 65 // Block operation 66 s.AssertConfigParameterUpdated(c, "block-all-changes", true) 67 68 ctx := coretesting.ContextForDir(c, s.dir) 69 code := cmd.Main(envcmd.Wrap(&UnsetCommand{}), ctx, append([]string{"dummy-service"}, []string{"username"}...)) 70 c.Check(code, gc.Equals, 1) 71 // msg is logged 72 stripped := strings.Replace(c.GetTestLog(), "\n", "", -1) 73 c.Check(stripped, gc.Matches, ".*To unblock changes.*") 74 } 75 76 func (s *UnsetSuite) TestUnsetOptionMultipleAtOnceSuccess(c *gc.C) { 77 // Set options as preparation. 78 assertSetSuccess(c, s.dir, s.svc, []string{ 79 "username=hello", 80 "outlook=hello@world.tld", 81 }, charm.Settings{ 82 "username": "hello", 83 "outlook": "hello@world.tld", 84 }) 85 86 // Unset multiple options at once. 87 assertUnsetSuccess(c, s.dir, s.svc, []string{"username", "outlook"}, charm.Settings{}) 88 } 89 90 func (s *UnsetSuite) TestUnsetOptionFail(c *gc.C) { 91 assertUnsetFail(c, s.dir, []string{}, "error: no configuration options specified\n") 92 assertUnsetFail(c, s.dir, []string{"invalid"}, "error: unknown option \"invalid\"\n") 93 assertUnsetFail(c, s.dir, []string{"username=bar"}, "error: unknown option \"username=bar\"\n") 94 assertUnsetFail(c, s.dir, []string{ 95 "username", 96 "outlook", 97 "invalid", 98 }, "error: unknown option \"invalid\"\n") 99 } 100 101 // assertUnsetSuccess unsets configuration options and checks the expected settings. 102 func assertUnsetSuccess(c *gc.C, dir string, svc *state.Service, args []string, expect charm.Settings) { 103 ctx := coretesting.ContextForDir(c, dir) 104 code := cmd.Main(envcmd.Wrap(&UnsetCommand{}), ctx, append([]string{"dummy-service"}, args...)) 105 c.Check(code, gc.Equals, 0) 106 settings, err := svc.ConfigSettings() 107 c.Assert(err, jc.ErrorIsNil) 108 c.Assert(settings, gc.DeepEquals, expect) 109 } 110 111 // assertUnsetFail unsets configuration options and checks the expected error. 112 func assertUnsetFail(c *gc.C, dir string, args []string, err string) { 113 ctx := coretesting.ContextForDir(c, dir) 114 code := cmd.Main(envcmd.Wrap(&UnsetCommand{}), ctx, append([]string{"dummy-service"}, args...)) 115 c.Check(code, gc.Not(gc.Equals), 0) 116 c.Assert(ctx.Stderr.(*bytes.Buffer).String(), gc.Matches, err) 117 }