github.com/juju/juju@v0.0.0-20240327075706-a90865de2538/worker/caasunitprovisioner/manifold_test.go (about) 1 // Copyright 2017 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package caasunitprovisioner_test 5 6 import ( 7 "github.com/juju/errors" 8 "github.com/juju/loggo" 9 "github.com/juju/testing" 10 jc "github.com/juju/testing/checkers" 11 "github.com/juju/worker/v3" 12 "github.com/juju/worker/v3/dependency" 13 dt "github.com/juju/worker/v3/dependency/testing" 14 "github.com/juju/worker/v3/workertest" 15 gc "gopkg.in/check.v1" 16 17 "github.com/juju/juju/api/base" 18 "github.com/juju/juju/worker/caasunitprovisioner" 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 = caasunitprovisioner.Manifold(s.validConfig()) 40 } 41 42 func (s *ManifoldSuite) validConfig() caasunitprovisioner.ManifoldConfig { 43 return caasunitprovisioner.ManifoldConfig{ 44 APICallerName: "api-caller", 45 BrokerName: "broker", 46 NewClient: s.newClient, 47 NewWorker: s.newWorker, 48 Logger: loggo.GetLogger("test"), 49 } 50 } 51 52 func (s *ManifoldSuite) newClient(apiCaller base.APICaller) caasunitprovisioner.Client { 53 s.MethodCall(s, "NewClient", apiCaller) 54 return &s.client 55 } 56 57 func (s *ManifoldSuite) newWorker(config caasunitprovisioner.Config) (worker.Worker, error) { 58 s.MethodCall(s, "NewWorker", config) 59 if err := s.NextErr(); err != nil { 60 return nil, err 61 } 62 w := worker.NewRunner(worker.RunnerParams{}) 63 s.AddCleanup(func(c *gc.C) { workertest.DirtyKill(c, w) }) 64 return w, nil 65 } 66 67 func (s *ManifoldSuite) newContext(overlay map[string]interface{}) dependency.Context { 68 resources := map[string]interface{}{ 69 "api-caller": &s.apiCaller, 70 "broker": &s.broker, 71 } 72 for k, v := range overlay { 73 resources[k] = v 74 } 75 return dt.StubContext(nil, resources) 76 } 77 78 func (s *ManifoldSuite) TestMissingAPICallerName(c *gc.C) { 79 config := s.validConfig() 80 config.APICallerName = "" 81 s.checkConfigInvalid(c, config, "empty APICallerName not valid") 82 } 83 84 func (s *ManifoldSuite) TestMissingBrokerName(c *gc.C) { 85 config := s.validConfig() 86 config.BrokerName = "" 87 s.checkConfigInvalid(c, config, "empty BrokerName not valid") 88 } 89 90 func (s *ManifoldSuite) TestMissingNewWorker(c *gc.C) { 91 config := s.validConfig() 92 config.NewWorker = nil 93 s.checkConfigInvalid(c, config, "nil NewWorker not valid") 94 } 95 96 func (s *ManifoldSuite) TestMissingLogger(c *gc.C) { 97 config := s.validConfig() 98 config.Logger = nil 99 s.checkConfigInvalid(c, config, "nil Logger not valid") 100 } 101 102 func (s *ManifoldSuite) checkConfigInvalid(c *gc.C, config caasunitprovisioner.ManifoldConfig, expect string) { 103 err := config.Validate() 104 c.Check(err, gc.ErrorMatches, expect) 105 c.Check(err, jc.Satisfies, errors.IsNotValid) 106 } 107 108 var expectedInputs = []string{"api-caller", "broker"} 109 110 func (s *ManifoldSuite) TestInputs(c *gc.C) { 111 c.Assert(s.manifold.Inputs, jc.SameContents, expectedInputs) 112 } 113 114 func (s *ManifoldSuite) TestMissingInputs(c *gc.C) { 115 for _, input := range expectedInputs { 116 context := s.newContext(map[string]interface{}{ 117 input: dependency.ErrMissing, 118 }) 119 _, err := s.manifold.Start(context) 120 c.Assert(errors.Cause(err), gc.Equals, dependency.ErrMissing) 121 } 122 } 123 124 func (s *ManifoldSuite) TestStart(c *gc.C) { 125 w, err := s.manifold.Start(s.context) 126 c.Assert(err, jc.ErrorIsNil) 127 workertest.CleanKill(c, w) 128 129 s.CheckCallNames(c, "NewClient", "NewWorker") 130 s.CheckCall(c, 0, "NewClient", &s.apiCaller) 131 132 args := s.Calls()[1].Args 133 c.Assert(args, gc.HasLen, 1) 134 c.Assert(args[0], gc.FitsTypeOf, caasunitprovisioner.Config{}) 135 config := args[0].(caasunitprovisioner.Config) 136 137 c.Assert(config, jc.DeepEquals, caasunitprovisioner.Config{ 138 ApplicationGetter: &s.client, 139 ApplicationUpdater: &s.client, 140 ServiceBroker: &s.broker, 141 ContainerBroker: &s.broker, 142 ProvisioningInfoGetter: &s.client, 143 ProvisioningStatusSetter: &s.client, 144 LifeGetter: &s.client, 145 CharmGetter: &s.client, 146 UnitUpdater: &s.client, 147 Logger: loggo.GetLogger("test"), 148 }) 149 }