launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/agent/format_whitebox_test.go (about) 1 // Copyright 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package agent 5 6 import ( 7 "io/ioutil" 8 "path" 9 10 gc "launchpad.net/gocheck" 11 12 "launchpad.net/juju-core/testing/testbase" 13 ) 14 15 type formatSuite struct { 16 testbase.LoggingSuite 17 } 18 19 var _ = gc.Suite(&formatSuite{}) 20 21 // The agentParams are used by the specific formatter whitebox tests, and is 22 // located here for easy reuse. 23 var agentParams = AgentConfigParams{ 24 Tag: "omg", 25 Password: "sekrit", 26 CACert: []byte("ca cert"), 27 StateAddresses: []string{"localhost:1234"}, 28 APIAddresses: []string{"localhost:1235"}, 29 Nonce: "a nonce", 30 } 31 32 func (*formatSuite) TestReadFormatEmptyDir(c *gc.C) { 33 // Since the previous format didn't have a format file, a missing format 34 // should return the previous format. Once we are over the hump of 35 // missing format files, a missing format file should generate an error. 36 dir := c.MkDir() 37 format, err := readFormat(dir) 38 c.Assert(format, gc.Equals, previousFormat) 39 c.Assert(err, gc.IsNil) 40 } 41 42 func (*formatSuite) TestReadFormat(c *gc.C) { 43 dir := c.MkDir() 44 // Make sure that the write adds the carriage return to show that 45 // this is stripped off for the returned format. 46 err := ioutil.WriteFile(path.Join(dir, formatFilename), []byte("some format\n"), 0644) 47 c.Assert(err, gc.IsNil) 48 format, err := readFormat(dir) 49 c.Assert(format, gc.Equals, "some format") 50 c.Assert(err, gc.IsNil) 51 } 52 53 func (*formatSuite) TestNewFormatter(c *gc.C) { 54 formatter, err := newFormatter(currentFormat) 55 c.Assert(formatter, gc.NotNil) 56 c.Assert(err, gc.IsNil) 57 58 formatter, err = newFormatter(previousFormat) 59 c.Assert(formatter, gc.NotNil) 60 c.Assert(err, gc.IsNil) 61 62 formatter, err = newFormatter("other") 63 c.Assert(formatter, gc.IsNil) 64 c.Assert(err, gc.ErrorMatches, "unknown agent config format") 65 } 66 67 func (*formatSuite) TestWriteFormat(c *gc.C) { 68 dir := c.MkDir() 69 testDir := path.Join(dir, "test") 70 err := writeFormatFile(testDir, "some format") 71 c.Assert(err, gc.IsNil) 72 format, err := readFormat(testDir) 73 c.Assert(format, gc.Equals, "some format") 74 c.Assert(err, gc.IsNil) 75 // Make sure the carriage return is there as it makes catting the file nicer. 76 content, err := ioutil.ReadFile(path.Join(testDir, formatFilename)) 77 c.Assert(err, gc.IsNil) 78 c.Assert(string(content), gc.Equals, "some format\n") 79 } 80 81 func (*formatSuite) TestWriteCommandsForFormat(c *gc.C) { 82 dir := c.MkDir() 83 testDir := path.Join(dir, "test") 84 commands := writeCommandsForFormat(testDir, "some format") 85 c.Assert(commands, gc.HasLen, 3) 86 c.Assert(commands[0], gc.Matches, `mkdir -p \S+`) 87 c.Assert(commands[1], gc.Matches, `install -m 644 /dev/null '\S+/format'`) 88 c.Assert(commands[2], gc.Matches, `printf '%s\\n' '.*' > '\S+/format'`) 89 } 90 91 func (*formatSuite) TestReadPreviousFormatWritesNew(c *gc.C) { 92 config := newTestConfig(c) 93 94 err := previousFormatter.write(config) 95 c.Assert(err, gc.IsNil) 96 97 _, err = ReadConf(config.DataDir(), config.Tag()) 98 c.Assert(err, gc.IsNil) 99 format, err := readFormat(config.Dir()) 100 c.Assert(err, gc.IsNil) 101 c.Assert(format, gc.Equals, currentFormat) 102 }