Log EppExceptions in EppController at INFO (vs FlowRunner at WARNING)

The logging for exceptions in FlowRunner - always at WARNING - has long been sub-optimal.  For EppExceptions it's too aggressive/spammy to log at WARNING because it's generally not actionable - EppException gets properly thrown for all kinds of ordinary reasons (trying to create a resource when one already exists with that foreign key) and/or for client misbehavior that we can't control (sending bad parameter values, etc.).  For non-EppException RuntimeExceptions, it's redundant with existing logging in EppController.

This CL resolves this by removing that logging in FlowRunner entirely in favor of the EppController logging, where we're now logging EppExceptions at INFO in parallel with the existing logging of RuntimeExceptions at SEVERE.  This has the benefit that we're now logging EppExceptions that come from FlowPicker (by way of EppExceptionInProviderException),  which previously were unlogged.

Note however that this does mean that in places where we run FlowRunner without EppController - exclusively test code as it stands today - we'd no longer be logging EppExceptions.  If that seems like a loss, we could either reinstate logging there (at INFO) and just deal with redundant messages for most EppExceptions, or we could add it manually to places where we call FlowRunner.run() in tests and avoid the redundancy that way.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=154733365
This commit is contained in:
nickfelt 2017-05-01 11:00:14 -07:00 committed by Ben McIlwain
parent 11e7374c0f
commit f640d765e8
6 changed files with 196 additions and 76 deletions

View file

@ -118,12 +118,14 @@ public final class EppController {
try { try {
return flowComponent.flowRunner().run(eppMetricBuilder); return flowComponent.flowRunner().run(eppMetricBuilder);
} catch (EppException | EppExceptionInProviderException e) { } catch (EppException | EppExceptionInProviderException e) {
// The command failed. Send the client an error message. // The command failed. Send the client an error message, but only log at INFO since many of
// these failures are innocuous or due to client error, so there's nothing we have to change.
logger.info(e, "Flow returned failure response");
EppException eppEx = (EppException) (e instanceof EppException ? e : e.getCause()); EppException eppEx = (EppException) (e instanceof EppException ? e : e.getCause());
return getErrorResponse(eppEx.getResult(), flowComponent.trid()); return getErrorResponse(eppEx.getResult(), flowComponent.trid());
} catch (Throwable e) { } catch (Throwable e) {
// Something bad and unexpected happened. Send the client a generic error, and log it. // Something bad and unexpected happened. Send the client a generic error, and log at SEVERE.
logger.severe(e, "Unexpected failure"); logger.severe(e, "Unexpected failure in flow execution");
return getErrorResponse(Result.create(Code.COMMAND_FAILED), flowComponent.trid()); return getErrorResponse(Result.create(Code.COMMAND_FAILED), flowComponent.trid());
} }
} }

View file

@ -14,7 +14,6 @@
package google.registry.flows; package google.registry.flows;
import static com.google.common.base.Throwables.getStackTraceAsString;
import static google.registry.model.ofy.ObjectifyService.ofy; import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.xml.XmlTransformer.prettyPrint; import static google.registry.xml.XmlTransformer.prettyPrint;
@ -100,17 +99,13 @@ public class FlowRunner {
} }
return output; return output;
} catch (EppException e) { } catch (EppException e) {
throw new RuntimeException(e); throw new EppRuntimeException(e);
} }
}}); }});
} catch (DryRunException e) { } catch (DryRunException e) {
return e.output; return e.output;
} catch (RuntimeException e) { } catch (EppRuntimeException e) {
logger.warning(getStackTraceAsString(e)); throw e.getCause();
if (e.getCause() instanceof EppException) {
throw (EppException) e.getCause();
}
throw e;
} }
} }
@ -122,4 +117,16 @@ public class FlowRunner {
this.output = output; this.output = output;
} }
} }
/** Exception for explicitly propagating an EppException out of the transactional {@code Work}. */
private static class EppRuntimeException extends RuntimeException {
EppRuntimeException(EppException cause) {
super(cause);
}
@Override
public synchronized EppException getCause() {
return (EppException) super.getCause();
}
}
} }

View file

@ -14,15 +14,24 @@
package google.registry.flows; package google.registry.flows;
import static com.google.common.io.BaseEncoding.base64;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static google.registry.flows.EppXmlTransformer.marshal; import static google.registry.flows.EppXmlTransformer.marshal;
import static google.registry.testing.LogsSubject.assertAboutLogs;
import static google.registry.testing.TestDataHelper.loadFileWithSubstitutions; import static google.registry.testing.TestDataHelper.loadFileWithSubstitutions;
import static google.registry.testing.TestLogHandlerUtils.findFirstLogRecordWithMessagePrefix;
import static java.nio.charset.StandardCharsets.UTF_8; import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.logging.Level.INFO;
import static java.util.logging.Level.SEVERE;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.common.testing.TestLogHandler;
import google.registry.flows.EppException.UnimplementedExtensionException;
import google.registry.flows.EppTestComponent.FakeServerTridProvider; import google.registry.flows.EppTestComponent.FakeServerTridProvider;
import google.registry.flows.FlowModule.EppExceptionInProviderException;
import google.registry.model.eppcommon.Trid; import google.registry.model.eppcommon.Trid;
import google.registry.model.eppoutput.EppOutput; import google.registry.model.eppoutput.EppOutput;
import google.registry.model.eppoutput.EppResponse; import google.registry.model.eppoutput.EppResponse;
@ -35,7 +44,12 @@ import google.registry.testing.FakeClock;
import google.registry.testing.ShardableTestCase; import google.registry.testing.ShardableTestCase;
import google.registry.util.Clock; import google.registry.util.Clock;
import google.registry.xml.ValidationMode; import google.registry.xml.ValidationMode;
import java.util.List;
import java.util.Map;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import org.joda.time.DateTime; import org.joda.time.DateTime;
import org.json.simple.JSONValue;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
@ -66,11 +80,18 @@ public class EppControllerTest extends ShardableTestCase {
private static final DateTime startTime = DateTime.parse("2016-09-01T00:00:00Z"); private static final DateTime startTime = DateTime.parse("2016-09-01T00:00:00Z");
private final Clock clock = new FakeClock(startTime); private final Clock clock = new FakeClock(startTime);
private final TestLogHandler logHandler = new TestLogHandler();
private final String domainCreateXml =
loadFileWithSubstitutions(
getClass(), "domain_create_prettyprinted.xml", ImmutableMap.<String, String>of());
private EppController eppController; private EppController eppController;
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
Logger.getLogger(EppController.class.getCanonicalName()).addHandler(logHandler);
when(sessionMetadata.getClientId()).thenReturn("some-client"); when(sessionMetadata.getClientId()).thenReturn("some-client");
when(flowComponentBuilder.flowModule(Matchers.<FlowModule>any())) when(flowComponentBuilder.flowModule(Matchers.<FlowModule>any()))
.thenReturn(flowComponentBuilder); .thenReturn(flowComponentBuilder);
@ -99,8 +120,7 @@ public class EppControllerTest extends ShardableTestCase {
} }
@Test @Test
public void testHandleEppCommand_unmarshallableData_exportsMetric() { public void testHandleEppCommand_unmarshallableData_exportsMetric() throws Exception {
ArgumentCaptor<EppMetric> metricCaptor = ArgumentCaptor.forClass(EppMetric.class);
eppController.handleEppCommand( eppController.handleEppCommand(
sessionMetadata, sessionMetadata,
transportCredentials, transportCredentials,
@ -109,6 +129,7 @@ public class EppControllerTest extends ShardableTestCase {
false, false,
new byte[0]); new byte[0]);
ArgumentCaptor<EppMetric> metricCaptor = ArgumentCaptor.forClass(EppMetric.class);
verify(metricsEnqueuer).export(metricCaptor.capture()); verify(metricsEnqueuer).export(metricCaptor.capture());
EppMetric metric = metricCaptor.getValue(); EppMetric metric = metricCaptor.getValue();
assertThat(metric.getRequestId()).isEqualTo("request-id-1"); assertThat(metric.getRequestId()).isEqualTo("request-id-1");
@ -120,11 +141,7 @@ public class EppControllerTest extends ShardableTestCase {
} }
@Test @Test
public void testHandleEppCommand_regularEppCommand_exportsMetric() { public void testHandleEppCommand_regularEppCommand_exportsMetric() throws Exception {
ArgumentCaptor<EppMetric> metricCaptor = ArgumentCaptor.forClass(EppMetric.class);
String domainCreateXml =
loadFileWithSubstitutions(
getClass(), "domain_create_prettyprinted.xml", ImmutableMap.<String, String>of());
eppController.handleEppCommand( eppController.handleEppCommand(
sessionMetadata, sessionMetadata,
transportCredentials, transportCredentials,
@ -133,6 +150,7 @@ public class EppControllerTest extends ShardableTestCase {
true, true,
domainCreateXml.getBytes(UTF_8)); domainCreateXml.getBytes(UTF_8));
ArgumentCaptor<EppMetric> metricCaptor = ArgumentCaptor.forClass(EppMetric.class);
verify(metricsEnqueuer).export(metricCaptor.capture()); verify(metricsEnqueuer).export(metricCaptor.capture());
EppMetric metric = metricCaptor.getValue(); EppMetric metric = metricCaptor.getValue();
assertThat(metric.getRequestId()).isEqualTo("request-id-1"); assertThat(metric.getRequestId()).isEqualTo("request-id-1");
@ -143,4 +161,87 @@ public class EppControllerTest extends ShardableTestCase {
assertThat(metric.getStatus()).hasValue(Code.SUCCESS_WITH_NO_MESSAGES); assertThat(metric.getStatus()).hasValue(Code.SUCCESS_WITH_NO_MESSAGES);
assertThat(metric.getEppTarget()).hasValue("example.tld"); assertThat(metric.getEppTarget()).hasValue("example.tld");
} }
@Test
public void testHandleEppCommand_unmarshallableData_loggedAtInfo_withJsonData() throws Exception {
eppController.handleEppCommand(
sessionMetadata,
transportCredentials,
EppRequestSource.UNIT_TEST,
false,
false,
"GET / HTTP/1.1\n\n".getBytes(UTF_8));
assertAboutLogs().that(logHandler)
.hasLogAtLevelWithMessage(INFO, "EPP request XML unmarshalling failed");
LogRecord logRecord =
findFirstLogRecordWithMessagePrefix(logHandler, "EPP request XML unmarshalling failed");
List<String> messageParts = Splitter.on('\n').splitToList(logRecord.getMessage());
assertThat(messageParts.size()).isAtLeast(2);
Map<String, Object> json = parseJsonMap(messageParts.get(1));
assertThat(json).containsEntry("clientId", "some-client");
assertThat(json).containsEntry("resultCode", 2001L); // Must be Long to compare equal.
assertThat(json).containsEntry("resultMessage", "Command syntax error");
assertThat(json)
.containsEntry("xmlBytes", base64().encode("GET / HTTP/1.1\n\n".getBytes(UTF_8)));
}
@Test
public void testHandleEppCommand_throwsEppException_loggedAtInfo() throws Exception {
when(flowRunner.run(eppController.eppMetricBuilder))
.thenThrow(new UnimplementedExtensionException());
eppController.handleEppCommand(
sessionMetadata,
transportCredentials,
EppRequestSource.UNIT_TEST,
false,
true,
domainCreateXml.getBytes(UTF_8));
assertAboutLogs().that(logHandler)
.hasLogAtLevelWithMessage(INFO, "Flow returned failure response");
LogRecord logRecord =
findFirstLogRecordWithMessagePrefix(logHandler, "Flow returned failure response");
assertThat(logRecord.getThrown()).isInstanceOf(UnimplementedExtensionException.class);
}
@Test
public void testHandleEppCommand_throwsEppExceptionInProviderException_loggedAtInfo()
throws Exception {
when(flowRunner.run(eppController.eppMetricBuilder))
.thenThrow(new EppExceptionInProviderException(new UnimplementedExtensionException()));
eppController.handleEppCommand(
sessionMetadata,
transportCredentials,
EppRequestSource.UNIT_TEST,
false,
true,
domainCreateXml.getBytes(UTF_8));
assertAboutLogs().that(logHandler)
.hasLogAtLevelWithMessage(INFO, "Flow returned failure response");
LogRecord logRecord =
findFirstLogRecordWithMessagePrefix(logHandler, "Flow returned failure response");
assertThat(logRecord.getThrown()).isInstanceOf(EppExceptionInProviderException.class);
}
@Test
public void testHandleEppCommand_throwsRuntimeException_loggedAtSevere() throws Exception {
when(flowRunner.run(eppController.eppMetricBuilder)).thenThrow(new IllegalStateException());
eppController.handleEppCommand(
sessionMetadata,
transportCredentials,
EppRequestSource.UNIT_TEST,
false,
true,
domainCreateXml.getBytes(UTF_8));
assertAboutLogs().that(logHandler)
.hasLogAtLevelWithMessage(SEVERE, "Unexpected failure in flow execution");
LogRecord logRecord =
findFirstLogRecordWithMessagePrefix(logHandler, "Unexpected failure in flow execution");
assertThat(logRecord.getThrown()).isInstanceOf(IllegalStateException.class);
}
@SuppressWarnings("unchecked")
private static Map<String, Object> parseJsonMap(String json) throws Exception {
return (Map<String, Object>) JSONValue.parseWithException(json);
}
} }

View file

@ -17,15 +17,14 @@ package google.registry.flows;
import static com.google.common.io.BaseEncoding.base64; import static com.google.common.io.BaseEncoding.base64;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static google.registry.testing.TestDataHelper.loadFileWithSubstitutions; import static google.registry.testing.TestDataHelper.loadFileWithSubstitutions;
import static google.registry.testing.TestLogHandlerUtils.findFirstLogMessageByPrefix;
import static java.nio.charset.StandardCharsets.UTF_8; import static java.nio.charset.StandardCharsets.UTF_8;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import com.google.common.base.Optional; import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import com.google.common.testing.TestLogHandler; import com.google.common.testing.TestLogHandler;
import google.registry.flows.annotations.ReportingSpec; import google.registry.flows.annotations.ReportingSpec;
import google.registry.model.eppcommon.Trid; import google.registry.model.eppcommon.Trid;
@ -35,7 +34,6 @@ import google.registry.model.eppoutput.EppResponse;
import google.registry.model.reporting.IcannReportingTypes.ActivityReportField; import google.registry.model.reporting.IcannReportingTypes.ActivityReportField;
import google.registry.testing.ShardableTestCase; import google.registry.testing.ShardableTestCase;
import java.util.Map; import java.util.Map;
import java.util.logging.LogRecord;
import java.util.logging.Logger; import java.util.logging.Logger;
import org.json.simple.JSONValue; import org.json.simple.JSONValue;
import org.junit.Before; import org.junit.Before;
@ -82,7 +80,7 @@ public class FlowReporterTest extends ShardableTestCase {
@Test @Test
public void testRecordToLogs_eppInput_basic() throws Exception { public void testRecordToLogs_eppInput_basic() throws Exception {
flowReporter.recordToLogs(); flowReporter.recordToLogs();
assertThat(parseJsonMap(findLogMessageByPrefix(handler, "FLOW-LOG-SIGNATURE-EPPINPUT: "))) assertThat(parseJsonMap(findFirstLogMessageByPrefix(handler, "FLOW-LOG-SIGNATURE-EPPINPUT: ")))
.containsExactly( .containsExactly(
"xml", "<?xml version=\"1.0\" encoding=\"UTF-8\"?><xml/>\n", "xml", "<?xml version=\"1.0\" encoding=\"UTF-8\"?><xml/>\n",
"xmlBytes", "PHhtbC8+"); // Base64-encoding of "<xml/>". "xmlBytes", "PHhtbC8+"); // Base64-encoding of "<xml/>".
@ -94,7 +92,7 @@ public class FlowReporterTest extends ShardableTestCase {
getClass(), "domain_create_prettyprinted.xml", ImmutableMap.<String, String>of()); getClass(), "domain_create_prettyprinted.xml", ImmutableMap.<String, String>of());
flowReporter.inputXmlBytes = domainCreateXml.getBytes(UTF_8); flowReporter.inputXmlBytes = domainCreateXml.getBytes(UTF_8);
flowReporter.recordToLogs(); flowReporter.recordToLogs();
assertThat(parseJsonMap(findLogMessageByPrefix(handler, "FLOW-LOG-SIGNATURE-EPPINPUT: "))) assertThat(parseJsonMap(findFirstLogMessageByPrefix(handler, "FLOW-LOG-SIGNATURE-EPPINPUT: ")))
.containsExactly( .containsExactly(
"xml", domainCreateXml, "xml", domainCreateXml,
"xmlBytes", base64().encode(domainCreateXml.getBytes(UTF_8))); "xmlBytes", base64().encode(domainCreateXml.getBytes(UTF_8)));
@ -103,7 +101,7 @@ public class FlowReporterTest extends ShardableTestCase {
@Test @Test
public void testRecordToLogs_metadata_basic() throws Exception { public void testRecordToLogs_metadata_basic() throws Exception {
flowReporter.recordToLogs(); flowReporter.recordToLogs();
assertThat(parseJsonMap(findLogMessageByPrefix(handler, "FLOW-LOG-SIGNATURE-METADATA: "))) assertThat(parseJsonMap(findFirstLogMessageByPrefix(handler, "FLOW-LOG-SIGNATURE-METADATA: ")))
.containsExactly( .containsExactly(
"serverTrid", "server-456", "serverTrid", "server-456",
"clientId", "TheRegistrar", "clientId", "TheRegistrar",
@ -122,7 +120,7 @@ public class FlowReporterTest extends ShardableTestCase {
flowReporter.flowClass = TestReportingSpecCommandFlow.class; flowReporter.flowClass = TestReportingSpecCommandFlow.class;
flowReporter.recordToLogs(); flowReporter.recordToLogs();
Map<String, Object> json = Map<String, Object> json =
parseJsonMap(findLogMessageByPrefix(handler, "FLOW-LOG-SIGNATURE-METADATA: ")); parseJsonMap(findFirstLogMessageByPrefix(handler, "FLOW-LOG-SIGNATURE-METADATA: "));
assertThat(json).containsEntry("flowClassName", "TestReportingSpecCommandFlow"); assertThat(json).containsEntry("flowClassName", "TestReportingSpecCommandFlow");
assertThat(json).containsEntry("icannActivityReportField", "srs-cont-check"); assertThat(json).containsEntry("icannActivityReportField", "srs-cont-check");
} }
@ -132,7 +130,7 @@ public class FlowReporterTest extends ShardableTestCase {
flowReporter.clientId = ""; flowReporter.clientId = "";
flowReporter.recordToLogs(); flowReporter.recordToLogs();
Map<String, Object> json = Map<String, Object> json =
parseJsonMap(findLogMessageByPrefix(handler, "FLOW-LOG-SIGNATURE-METADATA: ")); parseJsonMap(findFirstLogMessageByPrefix(handler, "FLOW-LOG-SIGNATURE-METADATA: "));
assertThat(json).containsEntry("clientId", ""); assertThat(json).containsEntry("clientId", "");
} }
@ -141,7 +139,7 @@ public class FlowReporterTest extends ShardableTestCase {
when(flowReporter.eppInput.getResourceType()).thenReturn(Optional.<String>absent()); when(flowReporter.eppInput.getResourceType()).thenReturn(Optional.<String>absent());
flowReporter.recordToLogs(); flowReporter.recordToLogs();
Map<String, Object> json = Map<String, Object> json =
parseJsonMap(findLogMessageByPrefix(handler, "FLOW-LOG-SIGNATURE-METADATA: ")); parseJsonMap(findFirstLogMessageByPrefix(handler, "FLOW-LOG-SIGNATURE-METADATA: "));
assertThat(json).containsEntry("resourceType", ""); assertThat(json).containsEntry("resourceType", "");
assertThat(json).containsEntry("tld", ""); assertThat(json).containsEntry("tld", "");
assertThat(json).containsEntry("tlds", ImmutableList.of()); assertThat(json).containsEntry("tlds", ImmutableList.of());
@ -153,7 +151,7 @@ public class FlowReporterTest extends ShardableTestCase {
when(flowReporter.eppInput.getResourceType()).thenReturn(Optional.of("contact")); when(flowReporter.eppInput.getResourceType()).thenReturn(Optional.of("contact"));
flowReporter.recordToLogs(); flowReporter.recordToLogs();
Map<String, Object> json = Map<String, Object> json =
parseJsonMap(findLogMessageByPrefix(handler, "FLOW-LOG-SIGNATURE-METADATA: ")); parseJsonMap(findFirstLogMessageByPrefix(handler, "FLOW-LOG-SIGNATURE-METADATA: "));
assertThat(json).containsEntry("resourceType", "contact"); assertThat(json).containsEntry("resourceType", "contact");
assertThat(json).containsEntry("tld", ""); assertThat(json).containsEntry("tld", "");
assertThat(json).containsEntry("tlds", ImmutableList.of()); assertThat(json).containsEntry("tlds", ImmutableList.of());
@ -165,7 +163,7 @@ public class FlowReporterTest extends ShardableTestCase {
when(flowReporter.eppInput.getTargetIds()).thenReturn(ImmutableList.of("target.co.uk")); when(flowReporter.eppInput.getTargetIds()).thenReturn(ImmutableList.of("target.co.uk"));
flowReporter.recordToLogs(); flowReporter.recordToLogs();
Map<String, Object> json = Map<String, Object> json =
parseJsonMap(findLogMessageByPrefix(handler, "FLOW-LOG-SIGNATURE-METADATA: ")); parseJsonMap(findFirstLogMessageByPrefix(handler, "FLOW-LOG-SIGNATURE-METADATA: "));
assertThat(json).containsEntry("targetId", "target.co.uk"); assertThat(json).containsEntry("targetId", "target.co.uk");
assertThat(json).containsEntry("targetIds", ImmutableList.of("target.co.uk")); assertThat(json).containsEntry("targetIds", ImmutableList.of("target.co.uk"));
assertThat(json).containsEntry("tld", "co.uk"); assertThat(json).containsEntry("tld", "co.uk");
@ -179,7 +177,7 @@ public class FlowReporterTest extends ShardableTestCase {
.thenReturn(ImmutableList.of("target.co.uk", "foo.uk", "bar.uk", "baz.com")); .thenReturn(ImmutableList.of("target.co.uk", "foo.uk", "bar.uk", "baz.com"));
flowReporter.recordToLogs(); flowReporter.recordToLogs();
Map<String, Object> json = Map<String, Object> json =
parseJsonMap(findLogMessageByPrefix(handler, "FLOW-LOG-SIGNATURE-METADATA: ")); parseJsonMap(findFirstLogMessageByPrefix(handler, "FLOW-LOG-SIGNATURE-METADATA: "));
assertThat(json).containsEntry("targetId", ""); assertThat(json).containsEntry("targetId", "");
assertThat(json).containsEntry( assertThat(json).containsEntry(
"targetIds", ImmutableList.of("target.co.uk", "foo.uk", "bar.uk", "baz.com")); "targetIds", ImmutableList.of("target.co.uk", "foo.uk", "bar.uk", "baz.com"));
@ -193,7 +191,7 @@ public class FlowReporterTest extends ShardableTestCase {
when(flowReporter.eppInput.getTargetIds()).thenReturn(ImmutableList.of("TARGET.FOO")); when(flowReporter.eppInput.getTargetIds()).thenReturn(ImmutableList.of("TARGET.FOO"));
flowReporter.recordToLogs(); flowReporter.recordToLogs();
Map<String, Object> json = Map<String, Object> json =
parseJsonMap(findLogMessageByPrefix(handler, "FLOW-LOG-SIGNATURE-METADATA: ")); parseJsonMap(findFirstLogMessageByPrefix(handler, "FLOW-LOG-SIGNATURE-METADATA: "));
assertThat(json).containsEntry("targetId", "TARGET.FOO"); assertThat(json).containsEntry("targetId", "TARGET.FOO");
assertThat(json).containsEntry("targetIds", ImmutableList.of("TARGET.FOO")); assertThat(json).containsEntry("targetIds", ImmutableList.of("TARGET.FOO"));
assertThat(json).containsEntry("tld", "foo"); assertThat(json).containsEntry("tld", "foo");
@ -206,7 +204,7 @@ public class FlowReporterTest extends ShardableTestCase {
when(flowReporter.eppInput.getTargetIds()).thenReturn(ImmutableList.of("<foo@bar.com>")); when(flowReporter.eppInput.getTargetIds()).thenReturn(ImmutableList.of("<foo@bar.com>"));
flowReporter.recordToLogs(); flowReporter.recordToLogs();
Map<String, Object> json = Map<String, Object> json =
parseJsonMap(findLogMessageByPrefix(handler, "FLOW-LOG-SIGNATURE-METADATA: ")); parseJsonMap(findFirstLogMessageByPrefix(handler, "FLOW-LOG-SIGNATURE-METADATA: "));
assertThat(json).containsEntry("targetId", "<foo@bar.com>"); assertThat(json).containsEntry("targetId", "<foo@bar.com>");
assertThat(json).containsEntry("targetIds", ImmutableList.of("<foo@bar.com>")); assertThat(json).containsEntry("targetIds", ImmutableList.of("<foo@bar.com>"));
assertThat(json).containsEntry("tld", "com>"); assertThat(json).containsEntry("tld", "com>");
@ -219,7 +217,7 @@ public class FlowReporterTest extends ShardableTestCase {
when(flowReporter.eppInput.getTargetIds()).thenReturn(ImmutableList.of("target,foo")); when(flowReporter.eppInput.getTargetIds()).thenReturn(ImmutableList.of("target,foo"));
flowReporter.recordToLogs(); flowReporter.recordToLogs();
Map<String, Object> json = Map<String, Object> json =
parseJsonMap(findLogMessageByPrefix(handler, "FLOW-LOG-SIGNATURE-METADATA: ")); parseJsonMap(findFirstLogMessageByPrefix(handler, "FLOW-LOG-SIGNATURE-METADATA: "));
assertThat(json).containsEntry("targetId", "target,foo"); assertThat(json).containsEntry("targetId", "target,foo");
assertThat(json).containsEntry("targetIds", ImmutableList.of("target,foo")); assertThat(json).containsEntry("targetIds", ImmutableList.of("target,foo"));
assertThat(json).containsEntry("tld", ""); assertThat(json).containsEntry("tld", "");
@ -230,21 +228,4 @@ public class FlowReporterTest extends ShardableTestCase {
private static Map<String, Object> parseJsonMap(String json) throws Exception { private static Map<String, Object> parseJsonMap(String json) throws Exception {
return (Map<String, Object>) JSONValue.parseWithException(json); return (Map<String, Object>) JSONValue.parseWithException(json);
} }
/**
* Find the first log message stored in the handler that has the provided prefix, and return
* that message with the prefix stripped off.
*/
private static String findLogMessageByPrefix(TestLogHandler handler, final String prefix) {
return Iterables.find(
handler.getStoredLogRecords(),
new Predicate<LogRecord>() {
@Override
public boolean apply(LogRecord logRecord) {
return logRecord.getMessage().startsWith(prefix);
}
})
.getMessage()
.replaceFirst("^" + prefix, "");
}
} }

View file

@ -16,6 +16,7 @@ package google.registry.flows;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static google.registry.testing.TestDataHelper.loadFileWithSubstitutions; import static google.registry.testing.TestDataHelper.loadFileWithSubstitutions;
import static google.registry.testing.TestLogHandlerUtils.findFirstLogMessageByPrefix;
import static java.nio.charset.StandardCharsets.UTF_8; import static java.nio.charset.StandardCharsets.UTF_8;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never; import static org.mockito.Mockito.never;
@ -24,11 +25,9 @@ import static org.mockito.Mockito.verify;
import com.google.appengine.api.users.User; import com.google.appengine.api.users.User;
import com.google.common.base.Joiner; import com.google.common.base.Joiner;
import com.google.common.base.Optional; import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.base.Splitter; import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.testing.TestLogHandler; import com.google.common.testing.TestLogHandler;
import google.registry.model.eppcommon.Trid; import google.registry.model.eppcommon.Trid;
import google.registry.model.eppoutput.EppOutput.ResponseOrGreeting; import google.registry.model.eppoutput.EppOutput.ResponseOrGreeting;
@ -40,7 +39,6 @@ import google.registry.testing.FakeHttpSession;
import google.registry.testing.Providers; import google.registry.testing.Providers;
import google.registry.testing.ShardableTestCase; import google.registry.testing.ShardableTestCase;
import java.util.List; import java.util.List;
import java.util.logging.LogRecord;
import java.util.logging.Logger; import java.util.logging.Logger;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
@ -129,7 +127,7 @@ public class FlowRunnerTest extends ShardableTestCase {
@Test @Test
public void testRun_legacyLoggingStatement_basic() throws Exception { public void testRun_legacyLoggingStatement_basic() throws Exception {
flowRunner.run(eppMetricBuilder); flowRunner.run(eppMetricBuilder);
assertThat(Splitter.on("\n\t").split(findLogMessageByPrefix(handler, "EPP Command\n\t"))) assertThat(Splitter.on("\n\t").split(findFirstLogMessageByPrefix(handler, "EPP Command\n\t")))
.containsExactly( .containsExactly(
"server-456", "server-456",
"TheRegistrar", "TheRegistrar",
@ -148,7 +146,7 @@ public class FlowRunnerTest extends ShardableTestCase {
flowRunner.sessionMetadata = new HttpSessionMetadata(new FakeHttpSession()); flowRunner.sessionMetadata = new HttpSessionMetadata(new FakeHttpSession());
flowRunner.sessionMetadata.setClientId("TheRegistrar"); flowRunner.sessionMetadata.setClientId("TheRegistrar");
flowRunner.run(eppMetricBuilder); flowRunner.run(eppMetricBuilder);
assertThat(Splitter.on("\n\t").split(findLogMessageByPrefix(handler, "EPP Command\n\t"))) assertThat(Splitter.on("\n\t").split(findFirstLogMessageByPrefix(handler, "EPP Command\n\t")))
.contains( .contains(
"HttpSessionMetadata" "HttpSessionMetadata"
+ "{clientId=TheRegistrar, failedLoginAttempts=0, serviceExtensionUris=}"); + "{clientId=TheRegistrar, failedLoginAttempts=0, serviceExtensionUris=}");
@ -159,7 +157,7 @@ public class FlowRunnerTest extends ShardableTestCase {
flowRunner.credentials = flowRunner.credentials =
GaeUserCredentials.forTestingUser(new User("user@example.com", "authDomain"), false); GaeUserCredentials.forTestingUser(new User("user@example.com", "authDomain"), false);
flowRunner.run(eppMetricBuilder); flowRunner.run(eppMetricBuilder);
assertThat(Splitter.on("\n\t").split(findLogMessageByPrefix(handler, "EPP Command\n\t"))) assertThat(Splitter.on("\n\t").split(findFirstLogMessageByPrefix(handler, "EPP Command\n\t")))
.contains("GaeUserCredentials{gaeUser=user@example.com, isAdmin=false}"); .contains("GaeUserCredentials{gaeUser=user@example.com, isAdmin=false}");
} }
@ -167,7 +165,7 @@ public class FlowRunnerTest extends ShardableTestCase {
public void testRun_legacyLoggingStatement_tlsCredentials() throws Exception { public void testRun_legacyLoggingStatement_tlsCredentials() throws Exception {
flowRunner.credentials = new TlsCredentials("abc123def", Optional.of("127.0.0.1"), "sni"); flowRunner.credentials = new TlsCredentials("abc123def", Optional.of("127.0.0.1"), "sni");
flowRunner.run(eppMetricBuilder); flowRunner.run(eppMetricBuilder);
assertThat(Splitter.on("\n\t").split(findLogMessageByPrefix(handler, "EPP Command\n\t"))) assertThat(Splitter.on("\n\t").split(findFirstLogMessageByPrefix(handler, "EPP Command\n\t")))
.contains( .contains(
"TlsCredentials{clientCertificateHash=abc123def, clientAddress=/127.0.0.1, sni=sni}"); "TlsCredentials{clientCertificateHash=abc123def, clientAddress=/127.0.0.1, sni=sni}");
} }
@ -176,7 +174,7 @@ public class FlowRunnerTest extends ShardableTestCase {
public void testRun_legacyLoggingStatement_dryRun() throws Exception { public void testRun_legacyLoggingStatement_dryRun() throws Exception {
flowRunner.isDryRun = true; flowRunner.isDryRun = true;
flowRunner.run(eppMetricBuilder); flowRunner.run(eppMetricBuilder);
assertThat(Splitter.on("\n\t").split(findLogMessageByPrefix(handler, "EPP Command\n\t"))) assertThat(Splitter.on("\n\t").split(findFirstLogMessageByPrefix(handler, "EPP Command\n\t")))
.contains("DRY_RUN"); .contains("DRY_RUN");
} }
@ -186,27 +184,10 @@ public class FlowRunnerTest extends ShardableTestCase {
getClass(), "domain_create_prettyprinted.xml", ImmutableMap.<String, String>of()); getClass(), "domain_create_prettyprinted.xml", ImmutableMap.<String, String>of());
flowRunner.inputXmlBytes = domainCreateXml.getBytes(UTF_8); flowRunner.inputXmlBytes = domainCreateXml.getBytes(UTF_8);
flowRunner.run(eppMetricBuilder); flowRunner.run(eppMetricBuilder);
String logMessage = findLogMessageByPrefix(handler, "EPP Command\n\t"); String logMessage = findFirstLogMessageByPrefix(handler, "EPP Command\n\t");
List<String> lines = Splitter.on("\n\t").splitToList(logMessage); List<String> lines = Splitter.on("\n\t").splitToList(logMessage);
assertThat(lines.size()).named("number of lines in log message").isAtLeast(9); assertThat(lines.size()).named("number of lines in log message").isAtLeast(9);
String xml = Joiner.on('\n').join(lines.subList(3, lines.size() - 3)); String xml = Joiner.on('\n').join(lines.subList(3, lines.size() - 3));
assertThat(xml).isEqualTo(domainCreateXml); assertThat(xml).isEqualTo(domainCreateXml);
} }
/**
* Find the first log message stored in the handler that has the provided prefix, and return
* that message with the prefix stripped off.
*/
private static String findLogMessageByPrefix(TestLogHandler handler, final String prefix) {
return Iterables.find(
handler.getStoredLogRecords(),
new Predicate<LogRecord>() {
@Override
public boolean apply(LogRecord logRecord) {
return logRecord.getMessage().startsWith(prefix);
}
})
.getMessage()
.replaceFirst("^" + prefix, "");
}
} }

View file

@ -0,0 +1,48 @@
// Copyright 2017 The Nomulus Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package google.registry.testing;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import com.google.common.testing.TestLogHandler;
import java.util.logging.LogRecord;
/** Utility methods for working with Guava's {@link TestLogHandler}. */
public final class TestLogHandlerUtils {
private TestLogHandlerUtils() {}
/**
* Find the first log message stored in the handler that has the provided prefix, and return that
* message with the prefix stripped off.
*/
public static String findFirstLogMessageByPrefix(TestLogHandler handler, String prefix) {
return findFirstLogRecordWithMessagePrefix(handler, prefix)
.getMessage()
.replaceFirst("^" + prefix, "");
}
/** Returns the first log record stored in handler whose message has the provided prefix. */
public static LogRecord findFirstLogRecordWithMessagePrefix(
TestLogHandler handler, final String prefix) {
return Iterables.find(
handler.getStoredLogRecords(),
new Predicate<LogRecord>() {
@Override
public boolean apply(LogRecord logRecord) {
return logRecord.getMessage().startsWith(prefix);
}
});
}
}