Add tests to make sure ssh commands can be executed

Recently a commit was submitted that caused the execution of some ssh
commands to fail with internal server error, and this was not noticed
during review/test because we don't have test coverage of all the ssh
commands.

Add a basic test that executes each of the ssh commands with the --help
option. This of course won't test all of the commands' functionality,
but at least will prevent any future breakages such as mentioned above.

Backported to the stable branch and adjusted to remove testing of the
`index activate` and `index start` commands. Since change I3db48f6b5
there is no binding of those commands in acceptance tests.

Change-Id: I69a88e593018d5838bd06943feb8df7992819f0b
(cherry picked from commit d9804401b6)
This commit is contained in:
David Pursehouse
2017-08-31 10:33:08 +09:00
parent b309ae4bc3
commit 25a1d868ab

View File

@@ -0,0 +1,112 @@
// Copyright (C) 2017 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.ssh;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;
import static com.google.gerrit.server.group.SystemGroupBackend.REGISTERED_USERS;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.gerrit.acceptance.AbstractDaemonTest;
import com.google.gerrit.acceptance.NoHttpd;
import com.google.gerrit.acceptance.UseSsh;
import com.google.gerrit.common.data.GlobalCapability;
import com.google.gerrit.sshd.Commands;
import java.util.List;
import java.util.Map;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@NoHttpd
@UseSsh
public class SshCommandsIT extends AbstractDaemonTest {
private static final Logger log = LoggerFactory.getLogger(SshCommandsIT.class);
//TODO: It would be better to dynamically generate this list
private static final Map<String, List<String>> COMMANDS =
ImmutableMap.of(
Commands.ROOT,
ImmutableList.of(
"apropos",
"ban-commit",
"close-connection",
"create-account",
"create-branch",
"create-group",
"create-project",
"flush-caches",
"gc",
"gsql",
"index",
"logging",
"ls-groups",
"ls-members",
"ls-projects",
"ls-user-refs",
"plugin",
"query",
"receive-pack",
"rename-group",
"review",
"set-account",
"set-head",
"set-members",
"set-project",
"set-project-parent",
"set-reviewers",
"show-caches",
"show-connections",
"show-queue",
"stream-events",
"test-submit",
"version"),
"index",
ImmutableList.of("changes", "project"), // "activate" and "start" are not included
"plugin",
ImmutableList.of("add", "enable", "install", "ls", "reload", "remove", "rm"),
"test-submit",
ImmutableList.of("rule", "type"));
@Test
public void sshCommandCanBeExecuted() throws Exception {
// Access Database capability is required to run the "gerrit gsql" command
allowGlobalCapabilities(REGISTERED_USERS, GlobalCapability.ACCESS_DATABASE);
for (String root : COMMANDS.keySet()) {
for (String command : COMMANDS.get(root)) {
// We can't assert that adminSshSession.hasError() is false, because using the --help
// option causes the usage info to be written to stderr. Instead, we assert on the
// content of the stderr, which will always start with "gerrit command" when the --help
// option is used.
String cmd = String.format("gerrit%s%s %s", root.isEmpty() ? "" : " ", root, command);
log.debug(cmd);
adminSshSession.exec(String.format("%s --help", cmd));
String response = adminSshSession.getError();
assertWithMessage(String.format("command %s failed: %s", command, response))
.that(response)
.startsWith(cmd);
}
}
}
@Test
public void nonExistingCommandFails() throws Exception {
adminSshSession.exec("gerrit non-existing-command --help");
assertThat(adminSshSession.getError())
.startsWith("fatal: gerrit: non-existing-command: not found");
}
}