github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/agent/identity_test.go (about) 1 // Copyright 2014 Canonical Ltd. 2 // Copyright 2014 Cloudbase Solutions SRL 3 // Licensed under the AGPLv3, see LICENCE file for details. 4 5 package agent 6 7 import ( 8 "io/ioutil" 9 "os" 10 "runtime" 11 12 "github.com/juju/names" 13 jc "github.com/juju/testing/checkers" 14 gc "gopkg.in/check.v1" 15 16 "github.com/juju/juju/apiserver/params" 17 "github.com/juju/juju/testing" 18 jujuversion "github.com/juju/juju/version" 19 ) 20 21 type identitySuite struct { 22 testing.BaseSuite 23 mongodConfigPath string 24 mongodPath string 25 } 26 27 var _ = gc.Suite(&identitySuite{}) 28 29 var attributeParams = AgentConfigParams{ 30 Tag: names.NewMachineTag("1"), 31 UpgradedToVersion: jujuversion.Current, 32 Password: "sekrit", 33 CACert: "ca cert", 34 StateAddresses: []string{"localhost:1234"}, 35 APIAddresses: []string{"localhost:1235"}, 36 Nonce: "a nonce", 37 Model: testing.ModelTag, 38 } 39 40 var servingInfo = params.StateServingInfo{ 41 Cert: "old cert", 42 PrivateKey: "old key", 43 CAPrivateKey: "old ca key", 44 StatePort: 69, 45 APIPort: 47, 46 SharedSecret: "shared", 47 SystemIdentity: "identity", 48 } 49 50 func (s *identitySuite) TestWriteSystemIdentityFile(c *gc.C) { 51 params := attributeParams 52 params.Paths.DataDir = c.MkDir() 53 conf, err := NewStateMachineConfig(params, servingInfo) 54 c.Assert(err, jc.ErrorIsNil) 55 err = WriteSystemIdentityFile(conf) 56 c.Assert(err, jc.ErrorIsNil) 57 58 contents, err := ioutil.ReadFile(conf.SystemIdentityPath()) 59 c.Assert(err, jc.ErrorIsNil) 60 c.Check(string(contents), gc.Equals, servingInfo.SystemIdentity) 61 62 fi, err := os.Stat(conf.SystemIdentityPath()) 63 c.Assert(err, jc.ErrorIsNil) 64 65 // Windows is not fully POSIX compliant. Chmod() and Chown() have unexpected behavior 66 // compared to linux/unix 67 if runtime.GOOS != "windows" { 68 c.Check(fi.Mode().Perm(), gc.Equals, os.FileMode(0600)) 69 } 70 // ensure that file is deleted when SystemIdentity is empty 71 info := servingInfo 72 info.SystemIdentity = "" 73 conf, err = NewStateMachineConfig(params, info) 74 c.Assert(err, jc.ErrorIsNil) 75 err = WriteSystemIdentityFile(conf) 76 c.Assert(err, jc.ErrorIsNil) 77 fi, err = os.Stat(conf.SystemIdentityPath()) 78 c.Assert(err, jc.Satisfies, os.IsNotExist) 79 }