From 7b84a88799efe219852e5c123900f13c76e58a0d Mon Sep 17 00:00:00 2001 From: Iurii Ignatko Date: Tue, 17 Sep 2024 13:55:11 +0200 Subject: [PATCH] Update tests --- .../injection/DevelocityComputerListener.java | 5 +- .../InjectionConfigChangeListener.java | 4 +- .../MavenExtensionDownloadHandler.java | 10 ++- ...ldScanInjectionMavenIntegrationTest.groovy | 20 +++-- .../MavenExtensionDownloadHandlerTest.groovy | 90 +++++++++++++++++++ 5 files changed, 118 insertions(+), 11 deletions(-) create mode 100644 src/test/groovy/hudson/plugins/gradle/injection/MavenExtensionDownloadHandlerTest.groovy diff --git a/src/main/java/hudson/plugins/gradle/injection/DevelocityComputerListener.java b/src/main/java/hudson/plugins/gradle/injection/DevelocityComputerListener.java index 4ad16da5..15834706 100644 --- a/src/main/java/hudson/plugins/gradle/injection/DevelocityComputerListener.java +++ b/src/main/java/hudson/plugins/gradle/injection/DevelocityComputerListener.java @@ -7,6 +7,7 @@ import hudson.model.Node; import hudson.model.TaskListener; import hudson.slaves.ComputerListener; +import jenkins.model.Jenkins; import java.util.Map; import java.util.function.Supplier; @@ -59,7 +60,9 @@ public void onOnline(Computer computer, TaskListener listener) { return; } - Map extensionsDigest = mavenExtensionDownloadHandler.ensureExtensionsDownloaded(injectionConfig); + Map extensionsDigest = mavenExtensionDownloadHandler.ensureExtensionsDownloaded( + Jenkins.get().getRootDir(), injectionConfig + ); Node node = computer.getNode(); EnvVars computerEnvVars = computer.getEnvironment(); diff --git a/src/main/java/hudson/plugins/gradle/injection/InjectionConfigChangeListener.java b/src/main/java/hudson/plugins/gradle/injection/InjectionConfigChangeListener.java index 301b4b46..151ff432 100644 --- a/src/main/java/hudson/plugins/gradle/injection/InjectionConfigChangeListener.java +++ b/src/main/java/hudson/plugins/gradle/injection/InjectionConfigChangeListener.java @@ -76,7 +76,9 @@ public void onChange(Saveable saveable, XmlFile file) { } try { - Map extensionsDigest = mavenExtensionDownloadHandler.ensureExtensionsDownloaded(injectionConfig); + Map extensionsDigest = mavenExtensionDownloadHandler.ensureExtensionsDownloaded( + Jenkins.get().getRootDir(), injectionConfig + ); for (Computer computer : computersSupplier.get()) { if (computer.isOnline()) { diff --git a/src/main/java/hudson/plugins/gradle/injection/MavenExtensionDownloadHandler.java b/src/main/java/hudson/plugins/gradle/injection/MavenExtensionDownloadHandler.java index 22953480..ac37b102 100644 --- a/src/main/java/hudson/plugins/gradle/injection/MavenExtensionDownloadHandler.java +++ b/src/main/java/hudson/plugins/gradle/injection/MavenExtensionDownloadHandler.java @@ -2,11 +2,13 @@ import com.cloudbees.plugins.credentials.CredentialsProvider; import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials; +import hudson.FilePath; import hudson.Util; import hudson.plugins.gradle.injection.extension.ExtensionClient; import jenkins.model.Jenkins; import java.io.BufferedOutputStream; +import java.io.File; import java.io.IOException; import java.io.OutputStream; import java.nio.file.Files; @@ -20,10 +22,10 @@ public class MavenExtensionDownloadHandler implements MavenInjectionAware { public static final String DOWNLOAD_CACHE_DIR = "jenkins-gradle-plugin/cache"; - public Map ensureExtensionsDownloaded(InjectionConfig injectionConfig) throws IOException { + public Map ensureExtensionsDownloaded(File root, InjectionConfig injectionConfig) throws IOException { if (!isInjectionDisabledGlobally(injectionConfig)) { Map extensionsDigest = new HashMap<>(); - Path parent = Jenkins.get().getRootDir().toPath().resolve(DOWNLOAD_CACHE_DIR); + Path parent = root.toPath().resolve(DOWNLOAD_CACHE_DIR); MavenExtension develocityMavenExtension = MavenExtension.getDevelocityMavenExtension(injectionConfig.getMavenExtensionVersion()); @@ -84,6 +86,10 @@ private static String downloadExtension( } private static MavenExtension.RepositoryCredentials getRepositoryCredentials(String repositoryCredentialId) { + if (repositoryCredentialId == null) { + return null; + } + List allCredentials = CredentialsProvider.lookupCredentialsInItem(StandardUsernamePasswordCredentials.class, null, null); diff --git a/src/test/groovy/hudson/plugins/gradle/injection/BuildScanInjectionMavenIntegrationTest.groovy b/src/test/groovy/hudson/plugins/gradle/injection/BuildScanInjectionMavenIntegrationTest.groovy index 2b9167eb..ebd7fb04 100644 --- a/src/test/groovy/hudson/plugins/gradle/injection/BuildScanInjectionMavenIntegrationTest.groovy +++ b/src/test/groovy/hudson/plugins/gradle/injection/BuildScanInjectionMavenIntegrationTest.groovy @@ -42,7 +42,7 @@ class BuildScanInjectionMavenIntegrationTest extends BaseMavenIntegrationTest { private static final String POM_XML = '4.0.0com.examplemy-pom0.1-SNAPSHOTpommy-pommy-pom' private static final String INJECT_CCUD = '[DEBUG] Executing extension: CommonCustomUserDataDevelocityListener' - def 'does not copy configuration extension if it was not changed'() { + def 'does not copy #extension if it was not changed'() { when: def slave = createSlaveAndTurnOnInjection() turnOnBuildInjectionAndRestart(slave) @@ -51,7 +51,7 @@ class BuildScanInjectionMavenIntegrationTest extends BaseMavenIntegrationTest { then: extensionDirectory.list().size() == 3 - def originalExtension = extensionDirectory.list().find { it.name == MavenExtension.CONFIGURATION.getTargetJarName() } + def originalExtension = extensionDirectory.list().find { it.name == extension } originalExtension != null def originalExtensionLastModified = originalExtension.lastModified() originalExtensionLastModified > 0 @@ -66,13 +66,16 @@ class BuildScanInjectionMavenIntegrationTest extends BaseMavenIntegrationTest { then: extensionDirectory.list().size() == 3 - def updatedExtension = extensionDirectory.list().find { it.name == MavenExtension.CONFIGURATION.getTargetJarName() } + def updatedExtension = extensionDirectory.list().find { it.name == extension } updatedExtension != null updatedExtension.lastModified() == originalExtensionLastModified updatedExtension.digest() == originalExtensionDigest + + where: + extension << [DEVELOCITY_EXTENSION_JAR, CCUD_EXTENSION_JAR, CONFIGURATION_EXTENSION_JAR] } - def 'copies a new version of configuration extension if it was changed'() { + def 'copies a new version of #extension if it was changed'() { when: def slave = createSlaveAndTurnOnInjection() turnOnBuildInjectionAndRestart(slave) @@ -81,7 +84,7 @@ class BuildScanInjectionMavenIntegrationTest extends BaseMavenIntegrationTest { then: extensionDirectory.list().size() == 3 - def originalExtension = extensionDirectory.list().find { it.name == MavenExtension.CONFIGURATION.getTargetJarName() } + def originalExtension = extensionDirectory.list().find { it.name == extension } originalExtension != null def originalExtensionLastModified = originalExtension.lastModified() originalExtensionLastModified > 0 @@ -98,7 +101,7 @@ class BuildScanInjectionMavenIntegrationTest extends BaseMavenIntegrationTest { extensionDirectory = slave.toComputer().node.rootPath.child(MavenExtensionsHandler.LIB_DIR_PATH) then: - extensionDirectory.list().find { it.name == MavenExtension.CONFIGURATION.getTargetJarName() }?.lastModified() != originalExtensionLastModified + extensionDirectory.list().find { it.name == extension }?.lastModified() != originalExtensionLastModified when: restartSlave(slave) @@ -106,10 +109,13 @@ class BuildScanInjectionMavenIntegrationTest extends BaseMavenIntegrationTest { extensionDirectory = slave.toComputer().node.rootPath.child(MavenExtensionsHandler.LIB_DIR_PATH) then: - def updatedGeExtension = extensionDirectory.list().find { it.name == MavenExtension.CONFIGURATION.getTargetJarName() } + def updatedGeExtension = extensionDirectory.list().find { it.name == extension } updatedGeExtension != null updatedGeExtension.lastModified() != originalExtensionLastModified updatedGeExtension.digest() == originalExtensionDigest + + where: + extension << [DEVELOCITY_EXTENSION_JAR, CCUD_EXTENSION_JAR, CONFIGURATION_EXTENSION_JAR] } @Issue('https://issues.jenkins.io/browse/JENKINS-70663') diff --git a/src/test/groovy/hudson/plugins/gradle/injection/MavenExtensionDownloadHandlerTest.groovy b/src/test/groovy/hudson/plugins/gradle/injection/MavenExtensionDownloadHandlerTest.groovy new file mode 100644 index 00000000..4f170b0a --- /dev/null +++ b/src/test/groovy/hudson/plugins/gradle/injection/MavenExtensionDownloadHandlerTest.groovy @@ -0,0 +1,90 @@ +package hudson.plugins.gradle.injection + +import org.junit.Rule +import org.junit.rules.TemporaryFolder +import spock.lang.Specification +import spock.lang.Subject + +class MavenExtensionDownloadHandlerTest extends Specification { + + @Rule + public TemporaryFolder tempFolder = new TemporaryFolder() + + @Subject + private final MavenExtensionDownloadHandler mavenExtensionDownloadHandler = new MavenExtensionDownloadHandler() + + def 'extensions are not redonwloaded if config has not changed'() { + given: + def controllerFolder = tempFolder.newFolder() + + def originalConfig = Mock(InjectionConfig) + with(originalConfig) { + enabled >> true + server >> 'https://scans.gradle.com' + mavenExtensionVersion >> '1.22.1' + ccudExtensionVersion >> '2.0.1' + } + + when: + def extensionsDownloaded = mavenExtensionDownloadHandler.ensureExtensionsDownloaded(controllerFolder, originalConfig) + + then: + extensionsDownloaded.size() == 2 + def originalDevelocityDigest = extensionsDownloaded.get(MavenExtension.DEVELOCITY) + def originalCcudDigest = extensionsDownloaded.get(MavenExtension.CCUD) + + originalDevelocityDigest != null && originalCcudDigest != null + + when: + def sameExtensions = mavenExtensionDownloadHandler.ensureExtensionsDownloaded(controllerFolder, originalConfig) + + then: + sameExtensions.size() == 2 + sameExtensions.get(MavenExtension.DEVELOCITY) == originalDevelocityDigest && sameExtensions.get(MavenExtension.CCUD) == originalCcudDigest + } + + def 'configuration change triggers re-download of the extensions'() { + given: + def controllerFolder = tempFolder.newFolder() + + def originalConfig = Mock(InjectionConfig) + with(originalConfig) { + enabled >> true + server >> 'https://scans.gradle.com' + mavenExtensionVersion >> '1.22' + ccudExtensionVersion >> '2.0' + } + + when: + def extensionsDownloaded = mavenExtensionDownloadHandler.ensureExtensionsDownloaded(controllerFolder, originalConfig) + + then: + extensionsDownloaded.size() == 2 + def originalDevelocityDigest = extensionsDownloaded.get(MavenExtension.DEVELOCITY) + def originalCcudDigest = extensionsDownloaded.get(MavenExtension.CCUD) + + originalDevelocityDigest != null && originalCcudDigest != null + + when: + def updatedConfig = Mock(InjectionConfig) + with(updatedConfig) { + enabled >> true + server >> 'https://scans.gradle.com' + mavenExtensionVersion >> '1.22.1' + ccudExtensionVersion >> '2.0.1' + } + + def redownloadedExtensions = mavenExtensionDownloadHandler.ensureExtensionsDownloaded(controllerFolder, updatedConfig) + + then: + redownloadedExtensions.size() == 2 + def redownloadedDevelocityDigest = redownloadedExtensions.get(MavenExtension.DEVELOCITY) + def redownloadedCcudDigest = redownloadedExtensions.get(MavenExtension.CCUD) + + redownloadedDevelocityDigest != null && redownloadedCcudDigest != null + + and: + redownloadedDevelocityDigest != originalDevelocityDigest && redownloadedCcudDigest != originalCcudDigest + } + +}