def test__tryUpdate_logs_errors_from_broken_method(self): # Patch the logger in the clusterservice so no log messages are printed # because the tests run in debug mode. self.patch(common.log, 'debug') rpc_service, _ = yield prepareRegion(self) self.patch_autospec(ntp, "configure_rack") # No-op configuration. service = ntp.RackNetworkTimeProtocolService(rpc_service, reactor) broken_method = self.patch_autospec(service, self.method) broken_method.side_effect = factory.make_exception() # Ensure that we never actually execute against systemd. self.patch_autospec(service_monitor, "restartService") self.useFixture(MAASRootFixture()) with TwistedLoggerFixture() as logger: yield service._tryUpdate() self.assertThat( logger.output, DocTestMatches(""" Failed to update NTP configuration. Traceback (most recent call last): ... maastesting.factory.TestException#... """))
def setUp(self): self.assertEqual = super(testtools.TestCase, self).assertEqual # Every test gets a pristine MAAS_ROOT, when it is defined. if "MAAS_ROOT" in os.environ: self.useFixture(MAASRootFixture()) if "MAAS_DATA" in os.environ: self.useFixture(MAASDataFixture()) rand_seed = os.environ.get("MAAS_RAND_SEED") random.seed(rand_seed) seed_info = [] if rand_seed is not None: seed_info.append(f"MAAS_RAND_SEED={rand_seed}") if "PYTHONHASHSEED" in os.environ: seed_info.append("PYTHONHASHSEED={}".format( os.environ["PYTHONHASHSEED"])) if seed_info: self.addDetail("Seeds", text_content(" ".join(seed_info))) # Capture Twisted logs and add them as a test detail. twistedLog = self.useFixture(TwistedLoggerFixture()) self.addDetail("Twisted logs", twistedLog.getContent()) self.maybeCloseDatabaseConnections() super().setUp() self.setUpResources() self.addCleanup(self.tearDownResources)
def test_sets_dns_rack_service_to_any_when_is_region(self): # Patch the logger in the clusterservice so no log messages are printed # because the tests run in debug mode. self.patch(common.log, "debug") self.useFixture(MAASRootFixture()) rpc_service, _ = yield prepareRegion(self, is_region=True) service, dns = self.make_RackDNS_ExternalService(rpc_service, reactor) self.patch_autospec(dns, "_configure") # No-op configuration. # There is no most recently applied configuration. self.assertThat(dns._configuration, Is(None)) with TwistedLoggerFixture() as logger: yield service.startService() self.addCleanup((yield service.stopService)) yield service._orig_tryUpdate() # Ensure that the service was set to any. service = service_monitor.getServiceByName("dns_rack") self.assertEqual( (SERVICE_STATE.ANY, "managed by the region"), service.getExpectedState(), ) # The most recently applied configuration is set, though it was not # actually "applied" because this host was configured as a region+rack # controller, and the rack should not attempt to manage the DNS server # on a region+rack. self.assertThat( dns._configuration, IsInstance(external._DNSConfiguration) ) # The configuration was not applied. self.assertThat(dns._configure, MockNotCalled()) # Nothing was logged; there's no need for lots of chatter. self.assertThat(logger.output, Equals(""))
def test__tryUpdate_writes_nginx_config_reloads_nginx(self): self.useFixture(MAASRootFixture()) rpc_service, _ = yield prepareRegion(self) region_ips = self.extract_regions(rpc_service) resource_root = self.make_dir() + '/' service = self.make_startable_RackHTTPService(resource_root, rpc_service, reactor) # Mock service_monitor to catch reloadService. mock_reloadService = self.patch(http.service_monitor, 'reloadService') mock_reloadService.return_value = always_succeed_with(None) yield service.startService() self.addCleanup((yield service.stopService)) yield service._orig_tryUpdate() # Verify the contents of the written config. target_path = http.compose_http_config_path('rackd.nginx.conf') self.assertThat( target_path, FileContains(matcher=Contains('alias %s;' % resource_root))) for region_ip in region_ips: self.assertThat( target_path, FileContains(matcher=Contains('server %s:5240;' % region_ip))) self.assertThat( target_path, FileContains( matcher=Contains('proxy_pass http://maas-regions/MAAS/;'))) self.assertThat(mock_reloadService, MockCalledOnceWith('http')) # If the configuration has not changed then a second call to # `_tryUpdate` does not result in another call to `_configure`. yield service._orig_tryUpdate() self.assertThat(mock_reloadService, MockCalledOnceWith('http'))
def test__tryUpdate_logs_errors_from_broken_method(self): # Patch the logger in the clusterservice so no log messages are printed # because the tests run in debug mode. self.patch(common.log, "debug") rpc_service, _ = yield prepareRegion(self) self.patch_autospec(external, "configure_rack") # No-op configuration. ntp = external.RackNTP() service = make_startable_RackExternalService(self, rpc_service, reactor, [("NTP", ntp)]) broken_method = self.patch_autospec(ntp, self.method) broken_method.side_effect = factory.make_exception() # Ensure that we never actually execute against systemd. self.patch_autospec(service_monitor, "restartService") yield service.startService() self.addCleanup((yield service.stopService)) self.useFixture(MAASRootFixture()) with TwistedLoggerFixture() as logger: yield service._orig_tryUpdate() self.assertThat( logger.output, DocTestMatches(""" Failed to update NTP configuration. Traceback (most recent call last): ... maastesting.factory.TestException#... """), )
def test__tryUpdate_logs_errors_from_broken_method(self): # Patch the logger in the clusterservice so no log messages are printed # because the tests run in debug mode. self.patch(common.log, 'debug') rpc_service, _ = yield prepareRegion(self) service = self.make_startable_RackDNSService(rpc_service, reactor) self.patch_autospec(service, "_configure") # No-op configuration. broken_method = self.patch_autospec(service, self.method) broken_method.side_effect = factory.make_exception() self.useFixture(MAASRootFixture()) with TwistedLoggerFixture() as logger: yield service.startService() self.addCleanup((yield service.stopService)) yield service._orig_tryUpdate() self.assertThat( logger.output, DocTestMatches(""" Failed to update DNS configuration. Traceback (most recent call last): ... maastesting.factory.TestException#... """))
def test__tryUpdate_updates_ntp_server(self): self.useFixture(MAASRootFixture()) trusted_networks = self.make_trusted_networks() rpc_service, _ = yield prepareRegion(self, trusted_networks=trusted_networks) region_ips = self.extract_regions(rpc_service) service = self.make_startable_RackDNSService(rpc_service, reactor) bind_write_options = self.patch_autospec(dns, "bind_write_options") bind_write_configuration = self.patch_autospec( dns, "bind_write_configuration") bind_reload_with_retries = self.patch_autospec( dns, "bind_reload_with_retries") yield service.startService() self.addCleanup((yield service.stopService)) yield service._orig_tryUpdate() self.assertThat( bind_write_options, MockCalledOnceWith(upstream_dns=list(sorted(region_ips)), dnssec_validation='no')) self.assertThat(bind_write_configuration, MockCalledOnceWith([], list(sorted(trusted_networks)))) self.assertThat(bind_reload_with_retries, MockCalledOnceWith()) # If the configuration has not changed then a second call to # `_tryUpdate` does not result in another call to `_configure`. yield service._orig_tryUpdate() self.assertThat( bind_write_options, MockCalledOnceWith(upstream_dns=list(sorted(region_ips)), dnssec_validation='no')) self.assertThat(bind_write_configuration, MockCalledOnceWith([], list(sorted(trusted_networks)))) self.assertThat(bind_reload_with_retries, MockCalledOnceWith())
def test_is_silent_does_nothing_but_saves_config_when_is_region(self): # Patch the logger in the clusterservice so no log messages are printed # because the tests run in debug mode. self.patch(common.log, "debug") self.useFixture(MAASRootFixture()) rpc_service, _ = yield prepareRegion(self, is_region=True) service, ntp = self.make_RackNTP_ExternalService(rpc_service, reactor) self.patch_autospec(external, "configure_rack") # No-op configuration. # There is no most recently applied configuration. self.assertThat(ntp._configuration, Is(None)) yield service.startService() self.addCleanup((yield service.stopService)) with TwistedLoggerFixture() as logger: yield service._orig_tryUpdate() # The most recently applied configuration is set, though it was not # actually "applied" because this host was configured as a region+rack # controller, and the rack should not attempt to manage the NTP server # on a region+rack. self.assertThat( ntp._configuration, IsInstance(external._NTPConfiguration) ) # The configuration was not applied. self.assertThat(external.configure_rack, MockNotCalled()) # Nothing was logged; there's no need for lots of chatter. self.assertThat(logger.output, Equals(""))
def test_tryUpdate_logs_errors_from_broken_method(self): # Patch the logger in the clusterservice so no log messages are printed # because the tests run in debug mode. self.patch(common.log, "debug") # Mock service_monitor to catch reloadService. mock_reloadService = self.patch(http.service_monitor, "reloadService") mock_reloadService.return_value = always_succeed_with(None) rpc_service, _ = yield prepareRegion(self) service = self.make_startable_RackHTTPService(self.make_dir(), rpc_service, reactor) self.patch_autospec(service, "_configure") # No-op configuration. broken_method = self.patch_autospec(service, self.method) broken_method.side_effect = factory.make_exception() self.useFixture(MAASRootFixture()) with TwistedLoggerFixture() as logger: yield service.startService() self.addCleanup((yield service.stopService)) yield service._orig_tryUpdate() self.assertThat( logger.output, DocTestMatches(""" Failed to update HTTP configuration. Traceback (most recent call last): ... maastesting.factory.TestException#... """), )
def test__tryUpdate_updates_syslog_server(self): self.useFixture(MAASRootFixture()) port = factory.pick_port() rpc_service, _ = yield prepareRegion(self, syslog_port=port) forwarders = self.extract_regions(rpc_service) service, _ = self.make_RackSyslog_ExternalService(rpc_service, reactor) write_config = self.patch_autospec(external.syslog_config, "write_config") service_monitor = self.patch_autospec(external, "service_monitor") yield service.startService() self.addCleanup((yield service.stopService)) yield service._orig_tryUpdate() expected_forwards = [{ "name": name, "ip": ip } for name, ip in forwarders] self.assertThat( write_config, MockCalledOnceWith(False, forwarders=expected_forwards, port=port), ) self.assertThat(service_monitor.restartService, MockCalledOnceWith("syslog_rack")) # If the configuration has not changed then a second call to # `_tryUpdate` does not result in another call to `_configure`. yield service._orig_tryUpdate() self.assertThat( write_config, MockCalledOnceWith(False, forwarders=expected_forwards, port=port), ) self.assertThat(service_monitor.restartService, MockCalledOnceWith("syslog_rack"))
def test_breaks_when_MAAS_ROOT_is_not_a_directory(self): self.patch(fixtures_module, "root", self.make_file()) fixture = MAASRootFixture() error = self.assertRaises(NotADirectoryError, fixture._setUp) self.assertThat( str(error), DocTestMatches("Skeleton MAAS_ROOT (...) is not a directory."), )
def test_creates_populates_and_removes_new_directory(self): fixture = MAASRootFixture() with fixture: self.assertThat(fixture.path, PathExists()) self.assertThat(fixture.path, Not(SamePath(self.skel))) files_expected = set(listdirs(self.skel)) files_observed = set(listdirs(fixture.path)) self.assertThat(files_observed, Equals(files_expected)) self.assertThat(fixture.path, Not(PathExists()))
def test_tryUpdate_updates_dns_server(self): self.useFixture(MAASRootFixture()) trusted_networks = self.make_trusted_networks() rpc_service, _ = yield prepareRegion( self, trusted_networks=trusted_networks ) region_ips = self.extract_regions(rpc_service) service, _ = self.make_RackDNS_ExternalService(rpc_service, reactor) mock_ensureService = self.patch_autospec( service_monitor, "ensureService" ) mock_ensureService.side_effect = always_succeed_with(None) bind_write_options = self.patch_autospec( external, "bind_write_options" ) bind_write_configuration = self.patch_autospec( external, "bind_write_configuration" ) bind_reload_with_retries = self.patch_autospec( external, "bind_reload_with_retries" ) yield service.startService() self.addCleanup((yield service.stopService)) yield service._orig_tryUpdate() self.assertThat( bind_write_options, MockCalledOnceWith( upstream_dns=list(sorted(region_ips)), dnssec_validation="no" ), ) self.assertThat( bind_write_configuration, MockCalledOnceWith([], list(sorted(trusted_networks))), ) self.assertThat(mock_ensureService, MockCalledOnceWith("dns_rack")) self.assertThat(bind_reload_with_retries, MockCalledOnceWith()) # If the configuration has not changed then a second call to # `_tryUpdate` does not result in another call to `_configure`. yield service._orig_tryUpdate() self.assertThat( bind_write_options, MockCalledOnceWith( upstream_dns=list(sorted(region_ips)), dnssec_validation="no" ), ) self.assertThat( bind_write_configuration, MockCalledOnceWith([], list(sorted(trusted_networks))), ) self.assertThat(mock_ensureService, MockCalledOnceWith("dns_rack")) self.assertThat(bind_reload_with_retries, MockCalledOnceWith())
def test_tryUpdate_updates_proxy_server(self): self.useFixture(MAASRootFixture()) allowed_cidrs = self.make_cidrs() proxy_prefer_v4_proxy = factory.pick_bool() proxy_port = random.randint(1000, 8000) rpc_service, _ = yield prepareRegion( self, proxy_allowed_cidrs=allowed_cidrs, proxy_port=proxy_port, proxy_prefer_v4_proxy=proxy_prefer_v4_proxy, ) region_ips = self.extract_regions(rpc_service) service, _ = self.make_RackProxy_ExternalService(rpc_service, reactor) write_config = self.patch_autospec( external.proxy_config, "write_config" ) service_monitor = self.patch_autospec(external, "service_monitor") yield service.startService() self.addCleanup((yield service.stopService)) yield service._orig_tryUpdate() expected_peers = sorted( ["http://%s:%s" % (ip, proxy_port) for ip in region_ips] ) self.assertThat( write_config, MockCalledOnceWith( allowed_cidrs, peer_proxies=expected_peers, prefer_v4_proxy=proxy_prefer_v4_proxy, maas_proxy_port=proxy_port, ), ) self.assertThat( service_monitor.reloadService, MockCalledOnceWith("proxy_rack") ) # If the configuration has not changed then a second call to # `_tryUpdate` does not result in another call to `_configure`. yield service._orig_tryUpdate() self.assertThat( write_config, MockCalledOnceWith( allowed_cidrs, peer_proxies=expected_peers, prefer_v4_proxy=proxy_prefer_v4_proxy, maas_proxy_port=proxy_port, ), ) self.assertThat( service_monitor.reloadService, MockCalledOnceWith("proxy_rack") )
def test_is_silent_and_does_nothing_when_region_is_not_available(self): self.useFixture(MAASRootFixture()) service = ntp.RackNetworkTimeProtocolService( StubClusterClientService(), reactor) self.patch_autospec(service, "_maybeApplyConfiguration") with TwistedLoggerFixture() as logger: yield service._tryUpdate() self.assertThat(logger.output, Equals("")) self.assertThat(service._maybeApplyConfiguration, MockNotCalled())
def test_is_silent_and_does_nothing_when_rack_is_not_recognised(self): self.useFixture(MAASRootFixture()) rpc_service, protocol = yield prepareRegion(self) protocol.GetControllerType.side_effect = exceptions.NoSuchNode service = ntp.RackNetworkTimeProtocolService(rpc_service, reactor) self.patch_autospec(service, "_maybeApplyConfiguration") with TwistedLoggerFixture() as logger: yield service._tryUpdate() self.assertThat(logger.output, Equals("")) self.assertThat(service._maybeApplyConfiguration, MockNotCalled())
def test_is_silent_and_does_nothing_when_region_is_not_available(self): # Patch the logger in the clusterservice so no log messages are printed # because the tests run in debug mode. self.patch(common.log, 'debug') self.useFixture(MAASRootFixture()) service = dns.RackDNSService(StubClusterClientService(), reactor) self.patch_autospec(service, "_maybeApplyConfiguration") with TwistedLoggerFixture() as logger: yield service._tryUpdate() self.assertThat(logger.output, Equals("")) self.assertThat(service._maybeApplyConfiguration, MockNotCalled())
def setUp(self): # Every test gets a pristine MAAS_ROOT, when it is defined. if "MAAS_ROOT" in os.environ: self.useFixture(MAASRootFixture()) # Capture Twisted logs and add them as a test detail. twistedLog = self.useFixture(TwistedLoggerFixture()) self.addDetail("Twisted logs", twistedLog.getContent()) self.maybeCloseDatabaseConnections() super(MAASTestCase, self).setUp() self.setUpResources() self.addCleanup(self.tearDownResources)
def test_is_silent_and_does_nothing_when_rack_is_not_recognised(self): # Patch the logger in the clusterservice so no log messages are printed # because the tests run in debug mode. self.patch(common.log, 'debug') self.useFixture(MAASRootFixture()) rpc_service, protocol = yield prepareRegion(self) protocol.GetControllerType.side_effect = exceptions.NoSuchNode service = dns.RackDNSService(rpc_service, reactor) self.patch_autospec(service, "_maybeApplyConfiguration") with TwistedLoggerFixture() as logger: yield service._tryUpdate() self.assertThat(logger.output, Equals("")) self.assertThat(service._maybeApplyConfiguration, MockNotCalled())
def test__tryUpdate_updates_ntp_server(self): self.useFixture(MAASRootFixture()) servers, peers = self.make_servers_and_peers() rpc_service, _ = yield prepareRegion(self, servers=servers, peers=peers) service = ntp.RackNetworkTimeProtocolService(rpc_service, reactor) configure_rack = self.patch_autospec(ntp, "configure_rack") restartService = self.patch_autospec(service_monitor, "restartService") yield service._tryUpdate() self.assertThat(configure_rack, MockCalledOnceWith(servers, peers)) self.assertThat(restartService, MockCalledOnceWith("ntp_rack")) # If the configuration has not changed then a second call to # `_tryUpdate` does not result in another call to `configure`. yield service._tryUpdate() self.assertThat(configure_rack, MockCalledOnceWith(servers, peers)) self.assertThat(restartService, MockCalledOnceWith("ntp_rack"))
def test_is_silent_and_does_nothing_when_region_is_not_available(self): # Patch the logger in the clusterservice so no log messages are printed # because the tests run in debug mode. self.patch(common.log, "debug") self.useFixture(MAASRootFixture()) ntp = external.RackNTP() service = make_startable_RackExternalService( self, StubClusterClientService(), reactor, [("NTP", ntp)]) self.patch_autospec(ntp, "_tryUpdate") yield service.startService() self.addCleanup((yield service.stopService)) with TwistedLoggerFixture() as logger: yield service._tryUpdate() self.assertThat(logger.output, Equals("")) self.assertThat(ntp._tryUpdate, MockNotCalled())
def test_is_silent_and_does_nothing_when_rack_is_not_recognised(self): # Patch the logger in the clusterservice so no log messages are printed # because the tests run in debug mode. self.patch(common.log, "debug") self.useFixture(MAASRootFixture()) rpc_service, protocol = yield prepareRegion(self) protocol.GetControllerType.side_effect = exceptions.NoSuchNode ntp = external.RackNTP() service = make_startable_RackExternalService( self, StubClusterClientService(), reactor, [("NTP", ntp)]) self.patch_autospec(ntp, "_tryUpdate") yield service.startService() self.addCleanup((yield service.stopService)) with TwistedLoggerFixture() as logger: yield service._tryUpdate() self.assertThat(logger.output, Equals("")) self.assertThat(ntp._tryUpdate, MockNotCalled())
def test_is_silent_does_nothing_but_saves_config_when_is_region(self): self.useFixture(MAASRootFixture()) rpc_service, _ = yield prepareRegion(self, is_region=True) service = ntp.RackNetworkTimeProtocolService(rpc_service, reactor) self.patch_autospec(ntp, "configure_rack") # No-op configuration. # There is no most recently applied configuration. self.assertThat(service._configuration, Is(None)) with TwistedLoggerFixture() as logger: yield service._tryUpdate() # The most recently applied configuration is set, though it was not # actually "applied" because this host was configured as a region+rack # controller, and the rack should not attempt to manage the NTP server # on a region+rack. self.assertThat(service._configuration, IsInstance(ntp._Configuration)) # The configuration was not applied. self.assertThat(ntp.configure_rack, MockNotCalled()) # Nothing was logged; there's no need for lots of chatter. self.assertThat(logger.output, Equals(""))
def setUp(self): super(TestRegionSyslogService, self).setUp() self.useFixture(MAASRootFixture())
def test_updates_MAAS_ROOT_in_the_environment(self): self.assertThat(os.environ["MAAS_ROOT"], Not(SamePath(self.skel))) with MAASRootFixture() as fixture: self.assertThat(os.environ["MAAS_ROOT"], SamePath(fixture.path)) self.assertThat(os.environ["MAAS_ROOT"], Not(SamePath(self.skel)))
def setUp(self): super().setUp() self.useFixture(MAASRootFixture())
def setUp(self): super(TestConfigure, self).setUp() self.useFixture(MAASRootFixture())
def setUp(self): super(TestRegionNetworkTimeProtocolService, self).setUp() self.useFixture(MAASRootFixture())