github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/cmd/juju/service/unset_test.go (about) 1 // Copyright 2012-2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package service_test 5 6 import ( 7 "bytes" 8 "strings" 9 10 "github.com/juju/cmd" 11 gc "gopkg.in/check.v1" 12 13 "github.com/juju/juju/apiserver/common" 14 "github.com/juju/juju/cmd/envcmd" 15 "github.com/juju/juju/cmd/juju/service" 16 coretesting "github.com/juju/juju/testing" 17 ) 18 19 type UnsetSuite struct { 20 coretesting.FakeJujuHomeSuite 21 dir string 22 fake *fakeServiceAPI 23 } 24 25 var _ = gc.Suite(&UnsetSuite{}) 26 27 func (s *UnsetSuite) SetUpTest(c *gc.C) { 28 s.FakeJujuHomeSuite.SetUpTest(c) 29 s.fake = &fakeServiceAPI{servName: "dummy-service", values: map[string]interface{}{ 30 "username": "hello", 31 "outlook": "hello@world.tld", 32 }} 33 34 s.dir = c.MkDir() 35 setupConfigFile(c, s.dir) 36 } 37 38 func (s *UnsetSuite) TestUnsetCommandInit(c *gc.C) { 39 // missing args 40 err := coretesting.InitCommand(&service.UnsetCommand{}, []string{}) 41 c.Assert(err, gc.ErrorMatches, "no service name specified") 42 } 43 44 func (s *UnsetSuite) TestUnsetOptionOneByOneSuccess(c *gc.C) { 45 // Unset one by one. 46 s.assertUnsetSuccess(c, s.dir, []string{"username"}, map[string]interface{}{ 47 "outlook": "hello@world.tld", 48 }) 49 s.assertUnsetSuccess(c, s.dir, []string{"outlook"}, make(map[string]interface{})) 50 } 51 52 func (s *UnsetSuite) TestBlockUnset(c *gc.C) { 53 // Block operation 54 s.fake.err = common.ErrOperationBlocked("TestBlockUnset") 55 ctx := coretesting.ContextForDir(c, s.dir) 56 code := cmd.Main(envcmd.Wrap(service.NewUnsetCommand(s.fake)), ctx, []string{ 57 "dummy-service", 58 "username"}) 59 c.Check(code, gc.Equals, 1) 60 61 // msg is logged 62 stripped := strings.Replace(c.GetTestLog(), "\n", "", -1) 63 c.Check(stripped, gc.Matches, ".*TestBlockUnset.*") 64 } 65 66 func (s *UnsetSuite) TestUnsetOptionMultipleAtOnceSuccess(c *gc.C) { 67 // Unset multiple options at once. 68 s.assertUnsetSuccess(c, s.dir, []string{"username", "outlook"}, make(map[string]interface{})) 69 } 70 71 func (s *UnsetSuite) TestUnsetOptionFail(c *gc.C) { 72 s.assertUnsetFail(c, s.dir, []string{}, "error: no configuration options specified\n") 73 s.assertUnsetFail(c, s.dir, []string{"invalid"}, "error: unknown option \"invalid\"\n") 74 s.assertUnsetFail(c, s.dir, []string{"username=bar"}, "error: unknown option \"username=bar\"\n") 75 s.assertUnsetFail(c, s.dir, []string{ 76 "username", 77 "outlook", 78 "invalid", 79 }, "error: unknown option \"invalid\"\n") 80 } 81 82 // assertUnsetSuccess unsets configuration options and checks the expected settings. 83 func (s *UnsetSuite) assertUnsetSuccess(c *gc.C, dir string, args []string, expect map[string]interface{}) { 84 ctx := coretesting.ContextForDir(c, dir) 85 code := cmd.Main(envcmd.Wrap(service.NewUnsetCommand(s.fake)), ctx, append([]string{"dummy-service"}, args...)) 86 c.Check(code, gc.Equals, 0) 87 c.Assert(s.fake.values, gc.DeepEquals, expect) 88 } 89 90 // assertUnsetFail unsets configuration options and checks the expected error. 91 func (s *UnsetSuite) assertUnsetFail(c *gc.C, dir string, args []string, err string) { 92 ctx := coretesting.ContextForDir(c, dir) 93 code := cmd.Main(envcmd.Wrap(service.NewUnsetCommand(s.fake)), ctx, append([]string{"dummy-service"}, args...)) 94 c.Check(code, gc.Not(gc.Equals), 0) 95 c.Assert(ctx.Stderr.(*bytes.Buffer).String(), gc.Matches, err) 96 }