1
// Copyright 2013 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
11
gc "launchpad.net/gocheck"
13
"launchpad.net/juju-core/constraints"
14
"launchpad.net/juju-core/environs/tools"
15
"launchpad.net/juju-core/instance"
16
"launchpad.net/juju-core/juju/testing"
19
type provisionerSuite struct {
23
var _ = gc.Suite(&provisionerSuite{})
25
func (s *provisionerSuite) getArgs(c *gc.C) ProvisionMachineArgs {
26
hostname, err := os.Hostname()
27
c.Assert(err, gc.IsNil)
28
return ProvisionMachineArgs{
34
func (s *provisionerSuite) TestProvisionMachine(c *gc.C) {
35
// Prepare a mock ssh response for the detection phase.
36
detectionoutput := strings.Join([]string{
45
args.Host = "ubuntu@" + args.Host
47
defer sshresponse(c, detectionScript, detectionoutput, 0)()
48
defer sshresponse(c, checkProvisionedScript, "", 0)()
49
m, err := ProvisionMachine(args)
50
c.Assert(err, gc.ErrorMatches, "no matching tools available")
53
toolsList, err := tools.FindBootstrapTools(s.Conn.Environ, constraints.Value{})
54
c.Assert(err, gc.IsNil)
55
args.Tools = toolsList[0]
57
for _, errorCode := range []int{255, 0} {
58
defer sshresponse(c, "", "", errorCode)() // executing script
59
defer sshresponse(c, detectionScript, detectionoutput, 0)()
60
defer sshresponse(c, checkProvisionedScript, "", 0)()
61
m, err = ProvisionMachine(args)
63
c.Assert(err, gc.ErrorMatches, fmt.Sprintf("exit status %d", errorCode))
66
c.Assert(err, gc.IsNil)
67
c.Assert(m, gc.NotNil)
68
// machine ID will be 2, not 1. Even though we failed and the
69
// machine is removed, the ID is not reused.
70
c.Assert(m.Id(), gc.Equals, "2")
71
instanceId, err := m.InstanceId()
72
c.Assert(err, gc.IsNil)
73
c.Assert(instanceId, gc.Equals, instance.Id("manual:"+hostname))
77
// Attempting to provision a machine twice should fail. We effect
78
// this by checking for existing juju upstart configurations.
79
defer sshresponse(c, checkProvisionedScript, "/etc/init/jujud-machine-0.conf", 0)()
80
_, err = ProvisionMachine(args)
81
c.Assert(err, gc.Equals, ErrProvisioned)
82
defer sshresponse(c, checkProvisionedScript, "/etc/init/jujud-machine-0.conf", 255)()
83
_, err = ProvisionMachine(args)
84
c.Assert(err, gc.ErrorMatches, "error checking if provisioned: exit status 255")