github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/worker/caasfirewaller/manifold_test.go (about) 1 // Copyright 2017 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package caasfirewaller_test 5 6 import ( 7 "github.com/juju/errors" 8 "github.com/juju/testing" 9 jc "github.com/juju/testing/checkers" 10 gc "gopkg.in/check.v1" 11 "gopkg.in/juju/worker.v1" 12 "gopkg.in/juju/worker.v1/dependency" 13 dt "gopkg.in/juju/worker.v1/dependency/testing" 14 "gopkg.in/juju/worker.v1/workertest" 15 16 "github.com/juju/juju/api/base" 17 coretesting "github.com/juju/juju/testing" 18 "github.com/juju/juju/worker/caasfirewaller" 19 ) 20 21 type ManifoldSuite struct { 22 testing.IsolationSuite 23 testing.Stub 24 manifold dependency.Manifold 25 context dependency.Context 26 27 apiCaller fakeAPICaller 28 broker fakeBroker 29 client fakeClient 30 } 31 32 var _ = gc.Suite(&ManifoldSuite{}) 33 34 func (s *ManifoldSuite) SetUpTest(c *gc.C) { 35 s.IsolationSuite.SetUpTest(c) 36 s.ResetCalls() 37 38 s.context = s.newContext(nil) 39 s.manifold = caasfirewaller.Manifold(s.validConfig()) 40 } 41 42 func (s *ManifoldSuite) validConfig() caasfirewaller.ManifoldConfig { 43 return caasfirewaller.ManifoldConfig{ 44 APICallerName: "api-caller", 45 BrokerName: "broker", 46 ControllerUUID: coretesting.ControllerTag.Id(), 47 ModelUUID: coretesting.ModelTag.Id(), 48 NewClient: s.newClient, 49 NewWorker: s.newWorker, 50 } 51 } 52 53 func (s *ManifoldSuite) newClient(apiCaller base.APICaller) caasfirewaller.Client { 54 s.MethodCall(s, "NewClient", apiCaller) 55 return &s.client 56 } 57 58 func (s *ManifoldSuite) newWorker(config caasfirewaller.Config) (worker.Worker, error) { 59 s.MethodCall(s, "NewWorker", config) 60 if err := s.NextErr(); err != nil { 61 return nil, err 62 } 63 w := worker.NewRunner(worker.RunnerParams{}) 64 s.AddCleanup(func(c *gc.C) { workertest.DirtyKill(c, w) }) 65 return w, nil 66 } 67 68 func (s *ManifoldSuite) newContext(overlay map[string]interface{}) dependency.Context { 69 resources := map[string]interface{}{ 70 "api-caller": &s.apiCaller, 71 "broker": &s.broker, 72 } 73 for k, v := range overlay { 74 resources[k] = v 75 } 76 return dt.StubContext(nil, resources) 77 } 78 79 func (s *ManifoldSuite) TestMissingControllerUUID(c *gc.C) { 80 config := s.validConfig() 81 config.ControllerUUID = "" 82 s.checkConfigInvalid(c, config, "empty ControllerUUID not valid") 83 } 84 85 func (s *ManifoldSuite) TestMissingModelUUID(c *gc.C) { 86 config := s.validConfig() 87 config.ModelUUID = "" 88 s.checkConfigInvalid(c, config, "empty ModelUUID not valid") 89 } 90 91 func (s *ManifoldSuite) TestMissingAPICallerName(c *gc.C) { 92 config := s.validConfig() 93 config.APICallerName = "" 94 s.checkConfigInvalid(c, config, "empty APICallerName not valid") 95 } 96 97 func (s *ManifoldSuite) TestMissingBrokerName(c *gc.C) { 98 config := s.validConfig() 99 config.BrokerName = "" 100 s.checkConfigInvalid(c, config, "empty BrokerName not valid") 101 } 102 103 func (s *ManifoldSuite) TestMissingNewWorker(c *gc.C) { 104 config := s.validConfig() 105 config.NewWorker = nil 106 s.checkConfigInvalid(c, config, "nil NewWorker not valid") 107 } 108 109 func (s *ManifoldSuite) checkConfigInvalid(c *gc.C, config caasfirewaller.ManifoldConfig, expect string) { 110 err := config.Validate() 111 c.Check(err, gc.ErrorMatches, expect) 112 c.Check(err, jc.Satisfies, errors.IsNotValid) 113 } 114 115 var expectedInputs = []string{"api-caller", "broker"} 116 117 func (s *ManifoldSuite) TestInputs(c *gc.C) { 118 c.Assert(s.manifold.Inputs, jc.SameContents, expectedInputs) 119 } 120 121 func (s *ManifoldSuite) TestMissingInputs(c *gc.C) { 122 for _, input := range expectedInputs { 123 context := s.newContext(map[string]interface{}{ 124 input: dependency.ErrMissing, 125 }) 126 _, err := s.manifold.Start(context) 127 c.Assert(errors.Cause(err), gc.Equals, dependency.ErrMissing) 128 } 129 } 130 131 func (s *ManifoldSuite) TestStart(c *gc.C) { 132 w, err := s.manifold.Start(s.context) 133 c.Assert(err, jc.ErrorIsNil) 134 workertest.CleanKill(c, w) 135 136 s.CheckCallNames(c, "NewClient", "NewWorker") 137 s.CheckCall(c, 0, "NewClient", &s.apiCaller) 138 139 args := s.Calls()[1].Args 140 c.Assert(args, gc.HasLen, 1) 141 c.Assert(args[0], gc.FitsTypeOf, caasfirewaller.Config{}) 142 config := args[0].(caasfirewaller.Config) 143 144 c.Assert(config, jc.DeepEquals, caasfirewaller.Config{ 145 ControllerUUID: coretesting.ControllerTag.Id(), 146 ModelUUID: coretesting.ModelTag.Id(), 147 ApplicationGetter: &s.client, 148 ServiceExposer: &s.broker, 149 LifeGetter: &s.client, 150 }) 151 }