~juju-qa/ubuntu/xenial/juju/xenial-2.0-beta3

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/worker/toolsversionchecker/manifold_test.go

  • Committer: Martin Packman
  • Date: 2016-03-30 19:31:08 UTC
  • mfrom: (1.1.41)
  • Revision ID: martin.packman@canonical.com-20160330193108-h9iz3ak334uk0z5r
Merge new upstream source 2.0~beta3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2016 Canonical Ltd.
 
2
// Licensed under the AGPLv3, see LICENCE file for details.
 
3
 
 
4
package toolsversionchecker_test
 
5
 
 
6
import (
 
7
        "github.com/juju/names"
 
8
        "github.com/juju/testing"
 
9
        jc "github.com/juju/testing/checkers"
 
10
        gc "gopkg.in/check.v1"
 
11
 
 
12
        "github.com/juju/juju/agent"
 
13
        apitesting "github.com/juju/juju/api/base/testing"
 
14
        "github.com/juju/juju/apiserver/params"
 
15
        "github.com/juju/juju/state/multiwatcher"
 
16
        "github.com/juju/juju/worker"
 
17
        "github.com/juju/juju/worker/dependency"
 
18
        workertesting "github.com/juju/juju/worker/testing"
 
19
        "github.com/juju/juju/worker/toolsversionchecker"
 
20
)
 
21
 
 
22
type ManifoldSuite struct {
 
23
        testing.IsolationSuite
 
24
        newCalled bool
 
25
}
 
26
 
 
27
var _ = gc.Suite(&ManifoldSuite{})
 
28
 
 
29
func (s *ManifoldSuite) SetUpTest(c *gc.C) {
 
30
        s.newCalled = false
 
31
        s.PatchValue(&toolsversionchecker.New,
 
32
                func(api toolsversionchecker.Facade, params *toolsversionchecker.VersionCheckerParams) worker.Worker {
 
33
                        s.newCalled = true
 
34
                        return nil
 
35
                },
 
36
        )
 
37
}
 
38
 
 
39
func (s *ManifoldSuite) TestMachine(c *gc.C) {
 
40
        config := toolsversionchecker.ManifoldConfig(workertesting.PostUpgradeManifoldTestConfig())
 
41
        _, err := workertesting.RunPostUpgradeManifold(
 
42
                toolsversionchecker.Manifold(config),
 
43
                &fakeAgent{tag: names.NewMachineTag("42")},
 
44
                mockAPICaller(multiwatcher.JobManageModel))
 
45
        c.Assert(err, jc.ErrorIsNil)
 
46
        c.Assert(s.newCalled, jc.IsTrue)
 
47
}
 
48
 
 
49
func (s *ManifoldSuite) TestMachineNotModelManagerErrors(c *gc.C) {
 
50
        config := toolsversionchecker.ManifoldConfig(workertesting.PostUpgradeManifoldTestConfig())
 
51
        _, err := workertesting.RunPostUpgradeManifold(
 
52
                toolsversionchecker.Manifold(config),
 
53
                &fakeAgent{tag: names.NewMachineTag("42")},
 
54
                mockAPICaller(multiwatcher.JobHostUnits))
 
55
        c.Assert(err, gc.Equals, dependency.ErrMissing)
 
56
        c.Assert(s.newCalled, jc.IsFalse)
 
57
}
 
58
 
 
59
func (s *ManifoldSuite) TestNonMachineAgent(c *gc.C) {
 
60
        config := toolsversionchecker.ManifoldConfig(workertesting.PostUpgradeManifoldTestConfig())
 
61
        _, err := workertesting.RunPostUpgradeManifold(
 
62
                toolsversionchecker.Manifold(config),
 
63
                &fakeAgent{tag: names.NewUnitTag("foo/0")},
 
64
                mockAPICaller(""))
 
65
        c.Assert(err, gc.ErrorMatches, "this manifold may only be used inside a machine agent")
 
66
        c.Assert(s.newCalled, jc.IsFalse)
 
67
}
 
68
 
 
69
type fakeAgent struct {
 
70
        agent.Agent
 
71
        tag names.Tag
 
72
}
 
73
 
 
74
func (a *fakeAgent) CurrentConfig() agent.Config {
 
75
        return &fakeConfig{tag: a.tag}
 
76
}
 
77
 
 
78
type fakeConfig struct {
 
79
        agent.Config
 
80
        tag names.Tag
 
81
}
 
82
 
 
83
func (c *fakeConfig) Tag() names.Tag {
 
84
        return c.tag
 
85
}
 
86
 
 
87
func mockAPICaller(job multiwatcher.MachineJob) apitesting.APICallerFunc {
 
88
        return apitesting.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error {
 
89
                if res, ok := result.(*params.AgentGetEntitiesResults); ok {
 
90
                        res.Entities = []params.AgentGetEntitiesResult{
 
91
                                {Jobs: []multiwatcher.MachineJob{
 
92
                                        job,
 
93
                                }}}
 
94
                }
 
95
                return nil
 
96
        })
 
97
}