github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/cmd/juju/controller/listcontrollers_test.go (about) 1 // Copyright 2015,2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package controller_test 5 6 import ( 7 "encoding/json" 8 "fmt" 9 10 "github.com/juju/cmd" 11 "github.com/juju/errors" 12 jc "github.com/juju/testing/checkers" 13 gc "gopkg.in/check.v1" 14 15 "github.com/juju/juju/cmd/juju/controller" 16 "github.com/juju/juju/jujuclient/jujuclienttesting" 17 "github.com/juju/juju/testing" 18 ) 19 20 type ListControllersSuite struct { 21 baseControllerSuite 22 } 23 24 var _ = gc.Suite(&ListControllersSuite{}) 25 26 func (s *ListControllersSuite) TestListControllersEmptyStore(c *gc.C) { 27 s.expectedOutput = ` 28 CONTROLLER MODEL USER SERVER 29 30 `[1:] 31 32 s.store = jujuclienttesting.NewMemStore() 33 s.assertListControllers(c) 34 } 35 36 func (s *ListControllersSuite) TestListControllers(c *gc.C) { 37 s.expectedOutput = ` 38 CONTROLLER MODEL USER SERVER 39 local.aws-test - - this-is-aws-test-of-many-api-endpoints 40 local.mallards* my-model admin@local this-is-another-of-many-api-endpoints 41 local.mark-test-prodstack - - this-is-one-of-many-api-endpoints 42 43 `[1:] 44 45 s.createTestClientStore(c) 46 s.assertListControllers(c) 47 } 48 49 func (s *ListControllersSuite) TestListControllersYaml(c *gc.C) { 50 s.expectedOutput = ` 51 controllers: 52 local.aws-test: 53 recent-server: this-is-aws-test-of-many-api-endpoints 54 uuid: this-is-the-aws-test-uuid 55 api-endpoints: [this-is-aws-test-of-many-api-endpoints] 56 ca-cert: this-is-aws-test-ca-cert 57 local.mallards: 58 current-model: my-model 59 user: admin@local 60 recent-server: this-is-another-of-many-api-endpoints 61 uuid: this-is-another-uuid 62 api-endpoints: [this-is-another-of-many-api-endpoints, this-is-one-more-of-many-api-endpoints] 63 ca-cert: this-is-another-ca-cert 64 local.mark-test-prodstack: 65 recent-server: this-is-one-of-many-api-endpoints 66 uuid: this-is-a-uuid 67 api-endpoints: [this-is-one-of-many-api-endpoints] 68 ca-cert: this-is-a-ca-cert 69 current-controller: local.mallards 70 `[1:] 71 72 s.createTestClientStore(c) 73 s.assertListControllers(c, "--format", "yaml") 74 } 75 76 func (s *ListControllersSuite) TestListControllersJson(c *gc.C) { 77 s.expectedOutput = "" 78 s.createTestClientStore(c) 79 jsonOut := s.assertListControllers(c, "--format", "json") 80 var result controller.ControllerSet 81 err := json.Unmarshal([]byte(jsonOut), &result) 82 c.Assert(err, jc.ErrorIsNil) 83 c.Assert(result, jc.DeepEquals, controller.ControllerSet{ 84 Controllers: map[string]controller.ControllerItem{ 85 "local.aws-test": { 86 ControllerUUID: "this-is-the-aws-test-uuid", 87 Server: "this-is-aws-test-of-many-api-endpoints", 88 APIEndpoints: []string{"this-is-aws-test-of-many-api-endpoints"}, 89 CACert: "this-is-aws-test-ca-cert", 90 }, 91 "local.mallards": { 92 ControllerUUID: "this-is-another-uuid", 93 ModelName: "my-model", 94 User: "admin@local", 95 Server: "this-is-another-of-many-api-endpoints", 96 APIEndpoints: []string{"this-is-another-of-many-api-endpoints", "this-is-one-more-of-many-api-endpoints"}, 97 CACert: "this-is-another-ca-cert", 98 }, 99 "local.mark-test-prodstack": { 100 ControllerUUID: "this-is-a-uuid", 101 Server: "this-is-one-of-many-api-endpoints", 102 APIEndpoints: []string{"this-is-one-of-many-api-endpoints"}, 103 CACert: "this-is-a-ca-cert", 104 }, 105 }, 106 CurrentController: "local.mallards", 107 }) 108 } 109 110 func (s *ListControllersSuite) TestListControllersReadFromStoreErr(c *gc.C) { 111 msg := "fail getting all controllers" 112 errStore := jujuclienttesting.NewStubStore() 113 errStore.SetErrors(errors.New(msg)) 114 s.store = errStore 115 s.expectedErr = fmt.Sprintf("failed to list controllers: %v", msg) 116 s.assertListControllersFailed(c) 117 errStore.CheckCallNames(c, "AllControllers") 118 } 119 120 func (s *ListControllersSuite) TestListControllersUnrecognizedArg(c *gc.C) { 121 s.createTestClientStore(c) 122 s.expectedErr = `unrecognized args: \["whoops"\]` 123 s.assertListControllersFailed(c, "whoops") 124 } 125 126 func (s *ListControllersSuite) TestListControllersUnrecognizedFlag(c *gc.C) { 127 s.createTestClientStore(c) 128 s.expectedErr = `flag provided but not defined: -m` 129 s.assertListControllersFailed(c, "-m", "my.world") 130 } 131 132 func (s *ListControllersSuite) TestListControllersUnrecognizedOptionFlag(c *gc.C) { 133 s.createTestClientStore(c) 134 s.expectedErr = `flag provided but not defined: --model` 135 s.assertListControllersFailed(c, "--model", "still.my.world") 136 } 137 138 func (s *ListControllersSuite) runListControllers(c *gc.C, args ...string) (*cmd.Context, error) { 139 return testing.RunCommand(c, controller.NewListControllersCommandForTest(s.store), args...) 140 } 141 142 func (s *ListControllersSuite) assertListControllersFailed(c *gc.C, args ...string) { 143 _, err := s.runListControllers(c, args...) 144 c.Assert(err, gc.ErrorMatches, s.expectedErr) 145 } 146 147 func (s *ListControllersSuite) assertListControllers(c *gc.C, args ...string) string { 148 context, err := s.runListControllers(c, args...) 149 c.Assert(err, jc.ErrorIsNil) 150 output := testing.Stdout(context) 151 if s.expectedOutput != "" { 152 c.Assert(output, gc.Equals, s.expectedOutput) 153 } 154 return output 155 }