Merge "Add helper class to register extensions in acceptance tests"

This commit is contained in:
David Pursehouse
2019-10-10 01:16:55 +00:00
committed by Gerrit Code Review
3 changed files with 521 additions and 310 deletions

View File

@@ -124,6 +124,7 @@ java_library2(
"//java/com/google/gerrit/server", "//java/com/google/gerrit/server",
"//java/com/google/gerrit/server/audit", "//java/com/google/gerrit/server/audit",
"//java/com/google/gerrit/server/git/receive", "//java/com/google/gerrit/server/git/receive",
"//java/com/google/gerrit/server/logging",
"//java/com/google/gerrit/server/restapi", "//java/com/google/gerrit/server/restapi",
"//java/com/google/gerrit/server/schema", "//java/com/google/gerrit/server/schema",
"//java/com/google/gerrit/server/util/git", "//java/com/google/gerrit/server/util/git",

View File

@@ -0,0 +1,95 @@
// Copyright (C) 2019 The Android Open Source Project
//
// 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 com.google.gerrit.acceptance;
import com.google.gerrit.extensions.events.ChangeIndexedListener;
import com.google.gerrit.extensions.registration.DynamicSet;
import com.google.gerrit.extensions.registration.RegistrationHandle;
import com.google.gerrit.server.ExceptionHook;
import com.google.gerrit.server.git.validators.CommitValidationListener;
import com.google.gerrit.server.logging.PerformanceLogger;
import com.google.gerrit.server.rules.SubmitRule;
import com.google.gerrit.server.validators.ProjectCreationValidationListener;
import com.google.inject.Inject;
import java.util.ArrayList;
import java.util.List;
public class ExtensionRegistry {
private final DynamicSet<ChangeIndexedListener> changeIndexedListeners;
private final DynamicSet<CommitValidationListener> commitValidationListeners;
private final DynamicSet<ExceptionHook> exceptionHooks;
private final DynamicSet<PerformanceLogger> performanceLoggers;
private final DynamicSet<ProjectCreationValidationListener> projectCreationValidationListeners;
private final DynamicSet<SubmitRule> submitRules;
@Inject
ExtensionRegistry(
DynamicSet<ChangeIndexedListener> changeIndexedListeners,
DynamicSet<CommitValidationListener> commitValidationListeners,
DynamicSet<ExceptionHook> exceptionHooks,
DynamicSet<PerformanceLogger> performanceLoggers,
DynamicSet<ProjectCreationValidationListener> projectCreationValidationListeners,
DynamicSet<SubmitRule> submitRules) {
this.changeIndexedListeners = changeIndexedListeners;
this.commitValidationListeners = commitValidationListeners;
this.exceptionHooks = exceptionHooks;
this.performanceLoggers = performanceLoggers;
this.projectCreationValidationListeners = projectCreationValidationListeners;
this.submitRules = submitRules;
}
public Registration newRegistration() {
return new Registration();
}
public class Registration implements AutoCloseable {
private final List<RegistrationHandle> registrationHandles = new ArrayList<>();
public Registration add(ChangeIndexedListener changeIndexedListener) {
return add(changeIndexedListeners, changeIndexedListener);
}
public Registration add(CommitValidationListener commitValidationListener) {
return add(commitValidationListeners, commitValidationListener);
}
public Registration add(ExceptionHook exceptionHook) {
return add(exceptionHooks, exceptionHook);
}
public Registration add(PerformanceLogger performanceLogger) {
return add(performanceLoggers, performanceLogger);
}
public Registration add(ProjectCreationValidationListener projectCreationListener) {
return add(projectCreationValidationListeners, projectCreationListener);
}
public Registration add(SubmitRule submitRule) {
return add(submitRules, submitRule);
}
private <T> Registration add(DynamicSet<T> dynamicSet, T extension) {
RegistrationHandle registrationHandle = dynamicSet.add("gerrit", extension);
registrationHandles.add(registrationHandle);
return this;
}
@Override
public void close() {
registrationHandles.forEach(h -> h.remove());
}
}
}

View File

@@ -26,14 +26,14 @@ import com.google.common.collect.ImmutableSetMultimap;
import com.google.common.collect.Iterables; import com.google.common.collect.Iterables;
import com.google.common.truth.Expect; import com.google.common.truth.Expect;
import com.google.gerrit.acceptance.AbstractDaemonTest; import com.google.gerrit.acceptance.AbstractDaemonTest;
import com.google.gerrit.acceptance.ExtensionRegistry;
import com.google.gerrit.acceptance.ExtensionRegistry.Registration;
import com.google.gerrit.acceptance.GerritConfig; import com.google.gerrit.acceptance.GerritConfig;
import com.google.gerrit.acceptance.PushOneCommit; import com.google.gerrit.acceptance.PushOneCommit;
import com.google.gerrit.acceptance.RestResponse; import com.google.gerrit.acceptance.RestResponse;
import com.google.gerrit.common.data.SubmitRecord; import com.google.gerrit.common.data.SubmitRecord;
import com.google.gerrit.extensions.api.changes.ReviewInput; import com.google.gerrit.extensions.api.changes.ReviewInput;
import com.google.gerrit.extensions.events.ChangeIndexedListener; import com.google.gerrit.extensions.events.ChangeIndexedListener;
import com.google.gerrit.extensions.registration.DynamicSet;
import com.google.gerrit.extensions.registration.RegistrationHandle;
import com.google.gerrit.httpd.restapi.ParameterParser; import com.google.gerrit.httpd.restapi.ParameterParser;
import com.google.gerrit.httpd.restapi.RestApiServlet; import com.google.gerrit.httpd.restapi.RestApiServlet;
import com.google.gerrit.server.ExceptionHook; import com.google.gerrit.server.ExceptionHook;
@@ -58,8 +58,6 @@ import java.util.Optional;
import java.util.SortedMap; import java.util.SortedMap;
import java.util.SortedSet; import java.util.SortedSet;
import org.apache.http.message.BasicHeader; import org.apache.http.message.BasicHeader;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
@@ -81,50 +79,24 @@ import org.junit.Test;
public class TraceIT extends AbstractDaemonTest { public class TraceIT extends AbstractDaemonTest {
@Rule public final Expect expect = Expect.create(); @Rule public final Expect expect = Expect.create();
@Inject private DynamicSet<ProjectCreationValidationListener> projectCreationValidationListeners; @Inject private ExtensionRegistry extensionRegistry;
@Inject private DynamicSet<CommitValidationListener> commitValidationListeners;
@Inject private DynamicSet<ChangeIndexedListener> changeIndexedListeners;
@Inject private DynamicSet<PerformanceLogger> performanceLoggers;
@Inject private DynamicSet<SubmitRule> submitRules;
@Inject private DynamicSet<ExceptionHook> exceptionHooks;
@Inject private WorkQueue workQueue; @Inject private WorkQueue workQueue;
private TraceValidatingProjectCreationValidationListener projectCreationListener;
private RegistrationHandle projectCreationListenerRegistrationHandle;
private TraceValidatingCommitValidationListener commitValidationListener;
private RegistrationHandle commitValidationRegistrationHandle;
private TestPerformanceLogger testPerformanceLogger;
private RegistrationHandle performanceLoggerRegistrationHandle;
@Before
public void setup() {
projectCreationListener = new TraceValidatingProjectCreationValidationListener();
projectCreationListenerRegistrationHandle =
projectCreationValidationListeners.add("gerrit", projectCreationListener);
commitValidationListener = new TraceValidatingCommitValidationListener();
commitValidationRegistrationHandle =
commitValidationListeners.add("gerrit", commitValidationListener);
testPerformanceLogger = new TestPerformanceLogger();
performanceLoggerRegistrationHandle = performanceLoggers.add("gerrit", testPerformanceLogger);
}
@After
public void cleanup() {
projectCreationListenerRegistrationHandle.remove();
commitValidationRegistrationHandle.remove();
performanceLoggerRegistrationHandle.remove();
}
@Test @Test
public void restCallWithoutTrace() throws Exception { public void restCallWithoutTrace() throws Exception {
RestResponse response = adminRestSession.put("/projects/new1"); TraceValidatingProjectCreationValidationListener projectCreationListener =
assertThat(response.getStatusCode()).isEqualTo(SC_CREATED); new TraceValidatingProjectCreationValidationListener();
assertThat(response.getHeader(RestApiServlet.X_GERRIT_TRACE)).isNull(); try (Registration registration =
assertThat(projectCreationListener.traceId).isNull(); extensionRegistry.newRegistration().add(projectCreationListener)) {
assertThat(projectCreationListener.isLoggingForced).isFalse(); RestResponse response = adminRestSession.put("/projects/new1");
assertThat(response.getStatusCode()).isEqualTo(SC_CREATED);
assertThat(response.getHeader(RestApiServlet.X_GERRIT_TRACE)).isNull();
assertThat(projectCreationListener.traceId).isNull();
assertThat(projectCreationListener.isLoggingForced).isFalse();
// The logging tag with the project name is also set if tracing is off. // The logging tag with the project name is also set if tracing is off.
assertThat(projectCreationListener.tags.get("project")).containsExactly("new1"); assertThat(projectCreationListener.tags.get("project")).containsExactly("new1");
}
} }
@Test @Test
@@ -132,9 +104,8 @@ public class TraceIT extends AbstractDaemonTest {
String changeId = createChange().getChangeId(); String changeId = createChange().getChangeId();
TraceChangeIndexedListener changeIndexedListener = new TraceChangeIndexedListener(); TraceChangeIndexedListener changeIndexedListener = new TraceChangeIndexedListener();
RegistrationHandle registrationHandle = try (Registration registration =
changeIndexedListeners.add("gerrit", changeIndexedListener); extensionRegistry.newRegistration().add(changeIndexedListener)) {
try {
RestResponse response = RestResponse response =
adminRestSession.post( adminRestSession.post(
"/changes/" + changeId + "/revisions/current/review", ReviewInput.approve()); "/changes/" + changeId + "/revisions/current/review", ReviewInput.approve());
@@ -142,169 +113,223 @@ public class TraceIT extends AbstractDaemonTest {
// The logging tag with the project name is also set if tracing is off. // The logging tag with the project name is also set if tracing is off.
assertThat(changeIndexedListener.tags.get("project")).containsExactly(project.get()); assertThat(changeIndexedListener.tags.get("project")).containsExactly(project.get());
} finally {
registrationHandle.remove();
} }
} }
@Test @Test
public void restCallWithTraceRequestParam() throws Exception { public void restCallWithTraceRequestParam() throws Exception {
RestResponse response = TraceValidatingProjectCreationValidationListener projectCreationListener =
adminRestSession.put("/projects/new2?" + ParameterParser.TRACE_PARAMETER); new TraceValidatingProjectCreationValidationListener();
assertThat(response.getStatusCode()).isEqualTo(SC_CREATED); try (Registration registration =
assertThat(response.getHeader(RestApiServlet.X_GERRIT_TRACE)).isNotNull(); extensionRegistry.newRegistration().add(projectCreationListener)) {
assertThat(projectCreationListener.traceId).isNotNull(); RestResponse response =
assertThat(projectCreationListener.isLoggingForced).isTrue(); adminRestSession.put("/projects/new2?" + ParameterParser.TRACE_PARAMETER);
assertThat(projectCreationListener.tags.get("project")).containsExactly("new2"); assertThat(response.getStatusCode()).isEqualTo(SC_CREATED);
assertThat(response.getHeader(RestApiServlet.X_GERRIT_TRACE)).isNotNull();
assertThat(projectCreationListener.traceId).isNotNull();
assertThat(projectCreationListener.isLoggingForced).isTrue();
assertThat(projectCreationListener.tags.get("project")).containsExactly("new2");
}
} }
@Test @Test
public void restCallWithTraceRequestParamAndProvidedTraceId() throws Exception { public void restCallWithTraceRequestParamAndProvidedTraceId() throws Exception {
RestResponse response = TraceValidatingProjectCreationValidationListener projectCreationListener =
adminRestSession.put("/projects/new3?" + ParameterParser.TRACE_PARAMETER + "=issue/123"); new TraceValidatingProjectCreationValidationListener();
assertThat(response.getStatusCode()).isEqualTo(SC_CREATED); try (Registration registration =
assertThat(response.getHeader(RestApiServlet.X_GERRIT_TRACE)).isEqualTo("issue/123"); extensionRegistry.newRegistration().add(projectCreationListener)) {
assertThat(projectCreationListener.traceId).isEqualTo("issue/123"); RestResponse response =
assertThat(projectCreationListener.isLoggingForced).isTrue(); adminRestSession.put("/projects/new3?" + ParameterParser.TRACE_PARAMETER + "=issue/123");
assertThat(projectCreationListener.tags.get("project")).containsExactly("new3"); assertThat(response.getStatusCode()).isEqualTo(SC_CREATED);
assertThat(response.getHeader(RestApiServlet.X_GERRIT_TRACE)).isEqualTo("issue/123");
assertThat(projectCreationListener.traceId).isEqualTo("issue/123");
assertThat(projectCreationListener.isLoggingForced).isTrue();
assertThat(projectCreationListener.tags.get("project")).containsExactly("new3");
}
} }
@Test @Test
public void restCallWithTraceHeader() throws Exception { public void restCallWithTraceHeader() throws Exception {
RestResponse response = TraceValidatingProjectCreationValidationListener projectCreationListener =
adminRestSession.putWithHeader( new TraceValidatingProjectCreationValidationListener();
"/projects/new4", new BasicHeader(RestApiServlet.X_GERRIT_TRACE, null)); try (Registration registration =
assertThat(response.getStatusCode()).isEqualTo(SC_CREATED); extensionRegistry.newRegistration().add(projectCreationListener)) {
assertThat(response.getHeader(RestApiServlet.X_GERRIT_TRACE)).isNotNull(); RestResponse response =
assertThat(projectCreationListener.traceId).isNotNull(); adminRestSession.putWithHeader(
assertThat(projectCreationListener.isLoggingForced).isTrue(); "/projects/new4", new BasicHeader(RestApiServlet.X_GERRIT_TRACE, null));
assertThat(projectCreationListener.tags.get("project")).containsExactly("new4"); assertThat(response.getStatusCode()).isEqualTo(SC_CREATED);
assertThat(response.getHeader(RestApiServlet.X_GERRIT_TRACE)).isNotNull();
assertThat(projectCreationListener.traceId).isNotNull();
assertThat(projectCreationListener.isLoggingForced).isTrue();
assertThat(projectCreationListener.tags.get("project")).containsExactly("new4");
}
} }
@Test @Test
public void restCallWithTraceHeaderAndProvidedTraceId() throws Exception { public void restCallWithTraceHeaderAndProvidedTraceId() throws Exception {
RestResponse response = TraceValidatingProjectCreationValidationListener projectCreationListener =
adminRestSession.putWithHeader( new TraceValidatingProjectCreationValidationListener();
"/projects/new5", new BasicHeader(RestApiServlet.X_GERRIT_TRACE, "issue/123")); try (Registration registration =
assertThat(response.getStatusCode()).isEqualTo(SC_CREATED); extensionRegistry.newRegistration().add(projectCreationListener)) {
assertThat(response.getHeader(RestApiServlet.X_GERRIT_TRACE)).isEqualTo("issue/123"); RestResponse response =
assertThat(projectCreationListener.traceId).isEqualTo("issue/123"); adminRestSession.putWithHeader(
assertThat(projectCreationListener.isLoggingForced).isTrue(); "/projects/new5", new BasicHeader(RestApiServlet.X_GERRIT_TRACE, "issue/123"));
assertThat(projectCreationListener.tags.get("project")).containsExactly("new5"); assertThat(response.getStatusCode()).isEqualTo(SC_CREATED);
assertThat(response.getHeader(RestApiServlet.X_GERRIT_TRACE)).isEqualTo("issue/123");
assertThat(projectCreationListener.traceId).isEqualTo("issue/123");
assertThat(projectCreationListener.isLoggingForced).isTrue();
assertThat(projectCreationListener.tags.get("project")).containsExactly("new5");
}
} }
@Test @Test
public void restCallWithTraceRequestParamAndTraceHeader() throws Exception { public void restCallWithTraceRequestParamAndTraceHeader() throws Exception {
// trace ID only specified by trace header TraceValidatingProjectCreationValidationListener projectCreationListener =
RestResponse response = new TraceValidatingProjectCreationValidationListener();
adminRestSession.putWithHeader( try (Registration registration =
"/projects/new6?trace", new BasicHeader(RestApiServlet.X_GERRIT_TRACE, "issue/123")); extensionRegistry.newRegistration().add(projectCreationListener)) {
assertThat(response.getStatusCode()).isEqualTo(SC_CREATED); // trace ID only specified by trace header
assertThat(response.getHeader(RestApiServlet.X_GERRIT_TRACE)).isEqualTo("issue/123"); RestResponse response =
assertThat(projectCreationListener.traceId).isEqualTo("issue/123"); adminRestSession.putWithHeader(
assertThat(projectCreationListener.isLoggingForced).isTrue(); "/projects/new6?trace", new BasicHeader(RestApiServlet.X_GERRIT_TRACE, "issue/123"));
assertThat(projectCreationListener.tags.get("project")).containsExactly("new6"); assertThat(response.getStatusCode()).isEqualTo(SC_CREATED);
assertThat(response.getHeader(RestApiServlet.X_GERRIT_TRACE)).isEqualTo("issue/123");
assertThat(projectCreationListener.traceId).isEqualTo("issue/123");
assertThat(projectCreationListener.isLoggingForced).isTrue();
assertThat(projectCreationListener.tags.get("project")).containsExactly("new6");
// trace ID only specified by trace request parameter // trace ID only specified by trace request parameter
response = response =
adminRestSession.putWithHeader( adminRestSession.putWithHeader(
"/projects/new7?trace=issue/123", new BasicHeader(RestApiServlet.X_GERRIT_TRACE, null)); "/projects/new7?trace=issue/123",
assertThat(response.getStatusCode()).isEqualTo(SC_CREATED); new BasicHeader(RestApiServlet.X_GERRIT_TRACE, null));
assertThat(response.getHeader(RestApiServlet.X_GERRIT_TRACE)).isEqualTo("issue/123"); assertThat(response.getStatusCode()).isEqualTo(SC_CREATED);
assertThat(projectCreationListener.traceId).isEqualTo("issue/123"); assertThat(response.getHeader(RestApiServlet.X_GERRIT_TRACE)).isEqualTo("issue/123");
assertThat(projectCreationListener.isLoggingForced).isTrue(); assertThat(projectCreationListener.traceId).isEqualTo("issue/123");
assertThat(projectCreationListener.tags.get("project")).containsExactly("new7"); assertThat(projectCreationListener.isLoggingForced).isTrue();
assertThat(projectCreationListener.tags.get("project")).containsExactly("new7");
// same trace ID specified by trace header and trace request parameter // same trace ID specified by trace header and trace request parameter
response = response =
adminRestSession.putWithHeader( adminRestSession.putWithHeader(
"/projects/new8?trace=issue/123", "/projects/new8?trace=issue/123",
new BasicHeader(RestApiServlet.X_GERRIT_TRACE, "issue/123")); new BasicHeader(RestApiServlet.X_GERRIT_TRACE, "issue/123"));
assertThat(response.getStatusCode()).isEqualTo(SC_CREATED); assertThat(response.getStatusCode()).isEqualTo(SC_CREATED);
assertThat(response.getHeader(RestApiServlet.X_GERRIT_TRACE)).isEqualTo("issue/123"); assertThat(response.getHeader(RestApiServlet.X_GERRIT_TRACE)).isEqualTo("issue/123");
assertThat(projectCreationListener.traceId).isEqualTo("issue/123"); assertThat(projectCreationListener.traceId).isEqualTo("issue/123");
assertThat(projectCreationListener.isLoggingForced).isTrue(); assertThat(projectCreationListener.isLoggingForced).isTrue();
assertThat(projectCreationListener.tags.get("project")).containsExactly("new8"); assertThat(projectCreationListener.tags.get("project")).containsExactly("new8");
// different trace IDs specified by trace header and trace request parameter // different trace IDs specified by trace header and trace request parameter
response = response =
adminRestSession.putWithHeader( adminRestSession.putWithHeader(
"/projects/new9?trace=issue/123", "/projects/new9?trace=issue/123",
new BasicHeader(RestApiServlet.X_GERRIT_TRACE, "issue/456")); new BasicHeader(RestApiServlet.X_GERRIT_TRACE, "issue/456"));
assertThat(response.getStatusCode()).isEqualTo(SC_CREATED); assertThat(response.getStatusCode()).isEqualTo(SC_CREATED);
assertThat(response.getHeaders(RestApiServlet.X_GERRIT_TRACE)) assertThat(response.getHeaders(RestApiServlet.X_GERRIT_TRACE))
.containsExactly("issue/123", "issue/456"); .containsExactly("issue/123", "issue/456");
assertThat(projectCreationListener.traceIds).containsExactly("issue/123", "issue/456"); assertThat(projectCreationListener.traceIds).containsExactly("issue/123", "issue/456");
assertThat(projectCreationListener.isLoggingForced).isTrue(); assertThat(projectCreationListener.isLoggingForced).isTrue();
assertThat(projectCreationListener.tags.get("project")).containsExactly("new9"); assertThat(projectCreationListener.tags.get("project")).containsExactly("new9");
}
} }
@Test @Test
public void pushWithoutTrace() throws Exception { public void pushWithoutTrace() throws Exception {
PushOneCommit push = pushFactory.create(admin.newIdent(), testRepo); TraceValidatingCommitValidationListener commitValidationListener =
PushOneCommit.Result r = push.to("refs/heads/master"); new TraceValidatingCommitValidationListener();
r.assertOkStatus(); try (Registration registration =
assertThat(commitValidationListener.traceId).isNull(); extensionRegistry.newRegistration().add(commitValidationListener)) {
assertThat(commitValidationListener.isLoggingForced).isFalse(); PushOneCommit push = pushFactory.create(admin.newIdent(), testRepo);
PushOneCommit.Result r = push.to("refs/heads/master");
r.assertOkStatus();
assertThat(commitValidationListener.traceId).isNull();
assertThat(commitValidationListener.isLoggingForced).isFalse();
// The logging tag with the project name is also set if tracing is off. // The logging tag with the project name is also set if tracing is off.
assertThat(commitValidationListener.tags.get("project")).containsExactly(project.get()); assertThat(commitValidationListener.tags.get("project")).containsExactly(project.get());
}
} }
@Test @Test
public void pushWithTrace() throws Exception { public void pushWithTrace() throws Exception {
PushOneCommit push = pushFactory.create(admin.newIdent(), testRepo); TraceValidatingCommitValidationListener commitValidationListener =
push.setPushOptions(ImmutableList.of("trace")); new TraceValidatingCommitValidationListener();
PushOneCommit.Result r = push.to("refs/heads/master"); try (Registration registration =
r.assertOkStatus(); extensionRegistry.newRegistration().add(commitValidationListener)) {
assertThat(commitValidationListener.traceId).isNotNull(); PushOneCommit push = pushFactory.create(admin.newIdent(), testRepo);
assertThat(commitValidationListener.isLoggingForced).isTrue(); push.setPushOptions(ImmutableList.of("trace"));
assertThat(commitValidationListener.tags.get("project")).containsExactly(project.get()); PushOneCommit.Result r = push.to("refs/heads/master");
r.assertOkStatus();
assertThat(commitValidationListener.traceId).isNotNull();
assertThat(commitValidationListener.isLoggingForced).isTrue();
assertThat(commitValidationListener.tags.get("project")).containsExactly(project.get());
}
} }
@Test @Test
public void pushWithTraceAndProvidedTraceId() throws Exception { public void pushWithTraceAndProvidedTraceId() throws Exception {
PushOneCommit push = pushFactory.create(admin.newIdent(), testRepo); TraceValidatingCommitValidationListener commitValidationListener =
push.setPushOptions(ImmutableList.of("trace=issue/123")); new TraceValidatingCommitValidationListener();
PushOneCommit.Result r = push.to("refs/heads/master"); try (Registration registration =
r.assertOkStatus(); extensionRegistry.newRegistration().add(commitValidationListener)) {
assertThat(commitValidationListener.traceId).isEqualTo("issue/123"); PushOneCommit push = pushFactory.create(admin.newIdent(), testRepo);
assertThat(commitValidationListener.isLoggingForced).isTrue(); push.setPushOptions(ImmutableList.of("trace=issue/123"));
assertThat(commitValidationListener.tags.get("project")).containsExactly(project.get()); PushOneCommit.Result r = push.to("refs/heads/master");
r.assertOkStatus();
assertThat(commitValidationListener.traceId).isEqualTo("issue/123");
assertThat(commitValidationListener.isLoggingForced).isTrue();
assertThat(commitValidationListener.tags.get("project")).containsExactly(project.get());
}
} }
@Test @Test
public void pushForReviewWithoutTrace() throws Exception { public void pushForReviewWithoutTrace() throws Exception {
PushOneCommit push = pushFactory.create(admin.newIdent(), testRepo); TraceValidatingCommitValidationListener commitValidationListener =
PushOneCommit.Result r = push.to("refs/for/master"); new TraceValidatingCommitValidationListener();
r.assertOkStatus(); try (Registration registration =
assertThat(commitValidationListener.traceId).isNull(); extensionRegistry.newRegistration().add(commitValidationListener)) {
assertThat(commitValidationListener.isLoggingForced).isFalse(); PushOneCommit push = pushFactory.create(admin.newIdent(), testRepo);
PushOneCommit.Result r = push.to("refs/for/master");
r.assertOkStatus();
assertThat(commitValidationListener.traceId).isNull();
assertThat(commitValidationListener.isLoggingForced).isFalse();
// The logging tag with the project name is also set if tracing is off. // The logging tag with the project name is also set if tracing is off.
assertThat(commitValidationListener.tags.get("project")).containsExactly(project.get()); assertThat(commitValidationListener.tags.get("project")).containsExactly(project.get());
}
} }
@Test @Test
public void pushForReviewWithTrace() throws Exception { public void pushForReviewWithTrace() throws Exception {
PushOneCommit push = pushFactory.create(admin.newIdent(), testRepo); TraceValidatingCommitValidationListener commitValidationListener =
push.setPushOptions(ImmutableList.of("trace")); new TraceValidatingCommitValidationListener();
PushOneCommit.Result r = push.to("refs/for/master"); try (Registration registration =
r.assertOkStatus(); extensionRegistry.newRegistration().add(commitValidationListener)) {
assertThat(commitValidationListener.traceId).isNotNull(); PushOneCommit push = pushFactory.create(admin.newIdent(), testRepo);
assertThat(commitValidationListener.isLoggingForced).isTrue(); push.setPushOptions(ImmutableList.of("trace"));
assertThat(commitValidationListener.tags.get("project")).containsExactly(project.get()); PushOneCommit.Result r = push.to("refs/for/master");
r.assertOkStatus();
assertThat(commitValidationListener.traceId).isNotNull();
assertThat(commitValidationListener.isLoggingForced).isTrue();
assertThat(commitValidationListener.tags.get("project")).containsExactly(project.get());
}
} }
@Test @Test
public void pushForReviewWithTraceAndProvidedTraceId() throws Exception { public void pushForReviewWithTraceAndProvidedTraceId() throws Exception {
PushOneCommit push = pushFactory.create(admin.newIdent(), testRepo); TraceValidatingCommitValidationListener commitValidationListener =
push.setPushOptions(ImmutableList.of("trace=issue/123")); new TraceValidatingCommitValidationListener();
PushOneCommit.Result r = push.to("refs/for/master"); try (Registration registration =
r.assertOkStatus(); extensionRegistry.newRegistration().add(commitValidationListener)) {
assertThat(commitValidationListener.traceId).isEqualTo("issue/123"); PushOneCommit push = pushFactory.create(admin.newIdent(), testRepo);
assertThat(commitValidationListener.isLoggingForced).isTrue(); push.setPushOptions(ImmutableList.of("trace=issue/123"));
assertThat(commitValidationListener.tags.get("project")).containsExactly(project.get()); PushOneCommit.Result r = push.to("refs/for/master");
r.assertOkStatus();
assertThat(commitValidationListener.traceId).isEqualTo("issue/123");
assertThat(commitValidationListener.isLoggingForced).isTrue();
assertThat(commitValidationListener.tags.get("project")).containsExactly(project.get());
}
} }
@Test @Test
@@ -345,246 +370,345 @@ public class TraceIT extends AbstractDaemonTest {
@Test @Test
public void performanceLoggingForRestCall() throws Exception { public void performanceLoggingForRestCall() throws Exception {
RestResponse response = adminRestSession.put("/projects/new10"); TestPerformanceLogger testPerformanceLogger = new TestPerformanceLogger();
assertThat(response.getStatusCode()).isEqualTo(SC_CREATED); try (Registration registration =
extensionRegistry.newRegistration().add(testPerformanceLogger)) {
RestResponse response = adminRestSession.put("/projects/new10");
assertThat(response.getStatusCode()).isEqualTo(SC_CREATED);
// This assertion assumes that the server invokes the PerformanceLogger plugins before it sends // This assertion assumes that the server invokes the PerformanceLogger plugins before it
// the response to the client. If this assertion gets flaky it's likely that this got changed on // sends
// server-side. // the response to the client. If this assertion gets flaky it's likely that this got changed
assertThat(testPerformanceLogger.logEntries()).isNotEmpty(); // on
// server-side.
assertThat(testPerformanceLogger.logEntries()).isNotEmpty();
}
} }
@Test @Test
public void performanceLoggingForPush() throws Exception { public void performanceLoggingForPush() throws Exception {
PushOneCommit push = pushFactory.create(admin.newIdent(), testRepo); TestPerformanceLogger testPerformanceLogger = new TestPerformanceLogger();
PushOneCommit.Result r = push.to("refs/heads/master"); try (Registration registration =
r.assertOkStatus(); extensionRegistry.newRegistration().add(testPerformanceLogger)) {
assertThat(testPerformanceLogger.logEntries()).isNotEmpty(); PushOneCommit push = pushFactory.create(admin.newIdent(), testRepo);
PushOneCommit.Result r = push.to("refs/heads/master");
r.assertOkStatus();
assertThat(testPerformanceLogger.logEntries()).isNotEmpty();
}
} }
@Test @Test
@GerritConfig(name = "tracing.performanceLogging", value = "false") @GerritConfig(name = "tracing.performanceLogging", value = "false")
public void noPerformanceLoggingIfDisabled() throws Exception { public void noPerformanceLoggingIfDisabled() throws Exception {
RestResponse response = adminRestSession.put("/projects/new11"); TestPerformanceLogger testPerformanceLogger = new TestPerformanceLogger();
assertThat(response.getStatusCode()).isEqualTo(SC_CREATED); try (Registration registration =
extensionRegistry.newRegistration().add(testPerformanceLogger)) {
RestResponse response = adminRestSession.put("/projects/new11");
assertThat(response.getStatusCode()).isEqualTo(SC_CREATED);
PushOneCommit push = pushFactory.create(admin.newIdent(), testRepo); PushOneCommit push = pushFactory.create(admin.newIdent(), testRepo);
PushOneCommit.Result r = push.to("refs/heads/master"); PushOneCommit.Result r = push.to("refs/heads/master");
r.assertOkStatus(); r.assertOkStatus();
assertThat(testPerformanceLogger.logEntries()).isEmpty(); assertThat(testPerformanceLogger.logEntries()).isEmpty();
}
} }
@Test @Test
@GerritConfig(name = "tracing.issue123.projectPattern", value = "new12") @GerritConfig(name = "tracing.issue123.projectPattern", value = "new12")
public void traceProject() throws Exception { public void traceProject() throws Exception {
RestResponse response = adminRestSession.put("/projects/new12"); TraceValidatingProjectCreationValidationListener projectCreationListener =
assertThat(response.getStatusCode()).isEqualTo(SC_CREATED); new TraceValidatingProjectCreationValidationListener();
assertThat(response.getHeader(RestApiServlet.X_GERRIT_TRACE)).isNull(); try (Registration registration =
assertThat(projectCreationListener.traceId).isEqualTo("issue123"); extensionRegistry.newRegistration().add(projectCreationListener)) {
assertThat(projectCreationListener.isLoggingForced).isTrue(); RestResponse response = adminRestSession.put("/projects/new12");
assertThat(projectCreationListener.tags.get("project")).containsExactly("new12"); assertThat(response.getStatusCode()).isEqualTo(SC_CREATED);
assertThat(response.getHeader(RestApiServlet.X_GERRIT_TRACE)).isNull();
assertThat(projectCreationListener.traceId).isEqualTo("issue123");
assertThat(projectCreationListener.isLoggingForced).isTrue();
assertThat(projectCreationListener.tags.get("project")).containsExactly("new12");
}
} }
@Test @Test
@GerritConfig(name = "tracing.issue123.projectPattern", value = "new.*") @GerritConfig(name = "tracing.issue123.projectPattern", value = "new.*")
public void traceProjectMatchRegEx() throws Exception { public void traceProjectMatchRegEx() throws Exception {
RestResponse response = adminRestSession.put("/projects/new13"); TraceValidatingProjectCreationValidationListener projectCreationListener =
assertThat(response.getStatusCode()).isEqualTo(SC_CREATED); new TraceValidatingProjectCreationValidationListener();
assertThat(response.getHeader(RestApiServlet.X_GERRIT_TRACE)).isNull(); try (Registration registration =
assertThat(projectCreationListener.traceId).isEqualTo("issue123"); extensionRegistry.newRegistration().add(projectCreationListener)) {
assertThat(projectCreationListener.isLoggingForced).isTrue(); RestResponse response = adminRestSession.put("/projects/new13");
assertThat(projectCreationListener.tags.get("project")).containsExactly("new13"); assertThat(response.getStatusCode()).isEqualTo(SC_CREATED);
assertThat(response.getHeader(RestApiServlet.X_GERRIT_TRACE)).isNull();
assertThat(projectCreationListener.traceId).isEqualTo("issue123");
assertThat(projectCreationListener.isLoggingForced).isTrue();
assertThat(projectCreationListener.tags.get("project")).containsExactly("new13");
}
} }
@Test @Test
@GerritConfig(name = "tracing.issue123.projectPattern", value = "foo.*") @GerritConfig(name = "tracing.issue123.projectPattern", value = "foo.*")
public void traceProjectNoMatch() throws Exception { public void traceProjectNoMatch() throws Exception {
RestResponse response = adminRestSession.put("/projects/new13"); TraceValidatingProjectCreationValidationListener projectCreationListener =
assertThat(response.getStatusCode()).isEqualTo(SC_CREATED); new TraceValidatingProjectCreationValidationListener();
assertThat(response.getHeader(RestApiServlet.X_GERRIT_TRACE)).isNull(); try (Registration registration =
assertThat(projectCreationListener.traceId).isNull(); extensionRegistry.newRegistration().add(projectCreationListener)) {
assertThat(projectCreationListener.isLoggingForced).isFalse(); RestResponse response = adminRestSession.put("/projects/new13");
assertThat(response.getStatusCode()).isEqualTo(SC_CREATED);
assertThat(response.getHeader(RestApiServlet.X_GERRIT_TRACE)).isNull();
assertThat(projectCreationListener.traceId).isNull();
assertThat(projectCreationListener.isLoggingForced).isFalse();
// The logging tag with the project name is also set if tracing is off. // The logging tag with the project name is also set if tracing is off.
assertThat(projectCreationListener.tags.get("project")).containsExactly("new13"); assertThat(projectCreationListener.tags.get("project")).containsExactly("new13");
}
} }
@Test @Test
@GerritConfig(name = "tracing.issue123.projectPattern", value = "][") @GerritConfig(name = "tracing.issue123.projectPattern", value = "][")
public void traceProjectInvalidRegEx() throws Exception { public void traceProjectInvalidRegEx() throws Exception {
RestResponse response = adminRestSession.put("/projects/new14"); TraceValidatingProjectCreationValidationListener projectCreationListener =
assertThat(response.getStatusCode()).isEqualTo(SC_CREATED); new TraceValidatingProjectCreationValidationListener();
assertThat(response.getHeader(RestApiServlet.X_GERRIT_TRACE)).isNull(); try (Registration registration =
assertThat(projectCreationListener.traceId).isNull(); extensionRegistry.newRegistration().add(projectCreationListener)) {
assertThat(projectCreationListener.isLoggingForced).isFalse(); RestResponse response = adminRestSession.put("/projects/new14");
assertThat(response.getStatusCode()).isEqualTo(SC_CREATED);
assertThat(response.getHeader(RestApiServlet.X_GERRIT_TRACE)).isNull();
assertThat(projectCreationListener.traceId).isNull();
assertThat(projectCreationListener.isLoggingForced).isFalse();
// The logging tag with the project name is also set if tracing is off. // The logging tag with the project name is also set if tracing is off.
assertThat(projectCreationListener.tags.get("project")).containsExactly("new14"); assertThat(projectCreationListener.tags.get("project")).containsExactly("new14");
}
} }
@Test @Test
@GerritConfig(name = "tracing.issue123.account", value = "1000000") @GerritConfig(name = "tracing.issue123.account", value = "1000000")
public void traceAccount() throws Exception { public void traceAccount() throws Exception {
RestResponse response = adminRestSession.put("/projects/new15"); TraceValidatingProjectCreationValidationListener projectCreationListener =
assertThat(response.getStatusCode()).isEqualTo(SC_CREATED); new TraceValidatingProjectCreationValidationListener();
assertThat(response.getHeader(RestApiServlet.X_GERRIT_TRACE)).isNull(); try (Registration registration =
assertThat(projectCreationListener.traceId).isEqualTo("issue123"); extensionRegistry.newRegistration().add(projectCreationListener)) {
assertThat(projectCreationListener.isLoggingForced).isTrue(); RestResponse response = adminRestSession.put("/projects/new15");
assertThat(projectCreationListener.tags.get("project")).containsExactly("new15"); assertThat(response.getStatusCode()).isEqualTo(SC_CREATED);
assertThat(response.getHeader(RestApiServlet.X_GERRIT_TRACE)).isNull();
assertThat(projectCreationListener.traceId).isEqualTo("issue123");
assertThat(projectCreationListener.isLoggingForced).isTrue();
assertThat(projectCreationListener.tags.get("project")).containsExactly("new15");
}
} }
@Test @Test
@GerritConfig(name = "tracing.issue123.account", value = "1000001") @GerritConfig(name = "tracing.issue123.account", value = "1000001")
public void traceAccountNoMatch() throws Exception { public void traceAccountNoMatch() throws Exception {
RestResponse response = adminRestSession.put("/projects/new16"); TraceValidatingProjectCreationValidationListener projectCreationListener =
assertThat(response.getStatusCode()).isEqualTo(SC_CREATED); new TraceValidatingProjectCreationValidationListener();
assertThat(response.getHeader(RestApiServlet.X_GERRIT_TRACE)).isNull(); try (Registration registration =
assertThat(projectCreationListener.traceId).isNull(); extensionRegistry.newRegistration().add(projectCreationListener)) {
assertThat(projectCreationListener.isLoggingForced).isFalse(); RestResponse response = adminRestSession.put("/projects/new16");
assertThat(response.getStatusCode()).isEqualTo(SC_CREATED);
assertThat(response.getHeader(RestApiServlet.X_GERRIT_TRACE)).isNull();
assertThat(projectCreationListener.traceId).isNull();
assertThat(projectCreationListener.isLoggingForced).isFalse();
// The logging tag with the project name is also set if tracing is off. // The logging tag with the project name is also set if tracing is off.
assertThat(projectCreationListener.tags.get("project")).containsExactly("new16"); assertThat(projectCreationListener.tags.get("project")).containsExactly("new16");
}
} }
@Test @Test
@GerritConfig(name = "tracing.issue123.account", value = "999") @GerritConfig(name = "tracing.issue123.account", value = "999")
public void traceAccountNotFound() throws Exception { public void traceAccountNotFound() throws Exception {
RestResponse response = adminRestSession.put("/projects/new17"); TraceValidatingProjectCreationValidationListener projectCreationListener =
assertThat(response.getStatusCode()).isEqualTo(SC_CREATED); new TraceValidatingProjectCreationValidationListener();
assertThat(response.getHeader(RestApiServlet.X_GERRIT_TRACE)).isNull(); try (Registration registration =
assertThat(projectCreationListener.traceId).isNull(); extensionRegistry.newRegistration().add(projectCreationListener)) {
assertThat(projectCreationListener.isLoggingForced).isFalse(); RestResponse response = adminRestSession.put("/projects/new17");
assertThat(response.getStatusCode()).isEqualTo(SC_CREATED);
assertThat(response.getHeader(RestApiServlet.X_GERRIT_TRACE)).isNull();
assertThat(projectCreationListener.traceId).isNull();
assertThat(projectCreationListener.isLoggingForced).isFalse();
// The logging tag with the project name is also set if tracing is off. // The logging tag with the project name is also set if tracing is off.
assertThat(projectCreationListener.tags.get("project")).containsExactly("new17"); assertThat(projectCreationListener.tags.get("project")).containsExactly("new17");
}
} }
@Test @Test
@GerritConfig(name = "tracing.issue123.account", value = "invalid") @GerritConfig(name = "tracing.issue123.account", value = "invalid")
public void traceAccountInvalidId() throws Exception { public void traceAccountInvalidId() throws Exception {
RestResponse response = adminRestSession.put("/projects/new18"); TraceValidatingProjectCreationValidationListener projectCreationListener =
assertThat(response.getStatusCode()).isEqualTo(SC_CREATED); new TraceValidatingProjectCreationValidationListener();
assertThat(response.getHeader(RestApiServlet.X_GERRIT_TRACE)).isNull(); try (Registration registration =
assertThat(projectCreationListener.traceId).isNull(); extensionRegistry.newRegistration().add(projectCreationListener)) {
assertThat(projectCreationListener.isLoggingForced).isFalse(); RestResponse response = adminRestSession.put("/projects/new18");
assertThat(response.getStatusCode()).isEqualTo(SC_CREATED);
assertThat(response.getHeader(RestApiServlet.X_GERRIT_TRACE)).isNull();
assertThat(projectCreationListener.traceId).isNull();
assertThat(projectCreationListener.isLoggingForced).isFalse();
// The logging tag with the project name is also set if tracing is off. // The logging tag with the project name is also set if tracing is off.
assertThat(projectCreationListener.tags.get("project")).containsExactly("new18"); assertThat(projectCreationListener.tags.get("project")).containsExactly("new18");
}
} }
@Test @Test
@GerritConfig(name = "tracing.issue123.requestType", value = "REST") @GerritConfig(name = "tracing.issue123.requestType", value = "REST")
public void traceRequestType() throws Exception { public void traceRequestType() throws Exception {
RestResponse response = adminRestSession.put("/projects/new19"); TraceValidatingProjectCreationValidationListener projectCreationListener =
assertThat(response.getStatusCode()).isEqualTo(SC_CREATED); new TraceValidatingProjectCreationValidationListener();
assertThat(response.getHeader(RestApiServlet.X_GERRIT_TRACE)).isNull(); try (Registration registration =
assertThat(projectCreationListener.traceId).isEqualTo("issue123"); extensionRegistry.newRegistration().add(projectCreationListener)) {
assertThat(projectCreationListener.isLoggingForced).isTrue(); RestResponse response = adminRestSession.put("/projects/new19");
assertThat(projectCreationListener.tags.get("project")).containsExactly("new19"); assertThat(response.getStatusCode()).isEqualTo(SC_CREATED);
assertThat(response.getHeader(RestApiServlet.X_GERRIT_TRACE)).isNull();
assertThat(projectCreationListener.traceId).isEqualTo("issue123");
assertThat(projectCreationListener.isLoggingForced).isTrue();
assertThat(projectCreationListener.tags.get("project")).containsExactly("new19");
}
} }
@Test @Test
@GerritConfig(name = "tracing.issue123.requestType", value = "SSH") @GerritConfig(name = "tracing.issue123.requestType", value = "SSH")
public void traceRequestTypeNoMatch() throws Exception { public void traceRequestTypeNoMatch() throws Exception {
RestResponse response = adminRestSession.put("/projects/new20"); TraceValidatingProjectCreationValidationListener projectCreationListener =
assertThat(response.getStatusCode()).isEqualTo(SC_CREATED); new TraceValidatingProjectCreationValidationListener();
assertThat(response.getHeader(RestApiServlet.X_GERRIT_TRACE)).isNull(); try (Registration registration =
assertThat(projectCreationListener.traceId).isNull(); extensionRegistry.newRegistration().add(projectCreationListener)) {
assertThat(projectCreationListener.isLoggingForced).isFalse(); RestResponse response = adminRestSession.put("/projects/new20");
assertThat(response.getStatusCode()).isEqualTo(SC_CREATED);
assertThat(response.getHeader(RestApiServlet.X_GERRIT_TRACE)).isNull();
assertThat(projectCreationListener.traceId).isNull();
assertThat(projectCreationListener.isLoggingForced).isFalse();
// The logging tag with the project name is also set if tracing is off. // The logging tag with the project name is also set if tracing is off.
assertThat(projectCreationListener.tags.get("project")).containsExactly("new20"); assertThat(projectCreationListener.tags.get("project")).containsExactly("new20");
}
} }
@Test @Test
@GerritConfig(name = "tracing.issue123.requestType", value = "FOO") @GerritConfig(name = "tracing.issue123.requestType", value = "FOO")
public void traceProjectInvalidRequestType() throws Exception { public void traceProjectInvalidRequestType() throws Exception {
RestResponse response = adminRestSession.put("/projects/new21"); TraceValidatingProjectCreationValidationListener projectCreationListener =
assertThat(response.getStatusCode()).isEqualTo(SC_CREATED); new TraceValidatingProjectCreationValidationListener();
assertThat(response.getHeader(RestApiServlet.X_GERRIT_TRACE)).isNull(); try (Registration registration =
assertThat(projectCreationListener.traceId).isNull(); extensionRegistry.newRegistration().add(projectCreationListener)) {
assertThat(projectCreationListener.isLoggingForced).isFalse(); RestResponse response = adminRestSession.put("/projects/new21");
assertThat(response.getStatusCode()).isEqualTo(SC_CREATED);
assertThat(response.getHeader(RestApiServlet.X_GERRIT_TRACE)).isNull();
assertThat(projectCreationListener.traceId).isNull();
assertThat(projectCreationListener.isLoggingForced).isFalse();
// The logging tag with the project name is also set if tracing is off. // The logging tag with the project name is also set if tracing is off.
assertThat(projectCreationListener.tags.get("project")).containsExactly("new21"); assertThat(projectCreationListener.tags.get("project")).containsExactly("new21");
}
} }
@Test @Test
@GerritConfig(name = "tracing.issue123.account", value = "1000000") @GerritConfig(name = "tracing.issue123.account", value = "1000000")
@GerritConfig(name = "tracing.issue123.projectPattern", value = "new.*") @GerritConfig(name = "tracing.issue123.projectPattern", value = "new.*")
public void traceProjectForAccount() throws Exception { public void traceProjectForAccount() throws Exception {
RestResponse response = adminRestSession.put("/projects/new22"); TraceValidatingProjectCreationValidationListener projectCreationListener =
assertThat(response.getStatusCode()).isEqualTo(SC_CREATED); new TraceValidatingProjectCreationValidationListener();
assertThat(response.getHeader(RestApiServlet.X_GERRIT_TRACE)).isNull(); try (Registration registration =
assertThat(projectCreationListener.traceId).isEqualTo("issue123"); extensionRegistry.newRegistration().add(projectCreationListener)) {
assertThat(projectCreationListener.isLoggingForced).isTrue(); RestResponse response = adminRestSession.put("/projects/new22");
assertThat(projectCreationListener.tags.get("project")).containsExactly("new22"); assertThat(response.getStatusCode()).isEqualTo(SC_CREATED);
assertThat(response.getHeader(RestApiServlet.X_GERRIT_TRACE)).isNull();
assertThat(projectCreationListener.traceId).isEqualTo("issue123");
assertThat(projectCreationListener.isLoggingForced).isTrue();
assertThat(projectCreationListener.tags.get("project")).containsExactly("new22");
}
} }
@Test @Test
@GerritConfig(name = "tracing.issue123.account", value = "1000000") @GerritConfig(name = "tracing.issue123.account", value = "1000000")
@GerritConfig(name = "tracing.issue123.projectPattern", value = "foo.*") @GerritConfig(name = "tracing.issue123.projectPattern", value = "foo.*")
public void traceProjectForAccountNoProjectMatch() throws Exception { public void traceProjectForAccountNoProjectMatch() throws Exception {
RestResponse response = adminRestSession.put("/projects/new23"); TraceValidatingProjectCreationValidationListener projectCreationListener =
assertThat(response.getStatusCode()).isEqualTo(SC_CREATED); new TraceValidatingProjectCreationValidationListener();
assertThat(response.getHeader(RestApiServlet.X_GERRIT_TRACE)).isNull(); try (Registration registration =
assertThat(projectCreationListener.traceId).isNull(); extensionRegistry.newRegistration().add(projectCreationListener)) {
assertThat(projectCreationListener.isLoggingForced).isFalse(); RestResponse response = adminRestSession.put("/projects/new23");
assertThat(response.getStatusCode()).isEqualTo(SC_CREATED);
assertThat(response.getHeader(RestApiServlet.X_GERRIT_TRACE)).isNull();
assertThat(projectCreationListener.traceId).isNull();
assertThat(projectCreationListener.isLoggingForced).isFalse();
// The logging tag with the project name is also set if tracing is off. // The logging tag with the project name is also set if tracing is off.
assertThat(projectCreationListener.tags.get("project")).containsExactly("new23"); assertThat(projectCreationListener.tags.get("project")).containsExactly("new23");
}
} }
@Test @Test
@GerritConfig(name = "tracing.issue123.account", value = "1000001") @GerritConfig(name = "tracing.issue123.account", value = "1000001")
@GerritConfig(name = "tracing.issue123.projectPattern", value = "new.*") @GerritConfig(name = "tracing.issue123.projectPattern", value = "new.*")
public void traceProjectForAccountNoAccountMatch() throws Exception { public void traceProjectForAccountNoAccountMatch() throws Exception {
RestResponse response = adminRestSession.put("/projects/new24"); TraceValidatingProjectCreationValidationListener projectCreationListener =
assertThat(response.getStatusCode()).isEqualTo(SC_CREATED); new TraceValidatingProjectCreationValidationListener();
assertThat(response.getHeader(RestApiServlet.X_GERRIT_TRACE)).isNull(); try (Registration registration =
assertThat(projectCreationListener.traceId).isNull(); extensionRegistry.newRegistration().add(projectCreationListener)) {
assertThat(projectCreationListener.isLoggingForced).isFalse(); RestResponse response = adminRestSession.put("/projects/new24");
assertThat(response.getStatusCode()).isEqualTo(SC_CREATED);
assertThat(response.getHeader(RestApiServlet.X_GERRIT_TRACE)).isNull();
assertThat(projectCreationListener.traceId).isNull();
assertThat(projectCreationListener.isLoggingForced).isFalse();
// The logging tag with the project name is also set if tracing is off. // The logging tag with the project name is also set if tracing is off.
assertThat(projectCreationListener.tags.get("project")).containsExactly("new24"); assertThat(projectCreationListener.tags.get("project")).containsExactly("new24");
}
} }
@Test @Test
@GerritConfig(name = "tracing.issue123.requestUriPattern", value = "/projects/.*") @GerritConfig(name = "tracing.issue123.requestUriPattern", value = "/projects/.*")
public void traceRequestUri() throws Exception { public void traceRequestUri() throws Exception {
RestResponse response = adminRestSession.put("/projects/new23"); TraceValidatingProjectCreationValidationListener projectCreationListener =
assertThat(response.getStatusCode()).isEqualTo(SC_CREATED); new TraceValidatingProjectCreationValidationListener();
assertThat(response.getHeader(RestApiServlet.X_GERRIT_TRACE)).isNull(); try (Registration registration =
assertThat(projectCreationListener.traceId).isEqualTo("issue123"); extensionRegistry.newRegistration().add(projectCreationListener)) {
assertThat(projectCreationListener.isLoggingForced).isTrue(); RestResponse response = adminRestSession.put("/projects/new23");
assertThat(projectCreationListener.tags.get("project")).containsExactly("new23"); assertThat(response.getStatusCode()).isEqualTo(SC_CREATED);
assertThat(response.getHeader(RestApiServlet.X_GERRIT_TRACE)).isNull();
assertThat(projectCreationListener.traceId).isEqualTo("issue123");
assertThat(projectCreationListener.isLoggingForced).isTrue();
assertThat(projectCreationListener.tags.get("project")).containsExactly("new23");
}
} }
@Test @Test
@GerritConfig(name = "tracing.issue123.requestUriPattern", value = "/projects/.*/foo") @GerritConfig(name = "tracing.issue123.requestUriPattern", value = "/projects/.*/foo")
public void traceRequestUriNoMatch() throws Exception { public void traceRequestUriNoMatch() throws Exception {
RestResponse response = adminRestSession.put("/projects/new23"); TraceValidatingProjectCreationValidationListener projectCreationListener =
assertThat(response.getStatusCode()).isEqualTo(SC_CREATED); new TraceValidatingProjectCreationValidationListener();
assertThat(response.getHeader(RestApiServlet.X_GERRIT_TRACE)).isNull(); try (Registration registration =
assertThat(projectCreationListener.traceId).isNull(); extensionRegistry.newRegistration().add(projectCreationListener)) {
assertThat(projectCreationListener.isLoggingForced).isFalse(); RestResponse response = adminRestSession.put("/projects/new23");
assertThat(response.getStatusCode()).isEqualTo(SC_CREATED);
assertThat(response.getHeader(RestApiServlet.X_GERRIT_TRACE)).isNull();
assertThat(projectCreationListener.traceId).isNull();
assertThat(projectCreationListener.isLoggingForced).isFalse();
// The logging tag with the project name is also set if tracing is off. // The logging tag with the project name is also set if tracing is off.
assertThat(projectCreationListener.tags.get("project")).containsExactly("new23"); assertThat(projectCreationListener.tags.get("project")).containsExactly("new23");
}
} }
@Test @Test
@GerritConfig(name = "tracing.issue123.requestUriPattern", value = "][") @GerritConfig(name = "tracing.issue123.requestUriPattern", value = "][")
public void traceRequestUriInvalidRegEx() throws Exception { public void traceRequestUriInvalidRegEx() throws Exception {
RestResponse response = adminRestSession.put("/projects/new24"); TraceValidatingProjectCreationValidationListener projectCreationListener =
assertThat(response.getStatusCode()).isEqualTo(SC_CREATED); new TraceValidatingProjectCreationValidationListener();
assertThat(response.getHeader(RestApiServlet.X_GERRIT_TRACE)).isNull(); try (Registration registration =
assertThat(projectCreationListener.traceId).isNull(); extensionRegistry.newRegistration().add(projectCreationListener)) {
assertThat(projectCreationListener.isLoggingForced).isFalse(); RestResponse response = adminRestSession.put("/projects/new24");
assertThat(response.getStatusCode()).isEqualTo(SC_CREATED);
assertThat(response.getHeader(RestApiServlet.X_GERRIT_TRACE)).isNull();
assertThat(projectCreationListener.traceId).isNull();
assertThat(projectCreationListener.isLoggingForced).isFalse();
// The logging tag with the project name is also set if tracing is off. // The logging tag with the project name is also set if tracing is off.
assertThat(projectCreationListener.tags.get("project")).containsExactly("new24"); assertThat(projectCreationListener.tags.get("project")).containsExactly("new24");
}
} }
@Test @Test
@@ -595,15 +719,12 @@ public class TraceIT extends AbstractDaemonTest {
TraceSubmitRule traceSubmitRule = new TraceSubmitRule(); TraceSubmitRule traceSubmitRule = new TraceSubmitRule();
traceSubmitRule.failAlways = true; traceSubmitRule.failAlways = true;
RegistrationHandle submitRuleRegistrationHandle = submitRules.add("gerrit", traceSubmitRule); try (Registration registration = extensionRegistry.newRegistration().add(traceSubmitRule)) {
try {
RestResponse response = adminRestSession.post("/changes/" + changeId + "/submit"); RestResponse response = adminRestSession.post("/changes/" + changeId + "/submit");
assertThat(response.getStatusCode()).isEqualTo(SC_INTERNAL_SERVER_ERROR); assertThat(response.getStatusCode()).isEqualTo(SC_INTERNAL_SERVER_ERROR);
assertThat(response.getHeader(RestApiServlet.X_GERRIT_TRACE)).startsWith("retry-on-failure-"); assertThat(response.getHeader(RestApiServlet.X_GERRIT_TRACE)).startsWith("retry-on-failure-");
assertThat(traceSubmitRule.traceId).startsWith("retry-on-failure-"); assertThat(traceSubmitRule.traceId).startsWith("retry-on-failure-");
assertThat(traceSubmitRule.isLoggingForced).isTrue(); assertThat(traceSubmitRule.isLoggingForced).isTrue();
} finally {
submitRuleRegistrationHandle.remove();
} }
} }
@@ -615,25 +736,22 @@ public class TraceIT extends AbstractDaemonTest {
TraceSubmitRule traceSubmitRule = new TraceSubmitRule(); TraceSubmitRule traceSubmitRule = new TraceSubmitRule();
traceSubmitRule.failAlways = true; traceSubmitRule.failAlways = true;
RegistrationHandle submitRuleRegistrationHandle = submitRules.add("gerrit", traceSubmitRule); try (Registration registration =
RegistrationHandle exceptionHookRegistrationHandle = extensionRegistry
exceptionHooks.add( .newRegistration()
"gerrit", .add(traceSubmitRule)
new ExceptionHook() { .add(
@Override new ExceptionHook() {
public boolean shouldRetry(Throwable t) { @Override
return true; public boolean shouldRetry(Throwable t) {
} return true;
}); }
try { })) {
RestResponse response = adminRestSession.post("/changes/" + changeId + "/submit"); RestResponse response = adminRestSession.post("/changes/" + changeId + "/submit");
assertThat(response.getStatusCode()).isEqualTo(SC_INTERNAL_SERVER_ERROR); assertThat(response.getStatusCode()).isEqualTo(SC_INTERNAL_SERVER_ERROR);
assertThat(response.getHeader(RestApiServlet.X_GERRIT_TRACE)).isNull(); assertThat(response.getHeader(RestApiServlet.X_GERRIT_TRACE)).isNull();
assertThat(traceSubmitRule.traceId).isNull(); assertThat(traceSubmitRule.traceId).isNull();
assertThat(traceSubmitRule.isLoggingForced).isFalse(); assertThat(traceSubmitRule.isLoggingForced).isFalse();
} finally {
submitRuleRegistrationHandle.remove();
exceptionHookRegistrationHandle.remove();
} }
} }
@@ -644,15 +762,12 @@ public class TraceIT extends AbstractDaemonTest {
TraceSubmitRule traceSubmitRule = new TraceSubmitRule(); TraceSubmitRule traceSubmitRule = new TraceSubmitRule();
traceSubmitRule.failOnce = true; traceSubmitRule.failOnce = true;
RegistrationHandle submitRuleRegistrationHandle = submitRules.add("gerrit", traceSubmitRule); try (Registration registration = extensionRegistry.newRegistration().add(traceSubmitRule)) {
try {
RestResponse response = adminRestSession.post("/changes/" + changeId + "/submit"); RestResponse response = adminRestSession.post("/changes/" + changeId + "/submit");
assertThat(response.getStatusCode()).isEqualTo(SC_INTERNAL_SERVER_ERROR); assertThat(response.getStatusCode()).isEqualTo(SC_INTERNAL_SERVER_ERROR);
assertThat(response.getHeader(RestApiServlet.X_GERRIT_TRACE)).isNull(); assertThat(response.getHeader(RestApiServlet.X_GERRIT_TRACE)).isNull();
assertThat(traceSubmitRule.traceId).isNull(); assertThat(traceSubmitRule.traceId).isNull();
assertThat(traceSubmitRule.isLoggingForced).isFalse(); assertThat(traceSubmitRule.isLoggingForced).isFalse();
} finally {
submitRuleRegistrationHandle.remove();
} }
} }