github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/worker/proxyupdater/manifold_test.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package proxyupdater_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 "github.com/juju/juju/worker" 14 "github.com/juju/juju/worker/proxyupdater" 15 proxyup "github.com/juju/juju/worker/proxyupdater" 16 workertesting "github.com/juju/juju/worker/testing" 17 ) 18 19 type ManifoldSuite struct { 20 testing.IsolationSuite 21 newCalled bool 22 } 23 24 var _ = gc.Suite(&ManifoldSuite{}) 25 26 func (s *ManifoldSuite) SetUpTest(c *gc.C) { 27 s.newCalled = false 28 s.PatchValue(&proxyupdater.NewWorker, 29 func(_ proxyupdater.Config) (worker.Worker, error) { 30 s.newCalled = true 31 return nil, nil 32 }, 33 ) 34 } 35 36 func (s *ManifoldSuite) TestMachineShouldWrite(c *gc.C) { 37 config := proxyup.ManifoldConfig(workertesting.AgentApiManifoldTestConfig()) 38 _, err := workertesting.RunAgentApiManifold( 39 proxyup.Manifold(config), 40 &fakeAgent{tag: names.NewMachineTag("42")}, 41 nil) 42 c.Assert(err, jc.ErrorIsNil) 43 c.Assert(s.newCalled, jc.IsTrue) 44 } 45 46 func (s *ManifoldSuite) TestMachineShouldntWrite(c *gc.C) { 47 config := proxyup.ManifoldConfig(workertesting.AgentApiManifoldTestConfig()) 48 _, err := workertesting.RunAgentApiManifold( 49 proxyup.Manifold(config), 50 &fakeAgent{tag: names.NewMachineTag("42")}, 51 nil) 52 c.Assert(err, jc.ErrorIsNil) 53 c.Assert(s.newCalled, jc.IsTrue) 54 } 55 56 func (s *ManifoldSuite) TestUnit(c *gc.C) { 57 config := proxyup.ManifoldConfig(workertesting.AgentApiManifoldTestConfig()) 58 _, err := workertesting.RunAgentApiManifold( 59 proxyup.Manifold(config), 60 &fakeAgent{tag: names.NewUnitTag("foo/0")}, 61 nil) 62 c.Assert(err, jc.ErrorIsNil) 63 c.Assert(s.newCalled, jc.IsTrue) 64 } 65 66 func (s *ManifoldSuite) TestNonAgent(c *gc.C) { 67 config := proxyup.ManifoldConfig(workertesting.AgentApiManifoldTestConfig()) 68 _, err := workertesting.RunAgentApiManifold( 69 proxyup.Manifold(config), 70 &fakeAgent{tag: names.NewUserTag("foo")}, 71 nil) 72 c.Assert(err, gc.ErrorMatches, "unknown agent type:.+") 73 c.Assert(s.newCalled, jc.IsFalse) 74 } 75 76 type fakeAgent struct { 77 agent.Agent 78 tag names.Tag 79 } 80 81 func (a *fakeAgent) CurrentConfig() agent.Config { 82 return &fakeConfig{tag: a.tag} 83 } 84 85 type fakeConfig struct { 86 agent.Config 87 tag names.Tag 88 } 89 90 func (c *fakeConfig) Tag() names.Tag { 91 return c.tag 92 }