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