Line data Source code
1 : // Copyright (C) 2018 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.acceptance; 16 : 17 : import static java.util.Comparator.comparing; 18 : import static java.util.concurrent.TimeUnit.SECONDS; 19 : 20 : import com.google.common.collect.ImmutableList; 21 : import com.google.common.primitives.Ints; 22 : import com.google.gerrit.extensions.registration.DynamicSet; 23 : import com.google.gerrit.httpd.GitOverHttpServlet; 24 : import com.google.gerrit.server.AuditEvent; 25 : import com.google.gerrit.server.audit.AuditListener; 26 : import com.google.gerrit.server.audit.AuditService; 27 : import com.google.gerrit.server.audit.HttpAuditEvent; 28 : import com.google.gerrit.server.audit.group.GroupAuditListener; 29 : import com.google.gerrit.server.group.GroupAuditService; 30 : import com.google.gerrit.server.plugincontext.PluginSetContext; 31 : import com.google.inject.AbstractModule; 32 : import com.google.inject.Inject; 33 : import com.google.inject.Singleton; 34 : import java.util.ArrayList; 35 : import java.util.List; 36 : import java.util.concurrent.BlockingQueue; 37 : import java.util.concurrent.LinkedBlockingQueue; 38 : import java.util.concurrent.atomic.AtomicLong; 39 : 40 : @Singleton 41 : public class FakeGroupAuditService extends AuditService { 42 138 : public static class FakeGroupAuditServiceModule extends AbstractModule { 43 : @Override 44 : public void configure() { 45 138 : DynamicSet.setOf(binder(), GroupAuditListener.class); 46 138 : DynamicSet.setOf(binder(), AuditListener.class); 47 : 48 : // Use this fake service at the Guice level rather than depending on tests binding their own 49 : // audit listeners. If we used per-test listeners, then there would be a race between 50 : // dispatching the audit events from HTTP requests performed during test setup in 51 : // AbstractDaemonTest, and the later test setup binding the audit listener. Using a separate 52 : // audit service implementation ensures all events get recorded. 53 138 : bind(GroupAuditService.class).to(FakeGroupAuditService.class); 54 138 : } 55 : } 56 : 57 : private final GitOverHttpServlet.Metrics httpMetrics; 58 : private final BlockingQueue<HttpAuditEvent> httpEvents; 59 : private final AtomicLong drainedSoFar; 60 : 61 : @Inject 62 : FakeGroupAuditService( 63 : PluginSetContext<AuditListener> auditListeners, 64 : PluginSetContext<GroupAuditListener> groupAuditListeners, 65 : GitOverHttpServlet.Metrics httpMetrics) { 66 138 : super(auditListeners, groupAuditListeners); 67 138 : this.httpMetrics = httpMetrics; 68 138 : this.httpEvents = new LinkedBlockingQueue<>(); 69 138 : this.drainedSoFar = new AtomicLong(); 70 138 : } 71 : 72 : @Override 73 : public void dispatch(AuditEvent action) { 74 47 : super.dispatch(action); 75 47 : if (action instanceof HttpAuditEvent) { 76 36 : httpEvents.add((HttpAuditEvent) action); 77 : } 78 47 : } 79 : 80 : public ImmutableList<HttpAuditEvent> drainHttpAuditEvents() throws Exception { 81 : // Assumes that all HttpAuditEvents are produced by GitOverHttpServlet. 82 2 : int expectedSize = Ints.checkedCast(httpMetrics.getRequestsStarted() - drainedSoFar.get()); 83 2 : List<HttpAuditEvent> result = new ArrayList<>(); 84 2 : for (int i = 0; i < expectedSize; i++) { 85 2 : HttpAuditEvent e = httpEvents.poll(30, SECONDS); 86 2 : if (e == null) { 87 0 : throw new AssertionError( 88 0 : String.format("Timeout after receiving %d/%d audit events", i, expectedSize)); 89 : } 90 2 : drainedSoFar.incrementAndGet(); 91 2 : result.add(e); 92 : } 93 2 : return ImmutableList.sortedCopyOf(comparing(e -> e.when), result); 94 : } 95 : }