github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/worker/pubsub/manifold_test.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package pubsub_test 5 6 import ( 7 "time" 8 9 "github.com/juju/clock/testclock" 10 "github.com/juju/errors" 11 "github.com/juju/pubsub" 12 "github.com/juju/testing" 13 jc "github.com/juju/testing/checkers" 14 gc "gopkg.in/check.v1" 15 "gopkg.in/juju/names.v2" 16 "gopkg.in/juju/worker.v1" 17 "gopkg.in/juju/worker.v1/dependency" 18 dt "gopkg.in/juju/worker.v1/dependency/testing" 19 20 "github.com/juju/juju/agent" 21 "github.com/juju/juju/api" 22 psworker "github.com/juju/juju/worker/pubsub" 23 ) 24 25 type ManifoldSuite struct { 26 testing.IsolationSuite 27 config psworker.ManifoldConfig 28 } 29 30 var _ = gc.Suite(&ManifoldSuite{}) 31 32 func (s *ManifoldSuite) SetUpTest(c *gc.C) { 33 s.IsolationSuite.SetUpTest(c) 34 s.config = psworker.ManifoldConfig{ 35 AgentName: "agent", 36 CentralHubName: "central-hub", 37 Clock: testclock.NewClock(time.Now()), 38 } 39 } 40 41 func (s *ManifoldSuite) manifold() dependency.Manifold { 42 return psworker.Manifold(s.config) 43 } 44 45 func (s *ManifoldSuite) TestInputs(c *gc.C) { 46 c.Check(s.manifold().Inputs, jc.DeepEquals, []string{"agent", "central-hub"}) 47 } 48 49 func (s *ManifoldSuite) TestAgentMissing(c *gc.C) { 50 context := dt.StubContext(nil, map[string]interface{}{ 51 "agent": dependency.ErrMissing, 52 }) 53 54 worker, err := s.manifold().Start(context) 55 c.Check(worker, gc.IsNil) 56 c.Check(errors.Cause(err), gc.Equals, dependency.ErrMissing) 57 } 58 59 func (s *ManifoldSuite) TestCentralHubMissing(c *gc.C) { 60 context := dt.StubContext(nil, map[string]interface{}{ 61 "agent": &fakeAgent{}, 62 "central-hub": dependency.ErrMissing, 63 }) 64 65 worker, err := s.manifold().Start(context) 66 c.Check(worker, gc.IsNil) 67 c.Check(errors.Cause(err), gc.Equals, dependency.ErrMissing) 68 } 69 70 func (s *ManifoldSuite) TestAgentAPIInfoNotReady(c *gc.C) { 71 context := dt.StubContext(nil, map[string]interface{}{ 72 "agent": &fakeAgent{missingAPIinfo: true}, 73 "central-hub": pubsub.NewStructuredHub(nil), 74 }) 75 76 worker, err := s.manifold().Start(context) 77 c.Check(worker, gc.IsNil) 78 c.Check(errors.Cause(err), gc.Equals, dependency.ErrMissing) 79 } 80 81 func (s *ManifoldSuite) TestNewWorkerArgs(c *gc.C) { 82 clock := s.config.Clock 83 hub := pubsub.NewStructuredHub(nil) 84 var config psworker.WorkerConfig 85 s.config.NewWorker = func(c psworker.WorkerConfig) (worker.Worker, error) { 86 config = c 87 return &fakeWorker{}, nil 88 } 89 90 context := dt.StubContext(nil, map[string]interface{}{ 91 "agent": &fakeAgent{tag: names.NewMachineTag("42")}, 92 "central-hub": hub, 93 }) 94 95 worker, err := s.manifold().Start(context) 96 c.Check(err, jc.ErrorIsNil) 97 c.Check(worker, gc.NotNil) 98 99 c.Check(config.Origin, gc.Equals, "machine-42") 100 c.Check(config.Clock, gc.Equals, clock) 101 c.Check(config.Hub, gc.Equals, hub) 102 c.Check(config.APIInfo.CACert, gc.Equals, "fake as") 103 c.Check(config.NewWriter, gc.NotNil) 104 } 105 106 type fakeWorker struct { 107 worker.Worker 108 } 109 110 type fakeAgent struct { 111 agent.Agent 112 113 tag names.Tag 114 missingAPIinfo bool 115 } 116 117 type fakeConfig struct { 118 agent.Config 119 120 tag names.Tag 121 missingAPIinfo bool 122 } 123 124 func (f *fakeAgent) CurrentConfig() agent.Config { 125 return &fakeConfig{tag: f.tag, missingAPIinfo: f.missingAPIinfo} 126 } 127 128 func (f *fakeConfig) APIInfo() (*api.Info, bool) { 129 if f.missingAPIinfo { 130 return nil, false 131 } 132 return &api.Info{ 133 CACert: "fake as", 134 Tag: f.tag, 135 }, true 136 } 137 138 func (f *fakeConfig) Tag() names.Tag { 139 return f.tag 140 }