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