~rogpeppe/juju-core/438-local-instance-Addresses

« back to all changes in this revision

Viewing changes to provider/common/bootstrap_test.go

  • Committer: Tarmac
  • Author(s): William Reade
  • Date: 2013-10-02 10:38:04 UTC
  • mfrom: (1915.1.3 juju-core)
  • Revision ID: tarmac-20131002103804-zxlnv7ik17unxc3p
[r=fwereade] provider/common: Destroy, and tests

Added a common implementation of Destroy, and tests for Bootstrap which had
somehow never been written. Also drive-by removed the machineId arg from
consideration when bootstrapping, because, WTF... a non-0 bootstrap machine
would break *everything*.

https://codereview.appspot.com/14254043/

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 common_test
 
5
 
 
6
import (
 
7
        "fmt"
 
8
 
 
9
        gc "launchpad.net/gocheck"
 
10
        "launchpad.net/loggo"
 
11
 
 
12
        "launchpad.net/juju-core/constraints"
 
13
        "launchpad.net/juju-core/environs"
 
14
        "launchpad.net/juju-core/environs/cloudinit"
 
15
        "launchpad.net/juju-core/instance"
 
16
        "launchpad.net/juju-core/provider/common"
 
17
        jc "launchpad.net/juju-core/testing/checkers"
 
18
        "launchpad.net/juju-core/testing/testbase"
 
19
        "launchpad.net/juju-core/tools"
 
20
        "launchpad.net/juju-core/version"
 
21
)
 
22
 
 
23
type BootstrapSuite struct {
 
24
        testbase.LoggingSuite
 
25
}
 
26
 
 
27
var _ = gc.Suite(&BootstrapSuite{})
 
28
 
 
29
func (s *BootstrapSuite) TestCannotWriteStateFile(c *gc.C) {
 
30
        brokenStorage := &mockStorage{putErr: fmt.Errorf("noes!")}
 
31
        env := &mockEnviron{storage: brokenStorage}
 
32
        err := common.Bootstrap(env, constraints.Value{}, nil)
 
33
        c.Assert(err, gc.ErrorMatches, "cannot create initial state file: noes!")
 
34
}
 
35
 
 
36
func (s *BootstrapSuite) TestCannotStartInstance(c *gc.C) {
 
37
        stor := newStorage(s, c)
 
38
        checkURL, err := stor.URL(common.StateFile)
 
39
        c.Assert(err, gc.IsNil)
 
40
        checkCons := constraints.MustParse("mem=8G")
 
41
        checkTools := tools.List{&tools.Tools{Version: version.Current}}
 
42
 
 
43
        startInstance := func(
 
44
                cons constraints.Value, possibleTools tools.List, mcfg *cloudinit.MachineConfig,
 
45
        ) (
 
46
                instance.Instance, *instance.HardwareCharacteristics, error,
 
47
        ) {
 
48
                c.Assert(cons, gc.DeepEquals, checkCons)
 
49
                c.Assert(possibleTools, gc.DeepEquals, checkTools)
 
50
                c.Assert(mcfg, gc.DeepEquals, environs.NewBootstrapMachineConfig(checkURL))
 
51
                return nil, nil, fmt.Errorf("meh, not started")
 
52
        }
 
53
 
 
54
        env := &mockEnviron{
 
55
                storage:       stor,
 
56
                startInstance: startInstance,
 
57
        }
 
58
 
 
59
        err = common.Bootstrap(env, checkCons, checkTools)
 
60
        c.Assert(err, gc.ErrorMatches, "cannot start bootstrap instance: meh, not started")
 
61
}
 
62
 
 
63
func (s *BootstrapSuite) TestCannotRecordStartedInstance(c *gc.C) {
 
64
        innerStorage := newStorage(s, c)
 
65
        stor := &mockStorage{Storage: innerStorage}
 
66
 
 
67
        startInstance := func(
 
68
                _ constraints.Value, _ tools.List, _ *cloudinit.MachineConfig,
 
69
        ) (
 
70
                instance.Instance, *instance.HardwareCharacteristics, error,
 
71
        ) {
 
72
                stor.putErr = fmt.Errorf("suddenly a wild blah")
 
73
                return &mockInstance{id: "i-blah"}, nil, nil
 
74
        }
 
75
 
 
76
        var stopped []instance.Instance
 
77
        stopInstances := func(instances []instance.Instance) error {
 
78
                stopped = append(stopped, instances...)
 
79
                return nil
 
80
        }
 
81
 
 
82
        env := &mockEnviron{
 
83
                storage:       stor,
 
84
                startInstance: startInstance,
 
85
                stopInstances: stopInstances,
 
86
        }
 
87
 
 
88
        err := common.Bootstrap(env, constraints.Value{}, nil)
 
89
        c.Assert(err, gc.ErrorMatches, "cannot save state: suddenly a wild blah")
 
90
        c.Assert(stopped, gc.HasLen, 1)
 
91
        c.Assert(stopped[0].Id(), gc.Equals, instance.Id("i-blah"))
 
92
}
 
93
 
 
94
func (s *BootstrapSuite) TestCannotRecordThenCannotStop(c *gc.C) {
 
95
        innerStorage := newStorage(s, c)
 
96
        stor := &mockStorage{Storage: innerStorage}
 
97
 
 
98
        startInstance := func(
 
99
                _ constraints.Value, _ tools.List, _ *cloudinit.MachineConfig,
 
100
        ) (
 
101
                instance.Instance, *instance.HardwareCharacteristics, error,
 
102
        ) {
 
103
                stor.putErr = fmt.Errorf("suddenly a wild blah")
 
104
                return &mockInstance{id: "i-blah"}, nil, nil
 
105
        }
 
106
 
 
107
        var stopped []instance.Instance
 
108
        stopInstances := func(instances []instance.Instance) error {
 
109
                stopped = append(stopped, instances...)
 
110
                return fmt.Errorf("bork bork borken")
 
111
        }
 
112
 
 
113
        tw := &loggo.TestWriter{}
 
114
        c.Assert(loggo.RegisterWriter("bootstrap-tester", tw, loggo.DEBUG), gc.IsNil)
 
115
        defer loggo.RemoveWriter("bootstrap-tester")
 
116
 
 
117
        env := &mockEnviron{
 
118
                storage:       stor,
 
119
                startInstance: startInstance,
 
120
                stopInstances: stopInstances,
 
121
        }
 
122
 
 
123
        err := common.Bootstrap(env, constraints.Value{}, nil)
 
124
        c.Assert(err, gc.ErrorMatches, "cannot save state: suddenly a wild blah")
 
125
        c.Assert(stopped, gc.HasLen, 1)
 
126
        c.Assert(stopped[0].Id(), gc.Equals, instance.Id("i-blah"))
 
127
        c.Assert(tw.Log, jc.LogMatches, []jc.SimpleMessage{{
 
128
                loggo.ERROR, `cannot stop failed bootstrap instance "i-blah": bork bork borken`,
 
129
        }})
 
130
}
 
131
 
 
132
func (s *BootstrapSuite) TestSuccess(c *gc.C) {
 
133
        stor := newStorage(s, c)
 
134
        checkInstanceId := "i-success"
 
135
        checkHardware := instance.MustParseHardware("mem=2T")
 
136
 
 
137
        checkURL := ""
 
138
        startInstance := func(
 
139
                _ constraints.Value, _ tools.List, mcfg *cloudinit.MachineConfig,
 
140
        ) (
 
141
                instance.Instance, *instance.HardwareCharacteristics, error,
 
142
        ) {
 
143
                checkURL = mcfg.StateInfoURL
 
144
                return &mockInstance{id: checkInstanceId}, &checkHardware, nil
 
145
        }
 
146
 
 
147
        env := &mockEnviron{
 
148
                storage:       stor,
 
149
                startInstance: startInstance,
 
150
        }
 
151
        err := common.Bootstrap(env, constraints.Value{}, nil)
 
152
        c.Assert(err, gc.IsNil)
 
153
 
 
154
        savedState, err := common.LoadStateFromURL(checkURL)
 
155
        c.Assert(err, gc.IsNil)
 
156
        c.Assert(savedState, gc.DeepEquals, &common.BootstrapState{
 
157
                StateInstances:  []instance.Id{instance.Id(checkInstanceId)},
 
158
                Characteristics: []instance.HardwareCharacteristics{checkHardware},
 
159
        })
 
160
}