github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/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"
    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/state/multiwatcher"
    18  	"github.com/juju/juju/testing"
    19  	"github.com/juju/juju/version"
    20  )
    21  
    22  type formatSuite struct {
    23  	testing.BaseSuite
    24  }
    25  
    26  var _ = gc.Suite(&formatSuite{})
    27  
    28  // The agentParams are used by the specific formatter whitebox tests, and is
    29  // located here for easy reuse.
    30  var agentParams = AgentConfigParams{
    31  	Tag:               names.NewMachineTag("1"),
    32  	UpgradedToVersion: version.Current.Number,
    33  	Jobs:              []multiwatcher.MachineJob{multiwatcher.JobHostUnits},
    34  	Password:          "sekrit",
    35  	CACert:            "ca cert",
    36  	StateAddresses:    []string{"localhost:1234"},
    37  	APIAddresses:      []string{"localhost:1235"},
    38  	Nonce:             "a nonce",
    39  	PreferIPv6:        false,
    40  	Environment:       testing.EnvironmentTag,
    41  }
    42  
    43  func newTestConfig(c *gc.C) *configInternal {
    44  	params := agentParams
    45  	params.DataDir = c.MkDir()
    46  	params.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  	config := newTestConfig(c)
    54  	commands, err := config.WriteCommands("quantal")
    55  	c.Assert(err, jc.ErrorIsNil)
    56  	c.Assert(commands, gc.HasLen, 3)
    57  	c.Assert(commands[0], gc.Matches, `mkdir -p '\S+/agents/machine-1'`)
    58  	c.Assert(commands[1], gc.Matches, `install -m 600 /dev/null '\S+/agents/machine-1/agent.conf'`)
    59  	c.Assert(commands[2], gc.Matches, `printf '%s\\n' '(.|\n)*' > '\S+/agents/machine-1/agent.conf'`)
    60  }
    61  
    62  func (*formatSuite) TestWindowsWriteCommands(c *gc.C) {
    63  	config := newTestConfig(c)
    64  	commands, err := config.WriteCommands("win8")
    65  	c.Assert(err, jc.ErrorIsNil)
    66  	c.Assert(commands, gc.HasLen, 2)
    67  	c.Assert(commands[0], gc.Matches, `mkdir \S+\\agents\\machine-1`)
    68  	c.Assert(commands[1], gc.Matches, `Set-Content '\S+/agents/machine-1/agent.conf' @"
    69  (.|\n)*
    70  "@`)
    71  }
    72  
    73  func (*formatSuite) TestWriteAgentConfig(c *gc.C) {
    74  	config := newTestConfig(c)
    75  	err := config.Write()
    76  	c.Assert(err, jc.ErrorIsNil)
    77  
    78  	configPath := ConfigPath(config.DataDir(), config.Tag())
    79  	formatPath := filepath.Join(config.Dir(), legacyFormatFilename)
    80  	assertFileExists(c, configPath)
    81  	assertFileNotExist(c, formatPath)
    82  }
    83  
    84  func (*formatSuite) TestRead(c *gc.C) {
    85  	config := newTestConfig(c)
    86  	assertWriteAndRead(c, config)
    87  }
    88  
    89  func (*formatSuite) TestReadWriteStateConfig(c *gc.C) {
    90  	servingInfo := params.StateServingInfo{
    91  		Cert:         "some special cert",
    92  		PrivateKey:   "a special key",
    93  		CAPrivateKey: "ca special key",
    94  		StatePort:    12345,
    95  		APIPort:      23456,
    96  	}
    97  	params := agentParams
    98  	params.DataDir = c.MkDir()
    99  	params.Values = map[string]string{"foo": "bar", "wibble": "wobble"}
   100  	configInterface, err := NewStateMachineConfig(params, servingInfo)
   101  	c.Assert(err, jc.ErrorIsNil)
   102  	config, ok := configInterface.(*configInternal)
   103  	c.Assert(ok, jc.IsTrue)
   104  
   105  	assertWriteAndRead(c, config)
   106  }
   107  
   108  func assertWriteAndRead(c *gc.C, config *configInternal) {
   109  	err := config.Write()
   110  	c.Assert(err, jc.ErrorIsNil)
   111  	configPath := ConfigPath(config.DataDir(), config.Tag())
   112  	readConfig, err := ReadConfig(configPath)
   113  	c.Assert(err, jc.ErrorIsNil)
   114  	c.Assert(readConfig, jc.DeepEquals, config)
   115  }
   116  
   117  func assertFileExists(c *gc.C, path string) {
   118  	fileInfo, err := os.Stat(path)
   119  	c.Assert(err, jc.ErrorIsNil)
   120  	c.Assert(fileInfo.Mode().IsRegular(), jc.IsTrue)
   121  
   122  	// Windows is not fully POSIX compliant. Chmod() and Chown() have unexpected behavior
   123  	// compared to linux/unix
   124  	if runtime.GOOS != "windows" {
   125  		c.Assert(fileInfo.Mode().Perm(), gc.Equals, os.FileMode(0600))
   126  	}
   127  	c.Assert(fileInfo.Size(), jc.GreaterThan, 0)
   128  }
   129  
   130  func assertFileNotExist(c *gc.C, path string) {
   131  	_, err := os.Stat(path)
   132  	c.Assert(err, jc.Satisfies, os.IsNotExist)
   133  }