github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/worker/controllerport/worker_test.go (about) 1 // Copyright 2018 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package controllerport_test 5 6 import ( 7 "time" 8 9 "github.com/juju/errors" 10 "github.com/juju/loggo" 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/worker.v1" 16 "gopkg.in/juju/worker.v1/dependency" 17 "gopkg.in/juju/worker.v1/workertest" 18 19 "github.com/juju/juju/controller" 20 pscontroller "github.com/juju/juju/pubsub/controller" 21 coretesting "github.com/juju/juju/testing" 22 "github.com/juju/juju/worker/controllerport" 23 ) 24 25 type WorkerSuite struct { 26 testing.IsolationSuite 27 agentConfig *mockAgentConfig 28 hub *pubsub.StructuredHub 29 logger loggo.Logger 30 config controllerport.Config 31 stub testing.Stub 32 } 33 34 var _ = gc.Suite(&WorkerSuite{}) 35 36 func (s *WorkerSuite) SetUpTest(c *gc.C) { 37 s.IsolationSuite.SetUpTest(c) 38 s.agentConfig = &mockAgentConfig{port: 232323} 39 s.hub = pubsub.NewStructuredHub(nil) 40 s.logger = loggo.GetLogger("controllerport_worker_test") 41 s.stub.ResetCalls() 42 43 s.logger.SetLogLevel(loggo.TRACE) 44 45 s.config = controllerport.Config{ 46 AgentConfig: s.agentConfig, 47 Hub: s.hub, 48 Logger: s.logger, 49 50 ControllerAPIPort: 232323, 51 UpdateControllerAPIPort: s.updatePort, 52 } 53 } 54 55 func (s *WorkerSuite) updatePort(port int) error { 56 s.stub.MethodCall(s, "UpdatePort", port) 57 return s.stub.NextErr() 58 } 59 60 func (s *WorkerSuite) newWorker(c *gc.C, config controllerport.Config) worker.Worker { 61 w, err := controllerport.NewWorker(config) 62 c.Assert(err, jc.ErrorIsNil) 63 s.AddCleanup(func(c *gc.C) { workertest.DirtyKill(c, w) }) 64 return w 65 } 66 67 func (s *WorkerSuite) TestImmediateUpdate(c *gc.C) { 68 // Change the agent config so the ports are out of sync. 69 s.agentConfig.port = 23456 70 w := s.newWorker(c, s.config) 71 workertest.CheckAlive(c, w) 72 s.stub.CheckCall(c, 0, "UpdatePort", 232323) 73 } 74 75 func (s *WorkerSuite) TestNoChange(c *gc.C) { 76 w := s.newWorker(c, s.config) 77 processed, err := s.hub.Publish(pscontroller.ConfigChanged, pscontroller.ConfigChangedMessage{ 78 Config: controller.Config{ 79 "controller-api-port": 232323, 80 "some-other-field": "new value!", 81 }, 82 }) 83 c.Assert(err, jc.ErrorIsNil) 84 select { 85 case <-processed: 86 case <-time.After(coretesting.LongWait): 87 c.Fatalf("timed out waiting for processed") 88 } 89 workertest.CheckAlive(c, w) 90 s.stub.CheckCallNames(c) 91 } 92 93 func (s *WorkerSuite) TestChange(c *gc.C) { 94 w := s.newWorker(c, s.config) 95 processed, err := s.hub.Publish(pscontroller.ConfigChanged, pscontroller.ConfigChangedMessage{ 96 Config: controller.Config{"controller-api-port": 444444}, 97 }) 98 c.Assert(err, jc.ErrorIsNil) 99 select { 100 case <-processed: 101 case <-time.After(coretesting.LongWait): 102 c.Fatalf("timed out waiting for processed") 103 } 104 s.stub.CheckCall(c, 0, "UpdatePort", 444444) 105 err = workertest.CheckKilled(c, w) 106 c.Assert(errors.Cause(err), gc.Equals, dependency.ErrBounce) 107 } 108 109 func (s *WorkerSuite) TestValidate(c *gc.C) { 110 type test struct { 111 f func(*controllerport.Config) 112 expect string 113 } 114 tests := []test{{ 115 func(cfg *controllerport.Config) { cfg.AgentConfig = nil }, 116 "nil AgentConfig not valid", 117 }, { 118 func(cfg *controllerport.Config) { cfg.Hub = nil }, 119 "nil Hub not valid", 120 }, { 121 func(cfg *controllerport.Config) { cfg.Logger = nil }, 122 "nil Logger not valid", 123 }, { 124 func(cfg *controllerport.Config) { cfg.UpdateControllerAPIPort = nil }, 125 "nil UpdateControllerAPIPort not valid", 126 }} 127 for i, test := range tests { 128 c.Logf("test #%d (%s)", i, test.expect) 129 config := s.config 130 test.f(&config) 131 w, err := controllerport.NewWorker(config) 132 workertest.CheckNilOrKill(c, w) 133 c.Check(err, gc.ErrorMatches, test.expect) 134 } 135 }