Add @UseSsh Annotation and GERRIT_USE_SSH flag

Adding a @UseSsh annotation and a command line flag lets us decide if we
want to run tests that require SSH on each test run. In this way, we can
run the test suite against a Gerrit instance that does not support SSH
connections.

Change-Id: Ibb471f312c50f0c92e1c32e55f4a5667b33b6ab5
This commit is contained in:
Patrick Hiesel
2016-12-22 11:40:23 +01:00
parent e3db98f674
commit 244ff8c375
16 changed files with 157 additions and 16 deletions

View File

@@ -217,6 +217,12 @@ To run the tests against NoteDb backend:
bazel test --test_env=GERRIT_NOTEDB=READ_WRITE //...
----
To run only tests that do not use SSH:
----
bazel test --test_env=GERRIT_USE_SSH=NO //...
----
== Dependencies
Dependency JARs are normally downloaded as needed, but you can

View File

@@ -347,6 +347,12 @@ To run the tests against NoteDb backend:
GERRIT_NOTEDB=READ_WRITE buck test
----
To run only tests that do not use SSH:
----
GERRIT_USE_SSH=NO buck test
----
== Dependencies
Dependency JARs are normally downloaded automatically, but Buck can inspect

View File

@@ -15,6 +15,7 @@
package com.google.gerrit.acceptance;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.TruthJUnit.assume;
import static com.google.gerrit.acceptance.GitUtil.initSsh;
import static com.google.gerrit.extensions.api.changes.SubmittedTogetherOption.NON_VISIBLE_CHANGES;
import static com.google.gerrit.reviewdb.client.Patch.COMMIT_MSG;
@@ -95,6 +96,7 @@ import com.google.gerrit.server.query.change.InternalChangeQuery;
import com.google.gerrit.testutil.ConfigSuite;
import com.google.gerrit.testutil.FakeEmailSender;
import com.google.gerrit.testutil.FakeEmailSender.Message;
import com.google.gerrit.testutil.SshMode;
import com.google.gerrit.testutil.TempFileUtil;
import com.google.gerrit.testutil.TestNotesMigration;
import com.google.gson.Gson;
@@ -274,6 +276,7 @@ public abstract class AbstractDaemonTest {
private String resourcePrefix;
private List<Repository> toClose;
private boolean useSsh;
@Rule
public TestRule testRunner = new TestRule() {
@@ -306,6 +309,16 @@ public abstract class AbstractDaemonTest {
eventRecorder = eventRecorderFactory.create(admin);
}
@Before
public void assumeSshIfRequired() {
if (useSsh) {
// If the test uses ssh, we use assume() to make sure ssh is enabled on
// the test suite. JUnit will skip tests annotated with @UseSsh if we
// disable them using the command line flag.
assume().that(SshMode.useSsh()).isTrue();
}
}
@After
public void closeEventRecorder() {
eventRecorder.close();
@@ -379,20 +392,34 @@ public abstract class AbstractDaemonTest {
adminRestSession = new RestSession(server, admin);
userRestSession = new RestSession(server, user);
initSsh(admin);
db = reviewDbProvider.open();
Context ctx = newRequestContext(user);
atrScope.set(ctx);
userSshSession = ctx.getSession();
userSshSession.open();
ctx = newRequestContext(admin);
atrScope.set(ctx);
adminSshSession = ctx.getSession();
adminSshSession.open();
if (classDesc.useSsh() || methodDesc.useSsh()) {
useSsh = true;
if (SshMode.useSsh() && (adminSshSession == null ||
userSshSession == null)) {
// Create Ssh sessions
initSsh(admin);
Context ctx = newRequestContext(user);
atrScope.set(ctx);
userSshSession = ctx.getSession();
userSshSession.open();
ctx = newRequestContext(admin);
atrScope.set(ctx);
adminSshSession = ctx.getSession();
adminSshSession.open();
}
} else {
useSsh = false;
}
resourcePrefix = UNSAFE_PROJECT_NAME.matcher(
description.getClassName() + "_"
+ description.getMethodName() + "_").replaceAll("");
Context ctx = newRequestContext(admin);
atrScope.set(ctx);
project = createProject(projectInput(description));
testRepo = cloneProject(project, getCloneAsAccount(description));
}
@@ -517,8 +544,12 @@ public abstract class AbstractDaemonTest {
repo.close();
}
db.close();
adminSshSession.close();
userSshSession.close();
if (adminSshSession != null) {
adminSshSession.close();
}
if (userSshSession != null) {
userSshSession.close();
}
if (server != commonServer) {
server.stop();
}

View File

@@ -65,6 +65,7 @@ public class GerritServer {
true, // @UseLocalDisk is only valid on methods.
!has(NoHttpd.class, testDesc.getTestClass()),
has(Sandboxed.class, testDesc.getTestClass()),
has(UseSsh.class, testDesc.getTestClass()),
null, // @GerritConfig is only valid on methods.
null); // @GerritConfigs is only valid on methods.
@@ -79,6 +80,8 @@ public class GerritServer {
&& !has(NoHttpd.class, testDesc.getTestClass()),
testDesc.getAnnotation(Sandboxed.class) != null ||
has(Sandboxed.class, testDesc.getTestClass()),
testDesc.getAnnotation(UseSsh.class) != null ||
has(UseSsh.class, testDesc.getTestClass()),
testDesc.getAnnotation(GerritConfig.class),
testDesc.getAnnotation(GerritConfigs.class));
}
@@ -97,6 +100,7 @@ public class GerritServer {
abstract boolean memory();
abstract boolean httpd();
abstract boolean sandboxed();
abstract boolean useSsh();
@Nullable abstract GerritConfig config();
@Nullable abstract GerritConfigs configs();

View File

@@ -49,8 +49,14 @@ public class LightweightPluginDaemonTest extends AbstractDaemonTest {
@After
public void tearDown() {
plugin.stop(env);
env.onStopPlugin(plugin);
if (plugin != null) {
// plugin will be null if the plugin test requires ssh, but the command
// line flag says we are running tests without ssh as the assume()
// statement in AbstractDaemonTest will prevent the execution of setUp()
// in this class
plugin.stop(env);
env.onStopPlugin(plugin);
}
}
private static TestPlugin getTestPlugin(Class<?> clazz) {

View File

@@ -0,0 +1,27 @@
// Copyright (C) 2016 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 static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
@Target({TYPE, METHOD})
@Retention(RUNTIME)
public @interface UseSsh {
}

View File

@@ -80,13 +80,13 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
public abstract class AbstractPushForReview extends AbstractDaemonTest {
protected enum Protocol {
// TODO(dborowitz): TEST.
SSH, HTTP
}
private String sshUrl;
private LabelType patchSetLock;
@BeforeClass
@@ -101,7 +101,6 @@ public abstract class AbstractPushForReview extends AbstractDaemonTest {
@Before
public void setUp() throws Exception {
sshUrl = adminSshSession.getUrl();
ProjectConfig cfg = projectCache.checkedGet(project).getConfig();
patchSetLock = Util.patchSetLock();
cfg.getLabelSections().put(patchSetLock.getName(), patchSetLock);
@@ -117,7 +116,7 @@ public abstract class AbstractPushForReview extends AbstractDaemonTest {
String url;
switch (p) {
case SSH:
url = sshUrl;
url = adminSshSession.getUrl();
break;
case HTTP:
url = admin.getHttpUrl(server);

View File

@@ -16,9 +16,11 @@ package com.google.gerrit.acceptance.git;
import com.google.gerrit.acceptance.NoHttpd;
import com.google.gerrit.acceptance.UseSsh;
import org.junit.Before;
@NoHttpd
@UseSsh
public class SshPushForReviewIT extends AbstractPushForReview {
@Before
public void selectSshUrl() throws Exception {

View File

@@ -20,6 +20,7 @@ import static java.nio.charset.StandardCharsets.UTF_8;
import com.google.gerrit.acceptance.AbstractDaemonTest;
import com.google.gerrit.acceptance.GerritConfig;
import com.google.gerrit.acceptance.NoHttpd;
import com.google.gerrit.acceptance.UseSsh;
import com.google.gerrit.extensions.client.AccountFieldName;
import com.google.gerrit.extensions.client.AuthType;
import com.google.gerrit.extensions.common.ServerInfo;
@@ -129,6 +130,7 @@ public class ServerInfoIT extends AbstractDaemonTest {
}
@Test
@UseSsh
@GerritConfig(name = "plugins.allowRemoteAdmin", value = "true")
public void serverConfigWithPlugin() throws Exception {
Path plugins = tempSiteDir.newFolder("plugins").toPath();

View File

@@ -21,6 +21,7 @@ import com.google.common.collect.ImmutableList;
import com.google.gerrit.acceptance.AbstractDaemonTest;
import com.google.gerrit.acceptance.NoHttpd;
import com.google.gerrit.acceptance.PushOneCommit.Result;
import com.google.gerrit.acceptance.UseSsh;
import com.google.gerrit.extensions.common.ChangeInfo;
import com.google.gerrit.extensions.common.ChangeMessageInfo;
@@ -31,6 +32,7 @@ import java.util.List;
import java.util.Locale;
@NoHttpd
@UseSsh
public class AbandonRestoreIT extends AbstractDaemonTest {
@Test

View File

@@ -22,6 +22,7 @@ import static org.eclipse.jgit.transport.RemoteRefUpdate.Status.REJECTED_OTHER_R
import com.google.gerrit.acceptance.AbstractDaemonTest;
import com.google.gerrit.acceptance.NoHttpd;
import com.google.gerrit.acceptance.UseSsh;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.transport.RemoteRefUpdate;
import org.junit.Test;
@@ -29,6 +30,7 @@ import org.junit.Test;
import java.util.Locale;
@NoHttpd
@UseSsh
public class BanCommitIT extends AbstractDaemonTest {
@Test

View File

@@ -18,11 +18,13 @@ import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assert_;
import com.google.gerrit.acceptance.AbstractDaemonTest;
import com.google.gerrit.acceptance.UseSsh;
import com.google.gerrit.reviewdb.client.Project;
import com.google.gerrit.server.project.ProjectState;
import org.junit.Test;
@UseSsh
public class CreateProjectIT extends AbstractDaemonTest {
@Test

View File

@@ -21,6 +21,7 @@ import com.google.gerrit.acceptance.AbstractDaemonTest;
import com.google.gerrit.acceptance.GcAssert;
import com.google.gerrit.acceptance.NoHttpd;
import com.google.gerrit.acceptance.UseLocalDisk;
import com.google.gerrit.acceptance.UseSsh;
import com.google.gerrit.common.data.GarbageCollectionResult;
import com.google.gerrit.reviewdb.client.Project;
import com.google.gerrit.server.git.GarbageCollection;
@@ -34,6 +35,7 @@ import java.util.Arrays;
import java.util.Locale;
@NoHttpd
@UseSsh
public class GarbageCollectionIT extends AbstractDaemonTest {
@Inject

View File

@@ -23,6 +23,7 @@ import com.google.gerrit.acceptance.AbstractDaemonTest;
import com.google.gerrit.acceptance.NoHttpd;
import com.google.gerrit.acceptance.PushOneCommit;
import com.google.gerrit.acceptance.SshSession;
import com.google.gerrit.acceptance.UseSsh;
import com.google.gerrit.extensions.api.changes.AddReviewerInput;
import com.google.gerrit.extensions.api.changes.ReviewInput;
import com.google.gerrit.extensions.client.Side;
@@ -36,6 +37,7 @@ import java.util.HashMap;
import java.util.List;
@NoHttpd
@UseSsh
public class QueryIT extends AbstractDaemonTest {
private static Gson gson = new Gson();

View File

@@ -23,6 +23,7 @@ import com.google.gerrit.acceptance.AbstractDaemonTest;
import com.google.gerrit.acceptance.GerritConfig;
import com.google.gerrit.acceptance.NoHttpd;
import com.google.gerrit.acceptance.PushOneCommit;
import com.google.gerrit.acceptance.UseSsh;
import com.google.gerrit.testutil.NoteDbMode;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
@@ -40,6 +41,7 @@ import java.util.Set;
import java.util.TreeSet;
@NoHttpd
@UseSsh
public class UploadArchiveIT extends AbstractDaemonTest {
@Before

View File

@@ -0,0 +1,46 @@
// Copyright (C) 2016 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.testutil;
import static com.google.common.base.Preconditions.checkArgument;
import com.google.common.base.Enums;
import com.google.common.base.Strings;
public enum SshMode {
/** Tests annotated with UseSsh will be disabled. */
NO,
/** Tests annotated with UseSsh will be enabled. */
YES;
private static final String VAR = "GERRIT_USE_SSH";
public static SshMode get() {
String value = System.getenv(VAR);
if (Strings.isNullOrEmpty(value)) {
return YES;
}
value = value.toUpperCase();
SshMode mode = Enums.getIfPresent(SshMode.class, value).orNull();
checkArgument(mode != null,
"Invalid value for %s: %s", VAR, System.getenv(VAR));
return mode;
}
public static boolean useSsh() {
return get() == YES;
}
}