github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/agent/format_whitebox_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 "os" 9 "path/filepath" 10 "runtime" 11 12 "github.com/juju/names/v5" 13 jc "github.com/juju/testing/checkers" 14 gc "gopkg.in/check.v1" 15 16 "github.com/juju/juju/cloudconfig/cloudinit" 17 "github.com/juju/juju/controller" 18 "github.com/juju/juju/core/model" 19 "github.com/juju/juju/testing" 20 jujuversion "github.com/juju/juju/version" 21 ) 22 23 type formatSuite struct { 24 testing.BaseSuite 25 } 26 27 var _ = gc.Suite(&formatSuite{}) 28 29 // The agentParams are used by the specific formatter whitebox tests, and is 30 // located here for easy reuse. 31 var agentParams = AgentConfigParams{ 32 Tag: names.NewMachineTag("1"), 33 UpgradedToVersion: jujuversion.Current, 34 Jobs: []model.MachineJob{model.JobHostUnits}, 35 Password: "sekrit", 36 CACert: "ca cert", 37 APIAddresses: []string{"localhost:1235"}, 38 Nonce: "a nonce", 39 Model: testing.ModelTag, 40 Controller: testing.ControllerTag, 41 } 42 43 func newTestConfig(c *gc.C) *configInternal { 44 params := agentParams 45 params.Paths.DataDir = c.MkDir() 46 params.Paths.LogDir = c.MkDir() 47 config, err := NewAgentConfig(params) 48 c.Assert(err, jc.ErrorIsNil) 49 return config.(*configInternal) 50 } 51 52 func (*formatSuite) TestWriteCommands(c *gc.C) { 53 cloudcfg, err := cloudinit.New("ubuntu") 54 c.Assert(err, jc.ErrorIsNil) 55 config := newTestConfig(c) 56 commands, err := config.WriteCommands(cloudcfg.ShellRenderer()) 57 c.Assert(err, jc.ErrorIsNil) 58 c.Assert(commands, gc.HasLen, 3) 59 c.Assert(commands[0], gc.Matches, `mkdir -p '\S+/agents/machine-1'`) 60 c.Assert(commands[1], gc.Matches, `cat > '\S+/agents/machine-1/agent.conf' << 'EOF'\n(.|\n)*\nEOF`) 61 c.Assert(commands[2], gc.Matches, `chmod 0600 '\S+/agents/machine-1/agent.conf'`) 62 } 63 64 func (*formatSuite) TestWriteAgentConfig(c *gc.C) { 65 config := newTestConfig(c) 66 err := config.Write() 67 c.Assert(err, jc.ErrorIsNil) 68 69 configPath := ConfigPath(config.DataDir(), config.Tag()) 70 formatPath := filepath.Join(config.Dir(), "format") 71 assertFileExists(c, configPath) 72 assertFileNotExist(c, formatPath) 73 } 74 75 func (*formatSuite) TestRead(c *gc.C) { 76 config := newTestConfig(c) 77 assertWriteAndRead(c, config) 78 } 79 80 func (*formatSuite) TestReadWriteStateConfig(c *gc.C) { 81 servingInfo := controller.StateServingInfo{ 82 Cert: "some special cert", 83 PrivateKey: "a special key", 84 CAPrivateKey: "ca special key", 85 StatePort: 12345, 86 APIPort: 23456, 87 } 88 params := agentParams 89 params.Paths.DataDir = c.MkDir() 90 params.Values = map[string]string{"foo": "bar", "wibble": "wobble"} 91 configInterface, err := NewStateMachineConfig(params, servingInfo) 92 c.Assert(err, jc.ErrorIsNil) 93 config, ok := configInterface.(*configInternal) 94 c.Assert(ok, jc.IsTrue) 95 96 assertWriteAndRead(c, config) 97 } 98 99 func assertWriteAndRead(c *gc.C, config *configInternal) { 100 err := config.Write() 101 c.Assert(err, jc.ErrorIsNil) 102 configPath := ConfigPath(config.DataDir(), config.Tag()) 103 readConfig, err := ReadConfig(configPath) 104 c.Assert(err, jc.ErrorIsNil) 105 c.Assert(readConfig, jc.DeepEquals, config) 106 } 107 108 func assertFileExists(c *gc.C, path string) { 109 fileInfo, err := os.Stat(path) 110 c.Assert(err, jc.ErrorIsNil) 111 c.Assert(fileInfo.Mode().IsRegular(), jc.IsTrue) 112 113 // Windows is not fully POSIX compliant. Chmod() and Chown() have unexpected behavior 114 // compared to linux/unix 115 if runtime.GOOS != "windows" { 116 c.Assert(fileInfo.Mode().Perm(), gc.Equals, os.FileMode(0600)) 117 } 118 c.Assert(fileInfo.Size(), jc.GreaterThan, 0) 119 } 120 121 func assertFileNotExist(c *gc.C, path string) { 122 _, err := os.Stat(path) 123 c.Assert(err, jc.Satisfies, os.IsNotExist) 124 }