~james-page/ubuntu/saucy/juju-core/1.16.5

« back to all changes in this revision

Viewing changes to src/launchpad.net/juju-core/environs/manual/provisioner_test.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-09-03 14:22:22 UTC
  • mfrom: (1.1.7)
  • Revision ID: package-import@ubuntu.com-20130903142222-9mes2r8wqr0bs7lp
Tags: 1.13.3-0ubuntu1
New upstream point release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
        "strings"
 
10
 
 
11
        gc "launchpad.net/gocheck"
 
12
 
 
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"
 
17
)
 
18
 
 
19
type provisionerSuite struct {
 
20
        testing.JujuConnSuite
 
21
}
 
22
 
 
23
var _ = gc.Suite(&provisionerSuite{})
 
24
 
 
25
func (s *provisionerSuite) getArgs(c *gc.C) ProvisionMachineArgs {
 
26
        hostname, err := os.Hostname()
 
27
        c.Assert(err, gc.IsNil)
 
28
        return ProvisionMachineArgs{
 
29
                Host:  hostname,
 
30
                State: s.State,
 
31
        }
 
32
}
 
33
 
 
34
func (s *provisionerSuite) TestProvisionMachine(c *gc.C) {
 
35
        // Prepare a mock ssh response for the detection phase.
 
36
        detectionoutput := strings.Join([]string{
 
37
                "edgy",
 
38
                "armv4",
 
39
                "MemTotal: 4096 kB",
 
40
                "processor: 0",
 
41
        }, "\n")
 
42
 
 
43
        args := s.getArgs(c)
 
44
        hostname := args.Host
 
45
        args.Host = "ubuntu@" + args.Host
 
46
 
 
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")
 
51
        c.Assert(m, gc.IsNil)
 
52
 
 
53
        toolsList, err := tools.FindBootstrapTools(s.Conn.Environ, constraints.Value{})
 
54
        c.Assert(err, gc.IsNil)
 
55
        args.Tools = toolsList[0]
 
56
 
 
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)
 
62
                if errorCode != 0 {
 
63
                        c.Assert(err, gc.ErrorMatches, fmt.Sprintf("exit status %d", errorCode))
 
64
                        c.Assert(m, gc.IsNil)
 
65
                } else {
 
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))
 
74
                }
 
75
        }
 
76
 
 
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")
 
85
}