github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/worker/storageprovisioner/manifold_machine_test.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package storageprovisioner_test 5 6 import ( 7 "time" 8 9 "github.com/juju/testing" 10 jc "github.com/juju/testing/checkers" 11 gc "gopkg.in/check.v1" 12 "gopkg.in/juju/names.v2" 13 14 "github.com/juju/juju/agent" 15 "github.com/juju/juju/api" 16 apiagent "github.com/juju/juju/api/agent" 17 "github.com/juju/juju/apiserver/params" 18 "github.com/juju/juju/cmd/jujud/agent/engine/enginetest" 19 "github.com/juju/juju/state/multiwatcher" 20 "github.com/juju/juju/worker" 21 "github.com/juju/juju/worker/dependency" 22 "github.com/juju/juju/worker/storageprovisioner" 23 ) 24 25 type MachineManifoldSuite struct { 26 testing.IsolationSuite 27 config storageprovisioner.MachineManifoldConfig 28 newCalled bool 29 } 30 31 var ( 32 defaultClockStart time.Time 33 _ = gc.Suite(&MachineManifoldSuite{}) 34 ) 35 36 func (s *MachineManifoldSuite) SetUpTest(c *gc.C) { 37 s.newCalled = false 38 s.PatchValue(&storageprovisioner.NewStorageProvisioner, 39 func(config storageprovisioner.Config) (worker.Worker, error) { 40 s.newCalled = true 41 return nil, nil 42 }, 43 ) 44 config := enginetest.AgentAPIManifoldTestConfig() 45 s.config = storageprovisioner.MachineManifoldConfig{ 46 AgentName: config.AgentName, 47 APICallerName: config.APICallerName, 48 Clock: testing.NewClock(defaultClockStart), 49 } 50 } 51 52 func (s *MachineManifoldSuite) TestMachine(c *gc.C) { 53 _, err := enginetest.RunAgentAPIManifold( 54 storageprovisioner.MachineManifold(s.config), 55 &fakeAgent{tag: names.NewMachineTag("42")}, 56 &fakeAPIConn{}) 57 c.Assert(err, jc.ErrorIsNil) 58 c.Assert(s.newCalled, jc.IsTrue) 59 } 60 61 func (s *MachineManifoldSuite) TestMissingClock(c *gc.C) { 62 s.config.Clock = nil 63 _, err := enginetest.RunAgentAPIManifold( 64 storageprovisioner.MachineManifold(s.config), 65 &fakeAgent{tag: names.NewMachineTag("42")}, 66 &fakeAPIConn{}) 67 c.Assert(err, gc.Equals, dependency.ErrMissing) 68 c.Assert(s.newCalled, jc.IsFalse) 69 } 70 71 func (s *MachineManifoldSuite) TestUnit(c *gc.C) { 72 _, err := enginetest.RunAgentAPIManifold( 73 storageprovisioner.MachineManifold(s.config), 74 &fakeAgent{tag: names.NewUnitTag("foo/0")}, 75 &fakeAPIConn{}) 76 c.Assert(err, gc.ErrorMatches, "expected ModelTag or MachineTag, got names.UnitTag") 77 c.Assert(s.newCalled, jc.IsFalse) 78 } 79 80 func (s *MachineManifoldSuite) TestNonAgent(c *gc.C) { 81 _, err := enginetest.RunAgentAPIManifold( 82 storageprovisioner.MachineManifold(s.config), 83 &fakeAgent{tag: names.NewUserTag("foo")}, 84 &fakeAPIConn{}) 85 c.Assert(err, gc.ErrorMatches, "expected ModelTag or MachineTag, got names.UserTag") 86 c.Assert(s.newCalled, jc.IsFalse) 87 } 88 89 type fakeAgent struct { 90 agent.Agent 91 tag names.Tag 92 } 93 94 func (a *fakeAgent) CurrentConfig() agent.Config { 95 return &fakeConfig{tag: a.tag} 96 } 97 98 type fakeConfig struct { 99 agent.Config 100 tag names.Tag 101 } 102 103 func (c *fakeConfig) Tag() names.Tag { 104 return c.tag 105 } 106 107 func (_ fakeConfig) DataDir() string { 108 return "/path/to/data/dir" 109 } 110 111 type fakeAPIConn struct { 112 api.Connection 113 machineJob multiwatcher.MachineJob 114 } 115 116 func (f *fakeAPIConn) APICall(objType string, version int, id, request string, args interface{}, response interface{}) error { 117 if res, ok := response.(*params.AgentGetEntitiesResults); ok { 118 res.Entities = []params.AgentGetEntitiesResult{ 119 {Jobs: []multiwatcher.MachineJob{f.machineJob}}, 120 } 121 } 122 123 return nil 124 } 125 126 func (*fakeAPIConn) BestFacadeVersion(facade string) int { 127 return 42 128 } 129 130 func (f *fakeAPIConn) Agent() *apiagent.State { 131 return apiagent.NewState(f) 132 }