private static void dump( DiagnosticsProvider provider, DiagnosticsPhase phase, Log log ) { // Optimization to skip diagnostics dumping (which is time consuming) if there's no log anyway. // This is first and foremost useful for speeding up testing. if ( log == NullLog.getInstance() ) { return; } try { provider.dump( phase, log.infoLogger() ); } catch ( Exception cause ) { log.error( "Failure while logging diagnostics for " + provider, cause ); } }
private Logger getLogger( LogRecord record, Log log ) { int level = record.getLevel().intValue(); if ( level <= Level.FINE.intValue() ) { return log.debugLogger(); } else if ( level <= Level.INFO.intValue() ) { return log.infoLogger(); } else if ( level <= Level.WARNING.intValue() ) { return log.warnLogger(); } else { return log.errorLogger(); } }
@Test public void shouldNotLoadAnyProcedureIfConfigIsEmpty() throws Throwable { // Given ProcedureConfig config = new ProcedureConfig( Config.defaults( procedure_whitelist, "" ) ); Log log = mock(Log.class); ReflectiveProcedureCompiler procedureCompiler = new ReflectiveProcedureCompiler( new TypeMappers(), components, components, log, config ); // When List<CallableProcedure> proc = procedureCompiler.compileProcedure( SingleReadOnlyProcedure.class, null, false ); // Then verify( log ) .warn( "The procedure 'org.neo4j.kernel.impl.proc.listCoolPeople' is not on the whitelist and won't be loaded." ); assertThat( proc.isEmpty(), is(true) ); }
@Test public void shouldPrintDiskUsage() { // Not sure how to get around this w/o spying. The method that we're unit testing will construct // other File instances with this guy as parent and internally the File constructor uses the field 'path' // which, if purely mocked, won't be assigned. At the same time we want to control the total/free space methods // and what they return... a tough one. File storeDir = Mockito.spy( new File( "storeDir" ) ); DatabaseLayout layout = mock( DatabaseLayout.class ); when( layout.databaseDirectory() ).thenReturn( storeDir ); when( storeDir.getTotalSpace() ).thenReturn( 100L ); when( storeDir.getFreeSpace() ).thenReturn( 40L ); AssertableLogProvider logProvider = new AssertableLogProvider(); KernelDiagnostics.StoreFiles storeFiles = new KernelDiagnostics.StoreFiles( layout ); storeFiles.dump( logProvider.getLog( getClass() ).debugLogger() ); logProvider.assertContainsMessageContaining( "100 / 40 / 40" ); }
@Test public void shouldNotLoadNoneWhiteListedFunction() throws Throwable { // Given Log log = spy(Log.class); procedureCompiler = new ReflectiveProcedureCompiler( new TypeMappers(), components, new ComponentRegistry(), log, new ProcedureConfig( Config.defaults( GraphDatabaseSettings.procedure_whitelist, "WrongName" ) ) ); List<CallableUserAggregationFunction> method = compile( SingleAggregationFunction.class ); verify( log ).warn( "The function 'org.neo4j.kernel.impl.proc.collectCool' is not on the whitelist and won't be loaded." ); assertThat( method.size(), equalTo( 0 ) ); }
@Test public void shouldEnableAutoReadWhenAtLowWatermark() { BoltConnectionReadLimiter limiter = newLimiter( 1, 2 ); assertTrue( channel.config().isAutoRead() ); limiter.enqueued( connection, job ); limiter.enqueued( connection, job ); limiter.enqueued( connection, job ); limiter.drained( connection, Arrays.asList( job, job ) ); assertTrue( channel.config().isAutoRead() ); verify( log, times( 1 ) ).warn( contains( "disabled" ), eq( channel.remoteAddress() ), eq( 3 ) ); verify( log, times( 1 ) ).warn( contains( "enabled" ), eq( channel.remoteAddress() ), eq( 1 ) ); }
@Test public void shouldLogIfConfigFileCouldNotBeFound() { Log log = mock( Log.class ); File confFile = testDirectory.file( "test.conf" ); // Note: we don't create the file. Config config = Config.fromFile( confFile ).withNoThrowOnFileLoadFailure().build(); config.setLogger( log ); verify( log ).warn( "Config file [%s] does not exist.", confFile ); }
@Test public void updateDynamicShouldLogExceptionsFromUpdateListeners() { Config config = Config.builder().withConfigClasses( singletonList( new MyDynamicSettings() ) ).build(); IllegalStateException exception = new IllegalStateException( "Boo" ); config.registerDynamicUpdateListener( MyDynamicSettings.boolSetting, ( a, b ) -> { throw exception; } ); Log log = mock( Log.class ); config.setLogger( log ); String settingName = MyDynamicSettings.boolSetting.name(); config.updateDynamicSetting( settingName, "", ORIGIN ); verify( log ).error( "Failure when notifying listeners after dynamic setting change; " + "new setting might not have taken effect: Boo", exception ); }
@Test public void shouldLogIfConfigFileCouldNotBeRead() throws IOException { Log log = mock( Log.class ); File confFile = testDirectory.file( "test.conf" ); assertTrue( confFile.createNewFile() ); assumeTrue( confFile.setReadable( false ) ); Config config = Config.fromFile( confFile ).withNoThrowOnFileLoadFailure().build(); config.setLogger( log ); verify( log ).error( "Unable to load config file [%s]: %s", confFile, confFile + " (Permission denied)" ); }
@Test public void shouldInjectLogging() throws KernelException { // Given Log log = spy( Log.class ); components.register( Log.class, ctx -> log ); CallableUserFunction function = procedureCompiler.compileFunction( LoggingFunction.class ).get( 0 ); // When function.apply( new BasicContext(), new AnyValue[0] ); // Then verify( log ).debug( "1" ); verify( log ).info( "2" ); verify( log ).warn( "3" ); verify( log ).error( "4" ); }
@Test public void updateDynamicShouldLogChanges() { String settingName = MyDynamicSettings.boolSetting.name(); String changedMessage = "Setting changed: '%s' changed from '%s' to '%s' via '%s'"; Config config = Config.builder().withConfigClasses( singletonList( new MyDynamicSettings() ) ).build(); Log log = mock( Log.class ); config.setLogger( log ); config.updateDynamicSetting( settingName, "false", ORIGIN ); config.updateDynamicSetting( settingName, "true", ORIGIN ); config.updateDynamicSetting( settingName, "", ORIGIN ); InOrder order = inOrder( log ); order.verify( log ).info( changedMessage, settingName, "default (true)", "false", "test" ); order.verify( log ).info( changedMessage, settingName, "false", "true", "test" ); order.verify( log ).info( changedMessage, settingName, "true", "default (true)", "test" ); verifyNoMoreInteractions( log ); }
@Test public void shouldNotDisableAutoReadBelowHighWatermark() { BoltConnectionReadLimiter limiter = newLimiter( 1, 2 ); assertTrue( channel.config().isAutoRead() ); limiter.enqueued( connection, job ); assertTrue( channel.config().isAutoRead() ); verify( log, never() ).warn( anyString(), any(), any() ); }
@Test public void shouldFailNicelyWhenAllUsesUnsafeAPI() throws Throwable { //When compiler.compileFunction( FunctionsAndProcedureUnsafe.class ); compiler.compileProcedure( FunctionsAndProcedureUnsafe.class, null, false ); compiler.compileAggregationFunction( FunctionsAndProcedureUnsafe.class ); // Then verify( log ).warn( notAvailableMessage( "org.neo4j.kernel.impl.proc.safeUserFunctionInUnsafeAPIClass" ) ); verify( log ).warn( notAvailableMessage( "org.neo4j.kernel.impl.proc.listCoolPeopleProcedure" ) ); // With extra ' ' space at the end to distinguish from procedure form: verify( log ).warn( notAvailableMessage( "org.neo4j.kernel.impl.proc.listCoolPeople " ) ); }
@Test public void nonStrictRetainsSettings() { IndividualSettingsValidator iv = new IndividualSettingsValidator( singletonList( strict_config_validation ), true ); final Map<String,String> rawConfig = stringMap( strict_config_validation.name(), FALSE, "dbms.jibber.jabber", "bla", "external_plugin.foo", "bar" ); Config config = mockConfig( rawConfig ); iv.validate( config, log ); verify( log ).warn( "Unknown config option: %s", "dbms.jibber.jabber" ); verifyNoMoreInteractions( log ); }
private void generatePrivateKeyAndCertificate( Log log, String policyName, File keyCertChainFile, File privateKeyFile, File trustedCertificatesDir, File revokedCertificatesDir ) { log.info( format( "Generating key and self-signed certificate for SSL policy '%s'", policyName ) ); String hostname = config.get( default_advertised_address ); try { pkiUtils.createSelfSignedCertificate( keyCertChainFile, privateKeyFile, hostname ); trustedCertificatesDir.mkdir(); revokedCertificatesDir.mkdir(); } catch ( GeneralSecurityException | IOException | OperatorCreationException e ) { throw new RuntimeException( "Failed to generate private key and certificate", e ); } }
@Override public void info( String s ) { log.info( s ); }
@Override public void error( String s, Throwable throwable ) { log.error( s, throwable ); }