def test_asks_region_for_monitoring_state(self): fixture = self.useFixture(MockLiveClusterToRegionRPCFixture()) protocol, connecting = fixture.makeEventLoop( region.UpdateInterfaces, region.GetDiscoveryState ) self.addCleanup((yield connecting)) rpc_service = services.getServiceNamed("rpc") reactor = Clock() service = RackNetworksMonitoringService( rpc_service, reactor, enable_monitoring=False, enable_beaconing=False, ) protocol.GetDiscoveryState.return_value = {"interfaces": {}} # Put something in the cache. This tells recordInterfaces that refresh # has already run but the interfaces have changed thus they need to be # updated. service._recorded = {} yield service.startService() yield maybeDeferred(service.getDiscoveryState) yield service.stopService() self.assertThat( protocol.GetDiscoveryState, MockCalledOnceWith( protocol, system_id=rpc_service.getClient().localIdent ), )
def test_reports_interfaces_to_region(self): fixture = self.useFixture(MockLiveClusterToRegionRPCFixture()) protocol, connecting = fixture.makeEventLoop(region.UpdateInterfaces) self.addCleanup((yield connecting)) interfaces = { "eth0": { "type": "physical", "mac_address": factory.make_mac_address(), "parents": [], "links": [], "enabled": True, } } rpc_service = services.getServiceNamed('rpc') service = RackNetworksMonitoringService(rpc_service, Clock(), enable_monitoring=False) service.getInterfaces = lambda: succeed(interfaces) # Put something in the cache. This tells recordInterfaces that refresh # has already run but the interfaces have changed thus they need to be # updated. service._recorded = {} service.startService() yield service.stopService() self.assertThat( protocol.UpdateInterfaces, MockCalledOnceWith(protocol, system_id=rpc_service.getClient().localIdent, interfaces=interfaces))
def test_reports_interfaces_with_hints_if_beaconing_enabled(self): fixture = self.useFixture(MockLiveClusterToRegionRPCFixture()) protocol, connecting = fixture.makeEventLoop(region.UpdateInterfaces) # Don't actually wait for beaconing to complete. pause_mock = self.patch(services_module, "pause") queue_mcast_mock = self.patch( services_module.BeaconingSocketProtocol, "queueMulticastBeaconing" ) self.addCleanup((yield connecting)) interfaces = { "eth0": { "type": "physical", "mac_address": factory.make_mac_address(), "parents": [], "links": [], "enabled": True, } } rpc_service = services.getServiceNamed("rpc") service = RackNetworksMonitoringService( rpc_service, Clock(), enable_monitoring=False, enable_beaconing=True, ) service.getInterfaces = lambda: succeed(interfaces) # Put something in the cache. This tells recordInterfaces that refresh # has already run but the interfaces have changed thus they need to be # updated. service._recorded = {} service.startService() yield service.stopService() self.assertThat( protocol.UpdateInterfaces, MockCalledOnceWith( protocol, system_id=rpc_service.getClient().localIdent, interfaces=interfaces, topology_hints=[], ), ) # The service should have sent out beacons, waited three seconds, # solicited for more beacons, then waited another three seconds before # deciding that beaconing is complete. self.assertThat(pause_mock, MockCallsMatch(call(3.0), call(3.0))) self.assertThat( queue_mcast_mock, MockCallsMatch( # Called when the service starts. call(solicitation=True), # Called three seconds later. call(solicitation=True), # Not called again when the service shuts down. ), )
def test_reports_interfaces_to_region(self): def refresh( system_id, consumer_key, token_key, token_secret, maas_url=None, post_process_hook=None, ): self.assertEqual("", system_id) self.assertEqual(self.metadata_creds["consumer_key"], consumer_key) self.assertEqual(self.metadata_creds["token_key"], token_key) self.assertEqual(self.metadata_creds["token_secret"], token_secret) self.assertEqual("http://localhost/MAAS", maas_url) yield self.create_fake_rpc_service() self.mock_refresh.side_effect = refresh interfaces = { "eth0": { "type": "physical", "mac_address": factory.make_mac_address(), "parents": [], "links": [], "enabled": True, } } update_interfaces_deferred = Deferred() rpc_service = services.getServiceNamed("rpc") service = RackNetworksMonitoringService( rpc_service, Clock(), enable_monitoring=False, enable_beaconing=False, update_interfaces_deferred=update_interfaces_deferred, ) service.getInterfaces = lambda: succeed(interfaces) # Put something in the cache. This tells recordInterfaces that refresh # has already run but the interfaces have changed thus they need to be # updated. service._recorded = {} service.startService() yield update_interfaces_deferred yield service.stopService() self.assertEquals(1, self.mock_refresh.call_count)
def test_reports_interfaces_with_hints_if_beaconing_enabled(self): yield self.create_fake_rpc_service() # Don't actually wait for beaconing to complete. pause_mock = self.patch(services_module, "pause") queue_mcast_mock = self.patch( services_module.BeaconingSocketProtocol, "queueMulticastBeaconing" ) interfaces = { "eth0": { "type": "physical", "mac_address": factory.make_mac_address(), "parents": [], "links": [], "enabled": True, } } rpc_service = services.getServiceNamed("rpc") service = RackNetworksMonitoringService( rpc_service, Clock(), enable_monitoring=False, enable_beaconing=True, ) service.getInterfaces = lambda: succeed(interfaces) service.startService() # By stopping the interface_monitor first, we assure that the loop # happens at least once before the service stops completely. yield maybeDeferred(service.interface_monitor.stopService) yield service.stopService() # The service should have sent out beacons, waited three seconds, # solicited for more beacons, then waited another three seconds before # deciding that beaconing is complete. self.assertThat(pause_mock, MockCallsMatch(call(3.0), call(3.0))) self.assertThat( queue_mcast_mock, MockCallsMatch( # Called when the service starts. call(solicitation=True), # Called three seconds later. call(solicitation=True), # Not called again when the service shuts down. ), )
def test_runs_refresh_first_time(self): fixture = self.useFixture(MockLiveClusterToRegionRPCFixture()) protocol, connecting = fixture.makeEventLoop(region.RequestRackRefresh) self.addCleanup((yield connecting)) rpc_service = services.getServiceNamed('rpc') service = RackNetworksMonitoringService(rpc_service, Clock(), enable_monitoring=False) yield service.startService() yield service.stopService() self.assertThat( protocol.RequestRackRefresh, MockCalledOnceWith(protocol, system_id=rpc_service.getClient().localIdent))
def test_requests_beaconing_when_timer_fires(self): fixture = self.useFixture(MockLiveClusterToRegionRPCFixture()) protocol, connecting = fixture.makeEventLoop( region.UpdateInterfaces, region.GetDiscoveryState) self.addCleanup((yield connecting)) rpc_service = services.getServiceNamed('rpc') reactor = Clock() service = RackNetworksMonitoringService( rpc_service, reactor, enable_monitoring=False, enable_beaconing=True) service.beaconing_protocol = Mock() service.beaconing_protocol.queueMulticastBeaconing = Mock() service.getInterfaces = lambda: succeed({}) service._recorded = {} service.startService() yield service.stopService() self.assertThat( service.beaconing_protocol.queueMulticastBeaconing, MockCallsMatch(call()))