|
|
|
@@ -9,10 +9,15 @@ import java.io.IOException;
|
|
|
|
|
import java.nio.file.Files;
|
|
|
|
|
import java.nio.file.Path;
|
|
|
|
|
import java.time.Duration;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.Set;
|
|
|
|
|
import java.util.TreeMap;
|
|
|
|
|
import java.util.TreeSet;
|
|
|
|
|
import java.util.regex.Matcher;
|
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
import java.util.stream.Stream;
|
|
|
|
|
import org.junit.jupiter.api.DisplayName;
|
|
|
|
|
import org.junit.jupiter.api.Tag;
|
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
@@ -41,6 +46,30 @@ class DeveloperExperienceContractTest {
|
|
|
|
|
private static final String MAIN_WORKFLOW = ".gitea/workflows/main.yml";
|
|
|
|
|
private static final String RELEASE_WORKFLOW = ".gitea/workflows/release.yml";
|
|
|
|
|
|
|
|
|
|
/** The nightly job that runs the fileserver tests against a real NFS server, and its Compose. */
|
|
|
|
|
private static final String NFS_JOB = "fileserver-nfs-ambiguity";
|
|
|
|
|
|
|
|
|
|
private static final String NFS_COMPOSE =
|
|
|
|
|
"src/adapter/outbound/fileserver/compose/nfs/compose.yml";
|
|
|
|
|
|
|
|
|
|
/** Compose's global options that take a value: the subcommand is the first word after them. */
|
|
|
|
|
private static final Set<String> COMPOSE_OPTIONS_WITH_A_VALUE =
|
|
|
|
|
Set.of(
|
|
|
|
|
"-f",
|
|
|
|
|
"--file",
|
|
|
|
|
"-p",
|
|
|
|
|
"--project-name",
|
|
|
|
|
"--project-directory",
|
|
|
|
|
"--env-file",
|
|
|
|
|
"--profile",
|
|
|
|
|
"--ansi",
|
|
|
|
|
"--progress",
|
|
|
|
|
"--parallel");
|
|
|
|
|
|
|
|
|
|
private static final Pattern COMPOSE_IMAGE_LINE =
|
|
|
|
|
Pattern.compile("^\\s*image:\\s*[\"']?([^\"'\\s#]+)");
|
|
|
|
|
private static final Pattern IMAGE_DIGEST = Pattern.compile("@sha256:[0-9a-f]{64}$");
|
|
|
|
|
|
|
|
|
|
// The workflow-mutation fixtures that stood here are gone with the tests that used them: a
|
|
|
|
|
// hex-escaped `uses:`, a line-continued action reference, a `run:` block impersonating a
|
|
|
|
|
// validation step, a bare `if: always()` versus the guarded form, a gate-matrix step. Every one
|
|
|
|
@@ -351,6 +380,110 @@ class DeveloperExperienceContractTest {
|
|
|
|
|
.noneMatch(step -> step instanceof Map<?, ?> map && map.containsKey("uses"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
@Tag("repository-contract")
|
|
|
|
|
@DisplayName("NFS nightly의 호스트 확인 step은 서버 이미지가 요구하는 nfs·nfsd 두 모듈을 다 보고, 둘 다 올리는 명령을 안내한다")
|
|
|
|
|
void nfsHostCheckLooksForBothModulesTheServerImageRequires() throws IOException {
|
|
|
|
|
// given
|
|
|
|
|
Pattern moduleLoop = Pattern.compile("\\bfor\\s+\\w+\\s+in\\s+([^;\\n]+?)\\s*;\\s*do\\b");
|
|
|
|
|
Pattern loadHint = Pattern.compile("\\bmodprobe\\s+-a((?:\\s+[\\w-]+)+)");
|
|
|
|
|
Pattern bootList =
|
|
|
|
|
Pattern.compile("\\bprintf\\s+'([^']*)'\\s*\\|\\s*sudo\\s+tee\\s+/etc/modules-load\\.d/");
|
|
|
|
|
|
|
|
|
|
// when
|
|
|
|
|
List<String> checks =
|
|
|
|
|
nfsJobSteps().stream()
|
|
|
|
|
.map(DeveloperExperienceContractTest::stepRun)
|
|
|
|
|
.filter(run -> run.contains("/proc/filesystems"))
|
|
|
|
|
.toList();
|
|
|
|
|
|
|
|
|
|
// then
|
|
|
|
|
assertThat(checks)
|
|
|
|
|
.as("the one step that reads /proc/filesystems")
|
|
|
|
|
.singleElement()
|
|
|
|
|
.satisfies(
|
|
|
|
|
check -> {
|
|
|
|
|
assertThat(wordsMatchedBy(moduleLoop, check))
|
|
|
|
|
.as("the modules the check looks for")
|
|
|
|
|
.containsExactlyInAnyOrder("nfs", "nfsd");
|
|
|
|
|
assertThat(wordsMatchedBy(loadHint, check))
|
|
|
|
|
.as("the modules the printed command loads now")
|
|
|
|
|
.containsExactlyInAnyOrder("nfs", "nfsd");
|
|
|
|
|
assertThat(printfLines(groupMatchedBy(bootList, check)))
|
|
|
|
|
.as("the modules the printed command loads at every boot")
|
|
|
|
|
.containsExactlyInAnyOrder("nfs", "nfsd");
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
@Tag("repository-contract")
|
|
|
|
|
@DisplayName(
|
|
|
|
|
"NFS nightly의 Gradle step은 --rerun으로 돈다: FILESERVER_NFS_TESTS는 태스크 입력이 아니어서, --rerun이 없으면 캐시가 NFS 없이 초록을 돌려준다")
|
|
|
|
|
void nfsJobRerunsItsTestsInsteadOfTakingThemFromTheBuildCache() throws IOException {
|
|
|
|
|
// The flag as a whole word anywhere in the script, so a Gradle call wrapped in a container's
|
|
|
|
|
// `bash -c "…"` still counts.
|
|
|
|
|
Pattern rerun = Pattern.compile("(?<![\\w-])--rerun(?![\\w-])");
|
|
|
|
|
|
|
|
|
|
List<String> gradleRuns =
|
|
|
|
|
nfsJobSteps().stream()
|
|
|
|
|
.map(DeveloperExperienceContractTest::stepRun)
|
|
|
|
|
.filter(run -> run.contains("gradlew"))
|
|
|
|
|
.toList();
|
|
|
|
|
|
|
|
|
|
assertThat(gradleRuns)
|
|
|
|
|
.as("the steps that run the NFS tests with Gradle")
|
|
|
|
|
.isNotEmpty()
|
|
|
|
|
.allSatisfy(run -> assertThat(run).as(run).containsPattern(rerun));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
@Tag("repository-contract")
|
|
|
|
|
@DisplayName("NFS nightly는 실패하면 컨테이너를 지우는 down step보다 먼저 compose 로그를 남긴다")
|
|
|
|
|
void nfsJobPrintsTheComposeLogsOnFailureBeforeRemovingTheContainers() throws IOException {
|
|
|
|
|
// given
|
|
|
|
|
List<Map<?, ?>> composeSteps =
|
|
|
|
|
nfsJobSteps().stream()
|
|
|
|
|
.filter(step -> scriptWords(stepRun(step)).contains(NFS_COMPOSE))
|
|
|
|
|
.toList();
|
|
|
|
|
|
|
|
|
|
// when
|
|
|
|
|
List<String> subcommands =
|
|
|
|
|
composeSteps.stream().map(step -> composeSubcommand(stepRun(step))).toList();
|
|
|
|
|
List<String> logsConditions =
|
|
|
|
|
composeSteps.stream()
|
|
|
|
|
.filter(step -> composeSubcommand(stepRun(step)).equals("logs"))
|
|
|
|
|
.map(step -> String.valueOf(step.get("if")))
|
|
|
|
|
.toList();
|
|
|
|
|
|
|
|
|
|
// then
|
|
|
|
|
assertThat(subcommands)
|
|
|
|
|
.as("what the job asks %s to do, in order", NFS_COMPOSE)
|
|
|
|
|
.containsSubsequence("logs", "down");
|
|
|
|
|
assertThat(logsConditions)
|
|
|
|
|
.as("the logs step runs when an earlier step failed")
|
|
|
|
|
.singleElement()
|
|
|
|
|
.asString()
|
|
|
|
|
.contains("failure()");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
@Tag("repository-contract")
|
|
|
|
|
@DisplayName("workflow step이 docker compose -f로 띄우는 compose 파일의 image는 모두 sha256 digest로 고정한다")
|
|
|
|
|
void composeFilesAWorkflowStepStartsPinEveryImageByDigest() throws IOException {
|
|
|
|
|
// given
|
|
|
|
|
Set<String> composeFiles = composeFilesStartedByWorkflowSteps();
|
|
|
|
|
|
|
|
|
|
// when
|
|
|
|
|
Map<String, List<String>> unpinned = imagesWithoutADigest(composeFiles);
|
|
|
|
|
|
|
|
|
|
// then
|
|
|
|
|
assertThat(unpinned)
|
|
|
|
|
.as(
|
|
|
|
|
"the Compose files workflow steps start with -f, each with its image lines lacking a digest")
|
|
|
|
|
.isNotEmpty()
|
|
|
|
|
.allSatisfy((file, images) -> assertThat(images).as(file).isEmpty());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
@Tag("repository-contract")
|
|
|
|
|
@DisplayName("compose 종료 유예 시간은 웹 drain, async 대기, preStop, 여유를 모두 더한 값 이상이다")
|
|
|
|
@@ -601,6 +734,128 @@ class DeveloperExperienceContractTest {
|
|
|
|
|
return (String) value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static List<Map<?, ?>> nfsJobSteps() throws IOException {
|
|
|
|
|
Map<?, ?> job = requireMap(requireMap(parseYamlMap(read(MAIN_WORKFLOW)), "jobs"), NFS_JOB);
|
|
|
|
|
return requireList(job, "steps").stream().<Map<?, ?>>map(step -> (Map<?, ?>) step).toList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** A step's shell script, or "" for a step that uses an action. */
|
|
|
|
|
private static String stepRun(Map<?, ?> step) {
|
|
|
|
|
return step.get("run") instanceof String run ? run : "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** The word after the NFS Compose file in a {@code docker compose -f <file> <subcommand>} run. */
|
|
|
|
|
private static String composeSubcommand(String run) {
|
|
|
|
|
List<String> words = scriptWords(run);
|
|
|
|
|
int file = words.indexOf(NFS_COMPOSE);
|
|
|
|
|
return file >= 0 && file + 1 < words.size() ? words.get(file + 1) : "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Every file a {@code docker compose -f <file>} names in a step of {@code .gitea/workflows/*} or
|
|
|
|
|
* {@code .gitea/actions/<name>/action.yml}. Compose a script starts is not one of them.
|
|
|
|
|
*/
|
|
|
|
|
private static Set<String> composeFilesStartedByWorkflowSteps() throws IOException {
|
|
|
|
|
List<Path> definitions = new ArrayList<>();
|
|
|
|
|
try (Stream<Path> workflows = Files.list(REPOSITORY_ROOT.resolve(".gitea/workflows"))) {
|
|
|
|
|
workflows.filter(path -> path.toString().matches(".*\\.ya?ml")).forEach(definitions::add);
|
|
|
|
|
}
|
|
|
|
|
try (Stream<Path> actions = Files.list(REPOSITORY_ROOT.resolve(".gitea/actions"))) {
|
|
|
|
|
actions
|
|
|
|
|
.flatMap(action -> Stream.of(action.resolve("action.yml"), action.resolve("action.yaml")))
|
|
|
|
|
.filter(Files::isRegularFile)
|
|
|
|
|
.forEach(definitions::add);
|
|
|
|
|
}
|
|
|
|
|
Set<String> files = new TreeSet<>();
|
|
|
|
|
for (Path definition : definitions) {
|
|
|
|
|
stepRuns(parseYamlMap(Files.readString(definition)))
|
|
|
|
|
.forEach(run -> files.addAll(composeFilesNamedIn(run)));
|
|
|
|
|
}
|
|
|
|
|
return files;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Every {@code run:} script anywhere in a parsed workflow or action. */
|
|
|
|
|
private static Stream<String> stepRuns(Object node) {
|
|
|
|
|
if (node instanceof Map<?, ?> map) {
|
|
|
|
|
return map.entrySet().stream()
|
|
|
|
|
.flatMap(
|
|
|
|
|
entry ->
|
|
|
|
|
"run".equals(entry.getKey()) && entry.getValue() instanceof String run
|
|
|
|
|
? Stream.of(run)
|
|
|
|
|
: stepRuns(entry.getValue()));
|
|
|
|
|
}
|
|
|
|
|
return node instanceof List<?> list
|
|
|
|
|
? list.stream().flatMap(DeveloperExperienceContractTest::stepRuns)
|
|
|
|
|
: Stream.empty();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** The {@code -f}/{@code --file} values among the global options of each docker compose call. */
|
|
|
|
|
private static List<String> composeFilesNamedIn(String run) {
|
|
|
|
|
List<String> words = scriptWords(run);
|
|
|
|
|
List<String> files = new ArrayList<>();
|
|
|
|
|
for (int docker = 0; docker + 1 < words.size(); docker++) {
|
|
|
|
|
if (!words.get(docker).equals("docker") || !words.get(docker + 1).equals("compose")) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
int next = docker + 2;
|
|
|
|
|
while (next < words.size() && words.get(next).startsWith("-")) {
|
|
|
|
|
String option = words.get(next++);
|
|
|
|
|
if (option.startsWith("--file=")) {
|
|
|
|
|
files.add(option.substring("--file=".length()));
|
|
|
|
|
} else if (COMPOSE_OPTIONS_WITH_A_VALUE.contains(option) && next < words.size()) {
|
|
|
|
|
String value = words.get(next++);
|
|
|
|
|
if (option.equals("-f") || option.equals("--file")) {
|
|
|
|
|
files.add(value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return files;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Per Compose file, its {@code image:} lines (numbered) whose reference has no digest. */
|
|
|
|
|
private static Map<String, List<String>> imagesWithoutADigest(Set<String> composeFiles)
|
|
|
|
|
throws IOException {
|
|
|
|
|
Map<String, List<String>> unpinned = new TreeMap<>();
|
|
|
|
|
for (String file : composeFiles) {
|
|
|
|
|
List<String> lines = read(file).lines().toList();
|
|
|
|
|
List<String> withoutDigest = new ArrayList<>();
|
|
|
|
|
for (int line = 0; line < lines.size(); line++) {
|
|
|
|
|
Matcher image = COMPOSE_IMAGE_LINE.matcher(lines.get(line));
|
|
|
|
|
if (image.find() && !IMAGE_DIGEST.matcher(image.group(1)).find()) {
|
|
|
|
|
withoutDigest.add((line + 1) + ": " + lines.get(line).trim());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
unpinned.put(file, withoutDigest);
|
|
|
|
|
}
|
|
|
|
|
return unpinned;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** The first group the pattern captures in {@code text}; fails when it does not match. */
|
|
|
|
|
private static String groupMatchedBy(Pattern pattern, String text) {
|
|
|
|
|
Matcher matcher = pattern.matcher(text);
|
|
|
|
|
assertThat(matcher.find()).as("%s in:%n%s", pattern, text).isTrue();
|
|
|
|
|
return matcher.group(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** The shell words of the first group the pattern captures in {@code text}. */
|
|
|
|
|
private static List<String> wordsMatchedBy(Pattern pattern, String text) {
|
|
|
|
|
return shellWords(groupMatchedBy(pattern, text));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** The lines a printf format prints, its {@code \n} written with one or more backslashes. */
|
|
|
|
|
private static List<String> printfLines(String format) {
|
|
|
|
|
return Pattern.compile("(?:\\\\+n|\\s)+")
|
|
|
|
|
.splitAsStream(format)
|
|
|
|
|
.filter(line -> !line.isEmpty())
|
|
|
|
|
.toList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** The shell words of a step script, its backslash-newline continuations joined first. */
|
|
|
|
|
private static List<String> scriptWords(String run) {
|
|
|
|
|
return shellWords(run.replace("\\\n", " "));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static List<String> shellWords(String arguments) {
|
|
|
|
|
return Pattern.compile("\\s+")
|
|
|
|
|
.splitAsStream(arguments.trim())
|
|
|
|
|