launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/environs/manual/provisioner_test.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package manual
     5  
     6  import (
     7  	"fmt"
     8  	"os"
     9  
    10  	gc "launchpad.net/gocheck"
    11  
    12  	envtesting "launchpad.net/juju-core/environs/testing"
    13  	"launchpad.net/juju-core/instance"
    14  	"launchpad.net/juju-core/juju/testing"
    15  	"launchpad.net/juju-core/state"
    16  	"launchpad.net/juju-core/state/api/params"
    17  	"launchpad.net/juju-core/state/statecmd"
    18  	jc "launchpad.net/juju-core/testing/checkers"
    19  	"launchpad.net/juju-core/version"
    20  )
    21  
    22  type provisionerSuite struct {
    23  	testing.JujuConnSuite
    24  }
    25  
    26  var _ = gc.Suite(&provisionerSuite{})
    27  
    28  func (s *provisionerSuite) getArgs(c *gc.C) ProvisionMachineArgs {
    29  	hostname, err := os.Hostname()
    30  	c.Assert(err, gc.IsNil)
    31  	return ProvisionMachineArgs{
    32  		Host:    hostname,
    33  		EnvName: "dummyenv",
    34  	}
    35  }
    36  
    37  func (s *provisionerSuite) TestProvisionMachine(c *gc.C) {
    38  	const series = "precise"
    39  	const arch = "amd64"
    40  
    41  	args := s.getArgs(c)
    42  	hostname := args.Host
    43  	args.Host = "ubuntu@" + args.Host
    44  
    45  	envtesting.RemoveTools(c, s.Conn.Environ.Storage())
    46  	defer fakeSSH{
    47  		Series:             series,
    48  		Arch:               arch,
    49  		InitUbuntuUser:     true,
    50  		SkipProvisionAgent: true,
    51  	}.install(c).Restore()
    52  	// Attempt to provision a machine with no tools available, expect it to fail.
    53  	machineId, err := ProvisionMachine(args)
    54  	c.Assert(err, jc.Satisfies, params.IsCodeNotFound)
    55  	c.Assert(machineId, gc.Equals, "")
    56  
    57  	cfg := s.Conn.Environ.Config()
    58  	number, ok := cfg.AgentVersion()
    59  	c.Assert(ok, jc.IsTrue)
    60  	binVersion := version.Binary{number, series, arch}
    61  	envtesting.AssertUploadFakeToolsVersions(c, s.Conn.Environ.Storage(), binVersion)
    62  
    63  	for i, errorCode := range []int{255, 0} {
    64  		c.Logf("test %d: code %d", i, errorCode)
    65  		defer fakeSSH{
    66  			Series:                 series,
    67  			Arch:                   arch,
    68  			InitUbuntuUser:         true,
    69  			ProvisionAgentExitCode: errorCode,
    70  		}.install(c).Restore()
    71  		machineId, err = ProvisionMachine(args)
    72  		if errorCode != 0 {
    73  			c.Assert(err, gc.ErrorMatches, fmt.Sprintf("rc: %d", errorCode))
    74  			c.Assert(machineId, gc.Equals, "")
    75  		} else {
    76  			c.Assert(err, gc.IsNil)
    77  			c.Assert(machineId, gc.Not(gc.Equals), "")
    78  			// machine ID will be incremented. Even though we failed and the
    79  			// machine is removed, the ID is not reused.
    80  			c.Assert(machineId, gc.Equals, fmt.Sprint(i+1))
    81  			m, err := s.State.Machine(machineId)
    82  			c.Assert(err, gc.IsNil)
    83  			instanceId, err := m.InstanceId()
    84  			c.Assert(err, gc.IsNil)
    85  			c.Assert(instanceId, gc.Equals, instance.Id("manual:"+hostname))
    86  		}
    87  	}
    88  
    89  	// Attempting to provision a machine twice should fail. We effect
    90  	// this by checking for existing juju upstart configurations.
    91  	defer fakeSSH{
    92  		Provisioned:        true,
    93  		InitUbuntuUser:     true,
    94  		SkipDetection:      true,
    95  		SkipProvisionAgent: true,
    96  	}.install(c).Restore()
    97  	_, err = ProvisionMachine(args)
    98  	c.Assert(err, gc.Equals, ErrProvisioned)
    99  	defer fakeSSH{
   100  		Provisioned:              true,
   101  		CheckProvisionedExitCode: 255,
   102  		InitUbuntuUser:           true,
   103  		SkipDetection:            true,
   104  		SkipProvisionAgent:       true,
   105  	}.install(c).Restore()
   106  	_, err = ProvisionMachine(args)
   107  	c.Assert(err, gc.ErrorMatches, "error checking if provisioned: rc: 255")
   108  }
   109  
   110  func (s *provisionerSuite) TestFinishMachineConfig(c *gc.C) {
   111  	const series = "precise"
   112  	const arch = "amd64"
   113  	defer fakeSSH{
   114  		Series:         series,
   115  		Arch:           arch,
   116  		InitUbuntuUser: true,
   117  	}.install(c).Restore()
   118  	machineId, err := ProvisionMachine(s.getArgs(c))
   119  	c.Assert(err, gc.IsNil)
   120  
   121  	// Now check what we would've configured it with.
   122  	mcfg, err := statecmd.MachineConfig(s.State, machineId, state.BootstrapNonce, "/var/lib/juju")
   123  	c.Assert(err, gc.IsNil)
   124  	c.Check(mcfg, gc.NotNil)
   125  	c.Check(mcfg.APIInfo, gc.NotNil)
   126  	c.Check(mcfg.StateInfo, gc.NotNil)
   127  
   128  	stateInfo, apiInfo, err := s.APIConn.Environ.StateInfo()
   129  	c.Assert(err, gc.IsNil)
   130  	c.Check(mcfg.APIInfo.Addrs, gc.DeepEquals, apiInfo.Addrs)
   131  	c.Check(mcfg.StateInfo.Addrs, gc.DeepEquals, stateInfo.Addrs)
   132  }