github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/cmd/juju/model/defaultscommand_test.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 package model_test 4 5 import ( 6 "strings" 7 8 "github.com/juju/cmd" 9 jc "github.com/juju/testing/checkers" 10 gc "gopkg.in/check.v1" 11 12 "github.com/juju/juju/apiserver/common" 13 "github.com/juju/juju/cmd/juju/model" 14 "github.com/juju/juju/environs/config" 15 "github.com/juju/juju/jujuclient" 16 "github.com/juju/juju/jujuclient/jujuclienttesting" 17 "github.com/juju/juju/testing" 18 ) 19 20 type DefaultsCommandSuite struct { 21 fakeModelDefaultEnvSuite 22 store *jujuclienttesting.MemStore 23 } 24 25 var _ = gc.Suite(&DefaultsCommandSuite{}) 26 27 func (s *DefaultsCommandSuite) SetUpTest(c *gc.C) { 28 s.fakeModelDefaultEnvSuite.SetUpTest(c) 29 s.store = jujuclienttesting.NewMemStore() 30 s.store.CurrentControllerName = "controller" 31 s.store.Controllers["controller"] = jujuclient.ControllerDetails{} 32 } 33 34 func (s *DefaultsCommandSuite) run(c *gc.C, args ...string) (*cmd.Context, error) { 35 command := model.NewDefaultsCommandForTest(s.fake, s.store) 36 return testing.RunCommand(c, command, args...) 37 } 38 39 func (s *DefaultsCommandSuite) TestDefaultsInit(c *gc.C) { 40 for i, test := range []struct { 41 args []string 42 errorMatch string 43 nilErr bool 44 }{ 45 { 46 // Test set 47 // 0 48 args: []string{"special=extra", "special=other"}, 49 errorMatch: `key "special" specified more than once`, 50 }, { 51 // 1 52 args: []string{"agent-version=2.0.0"}, 53 errorMatch: `"agent-version" must be set via "upgrade-juju"`, 54 }, { 55 // 2 56 args: []string{"foo=bar", "baz=eggs"}, 57 nilErr: true, 58 }, { 59 // Test reset 60 // 3 61 args: []string{"--reset"}, 62 errorMatch: "no keys specified", 63 }, { 64 // 4 65 args: []string{"--reset", "something", "weird"}, 66 nilErr: true, 67 }, { 68 // 5 69 args: []string{"--reset", "agent-version"}, 70 errorMatch: `"agent-version" cannot be reset`, 71 }, { 72 // Test get 73 // 6 74 args: nil, 75 nilErr: true, 76 }, { 77 // 7 78 args: []string{"one"}, 79 nilErr: true, 80 }, { 81 // 8 82 args: []string{"one", "two"}, 83 errorMatch: "can only retrieve a single value, or all values", 84 }, 85 } { 86 c.Logf("test %d", i) 87 cmd := model.NewDefaultsCommandForTest(s.fake, s.store) 88 err := testing.InitCommand(cmd, test.args) 89 if test.nilErr { 90 c.Check(err, jc.ErrorIsNil) 91 continue 92 } 93 c.Check(err, gc.ErrorMatches, test.errorMatch) 94 } 95 } 96 97 func (s *DefaultsCommandSuite) TestResetUnknownValueLogs(c *gc.C) { 98 _, err := s.run(c, "--reset", "attr", "weird") 99 c.Assert(err, jc.ErrorIsNil) 100 expected := `key "weird" is not defined in the known model configuration: possible misspelling` 101 c.Check(c.GetTestLog(), jc.Contains, expected) 102 } 103 104 func (s *DefaultsCommandSuite) TestResetAttr(c *gc.C) { 105 _, err := s.run(c, "--reset", "attr", "unknown") 106 c.Assert(err, jc.ErrorIsNil) 107 c.Assert(s.fake.defaults, jc.DeepEquals, config.ModelDefaultAttributes{ 108 "attr2": {Controller: "bar", Default: nil, Regions: []config.RegionDefaultValue{{ 109 Name: "dummy-region", 110 Value: "dummy-value", 111 }}}, 112 }) 113 } 114 115 func (s *DefaultsCommandSuite) TestResetBlockedError(c *gc.C) { 116 s.fake.err = common.OperationBlockedError("TestBlockedError") 117 _, err := s.run(c, "--reset", "attr") 118 testing.AssertOperationWasBlocked(c, err, ".*TestBlockedError.*") 119 } 120 121 func (s *DefaultsCommandSuite) TestSetUnknownValueLogs(c *gc.C) { 122 _, err := s.run(c, "weird=foo") 123 c.Assert(err, jc.ErrorIsNil) 124 expected := `key "weird" is not defined in the known model configuration: possible misspelling` 125 c.Check(c.GetTestLog(), jc.Contains, expected) 126 } 127 128 func (s *DefaultsCommandSuite) TestSet(c *gc.C) { 129 _, err := s.run(c, "special=extra", "attr=baz") 130 c.Assert(err, jc.ErrorIsNil) 131 c.Assert(s.fake.defaults, jc.DeepEquals, config.ModelDefaultAttributes{ 132 "attr": {Controller: "baz", Default: nil, Regions: nil}, 133 "attr2": {Controller: "bar", Default: nil, Regions: []config.RegionDefaultValue{{ 134 Name: "dummy-region", 135 Value: "dummy-value", 136 }}}, 137 "special": {Controller: "extra", Default: nil, Regions: nil}, 138 }) 139 } 140 141 func (s *DefaultsCommandSuite) TestBlockedErrorOnSet(c *gc.C) { 142 s.fake.err = common.OperationBlockedError("TestBlockedError") 143 _, err := s.run(c, "special=extra") 144 testing.AssertOperationWasBlocked(c, err, ".*TestBlockedError.*") 145 } 146 147 func (s *DefaultsCommandSuite) TestGetSingleValue(c *gc.C) { 148 context, err := s.run(c, "attr2") 149 c.Assert(err, jc.ErrorIsNil) 150 151 output := strings.TrimSpace(testing.Stdout(context)) 152 expected := "" + 153 "ATTRIBUTE DEFAULT CONTROLLER\n" + 154 "attr2 - bar\n" + 155 " dummy-region dummy-value -" 156 c.Assert(output, gc.Equals, expected) 157 } 158 159 func (s *DefaultsCommandSuite) TestGetSingleValueJSON(c *gc.C) { 160 context, err := s.run(c, "--format=json", "attr2") 161 c.Assert(err, jc.ErrorIsNil) 162 163 output := strings.TrimSpace(testing.Stdout(context)) 164 c.Assert(output, gc.Equals, 165 `{"attr2":{"controller":"bar","regions":[{"name":"dummy-region","value":"dummy-value"}]}}`) 166 } 167 168 func (s *DefaultsCommandSuite) TestGetAllValuesYAML(c *gc.C) { 169 context, err := s.run(c, "--format=yaml") 170 c.Assert(err, jc.ErrorIsNil) 171 172 output := strings.TrimSpace(testing.Stdout(context)) 173 expected := "" + 174 "attr:\n" + 175 " default: foo\n" + 176 "attr2:\n" + 177 " controller: bar\n" + 178 " regions:\n" + 179 " - name: dummy-region\n" + 180 " value: dummy-value" 181 c.Assert(output, gc.Equals, expected) 182 } 183 184 func (s *DefaultsCommandSuite) TestGetAllValuesJSON(c *gc.C) { 185 context, err := s.run(c, "--format=json") 186 c.Assert(err, jc.ErrorIsNil) 187 188 output := strings.TrimSpace(testing.Stdout(context)) 189 expected := `{"attr":{"default":"foo"},"attr2":{"controller":"bar","regions":[{"name":"dummy-region","value":"dummy-value"}]}}` 190 c.Assert(output, gc.Equals, expected) 191 } 192 193 func (s *DefaultsCommandSuite) TestGetAllValuesTabular(c *gc.C) { 194 context, err := s.run(c) 195 c.Assert(err, jc.ErrorIsNil) 196 197 output := strings.TrimSpace(testing.Stdout(context)) 198 expected := "" + 199 "ATTRIBUTE DEFAULT CONTROLLER\n" + 200 "attr foo -\n" + 201 "attr2 - bar\n" + 202 " dummy-region dummy-value -" 203 c.Assert(output, gc.Equals, expected) 204 }