Record a version of EPP metrics with TLD for domain commands

Also fixes the issue that dry run EPP commands were incorrectly being
reported on.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=171062984
This commit is contained in:
mcilwain 2017-10-04 14:26:20 -07:00 committed by jianglai
parent 7e68ffa16a
commit 302a27f0db
13 changed files with 283 additions and 61 deletions

View file

@ -17,13 +17,16 @@ package google.registry.flows;
import static com.google.common.io.BaseEncoding.base64;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.flows.EppXmlTransformer.marshal;
import static google.registry.testing.DatastoreHelper.createTld;
import static google.registry.testing.LogsSubject.assertAboutLogs;
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.util.logging.Level.INFO;
import static java.util.logging.Level.SEVERE;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;
import com.google.common.base.Splitter;
@ -64,12 +67,12 @@ import org.mockito.runners.MockitoJUnitRunner;
public class EppControllerTest extends ShardableTestCase {
@Rule
public AppEngineRule appEngineRule = new AppEngineRule.Builder().build();
public AppEngineRule appEngineRule = new AppEngineRule.Builder().withDatastore().build();
@Mock SessionMetadata sessionMetadata;
@Mock TransportCredentials transportCredentials;
@Mock EppMetrics eppMetrics;
@Mock BigQueryMetricsEnqueuer metricsEnqueuer;
@Mock BigQueryMetricsEnqueuer bigQueryMetricsEnqueuer;
@Mock FlowComponent.Builder flowComponentBuilder;
@Mock FlowComponent flowComponent;
@Mock FlowRunner flowRunner;
@ -77,9 +80,9 @@ public class EppControllerTest extends ShardableTestCase {
@Mock EppResponse eppResponse;
@Mock Result result;
private static final DateTime startTime = DateTime.parse("2016-09-01T00:00:00Z");
private static final DateTime START_TIME = DateTime.parse("2016-09-01T00:00:00Z");
private final Clock clock = new FakeClock(startTime);
private final Clock clock = new FakeClock(START_TIME);
private final TestLogHandler logHandler = new TestLogHandler();
private final String domainCreateXml =
@ -105,7 +108,7 @@ public class EppControllerTest extends ShardableTestCase {
eppController = new EppController();
eppController.eppMetricBuilder = EppMetric.builderForRequest("request-id-1", clock);
when(flowRunner.run(eppController.eppMetricBuilder)).thenReturn(eppOutput);
eppController.bigQueryMetricsEnqueuer = metricsEnqueuer;
eppController.bigQueryMetricsEnqueuer = bigQueryMetricsEnqueuer;
eppController.flowComponentBuilder = flowComponentBuilder;
eppController.eppMetrics = eppMetrics;
eppController.serverTridProvider = new FakeServerTridProvider();
@ -130,10 +133,10 @@ public class EppControllerTest extends ShardableTestCase {
new byte[0]);
ArgumentCaptor<EppMetric> metricCaptor = ArgumentCaptor.forClass(EppMetric.class);
verify(metricsEnqueuer).export(metricCaptor.capture());
verify(bigQueryMetricsEnqueuer).export(metricCaptor.capture());
EppMetric metric = metricCaptor.getValue();
assertThat(metric.getRequestId()).isEqualTo("request-id-1");
assertThat(metric.getStartTimestamp()).isEqualTo(startTime);
assertThat(metric.getStartTimestamp()).isEqualTo(START_TIME);
assertThat(metric.getEndTimestamp()).isEqualTo(clock.nowUtc());
assertThat(metric.getClientId()).hasValue("some-client");
assertThat(metric.getPrivilegeLevel()).hasValue("NORMAL");
@ -141,7 +144,7 @@ public class EppControllerTest extends ShardableTestCase {
}
@Test
public void testHandleEppCommand_regularEppCommand_exportsMetric() throws Exception {
public void testHandleEppCommand_regularEppCommand_exportsBigQueryMetric() throws Exception {
eppController.handleEppCommand(
sessionMetadata,
transportCredentials,
@ -151,10 +154,10 @@ public class EppControllerTest extends ShardableTestCase {
domainCreateXml.getBytes(UTF_8));
ArgumentCaptor<EppMetric> metricCaptor = ArgumentCaptor.forClass(EppMetric.class);
verify(metricsEnqueuer).export(metricCaptor.capture());
verify(bigQueryMetricsEnqueuer).export(metricCaptor.capture());
EppMetric metric = metricCaptor.getValue();
assertThat(metric.getRequestId()).isEqualTo("request-id-1");
assertThat(metric.getStartTimestamp()).isEqualTo(startTime);
assertThat(metric.getStartTimestamp()).isEqualTo(START_TIME);
assertThat(metric.getEndTimestamp()).isEqualTo(clock.nowUtc());
assertThat(metric.getClientId()).hasValue("some-client");
assertThat(metric.getPrivilegeLevel()).hasValue("SUPERUSER");
@ -162,6 +165,46 @@ public class EppControllerTest extends ShardableTestCase {
assertThat(metric.getEppTarget()).hasValue("example.tld");
}
@Test
public void testHandleEppCommand_regularEppCommand_exportsEppMetrics() throws Exception {
createTld("tld");
// Note that some of the EPP metric fields, like # of attempts and command name, are set in
// FlowRunner, not EppController, and since FlowRunner is mocked out for these tests they won't
// actually get values.
EppMetric.Builder metricBuilder =
EppMetric.builderForRequest("request-id-1", clock)
.setClientId("some-client")
.setEppTarget("example.tld")
.setStatus(Code.SUCCESS_WITH_NO_MESSAGES)
.setTld("tld")
.setPrivilegeLevel("SUPERUSER");
eppController.handleEppCommand(
sessionMetadata,
transportCredentials,
EppRequestSource.UNIT_TEST,
false,
true,
domainCreateXml.getBytes(UTF_8));
EppMetric expectedMetric = metricBuilder.build();
verify(eppMetrics).incrementEppRequests(eq(expectedMetric));
verify(eppMetrics).recordProcessingTime(eq(expectedMetric));
}
@Test
public void testHandleEppCommand_dryRunEppCommand_doesNotExportMetric() throws Exception {
eppController.handleEppCommand(
sessionMetadata,
transportCredentials,
EppRequestSource.UNIT_TEST,
true,
true,
domainCreateXml.getBytes(UTF_8));
verifyZeroInteractions(bigQueryMetricsEnqueuer);
verifyZeroInteractions(eppMetrics);
}
@Test
public void testHandleEppCommand_unmarshallableData_loggedAtInfo_withJsonData() throws Exception {
eppController.handleEppCommand(

View file

@ -44,17 +44,19 @@ public class EppLifecycleContactTest extends EppTestCase {
assertCommandAndResponse(
"contact_create_sh8013.xml",
null,
"contact_create_response_sh8013.xml",
ImmutableMap.of("CRDATE", "2000-06-01T00:00:00Z"),
DateTime.parse("2000-06-01T00:00:00Z"));
"contact_create_response_sh8013.xml",
ImmutableMap.of("CRDATE", "2000-06-01T00:00:00Z"),
DateTime.parse("2000-06-01T00:00:00Z"));
assertThat(getRecordedEppMetric())
.hasClientId("NewRegistrar")
.and()
.hasCommandName("ContactCreate")
.and()
.hasEppTarget("sh8013")
.and()
.hasStatus(SUCCESS);
.hasClientId("NewRegistrar")
.and()
.hasNoTld()
.and()
.hasCommandName("ContactCreate")
.and()
.hasEppTarget("sh8013")
.and()
.hasStatus(SUCCESS);
assertCommandAndResponse(
"contact_info.xml",
"contact_info_from_create_response.xml",

View file

@ -206,6 +206,8 @@ public class EppLifecycleDomainTest extends EppTestCase {
assertThat(getRecordedEppMetric())
.hasClientId("NewRegistrar")
.and()
.hasNoTld()
.and()
.hasCommandName("Login")
.and()
.hasStatus(SUCCESS);
@ -233,6 +235,8 @@ public class EppLifecycleDomainTest extends EppTestCase {
assertThat(getRecordedEppMetric())
.hasClientId("NewRegistrar")
.and()
.hasTld("example")
.and()
.hasCommandName("DomainDelete")
.and()
.hasEppTarget("fakesite.example")
@ -353,6 +357,8 @@ public class EppLifecycleDomainTest extends EppTestCase {
.and()
.hasEppTarget("rich.example")
.and()
.hasTld("example")
.and()
.hasStatus(SUCCESS);
assertCommandAndResponse("logout.xml", "logout_response.xml");

View file

@ -100,6 +100,8 @@ public class FlowReporterTest extends ShardableTestCase {
@Test
public void testRecordToLogs_metadata_basic() throws Exception {
when(flowReporter.eppInput.isDomainResourceType()).thenReturn(true);
when(flowReporter.eppInput.getResourceType()).thenReturn(Optional.of("domain"));
flowReporter.recordToLogs();
assertThat(parseJsonMap(findFirstLogMessageByPrefix(handler, "FLOW-LOG-SIGNATURE-METADATA: ")))
.containsExactly(
@ -136,6 +138,7 @@ public class FlowReporterTest extends ShardableTestCase {
@Test
public void testRecordToLogs_metadata_notResourceFlow_noResourceTypeOrTld() throws Exception {
when(flowReporter.eppInput.isDomainResourceType()).thenReturn(false);
when(flowReporter.eppInput.getResourceType()).thenReturn(Optional.<String>absent());
flowReporter.recordToLogs();
Map<String, Object> json =
@ -148,6 +151,7 @@ public class FlowReporterTest extends ShardableTestCase {
@Test
public void testRecordToLogs_metadata_notDomainFlow_noTld() throws Exception {
when(flowReporter.eppInput.isDomainResourceType()).thenReturn(false);
when(flowReporter.eppInput.getResourceType()).thenReturn(Optional.of("contact"));
flowReporter.recordToLogs();
Map<String, Object> json =
@ -159,6 +163,8 @@ public class FlowReporterTest extends ShardableTestCase {
@Test
public void testRecordToLogs_metadata_multipartDomainName_multipartTld() throws Exception {
when(flowReporter.eppInput.isDomainResourceType()).thenReturn(true);
when(flowReporter.eppInput.getResourceType()).thenReturn(Optional.of("domain"));
when(flowReporter.eppInput.getSingleTargetId()).thenReturn(Optional.of("target.co.uk"));
when(flowReporter.eppInput.getTargetIds()).thenReturn(ImmutableList.of("target.co.uk"));
flowReporter.recordToLogs();
@ -172,6 +178,7 @@ public class FlowReporterTest extends ShardableTestCase {
@Test
public void testRecordToLogs_metadata_multipleTargetIds_uniqueTldSet() throws Exception {
when(flowReporter.eppInput.isDomainResourceType()).thenReturn(true);
when(flowReporter.eppInput.getSingleTargetId()).thenReturn(Optional.<String>absent());
when(flowReporter.eppInput.getTargetIds())
.thenReturn(ImmutableList.of("target.co.uk", "foo.uk", "bar.uk", "baz.com"));
@ -187,6 +194,7 @@ public class FlowReporterTest extends ShardableTestCase {
@Test
public void testRecordToLogs_metadata_uppercaseDomainName_lowercaseTld() throws Exception {
when(flowReporter.eppInput.isDomainResourceType()).thenReturn(true);
when(flowReporter.eppInput.getSingleTargetId()).thenReturn(Optional.of("TARGET.FOO"));
when(flowReporter.eppInput.getTargetIds()).thenReturn(ImmutableList.of("TARGET.FOO"));
flowReporter.recordToLogs();
@ -200,6 +208,7 @@ public class FlowReporterTest extends ShardableTestCase {
@Test
public void testRecordToLogs_metadata_invalidDomainName_stillGuessesTld() throws Exception {
when(flowReporter.eppInput.isDomainResourceType()).thenReturn(true);
when(flowReporter.eppInput.getSingleTargetId()).thenReturn(Optional.of("<foo@bar.com>"));
when(flowReporter.eppInput.getTargetIds()).thenReturn(ImmutableList.of("<foo@bar.com>"));
flowReporter.recordToLogs();
@ -213,6 +222,7 @@ public class FlowReporterTest extends ShardableTestCase {
@Test
public void testRecordToLogs_metadata_domainWithoutPeriod_noTld() throws Exception {
when(flowReporter.eppInput.isDomainResourceType()).thenReturn(true);
when(flowReporter.eppInput.getSingleTargetId()).thenReturn(Optional.of("target,foo"));
when(flowReporter.eppInput.getTargetIds()).thenReturn(ImmutableList.of("target,foo"));
flowReporter.recordToLogs();

View file

@ -15,12 +15,15 @@
package google.registry.monitoring.whitebox;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.testing.DatastoreHelper.createTld;
import static google.registry.testing.DatastoreHelper.createTlds;
import com.google.api.services.bigquery.model.TableFieldSchema;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import google.registry.model.eppoutput.Result.Code;
import google.registry.testing.AppEngineRule;
import google.registry.testing.FakeClock;
import org.joda.time.DateTime;
import org.junit.Rule;
import org.junit.Test;
@ -31,7 +34,45 @@ import org.junit.runners.JUnit4;
@RunWith(JUnit4.class)
public class EppMetricTest {
@Rule public final AppEngineRule appEngine = AppEngineRule.builder().build();
@Rule public final AppEngineRule appEngine = AppEngineRule.builder().withDatastore().build();
@Test
public void test_invalidTld_isRecordedAsInvalid() throws Exception {
EppMetric metric =
EppMetric.builderForRequest("request-id-1", new FakeClock())
.setTlds(ImmutableSet.of("notarealtld"))
.build();
assertThat(metric.getTld()).hasValue("_invalid");
}
@Test
public void test_validTld_isRecorded() throws Exception {
createTld("example");
EppMetric metric =
EppMetric.builderForRequest("request-id-1", new FakeClock())
.setTlds(ImmutableSet.of("example"))
.build();
assertThat(metric.getTld()).hasValue("example");
}
@Test
public void test_multipleTlds_areRecordedAsVarious() throws Exception {
createTlds("foo", "bar");
EppMetric metric =
EppMetric.builderForRequest("request-id-1", new FakeClock())
.setTlds(ImmutableSet.of("foo", "bar", "baz"))
.build();
assertThat(metric.getTld()).hasValue("_various");
}
@Test
public void test_zeroTlds_areRecordedAsAbsent() throws Exception {
EppMetric metric =
EppMetric.builderForRequest("request-id-1", new FakeClock())
.setTlds(ImmutableSet.<String>of())
.build();
assertThat(metric.getTld()).isAbsent();
}
@Test
public void testGetBigQueryRowEncoding_encodesCorrectly() throws Exception {
@ -42,6 +83,7 @@ public class EppMetricTest {
.setEndTimestamp(new DateTime(1338))
.setCommandName("command")
.setClientId("client")
.setTld("example")
.setPrivilegeLevel("level")
.setEppTarget("target")
.setStatus(Code.COMMAND_USE_ERROR)
@ -56,6 +98,7 @@ public class EppMetricTest {
.put("endTime", "1.338000")
.put("commandName", "command")
.put("clientId", "client")
.put("tld", "example")
.put("privilegeLevel", "level")
.put("eppTarget", "target")
.put("eppStatus", "2002")
@ -72,6 +115,7 @@ public class EppMetricTest {
.setEndTimestamp(new DateTime(1338))
.setCommandName("command")
.setClientId("client")
.setTld("example")
.setPrivilegeLevel("level")
.setEppTarget("target")
.setStatus(Code.COMMAND_USE_ERROR)

View file

@ -61,6 +61,17 @@ public class EppMetricSubject extends Subject<EppMetricSubject, EppMetric> {
return new And<>(this);
}
public And<EppMetricSubject> hasTld(String tld) {
return hasValue(tld, actual().getTld(), "has tld");
}
public And<EppMetricSubject> hasNoTld() {
if (actual().getTld().isPresent()) {
fail("has no tld");
}
return new And<>(this);
}
private <E> And<EppMetricSubject> hasValue(E expected, Optional<E> actual, String verb) {
checkArgumentNotNull(expected, "Expected value cannot be null");
if (actual == null) {