launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/provider/maas/environ_test.go (about) 1 // Copyright 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package maas_test 5 6 import ( 7 stdtesting "testing" 8 9 gc "launchpad.net/gocheck" 10 "launchpad.net/gomaasapi" 11 12 "launchpad.net/juju-core/environs/config" 13 envtesting "launchpad.net/juju-core/environs/testing" 14 "launchpad.net/juju-core/provider/maas" 15 coretesting "launchpad.net/juju-core/testing" 16 "launchpad.net/juju-core/testing/testbase" 17 ) 18 19 type environSuite struct { 20 testbase.LoggingSuite 21 envtesting.ToolsFixture 22 testMAASObject *gomaasapi.TestMAASObject 23 restoreTimeouts func() 24 } 25 26 var _ = gc.Suite(&environSuite{}) 27 28 func TestMAAS(t *stdtesting.T) { 29 gc.TestingT(t) 30 } 31 32 // TDOO: jam 2013-12-06 This is copied from the providerSuite which is in a 33 // whitebox package maas. Either move that into a whitebox test so it can be 34 // shared, or into a 'testing' package so we can use it here. 35 func (s *environSuite) SetUpSuite(c *gc.C) { 36 s.restoreTimeouts = envtesting.PatchAttemptStrategies(maas.ShortAttempt) 37 s.LoggingSuite.SetUpSuite(c) 38 TestMAASObject := gomaasapi.NewTestMAAS("1.0") 39 s.testMAASObject = TestMAASObject 40 } 41 42 func (s *environSuite) SetUpTest(c *gc.C) { 43 s.LoggingSuite.SetUpTest(c) 44 s.ToolsFixture.SetUpTest(c) 45 } 46 47 func (s *environSuite) TearDownTest(c *gc.C) { 48 s.testMAASObject.TestServer.Clear() 49 s.ToolsFixture.TearDownTest(c) 50 s.LoggingSuite.TearDownTest(c) 51 } 52 53 func (s *environSuite) TearDownSuite(c *gc.C) { 54 s.testMAASObject.Close() 55 s.restoreTimeouts() 56 s.LoggingSuite.TearDownSuite(c) 57 } 58 59 var _ = gc.Suite(&environSuite{}) 60 61 func getSimpleTestConfig(c *gc.C, extraAttrs coretesting.Attrs) *config.Config { 62 attrs := coretesting.FakeConfig() 63 attrs["type"] = "maas" 64 attrs["maas-server"] = "http://maas.testing.invalid" 65 attrs["maas-oauth"] = "a:b:c" 66 for k, v := range extraAttrs { 67 attrs[k] = v 68 } 69 cfg, err := config.New(config.NoDefaults, attrs) 70 c.Assert(err, gc.IsNil) 71 return cfg 72 } 73 74 func (*environSuite) TestSetConfigValidatesFirst(c *gc.C) { 75 // SetConfig() validates the config change and disallows, for example, 76 // changes in the environment name. 77 oldCfg := getSimpleTestConfig(c, coretesting.Attrs{"name": "old-name"}) 78 newCfg := getSimpleTestConfig(c, coretesting.Attrs{"name": "new-name"}) 79 env, err := maas.NewEnviron(oldCfg) 80 c.Assert(err, gc.IsNil) 81 82 // SetConfig() fails, even though both the old and the new config are 83 // individually valid. 84 err = env.SetConfig(newCfg) 85 c.Assert(err, gc.NotNil) 86 c.Check(err, gc.ErrorMatches, ".*cannot change name.*") 87 88 // The old config is still in place. The new config never took effect. 89 c.Check(env.Name(), gc.Equals, "old-name") 90 } 91 92 func (*environSuite) TestSetConfigRefusesChangingAgentName(c *gc.C) { 93 oldCfg := getSimpleTestConfig(c, coretesting.Attrs{"maas-agent-name": "agent-one"}) 94 newCfgTwo := getSimpleTestConfig(c, coretesting.Attrs{"maas-agent-name": "agent-two"}) 95 env, err := maas.NewEnviron(oldCfg) 96 c.Assert(err, gc.IsNil) 97 98 // SetConfig() fails, even though both the old and the new config are 99 // individually valid. 100 err = env.SetConfig(newCfgTwo) 101 c.Assert(err, gc.NotNil) 102 c.Check(err, gc.ErrorMatches, ".*cannot change maas-agent-name.*") 103 104 // The old config is still in place. The new config never took effect. 105 c.Check(maas.MAASAgentName(env), gc.Equals, "agent-one") 106 107 // It also refuses to set it to the empty string: 108 err = env.SetConfig(getSimpleTestConfig(c, coretesting.Attrs{"maas-agent-name": ""})) 109 c.Check(err, gc.ErrorMatches, ".*cannot change maas-agent-name.*") 110 111 // And to nil 112 err = env.SetConfig(getSimpleTestConfig(c, nil)) 113 c.Check(err, gc.ErrorMatches, ".*cannot change maas-agent-name.*") 114 } 115 116 func (*environSuite) TestSetConfigAllowsEmptyFromNilAgentName(c *gc.C) { 117 // bug #1256179 is that when using an older version of Juju (<1.16.2) 118 // we didn't include maas-agent-name in the database, so it was 'nil' 119 // in the OldConfig. However, when setting an environment, we would set 120 // it to "" (because maasEnvironConfig.Validate ensures it is a 'valid' 121 // string). We can't create that from NewEnviron or newConfig because 122 // both of them Validate the contents. 'cmd/juju/environment 123 // SetEnvironmentCommand' instead uses conn.State.EnvironConfig() which 124 // just reads the content of the database into a map, so we just create 125 // the map ourselves. 126 127 // Even though we use 'nil' here, it actually stores it as "" because 128 // 1.16.2 already validates the value 129 baseCfg := getSimpleTestConfig(c, coretesting.Attrs{"maas-agent-name": ""}) 130 c.Check(baseCfg.UnknownAttrs()["maas-agent-name"], gc.Equals, "") 131 env, err := maas.NewEnviron(baseCfg) 132 c.Assert(err, gc.IsNil) 133 provider := env.Provider() 134 135 attrs := coretesting.FakeConfig() 136 // These are attrs we need to make it a valid Config, but would usually 137 // be set by other infrastructure 138 attrs["type"] = "maas" 139 nilCfg, err := config.New(config.NoDefaults, attrs) 140 c.Assert(err, gc.IsNil) 141 validatedConfig, err := provider.Validate(baseCfg, nilCfg) 142 c.Assert(err, gc.IsNil) 143 c.Check(validatedConfig.UnknownAttrs()["maas-agent-name"], gc.Equals, "") 144 // However, you can't set it to an actual value if you haven't been using a value 145 valueCfg := getSimpleTestConfig(c, coretesting.Attrs{"maas-agent-name": "agent-name"}) 146 _, err = provider.Validate(valueCfg, nilCfg) 147 c.Check(err, gc.ErrorMatches, ".*cannot change maas-agent-name.*") 148 } 149 150 func (*environSuite) TestSetConfigAllowsChangingNilAgentNameToEmptyString(c *gc.C) { 151 oldCfg := getSimpleTestConfig(c, nil) 152 newCfgTwo := getSimpleTestConfig(c, coretesting.Attrs{"maas-agent-name": ""}) 153 env, err := maas.NewEnviron(oldCfg) 154 c.Assert(err, gc.IsNil) 155 156 err = env.SetConfig(newCfgTwo) 157 c.Assert(err, gc.IsNil) 158 c.Check(maas.MAASAgentName(env), gc.Equals, "") 159 } 160 161 func (*environSuite) TestSetConfigUpdatesConfig(c *gc.C) { 162 origAttrs := coretesting.Attrs{ 163 "server-name": "http://maas2.testing.invalid", 164 "maas-oauth": "a:b:c", 165 "admin-secret": "secret", 166 } 167 cfg := getSimpleTestConfig(c, origAttrs) 168 env, err := maas.NewEnviron(cfg) 169 c.Check(err, gc.IsNil) 170 c.Check(env.Name(), gc.Equals, "testenv") 171 172 anotherServer := "http://maas.testing.invalid" 173 anotherOauth := "c:d:e" 174 anotherSecret := "secret2" 175 newAttrs := coretesting.Attrs{ 176 "server-name": anotherServer, 177 "maas-oauth": anotherOauth, 178 "admin-secret": anotherSecret, 179 } 180 cfg2 := getSimpleTestConfig(c, newAttrs) 181 errSetConfig := env.SetConfig(cfg2) 182 c.Check(errSetConfig, gc.IsNil) 183 c.Check(env.Name(), gc.Equals, "testenv") 184 authClient, _ := gomaasapi.NewAuthenticatedClient(anotherServer, anotherOauth, maas.APIVersion) 185 maasClient := gomaasapi.NewMAAS(*authClient) 186 MAASServer := maas.GetMAASClient(env) 187 c.Check(MAASServer, gc.DeepEquals, maasClient) 188 } 189 190 func (*environSuite) TestNewEnvironSetsConfig(c *gc.C) { 191 cfg := getSimpleTestConfig(c, nil) 192 193 env, err := maas.NewEnviron(cfg) 194 195 c.Check(err, gc.IsNil) 196 c.Check(env.Name(), gc.Equals, "testenv") 197 }