Merge lp:~axwalk/juju-core/azure-test-ensure-called into lp:~go-bot/juju-core/trunk

Proposed by Andrew Wilkins
Status: Merged
Approved by: Andrew Wilkins
Approved revision: no longer in the source branch.
Merged at revision: 2565
Proposed branch: lp:~axwalk/juju-core/azure-test-ensure-called
Merge into: lp:~go-bot/juju-core/trunk
Diff against target: 218 lines (+99/-51)
1 file modified
provider/azure/environ_test.go (+99/-51)
To merge this branch: bzr merge lp:~axwalk/juju-core/azure-test-ensure-called
Reviewer Review Type Date Requested Status
Juju Engineering Pending
Review via email: mp+214147@code.launchpad.net

Commit message

provider/azure: ensure tests work correctly

Some tests I wrote call StartInstance to
check the parameters passed to createInstance.
The tests don't actually verify that it called
that method successfully, and all validation
is done within the method. Updated the tests
to ensure from outside that the method is called.

https://codereview.appspot.com/84290044/

Description of the change

provider/azure: ensure tests work correctly

Some tests I wrote call StartInstance to
check the parameters passed to createInstance.
The tests don't actually verify that it called
that method successfully, and all validation
is done within the method. Updated the tests
to ensure from outside that the method is called.

https://codereview.appspot.com/84290044/

To post a comment you must log in.
Revision history for this message
Andrew Wilkins (axwalk) wrote :
Download full text (3.7 KiB)

Reviewers: mp+214147_code.launchpad.net,

Message:
Please take a look.

Description:
provider/azure: ensure tests work correctly

Some tests I wrote call StartInstance to
check the parameters passed to createInstance.
The tests don't actually verify that it called
that method successfully, and all validation
is done within the method. Updated the tests
to ensure from outside that the method is called.

https://code.launchpad.net/~axwalk/juju-core/azure-test-ensure-called/+merge/214147

(do not edit description out of merge proposal)

Please review this at https://codereview.appspot.com/84290044/

Affected files (+26, -6 lines):
   A [revision details]
   M provider/azure/environ_test.go

Index: [revision details]
=== added file '[revision details]'
--- [revision details] 2012-01-01 00:00:00 +0000
+++ [revision details] 2012-01-01 00:00:00 +0000
@@ -0,0 +1,2 @@
+Old revision: tarmac-20140404003547-v7dzlndz9bq9h4qd
+New revision: <email address hidden>

Index: provider/azure/environ_test.go
=== modified file 'provider/azure/environ_test.go'
--- provider/azure/environ_test.go 2014-04-03 18:04:56 +0000
+++ provider/azure/environ_test.go 2014-04-04 01:54:24 +0000
@@ -1429,44 +1429,62 @@
    Tag: "machine-1",
   }
   var params environs.StartInstanceParams
- params.Tools = envtesting.AssertUploadFakeToolsVersions(c, env.storage,
envtesting.V120p...)
- params.MachineConfig = environs.NewMachineConfig("1", "yanonce", nil,
nil, stateInfo, apiInfo)
+ params.Tools = envtesting.AssertUploadFakeToolsVersions(
+ c, env.storage, envtesting.V120p...,
+ )
+ params.MachineConfig = environs.NewMachineConfig(
+ "1", "yanonce", nil, nil, stateInfo, apiInfo,
+ )

   // Start out with availability sets disabled.
   env.ecfg.attrs["availability-sets-enabled"] = false

   var expectServiceName string
   var expectStateServer bool
+ var called int
   s.PatchValue(&createInstance, func(env *azureEnviron, azure
*gwacl.ManagementAPI, role *gwacl.Role, serviceName string, stateServer
bool) (instance.Instance, error) {
    c.Assert(serviceName, gc.Equals, expectServiceName)
    c.Assert(stateServer, gc.Equals, expectStateServer)
+ called++
    return nil, nil
   })
   env.StartInstance(params)
+ c.Assert(called, gc.Equals, 1)

- // DistributionGroup won't have an effect if
availability-sets-enabled=false.
+ // DistributionGroup won't have an effect if
+ // availability-sets-enabled=false.
   expectServiceName = ""
   params.DistributionGroup = func() ([]instance.Id, error) {
- return []instance.Id{instance.Id(env.getEnvPrefix()
+ "whatever-role0")}, nil
+ return []instance.Id{
+ instance.Id(env.getEnvPrefix() + "whatever-role0"),
+ }, nil
   }
   env.StartInstance(params)
+ c.Assert(called, gc.Equals, 2)

   env.ecfg.attrs["availability-sets-enabled"] = true
   expectServiceName = "juju-testenv-whatever"
   env.StartInstance(params)
+ c.Assert(called, gc.Equals, 3)

   expectServiceName = ""
   params.DistributionGroup = nil
   env.StartInstance(params)
+ c.Assert(called, gc.Equals, 4)

   // Empty distribution group is equivalent to no DistributionGroup
function.
   params.DistributionGroup = func() ([]ins...

Read more...

Revision history for this message
Ian Booth (wallyworld) wrote :

LGTM. I think thst one big test should / could be split up with a common
method to set stuff up, and then in each test a call to start instance
for the specific scenario being tested.

https://codereview.appspot.com/84290044/

Revision history for this message
Andrew Wilkins (axwalk) wrote :
Revision history for this message
Andrew Wilkins (axwalk) wrote :

On 2014/04/04 04:06:14, wallyworld wrote:
> LGTM. I think thst one big test should / could be split up with a
common method
> to set stuff up, and then in each test a call to start instance for
the specific
> scenario being tested.

Done.

https://codereview.appspot.com/84290044/

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'provider/azure/environ_test.go'
2--- provider/azure/environ_test.go 2014-04-03 18:04:56 +0000
3+++ provider/azure/environ_test.go 2014-04-04 06:04:51 +0000
4@@ -16,6 +16,7 @@
5 "strings"
6 "sync"
7
8+ "github.com/juju/testing"
9 jc "github.com/juju/testing/checkers"
10 gc "launchpad.net/gocheck"
11 "launchpad.net/gwacl"
12@@ -34,14 +35,19 @@
13 "launchpad.net/juju-core/state"
14 "launchpad.net/juju-core/state/api"
15 apiparams "launchpad.net/juju-core/state/api/params"
16- "launchpad.net/juju-core/testing"
17+ coretesting "launchpad.net/juju-core/testing"
18 )
19
20+type baseEnvironSuite struct {
21+ providerSuite
22+}
23+
24 type environSuite struct {
25- providerSuite
26+ baseEnvironSuite
27 }
28
29 var _ = gc.Suite(&environSuite{})
30+var _ = gc.Suite(&startInstanceSuite{})
31
32 // makeEnviron creates a fake azureEnviron with arbitrary configuration.
33 func makeEnviron(c *gc.C) *azureEnviron {
34@@ -63,7 +69,7 @@
35 // setDummyStorage injects the local provider's fake storage implementation
36 // into the given environment, so that tests can manipulate storage as if it
37 // were real.
38-func (s *environSuite) setDummyStorage(c *gc.C, env *azureEnviron) {
39+func (s *baseEnvironSuite) setDummyStorage(c *gc.C, env *azureEnviron) {
40 closer, storage, _ := envtesting.CreateLocalTestStorage(c)
41 env.storage = storage
42 s.AddCleanup(func(c *gc.C) { closer.Close() })
43@@ -100,7 +106,7 @@
44
45 func (*environSuite) TestGetSnapshotLocksEnviron(c *gc.C) {
46 original := azureEnviron{}
47- testing.TestLockingFunction(&original.Mutex, func() { original.getSnapshot() })
48+ coretesting.TestLockingFunction(&original.Mutex, func() { original.getSnapshot() })
49 }
50
51 func (*environSuite) TestName(c *gc.C) {
52@@ -117,7 +123,7 @@
53
54 func (*environSuite) TestConfigLocksEnviron(c *gc.C) {
55 env := azureEnviron{name: "env", ecfg: new(azureEnvironConfig)}
56- testing.TestLockingFunction(&env.Mutex, func() { env.Config() })
57+ coretesting.TestLockingFunction(&env.Mutex, func() { env.Config() })
58 }
59
60 func (*environSuite) TestGetManagementAPI(c *gc.C) {
61@@ -456,7 +462,7 @@
62 cfg, err := config.New(config.NoDefaults, makeAzureConfigMap(c))
63 c.Assert(err, gc.IsNil)
64
65- testing.TestLockingFunction(&env.Mutex, func() { env.SetConfig(cfg) })
66+ coretesting.TestLockingFunction(&env.Mutex, func() { env.SetConfig(cfg) })
67 }
68
69 func (*environSuite) TestSetConfigWillNotUpdateName(c *gc.C) {
70@@ -1412,61 +1418,103 @@
71 c.Assert(err, gc.IsNil)
72 }
73
74-func (s *environSuite) TestStartInstance(c *gc.C) {
75- env := makeEnviron(c)
76- s.setDummyStorage(c, env)
77- env.ecfg.attrs["force-image-name"] = "my-image"
78+type startInstanceSuite struct {
79+ baseEnvironSuite
80+ env *azureEnviron
81+ params environs.StartInstanceParams
82+}
83+
84+func (s *startInstanceSuite) SetUpTest(c *gc.C) {
85+ s.baseEnvironSuite.SetUpTest(c)
86+ s.env = makeEnviron(c)
87+ s.setDummyStorage(c, s.env)
88+ s.env.ecfg.attrs["force-image-name"] = "my-image"
89 stateInfo := &state.Info{
90 Addrs: []string{"localhost:123"},
91- CACert: []byte(testing.CACert),
92+ CACert: []byte(coretesting.CACert),
93 Password: "password",
94 Tag: "machine-1",
95 }
96 apiInfo := &api.Info{
97 Addrs: []string{"localhost:124"},
98- CACert: []byte(testing.CACert),
99+ CACert: []byte(coretesting.CACert),
100 Password: "admin",
101 Tag: "machine-1",
102 }
103- var params environs.StartInstanceParams
104- params.Tools = envtesting.AssertUploadFakeToolsVersions(c, env.storage, envtesting.V120p...)
105- params.MachineConfig = environs.NewMachineConfig("1", "yanonce", nil, nil, stateInfo, apiInfo)
106-
107- // Start out with availability sets disabled.
108- env.ecfg.attrs["availability-sets-enabled"] = false
109-
110- var expectServiceName string
111- var expectStateServer bool
112- s.PatchValue(&createInstance, func(env *azureEnviron, azure *gwacl.ManagementAPI, role *gwacl.Role, serviceName string, stateServer bool) (instance.Instance, error) {
113- c.Assert(serviceName, gc.Equals, expectServiceName)
114- c.Assert(stateServer, gc.Equals, expectStateServer)
115+ s.params = environs.StartInstanceParams{
116+ Tools: envtesting.AssertUploadFakeToolsVersions(
117+ c, s.env.storage, envtesting.V120p...,
118+ ),
119+ MachineConfig: environs.NewMachineConfig(
120+ "1", "yanonce", nil, nil, stateInfo, apiInfo,
121+ ),
122+ }
123+}
124+
125+func (s *startInstanceSuite) startInstance(c *gc.C) (serviceName string, stateServer bool) {
126+ var called bool
127+ restore := testing.PatchValue(&createInstance, func(env *azureEnviron, azure *gwacl.ManagementAPI, role *gwacl.Role, serviceNameArg string, stateServerArg bool) (instance.Instance, error) {
128+ serviceName = serviceNameArg
129+ stateServer = stateServerArg
130+ called = true
131 return nil, nil
132 })
133- env.StartInstance(params)
134-
135- // DistributionGroup won't have an effect if availability-sets-enabled=false.
136- expectServiceName = ""
137- params.DistributionGroup = func() ([]instance.Id, error) {
138- return []instance.Id{instance.Id(env.getEnvPrefix() + "whatever-role0")}, nil
139- }
140- env.StartInstance(params)
141-
142- env.ecfg.attrs["availability-sets-enabled"] = true
143- expectServiceName = "juju-testenv-whatever"
144- env.StartInstance(params)
145-
146- expectServiceName = ""
147- params.DistributionGroup = nil
148- env.StartInstance(params)
149-
150- // Empty distribution group is equivalent to no DistributionGroup function.
151- params.DistributionGroup = func() ([]instance.Id, error) {
152- return nil, nil
153- }
154- env.StartInstance(params)
155-
156- // If the machine has the JobManagesEnviron job, we should see stateServer==true.
157- expectStateServer = true
158- params.MachineConfig.Jobs = []apiparams.MachineJob{apiparams.JobHostUnits, apiparams.JobManageEnviron}
159- env.StartInstance(params)
160+ defer restore()
161+ _, _, err := s.env.StartInstance(s.params)
162+ c.Assert(err, gc.IsNil)
163+ c.Assert(called, jc.IsTrue)
164+ return serviceName, stateServer
165+}
166+
167+func (s *startInstanceSuite) TestStartInstanceDistributionGroupError(c *gc.C) {
168+ s.params.DistributionGroup = func() ([]instance.Id, error) {
169+ return nil, fmt.Errorf("DistributionGroupError")
170+ }
171+ s.env.ecfg.attrs["availability-sets-enabled"] = true
172+ _, _, err := s.env.StartInstance(s.params)
173+ c.Assert(err, gc.ErrorMatches, "DistributionGroupError")
174+ // DistributionGroup should not be called if availability-sets-enabled=false.
175+ s.env.ecfg.attrs["availability-sets-enabled"] = false
176+ s.startInstance(c)
177+}
178+
179+func (s *startInstanceSuite) TestStartInstanceDistributionGroupEmpty(c *gc.C) {
180+ // serviceName will be empty if DistributionGroup is nil or returns nothing.
181+ s.env.ecfg.attrs["availability-sets-enabled"] = true
182+ serviceName, _ := s.startInstance(c)
183+ c.Assert(serviceName, gc.Equals, "")
184+ s.params.DistributionGroup = func() ([]instance.Id, error) { return nil, nil }
185+ serviceName, _ = s.startInstance(c)
186+ c.Assert(serviceName, gc.Equals, "")
187+}
188+
189+func (s *startInstanceSuite) TestStartInstanceDistributionGroup(c *gc.C) {
190+ s.params.DistributionGroup = func() ([]instance.Id, error) {
191+ return []instance.Id{
192+ instance.Id(s.env.getEnvPrefix() + "whatever-role0"),
193+ }, nil
194+ }
195+ // DistributionGroup will only have an effect if
196+ // availability-sets-enabled=true.
197+ s.env.ecfg.attrs["availability-sets-enabled"] = false
198+ serviceName, _ := s.startInstance(c)
199+ c.Assert(serviceName, gc.Equals, "")
200+ s.env.ecfg.attrs["availability-sets-enabled"] = true
201+ serviceName, _ = s.startInstance(c)
202+ c.Assert(serviceName, gc.Equals, "juju-testenv-whatever")
203+}
204+
205+func (s *startInstanceSuite) TestStartInstanceStateServerJobs(c *gc.C) {
206+ // If the machine has the JobManagesEnviron job,
207+ // we should see stateServer==true.
208+ s.params.MachineConfig.Jobs = []apiparams.MachineJob{
209+ apiparams.JobHostUnits,
210+ }
211+ _, stateServer := s.startInstance(c)
212+ c.Assert(stateServer, jc.IsFalse)
213+ s.params.MachineConfig.Jobs = []apiparams.MachineJob{
214+ apiparams.JobHostUnits, apiparams.JobManageEnviron,
215+ }
216+ _, stateServer = s.startInstance(c)
217+ c.Assert(stateServer, jc.IsTrue)
218 }

Subscribers

People subscribed via source and target branches

to status/vote changes: