github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/provider/azure/customdata_test.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package azure
     5  
     6  import (
     7  	"encoding/base64"
     8  	"path"
     9  
    10  	"github.com/juju/names"
    11  	jc "github.com/juju/testing/checkers"
    12  	gc "gopkg.in/check.v1"
    13  
    14  	"github.com/juju/juju/agent"
    15  	"github.com/juju/juju/api"
    16  	"github.com/juju/juju/environs"
    17  	"github.com/juju/juju/environs/cloudinit"
    18  	"github.com/juju/juju/juju/paths"
    19  	"github.com/juju/juju/mongo"
    20  	"github.com/juju/juju/state/multiwatcher"
    21  	"github.com/juju/juju/testing"
    22  	"github.com/juju/juju/tools"
    23  	"github.com/juju/juju/version"
    24  )
    25  
    26  type customDataSuite struct {
    27  	testing.BaseSuite
    28  }
    29  
    30  var _ = gc.Suite(&customDataSuite{})
    31  
    32  func must(s string, err error) string {
    33  	if err != nil {
    34  		panic(err)
    35  	}
    36  	return s
    37  }
    38  
    39  var logDir = must(paths.LogDir("precise"))
    40  var cloudInitOutputLog = path.Join(logDir, "cloud-init-output.log")
    41  
    42  // makeMachineConfig produces a valid cloudinit machine config.
    43  func makeMachineConfig(c *gc.C) *cloudinit.MachineConfig {
    44  	machineId := "0"
    45  	machineTag := names.NewMachineTag(machineId)
    46  	return &cloudinit.MachineConfig{
    47  		MachineId:    machineId,
    48  		MachineNonce: "gxshasqlnng",
    49  		DataDir:      environs.DataDir,
    50  		LogDir:       agent.DefaultLogDir,
    51  		Jobs: []multiwatcher.MachineJob{
    52  			multiwatcher.JobManageEnviron,
    53  			multiwatcher.JobHostUnits,
    54  			multiwatcher.JobManageNetworking,
    55  		},
    56  		CloudInitOutputLog: cloudInitOutputLog,
    57  		Tools: &tools.Tools{
    58  			Version: version.MustParseBinary("1.2.3-quantal-amd64"),
    59  			URL:     "http://testing.invalid/tools.tar.gz",
    60  		},
    61  		Series: "quantal",
    62  		MongoInfo: &mongo.MongoInfo{
    63  			Info: mongo.Info{
    64  				CACert: testing.CACert,
    65  				Addrs:  []string{"127.0.0.1:123"},
    66  			},
    67  			Tag:      machineTag,
    68  			Password: "password",
    69  		},
    70  		APIInfo: &api.Info{
    71  			CACert:     testing.CACert,
    72  			Addrs:      []string{"127.0.0.1:123"},
    73  			Tag:        machineTag,
    74  			EnvironTag: testing.EnvironmentTag,
    75  		},
    76  		MachineAgentServiceName: "jujud-machine-0",
    77  	}
    78  }
    79  
    80  // makeBadMachineConfig produces a cloudinit machine config that cloudinit
    81  // will reject as invalid.
    82  func makeBadMachineConfig() *cloudinit.MachineConfig {
    83  	// As it happens, a default-initialized config is invalid.
    84  	return &cloudinit.MachineConfig{Series: "quantal"}
    85  }
    86  
    87  func (*customDataSuite) TestMakeCustomDataPropagatesError(c *gc.C) {
    88  	_, err := makeCustomData(makeBadMachineConfig())
    89  	c.Assert(err, gc.NotNil)
    90  	c.Check(err, gc.ErrorMatches, "failure while generating custom data: invalid machine configuration: invalid machine id")
    91  }
    92  
    93  func (*customDataSuite) TestMakeCustomDataEncodesUserData(c *gc.C) {
    94  	cfg := makeMachineConfig(c)
    95  
    96  	encodedData, err := makeCustomData(cfg)
    97  	c.Assert(err, jc.ErrorIsNil)
    98  
    99  	data, err := base64.StdEncoding.DecodeString(encodedData)
   100  	c.Assert(err, jc.ErrorIsNil)
   101  	reference, err := environs.ComposeUserData(cfg, nil)
   102  	c.Assert(err, jc.ErrorIsNil)
   103  	c.Check(data, gc.DeepEquals, reference)
   104  }