github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/environs/bootstrap/config_test.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package bootstrap_test 5 6 import ( 7 "io/ioutil" 8 "time" 9 10 gitjujutesting "github.com/juju/testing" 11 jc "github.com/juju/testing/checkers" 12 gc "gopkg.in/check.v1" 13 14 "github.com/juju/juju/environs/bootstrap" 15 "github.com/juju/juju/juju/osenv" 16 "github.com/juju/juju/testing" 17 ) 18 19 type ConfigSuite struct { 20 testing.FakeJujuXDGDataHomeSuite 21 } 22 23 var _ = gc.Suite(&ConfigSuite{}) 24 25 func (*ConfigSuite) TestDefaultConfig(c *gc.C) { 26 cfg, err := bootstrap.NewConfig(nil) 27 c.Assert(err, jc.ErrorIsNil) 28 29 // These three things are generated. 30 c.Assert(cfg.AdminSecret, gc.Not(gc.HasLen), 0) 31 c.Assert(cfg.CACert, gc.Not(gc.HasLen), 0) 32 c.Assert(cfg.CAPrivateKey, gc.Not(gc.HasLen), 0) 33 34 c.Assert(cfg.BootstrapTimeout, gc.Equals, time.Second*1200) 35 c.Assert(cfg.BootstrapRetryDelay, gc.Equals, time.Second*5) 36 c.Assert(cfg.BootstrapAddressesDelay, gc.Equals, time.Second*10) 37 } 38 39 func (*ConfigSuite) TestConfigValuesSpecified(c *gc.C) { 40 cfg, err := bootstrap.NewConfig(map[string]interface{}{ 41 "admin-secret": "sekrit", 42 "ca-cert": testing.CACert, 43 "ca-private-key": testing.CAKey, 44 "bootstrap-timeout": 1, 45 "bootstrap-retry-delay": 2, 46 "bootstrap-addresses-delay": 3, 47 }) 48 c.Assert(err, jc.ErrorIsNil) 49 50 c.Assert(cfg, jc.DeepEquals, bootstrap.Config{ 51 AdminSecret: "sekrit", 52 CACert: testing.CACert, 53 CAPrivateKey: testing.CAKey, 54 BootstrapTimeout: time.Second * 1, 55 BootstrapRetryDelay: time.Second * 2, 56 BootstrapAddressesDelay: time.Second * 3, 57 }) 58 } 59 60 func (s *ConfigSuite) addFiles(c *gc.C, files ...gitjujutesting.TestFile) { 61 for _, f := range files { 62 err := ioutil.WriteFile(osenv.JujuXDGDataHomePath(f.Name), []byte(f.Data), 0666) 63 c.Assert(err, gc.IsNil) 64 } 65 } 66 67 func (s *ConfigSuite) TestDefaultConfigReadsDefaultCACertKeyFiles(c *gc.C) { 68 s.addFiles(c, []gitjujutesting.TestFile{ 69 {"ca-cert.pem", testing.CACert}, 70 {"ca-private-key.pem", testing.CAKey}, 71 }...) 72 73 cfg, err := bootstrap.NewConfig(nil) 74 c.Assert(err, jc.ErrorIsNil) 75 76 c.Assert(cfg.CACert, gc.Equals, testing.CACert) 77 c.Assert(cfg.CAPrivateKey, gc.Equals, testing.CAKey) 78 } 79 80 func (s *ConfigSuite) TestConfigReadsCACertKeyFilesFromPaths(c *gc.C) { 81 s.addFiles(c, []gitjujutesting.TestFile{ 82 {"ca-cert-2.pem", testing.OtherCACert}, 83 {"ca-private-key-2.pem", testing.OtherCAKey}, 84 }...) 85 86 cfg, err := bootstrap.NewConfig(map[string]interface{}{ 87 "ca-cert-path": "ca-cert-2.pem", 88 "ca-private-key-path": "ca-private-key-2.pem", 89 }) 90 c.Assert(err, jc.ErrorIsNil) 91 92 c.Assert(cfg.CACert, gc.Equals, testing.OtherCACert) 93 c.Assert(cfg.CAPrivateKey, gc.Equals, testing.OtherCAKey) 94 } 95 96 func (s *ConfigSuite) TestConfigNonExistentPath(c *gc.C) { 97 s.testConfigError(c, map[string]interface{}{ 98 "ca-cert-path": "not/there", 99 }, `reading "ca-cert" from file: "ca-cert" not set, and could not read from "not/there": .*`) 100 } 101 102 func (s *ConfigSuite) TestConfigInvalidCACert(c *gc.C) { 103 s.testConfigError(c, map[string]interface{}{ 104 "ca-cert": invalidCACert, 105 "ca-private-key": testing.CAKey, 106 }, "validating ca-cert and ca-private-key: asn1: syntax error: data truncated") 107 } 108 109 func (s *ConfigSuite) TestConfigInvalidCAKey(c *gc.C) { 110 s.testConfigError(c, map[string]interface{}{ 111 "ca-cert": testing.CACert, 112 "ca-private-key": invalidCAKey, 113 }, "validating ca-cert and ca-private-key: (crypto/)?tls: failed to parse private key") 114 } 115 116 func (s *ConfigSuite) TestConfigCACertKeyMismatch(c *gc.C) { 117 s.testConfigError(c, map[string]interface{}{ 118 "ca-cert": testing.CACert, 119 "ca-private-key": testing.OtherCAKey, 120 }, "validating ca-cert and ca-private-key: (crypto/)?tls: private key does not match public key") 121 } 122 123 func (s *ConfigSuite) TestConfigCACertWithEmptyKey(c *gc.C) { 124 s.testConfigError(c, map[string]interface{}{ 125 "ca-cert": testing.CACert, 126 }, "validating ca-cert and ca-private-key: (crypto/)?tls: failed to find any PEM data in key input") 127 } 128 129 func (s *ConfigSuite) TestConfigEmptyCACertWithKey(c *gc.C) { 130 s.testConfigError(c, map[string]interface{}{ 131 "ca-private-key": testing.CAKey, 132 }, "validating ca-cert and ca-private-key: (crypto/)?tls: failed to find any PEM data in certificate input") 133 } 134 135 func (*ConfigSuite) testConfigError(c *gc.C, attrs map[string]interface{}, expect string) { 136 _, err := bootstrap.NewConfig(attrs) 137 c.Assert(err, gc.ErrorMatches, expect) 138 } 139 140 func (*ConfigSuite) TestValidate(c *gc.C) { 141 c.Assert(validConfig().Validate(), jc.ErrorIsNil) 142 } 143 144 func (*ConfigSuite) TestValidateAdminSecret(c *gc.C) { 145 cfg := validConfig() 146 cfg.AdminSecret = "" 147 c.Assert(cfg.Validate(), gc.ErrorMatches, "empty admin-secret not valid") 148 } 149 150 func (*ConfigSuite) TestValidateBootstrapTimeout(c *gc.C) { 151 cfg := validConfig() 152 cfg.BootstrapTimeout = 0 153 c.Assert(cfg.Validate(), gc.ErrorMatches, "bootstrap-timeout of 0s? not valid") 154 } 155 156 func (*ConfigSuite) TestValidateBootstrapRetryDelay(c *gc.C) { 157 cfg := validConfig() 158 cfg.BootstrapRetryDelay = -1 * time.Second 159 c.Assert(cfg.Validate(), gc.ErrorMatches, "bootstrap-retry-delay of -1s not valid") 160 } 161 162 func (*ConfigSuite) TestValidateBootstrapAddressesDelay(c *gc.C) { 163 cfg := validConfig() 164 cfg.BootstrapAddressesDelay = -2 * time.Minute 165 c.Assert(cfg.Validate(), gc.ErrorMatches, "bootstrap-addresses-delay of -2m0s not valid") 166 } 167 168 func validConfig() bootstrap.Config { 169 return bootstrap.Config{ 170 AdminSecret: "sekrit", 171 CACert: testing.CACert, 172 CAPrivateKey: testing.CAKey, 173 BootstrapTimeout: time.Second * 1, 174 BootstrapRetryDelay: time.Second * 2, 175 BootstrapAddressesDelay: time.Second * 3, 176 } 177 } 178 179 var invalidCAKey = ` 180 -----BEGIN RSA PRIVATE KEY----- 181 MIIBOgIBAAJAZabKgKInuOxj5vDWLwHHQtK3/45KB+32D15w94Nt83BmuGxo90lw 182 -----END RSA PRIVATE KEY----- 183 `[1:] 184 185 var invalidCACert = ` 186 -----BEGIN CERTIFICATE----- 187 MIIBOgIBAAJAZabKgKInuOxj5vDWLwHHQtK3/45KB+32D15w94Nt83BmuGxo90lw 188 -----END CERTIFICATE----- 189 `[1:]