github.com/rogpeppe/juju@v0.0.0-20140613142852-6337964b789e/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 9 "github.com/juju/charm" 10 "github.com/juju/cmd" 11 gc "launchpad.net/gocheck" 12 13 "github.com/juju/juju/cmd/envcmd" 14 "github.com/juju/juju/juju/testing" 15 "github.com/juju/juju/state" 16 coretesting "github.com/juju/juju/testing" 17 ) 18 19 type UnsetSuite struct { 20 testing.JujuConnSuite 21 svc *state.Service 22 dir string 23 } 24 25 var _ = gc.Suite(&UnsetSuite{}) 26 27 func (s *UnsetSuite) SetUpTest(c *gc.C) { 28 s.JujuConnSuite.SetUpTest(c) 29 ch := s.AddTestingCharm(c, "dummy") 30 svc := s.AddTestingService(c, "dummy-service", ch) 31 s.svc = svc 32 s.dir = c.MkDir() 33 setupConfigFile(c, s.dir) 34 } 35 36 func (s *UnsetSuite) TestUnsetOptionOneByOneSuccess(c *gc.C) { 37 // Set options as preparation. 38 assertSetSuccess(c, s.dir, s.svc, []string{ 39 "username=hello", 40 "outlook=hello@world.tld", 41 }, charm.Settings{ 42 "username": "hello", 43 "outlook": "hello@world.tld", 44 }) 45 46 // Unset one by one. 47 assertUnsetSuccess(c, s.dir, s.svc, []string{"username"}, charm.Settings{ 48 "outlook": "hello@world.tld", 49 }) 50 assertUnsetSuccess(c, s.dir, s.svc, []string{"outlook"}, charm.Settings{}) 51 } 52 53 func (s *UnsetSuite) TestUnsetOptionMultipleAtOnceSuccess(c *gc.C) { 54 // Set options as preparation. 55 assertSetSuccess(c, s.dir, s.svc, []string{ 56 "username=hello", 57 "outlook=hello@world.tld", 58 }, charm.Settings{ 59 "username": "hello", 60 "outlook": "hello@world.tld", 61 }) 62 63 // Unset multiple options at once. 64 assertUnsetSuccess(c, s.dir, s.svc, []string{"username", "outlook"}, charm.Settings{}) 65 } 66 67 func (s *UnsetSuite) TestUnsetOptionFail(c *gc.C) { 68 assertUnsetFail(c, s.dir, []string{}, "error: no configuration options specified\n") 69 assertUnsetFail(c, s.dir, []string{"invalid"}, "error: unknown option \"invalid\"\n") 70 assertUnsetFail(c, s.dir, []string{"username=bar"}, "error: unknown option \"username=bar\"\n") 71 assertUnsetFail(c, s.dir, []string{ 72 "username", 73 "outlook", 74 "invalid", 75 }, "error: unknown option \"invalid\"\n") 76 } 77 78 // assertUnsetSuccess unsets configuration options and checks the expected settings. 79 func assertUnsetSuccess(c *gc.C, dir string, svc *state.Service, args []string, expect charm.Settings) { 80 ctx := coretesting.ContextForDir(c, dir) 81 code := cmd.Main(envcmd.Wrap(&UnsetCommand{}), ctx, append([]string{"dummy-service"}, args...)) 82 c.Check(code, gc.Equals, 0) 83 settings, err := svc.ConfigSettings() 84 c.Assert(err, gc.IsNil) 85 c.Assert(settings, gc.DeepEquals, expect) 86 } 87 88 // assertUnsetFail unsets configuration options and checks the expected error. 89 func assertUnsetFail(c *gc.C, dir string, args []string, err string) { 90 ctx := coretesting.ContextForDir(c, dir) 91 code := cmd.Main(envcmd.Wrap(&UnsetCommand{}), ctx, append([]string{"dummy-service"}, args...)) 92 c.Check(code, gc.Not(gc.Equals), 0) 93 c.Assert(ctx.Stderr.(*bytes.Buffer).String(), gc.Matches, err) 94 }