github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/cmd/juju/controller/unregister_test.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package controller_test 5 6 import ( 7 "bytes" 8 "time" 9 10 "github.com/juju/cmd" 11 "github.com/juju/errors" 12 jt "github.com/juju/testing" 13 jc "github.com/juju/testing/checkers" 14 gc "gopkg.in/check.v1" 15 16 "github.com/juju/juju/cmd/juju/controller" 17 cmdtesting "github.com/juju/juju/cmd/testing" 18 "github.com/juju/juju/jujuclient" 19 "github.com/juju/juju/testing" 20 ) 21 22 // Test that the expected methods are called during unregister: 23 // ControllerByName and RemoveController. 24 type fakeStore struct { 25 jujuclient.ClientStore 26 lookupName string 27 removedName string 28 } 29 30 func (s *fakeStore) ControllerByName(name string) (*jujuclient.ControllerDetails, error) { 31 s.lookupName = name 32 if name != "fake1" { 33 return nil, errors.NotFoundf("controller %s", name) 34 } 35 return &jujuclient.ControllerDetails{}, nil 36 } 37 38 func (s *fakeStore) RemoveController(name string) error { 39 // Removing a controller that doesn't exist also returns nil, 40 // so no need to check. 41 s.removedName = name 42 return nil 43 } 44 45 type UnregisterSuite struct { 46 jt.IsolationSuite 47 store *fakeStore 48 } 49 50 var _ = gc.Suite(&UnregisterSuite{}) 51 52 func (s *UnregisterSuite) SetUpTest(c *gc.C) { 53 s.IsolationSuite.SetUpTest(c) 54 s.store = &fakeStore{} 55 } 56 57 func (s *UnregisterSuite) TestInit(c *gc.C) { 58 unregisterCommand := controller.NewUnregisterCommand(s.store) 59 60 err := testing.InitCommand(unregisterCommand, []string{}) 61 c.Assert(err, gc.ErrorMatches, "controller name must be specified") 62 63 err = testing.InitCommand(unregisterCommand, []string{"foo", "bar"}) 64 c.Assert(err, gc.ErrorMatches, `unrecognized args: \["bar"\]`) 65 } 66 67 func (s *UnregisterSuite) TestUnregisterUnknownController(c *gc.C) { 68 command := controller.NewUnregisterCommand(s.store) 69 _, err := testing.RunCommand(c, command, "fake3") 70 71 c.Assert(err, jc.Satisfies, errors.IsNotFound) 72 c.Assert(err, gc.ErrorMatches, "controller fake3 not found") 73 c.Check(s.store.lookupName, gc.Equals, "fake3") 74 } 75 76 func (s *UnregisterSuite) TestUnregisterController(c *gc.C) { 77 command := controller.NewUnregisterCommand(s.store) 78 _, err := testing.RunCommand(c, command, "fake1", "-y") 79 80 c.Assert(err, jc.ErrorIsNil) 81 c.Check(s.store.lookupName, gc.Equals, "fake1") 82 c.Check(s.store.removedName, gc.Equals, "fake1") 83 } 84 85 var unregisterMsg = ` 86 This command will remove connection information for controller "fake1". 87 Doing so will prevent you from accessing this controller until 88 you register it again. 89 90 Continue [y/N]?`[1:] 91 92 func (s *UnregisterSuite) unregisterCommandAborts(c *gc.C, answer string) { 93 var stdin, stdout bytes.Buffer 94 ctx, err := cmd.DefaultContext() 95 c.Assert(err, jc.ErrorIsNil) 96 ctx.Stdout = &stdout 97 ctx.Stdin = &stdin 98 99 // Ensure confirmation is requested if "-y" is not specified. 100 stdin.WriteString(answer) 101 _, errc := cmdtesting.RunCommand(ctx, controller.NewUnregisterCommand(s.store), "fake1") 102 select { 103 case err, ok := <-errc: 104 c.Assert(ok, jc.IsTrue) 105 c.Check(err, gc.ErrorMatches, "unregistering controller: aborted") 106 case <-time.After(testing.LongWait): 107 c.Fatalf("command took too long") 108 } 109 c.Check(testing.Stdout(ctx), gc.Equals, unregisterMsg) 110 c.Check(s.store.lookupName, gc.Equals, "fake1") 111 c.Check(s.store.removedName, gc.Equals, "") 112 } 113 114 func (s *UnregisterSuite) TestUnregisterCommandAbortsOnN(c *gc.C) { 115 s.unregisterCommandAborts(c, "n") 116 } 117 118 func (s *UnregisterSuite) TestUnregisterCommandAbortsOnNotY(c *gc.C) { 119 s.unregisterCommandAborts(c, "foo") 120 } 121 122 func (s *UnregisterSuite) unregisterCommandConfirms(c *gc.C, answer string) { 123 var stdin, stdout bytes.Buffer 124 ctx, err := cmd.DefaultContext() 125 c.Assert(err, jc.ErrorIsNil) 126 ctx.Stdout = &stdout 127 ctx.Stdin = &stdin 128 129 stdin.Reset() 130 stdout.Reset() 131 stdin.WriteString(answer) 132 _, errc := cmdtesting.RunCommand(ctx, controller.NewUnregisterCommand(s.store), "fake1") 133 select { 134 case err, ok := <-errc: 135 c.Assert(ok, jc.IsTrue) 136 c.Check(err, jc.ErrorIsNil) 137 case <-time.After(testing.LongWait): 138 c.Fatalf("command took too long") 139 } 140 c.Check(s.store.lookupName, gc.Equals, "fake1") 141 c.Check(s.store.removedName, gc.Equals, "fake1") 142 } 143 144 func (s *UnregisterSuite) TestUnregisterCommandConfirmsOnY(c *gc.C) { 145 s.unregisterCommandConfirms(c, "y") 146 }