github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/worker/identityfilewriter/manifold_test.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package identityfilewriter_test 5 6 import ( 7 "github.com/juju/juju/worker/dependency" 8 "github.com/juju/testing" 9 jc "github.com/juju/testing/checkers" 10 gc "gopkg.in/check.v1" 11 "gopkg.in/juju/names.v2" 12 13 "github.com/juju/juju/agent" 14 apitesting "github.com/juju/juju/api/base/testing" 15 "github.com/juju/juju/apiserver/params" 16 "github.com/juju/juju/cmd/jujud/agent/engine/enginetest" 17 "github.com/juju/juju/state/multiwatcher" 18 "github.com/juju/juju/worker" 19 "github.com/juju/juju/worker/identityfilewriter" 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(&identityfilewriter.NewWorker, 32 func(a agent.Config) (worker.Worker, error) { 33 s.newCalled = true 34 return nil, nil 35 }, 36 ) 37 } 38 39 func (s *ManifoldSuite) TestMachine(c *gc.C) { 40 config := identityfilewriter.ManifoldConfig(enginetest.AgentAPIManifoldTestConfig()) 41 _, err := enginetest.RunAgentAPIManifold( 42 identityfilewriter.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 := identityfilewriter.ManifoldConfig(enginetest.AgentAPIManifoldTestConfig()) 51 _, err := enginetest.RunAgentAPIManifold( 52 identityfilewriter.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 := identityfilewriter.ManifoldConfig(enginetest.AgentAPIManifoldTestConfig()) 61 _, err := enginetest.RunAgentAPIManifold( 62 identityfilewriter.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 }