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

@@ -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 {
}