def test_reports_mdns_to_region(self): fixture = self.useFixture(MockLiveClusterToRegionRPCFixture()) protocol, connecting = fixture.makeEventLoop( region.UpdateInterfaces, region.ReportMDNSEntries ) self.addCleanup((yield connecting)) rpc_service = services.getServiceNamed("rpc") service = RackNetworksMonitoringService( rpc_service, Clock(), enable_monitoring=False, enable_beaconing=False, ) mdns = [ { "interface": "eth0", "hostname": "boggle.example.com", "address": factory.make_ip_address(), } ] yield service.reportMDNSEntries(mdns) self.assertThat( protocol.ReportMDNSEntries, MockCalledOnceWith( protocol, system_id=rpc_service.getClient().localIdent, mdns=mdns, ), )
def patch_rpc_methods(self, side_effect=None): fixture = self.useFixture(MockLiveClusterToRegionRPCFixture()) protocol, connecting = fixture.makeEventLoop( region.SendEventIPAddress, region.RegisterEventType ) protocol.SendEventIPAddress.side_effect = side_effect return protocol, connecting
def set_up(self): fixture = self.testcase.useFixture(MockLiveClusterToRegionRPCFixture()) self.protocol, connecting = fixture.makeEventLoop( region.CreateNode, region.CommissionNode) self.testcase.addCleanup((yield connecting)) self.protocol.CreateNode.side_effect = self.create_node self.protocol.CommissionNode.side_effect = self.commission_node
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, enable_beaconing=False, ) yield maybeDeferred(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 maybeDeferred(service.stopService) self.assertThat( protocol.RequestRackRefresh, MockCalledOnceWith( protocol, system_id=rpc_service.getClient().localIdent ), )
def prepareRegion(test, *, is_region=False, is_rack=True, servers=None, peers=None): """Set up a mock region controller. It responds to `GetControllerType` and `GetTimeConfiguration`. :return: The running RPC service, and the protocol instance. """ fixture = test.useFixture(MockLiveClusterToRegionRPCFixture()) protocol, connecting = fixture.makeEventLoop(region.GetControllerType, region.GetTimeConfiguration) protocol.RegisterRackController.side_effect = always_succeed_with( {"system_id": factory.make_name("maas-id")}) protocol.GetControllerType.side_effect = always_succeed_with({ "is_region": is_region, "is_rack": is_rack }) protocol.GetTimeConfiguration.side_effect = always_succeed_with({ "servers": [] if servers is None else servers, "peers": [] if peers is None else peers, }) def connected(teardown): test.addCleanup(teardown) return services.getServiceNamed("rpc"), protocol return connecting.addCallback(connected)
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_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_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 patch_rpc_methods(self, return_value={}, side_effect=None): fixture = self.useFixture(MockLiveClusterToRegionRPCFixture()) protocol, connecting = fixture.makeEventLoop( region.MarkNodeFailed, region.UpdateNodePowerState, region.SendEvent) protocol.MarkNodeFailed.return_value = return_value protocol.MarkNodeFailed.side_effect = side_effect self.addCleanup((yield connecting)) returnValue(protocol.MarkNodeFailed)
def test_update_last_image_sync_end_to_end_import_not_performed(self): self.useFixture(ClusterConfigurationFixture()) fixture = self.useFixture(MockLiveClusterToRegionRPCFixture()) protocol, connecting = fixture.makeEventLoop( region.UpdateLastImageSync) protocol.UpdateLastImageSync.return_value = succeed({}) self.addCleanup((yield connecting)) self.patch_autospec(boot_resources, 'import_images') boot_resources.import_images.return_value = False sources, hosts = make_sources() yield boot_images.import_boot_images(sources) self.assertThat(boot_resources.import_images, MockCalledOnceWith(sources)) self.assertThat(protocol.UpdateLastImageSync, MockNotCalled())
def test_reports_neighbours_to_region(self): fixture = self.useFixture(MockLiveClusterToRegionRPCFixture()) protocol, connecting = fixture.makeEventLoop( region.UpdateInterfaces, region.ReportNeighbours) self.addCleanup((yield connecting)) rpc_service = services.getServiceNamed('rpc') service = RackNetworksMonitoringService( rpc_service, Clock(), enable_monitoring=False, enable_beaconing=False) neighbours = [{"ip": factory.make_ip_address()}] yield service.reportNeighbours(neighbours) self.assertThat( protocol.ReportNeighbours, MockCalledOnceWith( protocol, system_id=rpc_service.getClient().localIdent, neighbours=neighbours))
def prepareRegion(test): """Set up a mock region controller. :return: The running RPC service, and the protocol instance. """ fixture = test.useFixture(MockLiveClusterToRegionRPCFixture()) protocol, connecting = fixture.makeEventLoop() protocol.RegisterRackController.side_effect = always_succeed_with( {"system_id": factory.make_name("maas-id")}) def connected(teardown): test.addCleanup(teardown) return services.getServiceNamed("rpc"), protocol return connecting.addCallback(connected)
def test_update_last_image_sync_end_to_end_import_not_performed(self): fixture = self.useFixture(MockLiveClusterToRegionRPCFixture()) protocol, connecting = fixture.makeEventLoop( region.UpdateLastImageSync) protocol.UpdateLastImageSync.return_value = succeed({}) self.addCleanup((yield connecting)) self.patch_autospec(boot_resources, "import_images") boot_resources.import_images.return_value = False sources, hosts = make_sources() maas_url = factory.make_simple_http_url() yield boot_images.import_boot_images(sources, maas_url) self.assertThat( boot_resources.import_images, MockCalledOnceWith(fix_sources_for_cluster(sources, maas_url)), ) self.assertThat(protocol.UpdateLastImageSync, MockNotCalled())
def test_update_last_image_sync_end_to_end(self): get_maas_id = self.patch(boot_images, "get_maas_id") get_maas_id.return_value = factory.make_string() self.useFixture(ClusterConfigurationFixture()) fixture = self.useFixture(MockLiveClusterToRegionRPCFixture()) protocol, connecting = fixture.makeEventLoop( region.UpdateLastImageSync) protocol.UpdateLastImageSync.return_value = succeed({}) self.addCleanup((yield connecting)) self.patch_autospec(boot_resources, 'import_images') boot_resources.import_images.return_value = True sources, hosts = make_sources() yield boot_images.import_boot_images(sources) self.assertThat(boot_resources.import_images, MockCalledOnceWith(sources)) self.assertThat(protocol.UpdateLastImageSync, MockCalledOnceWith(protocol, system_id=get_maas_id()))
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()))
def prepareRegionForGetControllerType(test, is_region=False, is_rack=True): """Set up a mock region controller that responds to `GetControllerType`. In addition it sets the MAAS ID to a new random value. It arranges for tear-down at the end of the test. :return: The running RPC service, and the protocol instance. """ fixture = test.useFixture(MockLiveClusterToRegionRPCFixture()) protocol, connecting = fixture.makeEventLoop(region.GetControllerType) protocol.RegisterRackController.side_effect = always_succeed_with( {"system_id": factory.make_name("maas-id")}) protocol.GetControllerType.side_effect = always_succeed_with({ "is_region": is_region, "is_rack": is_rack }) def connected(teardown): test.addCleanup(teardown) return services.getServiceNamed("rpc"), protocol return connecting.addCallback(connected)
def patch_rpc_methods(self): fixture = self.useFixture(MockLiveClusterToRegionRPCFixture()) protocol, connecting = fixture.makeEventLoop( region.ReportForeignDHCPServer) return protocol, connecting
def patch_rpc_UpdateLease(self): fixture = self.useFixture(MockLiveClusterToRegionRPCFixture()) protocol, connecting = fixture.makeEventLoop(UpdateLease) return protocol, connecting
def patch_rpc_methods(self): fixture = self.useFixture(MockLiveClusterToRegionRPCFixture()) protocol, connecting = fixture.makeEventLoop(region.UpdateServices) return protocol, connecting
def patch_rpc_methods(self, return_value=None): fixture = self.useFixture(MockLiveClusterToRegionRPCFixture()) protocol, connecting = fixture.makeEventLoop(region.GetArchiveMirrors) protocol.GetArchiveMirrors.return_value = return_value return protocol, connecting
def prepare_region_rpc(self): fixture = self.useFixture(MockLiveClusterToRegionRPCFixture()) protocol, connecting = fixture.makeEventLoop(region.CreateNode) return protocol, connecting
def create_fake_rpc_service(self): fixture = self.useFixture(MockLiveClusterToRegionRPCFixture()) protocol, connecting = fixture.makeEventLoop(region.RequestRackRefresh) self.addCleanup((yield connecting)) protocol.RequestRackRefresh.return_value = self.metadata_creds returnValue(protocol)
def prepareRegion( test, *, is_region=False, is_rack=True, servers=None, peers=None, trusted_networks=None, proxy_enabled=True, proxy_port=8000, proxy_allowed_cidrs=None, proxy_prefer_v4_proxy=False, syslog_port=5247 ): """Set up a mock region controller. It responds to `GetControllerType`, `GetTimeConfiguration`, `GetDNSConfiguration`, `GetProxyConfiguration`, and `GetSyslogConfiguration`. :return: The running RPC service, and the protocol instance. """ fixture = test.useFixture(MockLiveClusterToRegionRPCFixture()) protocol, connecting = fixture.makeEventLoop( region.GetControllerType, region.GetTimeConfiguration, region.GetDNSConfiguration, region.GetProxyConfiguration, region.GetSyslogConfiguration, ) protocol.RegisterRackController.side_effect = always_succeed_with( {"system_id": factory.make_name("maas-id")} ) protocol.GetControllerType.side_effect = always_succeed_with( {"is_region": is_region, "is_rack": is_rack} ) protocol.GetTimeConfiguration.side_effect = always_succeed_with( { "servers": [] if servers is None else servers, "peers": [] if peers is None else peers, } ) protocol.GetDNSConfiguration.side_effect = always_succeed_with( { "trusted_networks": ( [] if trusted_networks is None else trusted_networks ) } ) if proxy_allowed_cidrs is None: proxy_allowed_cidrs = [] protocol.GetProxyConfiguration.side_effect = always_succeed_with( { "enabled": proxy_enabled, "port": proxy_port, "allowed_cidrs": proxy_allowed_cidrs, "prefer_v4_proxy": proxy_prefer_v4_proxy, } ) protocol.GetSyslogConfiguration.side_effect = always_succeed_with( {"port": syslog_port} ) def connected(teardown): test.addCleanup(teardown) return services.getServiceNamed("rpc"), protocol return connecting.addCallback(connected)