forked from UbuntuEvangelist/juju
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prechecker_test.go
145 lines (128 loc) · 5.01 KB
/
prechecker_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright 2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package state_test
import (
"fmt"
"github.com/juju/errors"
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"
"github.com/juju/juju/agent"
"github.com/juju/juju/constraints"
"github.com/juju/juju/environs/config"
"github.com/juju/juju/instance"
"github.com/juju/juju/state"
)
type PrecheckerSuite struct {
ConnSuite
prechecker mockPrechecker
}
var _ = gc.Suite(&PrecheckerSuite{})
type mockPrechecker struct {
precheckInstanceError error
precheckInstanceSeries string
precheckInstanceConstraints constraints.Value
precheckInstancePlacement string
}
func (p *mockPrechecker) PrecheckInstance(series string, cons constraints.Value, placement string) error {
p.precheckInstanceSeries = series
p.precheckInstanceConstraints = cons
p.precheckInstancePlacement = placement
return p.precheckInstanceError
}
func (s *PrecheckerSuite) SetUpTest(c *gc.C) {
s.ConnSuite.SetUpTest(c)
s.prechecker = mockPrechecker{}
s.policy.GetPrechecker = func(*config.Config) (state.Prechecker, error) {
return &s.prechecker, nil
}
}
func (s *PrecheckerSuite) TestPrecheckInstance(c *gc.C) {
// PrecheckInstance should be called with the specified
// series and placement, and the specified constraints
// merged with the environment constraints, when attempting
// to create an instance.
envCons := constraints.MustParse("mem=4G")
placement := "abc123"
template, err := s.addOneMachine(c, envCons, placement)
c.Assert(err, jc.ErrorIsNil)
c.Assert(s.prechecker.precheckInstanceSeries, gc.Equals, template.Series)
c.Assert(s.prechecker.precheckInstancePlacement, gc.Equals, placement)
validator := constraints.NewValidator()
cons, err := validator.Merge(envCons, template.Constraints)
c.Assert(err, jc.ErrorIsNil)
c.Assert(s.prechecker.precheckInstanceConstraints, gc.DeepEquals, cons)
}
func (s *PrecheckerSuite) TestPrecheckErrors(c *gc.C) {
// Ensure that AddOneMachine fails when PrecheckInstance returns an error.
s.prechecker.precheckInstanceError = fmt.Errorf("no instance for you")
_, err := s.addOneMachine(c, constraints.Value{}, "placement")
c.Assert(err, gc.ErrorMatches, ".*no instance for you")
// If the policy's Prechecker method fails, that will be returned first.
s.policy.GetPrechecker = func(*config.Config) (state.Prechecker, error) {
return nil, fmt.Errorf("no prechecker for you")
}
_, err = s.addOneMachine(c, constraints.Value{}, "placement")
c.Assert(err, gc.ErrorMatches, ".*no prechecker for you")
}
func (s *PrecheckerSuite) TestPrecheckPrecheckerUnimplemented(c *gc.C) {
var precheckerErr error
s.policy.GetPrechecker = func(*config.Config) (state.Prechecker, error) {
return nil, precheckerErr
}
_, err := s.addOneMachine(c, constraints.Value{}, "placement")
c.Assert(err, gc.ErrorMatches, "cannot add a new machine: policy returned nil prechecker without an error")
precheckerErr = errors.NotImplementedf("Prechecker")
_, err = s.addOneMachine(c, constraints.Value{}, "placement")
c.Assert(err, jc.ErrorIsNil)
}
func (s *PrecheckerSuite) TestPrecheckNoPolicy(c *gc.C) {
s.policy.GetPrechecker = func(*config.Config) (state.Prechecker, error) {
c.Errorf("should not have been invoked")
return nil, nil
}
state.SetPolicy(s.State, nil)
_, err := s.addOneMachine(c, constraints.Value{}, "placement")
c.Assert(err, jc.ErrorIsNil)
}
func (s *PrecheckerSuite) addOneMachine(c *gc.C, envCons constraints.Value, placement string) (state.MachineTemplate, error) {
err := s.State.SetEnvironConstraints(envCons)
c.Assert(err, jc.ErrorIsNil)
oneJob := []state.MachineJob{state.JobHostUnits}
extraCons := constraints.MustParse("cpu-cores=4")
template := state.MachineTemplate{
Series: "precise",
Constraints: extraCons,
Jobs: oneJob,
Placement: placement,
}
_, err = s.State.AddOneMachine(template)
return template, err
}
func (s *PrecheckerSuite) TestPrecheckInstanceInjectMachine(c *gc.C) {
template := state.MachineTemplate{
InstanceId: instance.Id("bootstrap"),
Series: "precise",
Nonce: agent.BootstrapNonce,
Jobs: []state.MachineJob{state.JobManageEnviron},
Placement: "anyoldthing",
}
_, err := s.State.AddOneMachine(template)
c.Assert(err, jc.ErrorIsNil)
// PrecheckInstance should not have been called, as we've
// injected a machine with an existing instance.
c.Assert(s.prechecker.precheckInstanceSeries, gc.Equals, "")
c.Assert(s.prechecker.precheckInstancePlacement, gc.Equals, "")
}
func (s *PrecheckerSuite) TestPrecheckContainerNewMachine(c *gc.C) {
// Attempting to add a container to a new machine should cause
// PrecheckInstance to be called.
template := state.MachineTemplate{
Series: "precise",
Jobs: []state.MachineJob{state.JobHostUnits},
Placement: "intertubes",
}
_, err := s.State.AddMachineInsideNewMachine(template, template, instance.LXC)
c.Assert(err, jc.ErrorIsNil)
c.Assert(s.prechecker.precheckInstanceSeries, gc.Equals, template.Series)
c.Assert(s.prechecker.precheckInstancePlacement, gc.Equals, template.Placement)
}