github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/apiserver/observer/request_notifier_test.go (about) 1 // Copyright 2017 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package observer_test 5 6 import ( 7 "time" 8 9 "github.com/juju/clock/testclock" 10 "github.com/juju/loggo" 11 "github.com/juju/testing" 12 jc "github.com/juju/testing/checkers" 13 gc "gopkg.in/check.v1" 14 "gopkg.in/juju/names.v2" 15 16 "github.com/juju/juju/apiserver/observer" 17 "github.com/juju/juju/pubsub/apiserver" 18 ) 19 20 type RequestObserverSuite struct { 21 testing.IsolationSuite 22 } 23 24 var _ = gc.Suite(&RequestObserverSuite{}) 25 26 func (*RequestObserverSuite) makeNotifier(c *gc.C) (*observer.RequestObserver, *connectionHub) { 27 hub := &connectionHub{c: c} 28 return observer.NewRequestObserver(observer.RequestObserverContext{ 29 Clock: testclock.NewClock(time.Now()), 30 Hub: hub, 31 Logger: loggo.GetLogger("test"), 32 }), hub 33 } 34 35 func (s *RequestObserverSuite) TestAgentConnectionPublished(c *gc.C) { 36 notifier, hub := s.makeNotifier(c) 37 38 agent := names.NewMachineTag("42") 39 model := names.NewModelTag("fake-uuid") 40 notifier.Login(agent, model, false, "user data") 41 42 c.Assert(hub.called, gc.Equals, 1) 43 c.Assert(hub.topic, gc.Equals, apiserver.ConnectTopic) 44 c.Assert(hub.details, jc.DeepEquals, apiserver.APIConnection{ 45 AgentTag: "machine-42", 46 ModelUUID: "fake-uuid", 47 UserData: "user data", 48 ConnectionID: 0, 49 }) 50 } 51 52 func (s *RequestObserverSuite) assertControllerAgentConnectionPublished(c *gc.C, agent names.Tag) { 53 notifier, hub := s.makeNotifier(c) 54 55 model := names.NewModelTag("fake-uuid") 56 notifier.Login(agent, model, true, "user data") 57 58 c.Assert(hub.called, gc.Equals, 1) 59 c.Assert(hub.topic, gc.Equals, apiserver.ConnectTopic) 60 c.Assert(hub.details, jc.DeepEquals, apiserver.APIConnection{ 61 AgentTag: agent.String(), 62 ModelUUID: "fake-uuid", 63 ControllerAgent: true, 64 UserData: "user data", 65 ConnectionID: 0, 66 }) 67 } 68 69 func (s *RequestObserverSuite) TestControllerMachineAgentConnectionPublished(c *gc.C) { 70 s.assertControllerAgentConnectionPublished(c, names.NewMachineTag("2")) 71 } 72 73 func (s *RequestObserverSuite) TestControllerUnitAgentConnectionPublished(c *gc.C) { 74 s.assertControllerAgentConnectionPublished(c, names.NewUnitTag("mariadb/0")) 75 } 76 77 func (s *RequestObserverSuite) TestControllerApplicationAgentConnectionPublished(c *gc.C) { 78 s.assertControllerAgentConnectionPublished(c, names.NewApplicationTag("gitlab")) 79 } 80 81 func (s *RequestObserverSuite) TestUserConnectionsNotPublished(c *gc.C) { 82 notifier, hub := s.makeNotifier(c) 83 84 user := names.NewUserTag("bob") 85 model := names.NewModelTag("fake-uuid") 86 notifier.Login(user, model, false, "user data") 87 88 c.Assert(hub.called, gc.Equals, 0) 89 } 90 91 func (s *RequestObserverSuite) TestAgentDisconnectionPublished(c *gc.C) { 92 notifier, hub := s.makeNotifier(c) 93 94 agent := names.NewMachineTag("42") 95 model := names.NewModelTag("fake-uuid") 96 // All details are saved from Login. 97 notifier.Login(agent, model, false, "user data") 98 notifier.Leave() 99 100 c.Assert(hub.called, gc.Equals, 2) 101 c.Assert(hub.topic, gc.Equals, apiserver.DisconnectTopic) 102 c.Assert(hub.details, jc.DeepEquals, apiserver.APIConnection{ 103 AgentTag: "machine-42", 104 ModelUUID: "fake-uuid", 105 ConnectionID: 0, 106 }) 107 } 108 109 func (s *RequestObserverSuite) TestControllerAgentDisconnectionPublished(c *gc.C) { 110 notifier, hub := s.makeNotifier(c) 111 112 agent := names.NewMachineTag("2") 113 model := names.NewModelTag("fake-uuid") 114 // All details are saved from Login. 115 notifier.Login(agent, model, true, "user data") 116 notifier.Leave() 117 118 c.Assert(hub.called, gc.Equals, 2) 119 c.Assert(hub.topic, gc.Equals, apiserver.DisconnectTopic) 120 c.Assert(hub.details, jc.DeepEquals, apiserver.APIConnection{ 121 AgentTag: "machine-2", 122 ModelUUID: "fake-uuid", 123 ControllerAgent: true, 124 ConnectionID: 0, 125 }) 126 } 127 128 func (s *RequestObserverSuite) TestUserDisconnectionsNotPublished(c *gc.C) { 129 notifier, hub := s.makeNotifier(c) 130 131 user := names.NewUserTag("bob") 132 model := names.NewModelTag("fake-uuid") 133 // All details are saved from Login. 134 notifier.Login(user, model, false, "user data") 135 notifier.Leave() 136 137 c.Assert(hub.called, gc.Equals, 0) 138 } 139 140 type connectionHub struct { 141 c *gc.C 142 called int 143 topic string 144 details apiserver.APIConnection 145 } 146 147 func (hub *connectionHub) Publish(topic string, data interface{}) (<-chan struct{}, error) { 148 hub.called++ 149 hub.topic = topic 150 hub.details = data.(apiserver.APIConnection) 151 return nil, nil 152 }