~dave-cheney/juju-core/153-fix-release-tools-script

« back to all changes in this revision

Viewing changes to environs/manual/provisioner_test.go

  • Committer: Tarmac
  • Author(s): Andrew Wilkins
  • Date: 2013-08-30 01:48:39 UTC
  • mfrom: (1628.11.22 juju-add-machine)
  • Revision ID: tarmac-20130830014839-infsllif52ywy6yq
[r=axwalk] Update add-machine for manual provisioning

juju add-machine is updated to use a new package,
environs/manual, to manually provision tools and
a machine agent to an existing machine.

When a manually provisioned machine is destroyed
via juju destroy-machine, the machine agent will
detect its termination and remove its upstart
configuration file. There is currently no cleanup
of the data or log directories; this will be done
in a follow-up pending discussion.

When the machine goes to Dead, a provisioner will
remove the machine from state just like any other
machine.

TODO: destroy-environment will currently leak
manually provisioned machines. A follow-up will
address this by requiring users to individually
destroy-machine before destroy-environment will
proceed. Alternatively (or perhaps additionally),
destroy-environment may take a flag to automatically
do this.

https://codereview.appspot.com/12831043/

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
}