Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

restore cache if cacheValidityDecidingFile is missing #122

Merged
merged 1 commit into from
Dec 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions src/main/java/jenkins/plugins/jobcacher/ArbitraryFileCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -195,13 +195,13 @@ private FilePath resolvePath(FilePath workspace, EnvVars initialEnvironment) {
}

private ExistingCache resolveExistingValidCache(ObjectPath cachesRoot, ObjectPath fallbackCachesRoot, FilePath workspace, TaskListener listener) throws IOException, InterruptedException {
ExistingCache cache = resolveExistingValidCache(cachesRoot, workspace);
ExistingCache cache = resolveExistingValidCache(cachesRoot, workspace, listener);
if (cache != null) {
logMessage("Found cache in job specific caches", listener);
return cache;
}

cache = resolveExistingValidCache(fallbackCachesRoot, workspace);
cache = resolveExistingValidCache(fallbackCachesRoot, workspace, listener);
if (cache != null) {
logMessage("Found cache in default caches", listener);
return cache;
Expand All @@ -210,17 +210,22 @@ private ExistingCache resolveExistingValidCache(ObjectPath cachesRoot, ObjectPat
return null;
}

private ExistingCache resolveExistingValidCache(ObjectPath cachesRoot, FilePath workspace) throws IOException, InterruptedException {
private ExistingCache resolveExistingValidCache(ObjectPath cachesRoot, FilePath workspace, TaskListener listener) throws IOException, InterruptedException {
ExistingCache existingCache = resolveExistingCache(cachesRoot);
if (existingCache == null) {
return null;
}

if (isCacheValidityDecidingFileConfigured() && isCacheOutdated(cachesRoot, workspace)) {
return null;
if (!isCacheValidityDecidingFileConfigured()) {
return existingCache;
}

if (!isOneCacheValidityDecidingFilePresent(workspace)) {
logMessage("cacheValidityDecidingFile configured, but file(s) not present in workspace - considering cache anyway", listener);
return existingCache;
}

return existingCache;
return isCacheOutdated(cachesRoot, workspace) ? null : existingCache;
}

private ExistingCache resolveExistingCache(ObjectPath cachesRoot) throws IOException, InterruptedException {
Expand Down Expand Up @@ -327,7 +332,7 @@ public void save(ObjectPath cachesRoot, Run<?, ?> build, FilePath workspace, Lau
}

if (isCacheValidityDecidingFileConfigured()) {
ExistingCache existingValidCache = resolveExistingValidCache(cachesRoot, workspace);
ExistingCache existingValidCache = resolveExistingValidCache(cachesRoot, workspace, listener);
if (existingValidCache != null) {
logMessage("Skip cache creation as the cache is up-to-date", listener);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,30 @@ public static void startAgent() throws Exception {
agent = jenkins.createSlave(Label.get("test-agent"));
}

@Test
@WithTimeout(600)
public void testMissingCacheValidityDecidingFile() throws Exception {
String cacheDefinition = "arbitraryFileCache(path: 'test-path', cacheValidityDecidingFile: 'cacheValidityDecidingFile.txt')";
WorkflowJob project = createTestProject(cacheDefinition);

WorkflowRun run1 = jenkins.assertBuildStatus(Result.SUCCESS, project.scheduleBuild2(0));
assertThat(run1.getLog())
.contains("[Cache for test-path] Skip restoring cache as no up-to-date cache exists")
.doesNotContain("expected output from test file")
.contains("[Cache for test-path] Creating cache...");

deleteCachedDirectoryInWorkspace(project);
setProjectDefinition(project, cacheDefinition, null);

WorkflowRun run2 = jenkins.assertBuildStatus(Result.SUCCESS, project.scheduleBuild2(0));
assertThat(run2.getLog())
.contains("[Cache for test-path] cacheValidityDecidingFile configured, but file(s) not present in workspace - considering cache anyway")
.contains("[Cache for test-path] Found cache in job specific caches")
.contains("[Cache for test-path] Restoring cache...")
.contains("expected output from test file")
.contains("[Cache for test-path] Skip cache creation as the cache is up-to-date");
}

@Test
@WithTimeout(600)
public void testMultipleCacheValidityDecidingFiles() throws Exception {
Expand Down Expand Up @@ -147,9 +171,11 @@ public void testNonExistingCacheValidityDecidingFile() throws Exception {

WorkflowRun run2 = jenkins.assertBuildStatus(Result.SUCCESS, project.scheduleBuild2(0));
assertThat(run2.getLog())
.contains("[Cache for test-path] Skip restoring cache as no up-to-date cache exists")
.doesNotContain("expected output from test file")
.contains("[Cache for test-path] Creating cache...");
.contains("[Cache for test-path] cacheValidityDecidingFile configured, but file(s) not present in workspace - considering cache anyway")
.contains("[Cache for test-path] Found cache in job specific caches")
.contains("[Cache for test-path] Restoring cache...")
.contains("expected output from test file")
.contains("[Cache for test-path] Skip cache creation as the cache is up-to-date");
}

@Test
Expand Down Expand Up @@ -209,14 +235,30 @@ private void setProjectDefinition(WorkflowJob project, String cacheDefinition) {
private void setProjectDefinition(WorkflowJob project, String cacheDefinition, String cacheValidityDecidingFileContent) {
String scriptedPipeline = ""
+ "node('test-agent') {\n"
+ " writeFile text: '" + cacheValidityDecidingFileContent + "', file: 'cacheValidityDecidingFile.txt'\n"
+ " " + cacheValidityDecidingFileCode(cacheValidityDecidingFileContent) + "\n"
+ " cache(maxCacheSize: 100, caches: [" + cacheDefinition + "]) {\n"
+ " " + fileCreationCode("test-path", "test-file") + "\n"
+ " }\n"
+ "}";
project.setDefinition(new CpsFlowDefinition(scriptedPipeline, true));
}

private String cacheValidityDecidingFileCode(String cacheValidityDecidingFileContent) {
if (cacheValidityDecidingFileContent != null) {
return "writeFile text: '" + cacheValidityDecidingFileContent + "', file: 'cacheValidityDecidingFile.txt'";
} else {
return fileDeletionCode("cacheValidityDecidingFile.txt");
}
}

private String fileDeletionCode(String file) {
if (SystemUtils.IS_OS_WINDOWS) {
return "bat '''del " + file + "'''";
} else {
return "sh '''rm " + file + "'''";
}
}

private String fileCreationCode(String folder, String file) {
if (SystemUtils.IS_OS_WINDOWS) {
return "bat '''" + fileCreationCodeForWindows(folder, file + ".bat") + "'''";
Expand Down