Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
welandaz committed Sep 17, 2024
1 parent 4622e4c commit 7b84a88
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -59,7 +60,9 @@ public void onOnline(Computer computer, TaskListener listener) {
return;
}

Map<MavenExtension, String> extensionsDigest = mavenExtensionDownloadHandler.ensureExtensionsDownloaded(injectionConfig);
Map<MavenExtension, String> extensionsDigest = mavenExtensionDownloadHandler.ensureExtensionsDownloaded(
Jenkins.get().getRootDir(), injectionConfig
);

Node node = computer.getNode();
EnvVars computerEnvVars = computer.getEnvironment();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ public void onChange(Saveable saveable, XmlFile file) {
}

try {
Map<MavenExtension, String> extensionsDigest = mavenExtensionDownloadHandler.ensureExtensionsDownloaded(injectionConfig);
Map<MavenExtension, String> extensionsDigest = mavenExtensionDownloadHandler.ensureExtensionsDownloaded(
Jenkins.get().getRootDir(), injectionConfig
);

for (Computer computer : computersSupplier.get()) {
if (computer.isOnline()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -20,10 +22,10 @@ public class MavenExtensionDownloadHandler implements MavenInjectionAware {

public static final String DOWNLOAD_CACHE_DIR = "jenkins-gradle-plugin/cache";

public Map<MavenExtension, String> ensureExtensionsDownloaded(InjectionConfig injectionConfig) throws IOException {
public Map<MavenExtension, String> ensureExtensionsDownloaded(File root, InjectionConfig injectionConfig) throws IOException {
if (!isInjectionDisabledGlobally(injectionConfig)) {
Map<MavenExtension, String> 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());

Expand Down Expand Up @@ -84,6 +86,10 @@ private static String downloadExtension(
}

private static MavenExtension.RepositoryCredentials getRepositoryCredentials(String repositoryCredentialId) {
if (repositoryCredentialId == null) {
return null;
}

List<StandardUsernamePasswordCredentials> allCredentials
= CredentialsProvider.lookupCredentialsInItem(StandardUsernamePasswordCredentials.class, null, null);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class BuildScanInjectionMavenIntegrationTest extends BaseMavenIntegrationTest {
private static final String POM_XML = '<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.example</groupId><artifactId>my-pom</artifactId><version>0.1-SNAPSHOT</version><packaging>pom</packaging><name>my-pom</name><description>my-pom</description></project>'
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)
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -98,18 +101,21 @@ 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)

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')
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}

}

0 comments on commit 7b84a88

Please sign in to comment.