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
=== 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 06:04:51 +0000
@@ -16,6 +16,7 @@
16 "strings"16 "strings"
17 "sync"17 "sync"
1818
19 "github.com/juju/testing"
19 jc "github.com/juju/testing/checkers"20 jc "github.com/juju/testing/checkers"
20 gc "launchpad.net/gocheck"21 gc "launchpad.net/gocheck"
21 "launchpad.net/gwacl"22 "launchpad.net/gwacl"
@@ -34,14 +35,19 @@
34 "launchpad.net/juju-core/state"35 "launchpad.net/juju-core/state"
35 "launchpad.net/juju-core/state/api"36 "launchpad.net/juju-core/state/api"
36 apiparams "launchpad.net/juju-core/state/api/params"37 apiparams "launchpad.net/juju-core/state/api/params"
37 "launchpad.net/juju-core/testing"38 coretesting "launchpad.net/juju-core/testing"
38)39)
3940
41type baseEnvironSuite struct {
42 providerSuite
43}
44
40type environSuite struct {45type environSuite struct {
41 providerSuite46 baseEnvironSuite
42}47}
4348
44var _ = gc.Suite(&environSuite{})49var _ = gc.Suite(&environSuite{})
50var _ = gc.Suite(&startInstanceSuite{})
4551
46// makeEnviron creates a fake azureEnviron with arbitrary configuration.52// makeEnviron creates a fake azureEnviron with arbitrary configuration.
47func makeEnviron(c *gc.C) *azureEnviron {53func makeEnviron(c *gc.C) *azureEnviron {
@@ -63,7 +69,7 @@
63// setDummyStorage injects the local provider's fake storage implementation69// setDummyStorage injects the local provider's fake storage implementation
64// into the given environment, so that tests can manipulate storage as if it70// into the given environment, so that tests can manipulate storage as if it
65// were real.71// were real.
66func (s *environSuite) setDummyStorage(c *gc.C, env *azureEnviron) {72func (s *baseEnvironSuite) setDummyStorage(c *gc.C, env *azureEnviron) {
67 closer, storage, _ := envtesting.CreateLocalTestStorage(c)73 closer, storage, _ := envtesting.CreateLocalTestStorage(c)
68 env.storage = storage74 env.storage = storage
69 s.AddCleanup(func(c *gc.C) { closer.Close() })75 s.AddCleanup(func(c *gc.C) { closer.Close() })
@@ -100,7 +106,7 @@
100106
101func (*environSuite) TestGetSnapshotLocksEnviron(c *gc.C) {107func (*environSuite) TestGetSnapshotLocksEnviron(c *gc.C) {
102 original := azureEnviron{}108 original := azureEnviron{}
103 testing.TestLockingFunction(&original.Mutex, func() { original.getSnapshot() })109 coretesting.TestLockingFunction(&original.Mutex, func() { original.getSnapshot() })
104}110}
105111
106func (*environSuite) TestName(c *gc.C) {112func (*environSuite) TestName(c *gc.C) {
@@ -117,7 +123,7 @@
117123
118func (*environSuite) TestConfigLocksEnviron(c *gc.C) {124func (*environSuite) TestConfigLocksEnviron(c *gc.C) {
119 env := azureEnviron{name: "env", ecfg: new(azureEnvironConfig)}125 env := azureEnviron{name: "env", ecfg: new(azureEnvironConfig)}
120 testing.TestLockingFunction(&env.Mutex, func() { env.Config() })126 coretesting.TestLockingFunction(&env.Mutex, func() { env.Config() })
121}127}
122128
123func (*environSuite) TestGetManagementAPI(c *gc.C) {129func (*environSuite) TestGetManagementAPI(c *gc.C) {
@@ -456,7 +462,7 @@
456 cfg, err := config.New(config.NoDefaults, makeAzureConfigMap(c))462 cfg, err := config.New(config.NoDefaults, makeAzureConfigMap(c))
457 c.Assert(err, gc.IsNil)463 c.Assert(err, gc.IsNil)
458464
459 testing.TestLockingFunction(&env.Mutex, func() { env.SetConfig(cfg) })465 coretesting.TestLockingFunction(&env.Mutex, func() { env.SetConfig(cfg) })
460}466}
461467
462func (*environSuite) TestSetConfigWillNotUpdateName(c *gc.C) {468func (*environSuite) TestSetConfigWillNotUpdateName(c *gc.C) {
@@ -1412,61 +1418,103 @@
1412 c.Assert(err, gc.IsNil)1418 c.Assert(err, gc.IsNil)
1413}1419}
14141420
1415func (s *environSuite) TestStartInstance(c *gc.C) {1421type startInstanceSuite struct {
1416 env := makeEnviron(c)1422 baseEnvironSuite
1417 s.setDummyStorage(c, env)1423 env *azureEnviron
1418 env.ecfg.attrs["force-image-name"] = "my-image"1424 params environs.StartInstanceParams
1425}
1426
1427func (s *startInstanceSuite) SetUpTest(c *gc.C) {
1428 s.baseEnvironSuite.SetUpTest(c)
1429 s.env = makeEnviron(c)
1430 s.setDummyStorage(c, s.env)
1431 s.env.ecfg.attrs["force-image-name"] = "my-image"
1419 stateInfo := &state.Info{1432 stateInfo := &state.Info{
1420 Addrs: []string{"localhost:123"},1433 Addrs: []string{"localhost:123"},
1421 CACert: []byte(testing.CACert),1434 CACert: []byte(coretesting.CACert),
1422 Password: "password",1435 Password: "password",
1423 Tag: "machine-1",1436 Tag: "machine-1",
1424 }1437 }
1425 apiInfo := &api.Info{1438 apiInfo := &api.Info{
1426 Addrs: []string{"localhost:124"},1439 Addrs: []string{"localhost:124"},
1427 CACert: []byte(testing.CACert),1440 CACert: []byte(coretesting.CACert),
1428 Password: "admin",1441 Password: "admin",
1429 Tag: "machine-1",1442 Tag: "machine-1",
1430 }1443 }
1431 var params environs.StartInstanceParams1444 s.params = environs.StartInstanceParams{
1432 params.Tools = envtesting.AssertUploadFakeToolsVersions(c, env.storage, envtesting.V120p...)1445 Tools: envtesting.AssertUploadFakeToolsVersions(
1433 params.MachineConfig = environs.NewMachineConfig("1", "yanonce", nil, nil, stateInfo, apiInfo)1446 c, s.env.storage, envtesting.V120p...,
14341447 ),
1435 // Start out with availability sets disabled.1448 MachineConfig: environs.NewMachineConfig(
1436 env.ecfg.attrs["availability-sets-enabled"] = false1449 "1", "yanonce", nil, nil, stateInfo, apiInfo,
14371450 ),
1438 var expectServiceName string1451 }
1439 var expectStateServer bool1452}
1440 s.PatchValue(&createInstance, func(env *azureEnviron, azure *gwacl.ManagementAPI, role *gwacl.Role, serviceName string, stateServer bool) (instance.Instance, error) {1453
1441 c.Assert(serviceName, gc.Equals, expectServiceName)1454func (s *startInstanceSuite) startInstance(c *gc.C) (serviceName string, stateServer bool) {
1442 c.Assert(stateServer, gc.Equals, expectStateServer)1455 var called bool
1456 restore := testing.PatchValue(&createInstance, func(env *azureEnviron, azure *gwacl.ManagementAPI, role *gwacl.Role, serviceNameArg string, stateServerArg bool) (instance.Instance, error) {
1457 serviceName = serviceNameArg
1458 stateServer = stateServerArg
1459 called = true
1443 return nil, nil1460 return nil, nil
1444 })1461 })
1445 env.StartInstance(params)1462 defer restore()
14461463 _, _, err := s.env.StartInstance(s.params)
1447 // DistributionGroup won't have an effect if availability-sets-enabled=false.1464 c.Assert(err, gc.IsNil)
1448 expectServiceName = ""1465 c.Assert(called, jc.IsTrue)
1449 params.DistributionGroup = func() ([]instance.Id, error) {1466 return serviceName, stateServer
1450 return []instance.Id{instance.Id(env.getEnvPrefix() + "whatever-role0")}, nil1467}
1451 }1468
1452 env.StartInstance(params)1469func (s *startInstanceSuite) TestStartInstanceDistributionGroupError(c *gc.C) {
14531470 s.params.DistributionGroup = func() ([]instance.Id, error) {
1454 env.ecfg.attrs["availability-sets-enabled"] = true1471 return nil, fmt.Errorf("DistributionGroupError")
1455 expectServiceName = "juju-testenv-whatever"1472 }
1456 env.StartInstance(params)1473 s.env.ecfg.attrs["availability-sets-enabled"] = true
14571474 _, _, err := s.env.StartInstance(s.params)
1458 expectServiceName = ""1475 c.Assert(err, gc.ErrorMatches, "DistributionGroupError")
1459 params.DistributionGroup = nil1476 // DistributionGroup should not be called if availability-sets-enabled=false.
1460 env.StartInstance(params)1477 s.env.ecfg.attrs["availability-sets-enabled"] = false
14611478 s.startInstance(c)
1462 // Empty distribution group is equivalent to no DistributionGroup function.1479}
1463 params.DistributionGroup = func() ([]instance.Id, error) {1480
1464 return nil, nil1481func (s *startInstanceSuite) TestStartInstanceDistributionGroupEmpty(c *gc.C) {
1465 }1482 // serviceName will be empty if DistributionGroup is nil or returns nothing.
1466 env.StartInstance(params)1483 s.env.ecfg.attrs["availability-sets-enabled"] = true
14671484 serviceName, _ := s.startInstance(c)
1468 // If the machine has the JobManagesEnviron job, we should see stateServer==true.1485 c.Assert(serviceName, gc.Equals, "")
1469 expectStateServer = true1486 s.params.DistributionGroup = func() ([]instance.Id, error) { return nil, nil }
1470 params.MachineConfig.Jobs = []apiparams.MachineJob{apiparams.JobHostUnits, apiparams.JobManageEnviron}1487 serviceName, _ = s.startInstance(c)
1471 env.StartInstance(params)1488 c.Assert(serviceName, gc.Equals, "")
1489}
1490
1491func (s *startInstanceSuite) TestStartInstanceDistributionGroup(c *gc.C) {
1492 s.params.DistributionGroup = func() ([]instance.Id, error) {
1493 return []instance.Id{
1494 instance.Id(s.env.getEnvPrefix() + "whatever-role0"),
1495 }, nil
1496 }
1497 // DistributionGroup will only have an effect if
1498 // availability-sets-enabled=true.
1499 s.env.ecfg.attrs["availability-sets-enabled"] = false
1500 serviceName, _ := s.startInstance(c)
1501 c.Assert(serviceName, gc.Equals, "")
1502 s.env.ecfg.attrs["availability-sets-enabled"] = true
1503 serviceName, _ = s.startInstance(c)
1504 c.Assert(serviceName, gc.Equals, "juju-testenv-whatever")
1505}
1506
1507func (s *startInstanceSuite) TestStartInstanceStateServerJobs(c *gc.C) {
1508 // If the machine has the JobManagesEnviron job,
1509 // we should see stateServer==true.
1510 s.params.MachineConfig.Jobs = []apiparams.MachineJob{
1511 apiparams.JobHostUnits,
1512 }
1513 _, stateServer := s.startInstance(c)
1514 c.Assert(stateServer, jc.IsFalse)
1515 s.params.MachineConfig.Jobs = []apiparams.MachineJob{
1516 apiparams.JobHostUnits, apiparams.JobManageEnviron,
1517 }
1518 _, stateServer = s.startInstance(c)
1519 c.Assert(stateServer, jc.IsTrue)
1472}1520}

Subscribers

People subscribed via source and target branches

to status/vote changes: