Skip to content

Commit

Permalink
Refresh plugin for August 2023 (Upgrade to a minimum core version 2.3…
Browse files Browse the repository at this point in the history
…87.3 and minimum Java version 11) (#403)

* Refresh plugin for June 2023

* Fix merge

* More updates

* More updates

* Remove usages of Mockito in integration tests to allow the test to run on Java 17

* Test on Java 17
  • Loading branch information
basil authored Sep 21, 2023
1 parent 0157d4f commit 8c8a38a
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 86 deletions.
2 changes: 1 addition & 1 deletion .mvn/extensions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
<extension>
<groupId>io.jenkins.tools.incrementals</groupId>
<artifactId>git-changelist-maven-extension</artifactId>
<version>1.6</version>
<version>1.7</version>
</extension>
</extensions>
7 changes: 3 additions & 4 deletions Jenkinsfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
buildPlugin(configurations: [
[ platform: 'linux', jdk: '8', jenkins: null ],
[ platform: 'linux', jdk: '11', jenkins: null ],
[ platform: 'windows', jdk: '8', jenkins: null ],
buildPlugin(useContainerAgent: true, configurations: [
[platform: 'linux', jdk: 17],
[platform: 'windows', jdk: 11],
])
40 changes: 10 additions & 30 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>4.45</version>
<version>4.72</version>
<relativePath/>
</parent>

<artifactId>google-compute-engine</artifactId>
<version>4.3.17-SNAPSHOT</version>
<version>${revision}${changelist}</version>
<packaging>hpi</packaging>

<name>Google Compute Engine Plugin</name>
<description>This plugin allows Jenkins to provision dynamic worker nodes on Google Compute Engine.</description>
<url>https://github.com/jenkinsci/google-compute-engine-plugin</url>
<url>https://github.com/jenkinsci/${project.artifactId}-plugin</url>
<organization>
<name>Google</name>
<url>https://www.google.com</url>
Expand Down Expand Up @@ -62,13 +62,11 @@
</scm>

<properties>
<revision>4.3.14</revision>
<revision>4.3.17</revision>
<changelist>-SNAPSHOT</changelist>
<gitHubRepo>jenkinsci/google-compute-engine-plugin</gitHubRepo>
<jenkins.version>2.346.1</jenkins.version>
<gitHubRepo>jenkinsci/${project.artifactId}-plugin</gitHubRepo>
<jenkins.version>2.387.3</jenkins.version>
<jenkinsRuleTimeout>0</jenkinsRuleTimeout>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<skip.surefire.tests>${skipTests}</skip.surefire.tests>
<skipITs>true</skipITs>
<it.windows>false</it.windows>
Expand All @@ -83,24 +81,18 @@
<hpi.compatibleSinceVersion>4.1.0</hpi.compatibleSinceVersion>
<concurrency>10</concurrency>
<it.runOrder>balanced</it.runOrder>
<hamcrest.version>2.2</hamcrest.version>
<delombok.output>${project.build.directory}/delombok</delombok.output>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.jenkins.tools.bom</groupId>
<artifactId>bom-2.346.x</artifactId>
<version>1643.v1cffef51df73</version>
<artifactId>bom-2.387.x</artifactId>
<version>2378.v3e03930028f2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
<version>2.2</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
Expand Down Expand Up @@ -164,11 +156,6 @@
<groupId>io.jenkins.plugins</groupId>
<artifactId>commons-text-api</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
Expand Down Expand Up @@ -201,7 +188,7 @@
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.18</version>
<version>1.18.28</version>
<scope>provided</scope>
</dependency>
<!-- gcp-client (abandoned?) actually depends on these libraries in _compile_ scope, so they must be bundled in the plugin. -->
Expand All @@ -214,14 +201,8 @@
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
<version>${hamcrest.version}</version>
</dependency>
<!-- Test Dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
Expand Down Expand Up @@ -288,7 +269,7 @@
<plugin>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-maven-plugin</artifactId>
<version>1.18.8.1</version>
<version>1.18.20.0</version>
<configuration>
<sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
<outputDirectory>${delombok.output}</outputDirectory>
Expand Down Expand Up @@ -420,7 +401,6 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import com.google.jenkins.plugins.computeengine.ssh.GoogleKeyPair;
import com.google.jenkins.plugins.computeengine.ssh.GooglePrivateKey;
import edu.umd.cs.findbugs.annotations.Nullable;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import hudson.Extension;
import hudson.RelativePath;
import hudson.Util;
Expand Down Expand Up @@ -1015,6 +1016,7 @@ public InstanceConfiguration build() {
}

// Private methods defined to exclude these from the builder and skip Lombok generating them.
@SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD", justification = "for Lombok")
private Builder numExecutors(Integer numExecutors) {
throw new NotImplementedException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,58 +20,32 @@
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThrows;

import com.cloudbees.plugins.credentials.Credentials;
import com.cloudbees.plugins.credentials.CredentialsStore;
import com.cloudbees.plugins.credentials.SecretBytes;
import com.cloudbees.plugins.credentials.SystemCredentialsProvider;
import com.cloudbees.plugins.credentials.domains.Domain;
import com.google.common.collect.Lists;
import com.google.jenkins.plugins.credentials.oauth.GoogleRobotPrivateKeyCredentials;
import com.google.jenkins.plugins.credentials.oauth.ServiceAccountConfig;
import com.google.jenkins.plugins.credentials.oauth.JsonServiceAccountConfig;
import hudson.model.Label;
import hudson.model.labels.LabelAtom;
import hudson.util.FormValidation;
import hudson.util.ListBoxModel;
import java.security.PrivateKey;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.jvnet.hudson.test.JenkinsRule;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class ComputeEngineCloudTest {
private static final PrivateKey PRIVATE_KEY;
private static final String ACCOUNT_ID = "test-account-id";
private static final String PK_ALGO = "test";
private static final String PK_FORMAT = "test";
private static final byte[] PK_BYTES = new byte[0];

static {
PRIVATE_KEY =
new PrivateKey() {
@Override
public String getAlgorithm() {
return PK_ALGO;
}

@Override
public String getFormat() {
return PK_FORMAT;
}

@Override
public byte[] getEncoded() {
return PK_BYTES;
}
};
}
private static final byte[] PK_BYTES =
"{\"client_email\": \"[email protected]\"}".getBytes(StandardCharsets.UTF_8);

private static final String INSTANCE_ID = "213123";
private static final String CLOUD_NAME = "test-cloud";
Expand All @@ -80,16 +54,12 @@ public byte[] getEncoded() {

@Rule public JenkinsRule r = new JenkinsRule();

@Mock public ServiceAccountConfig serviceAccountConfig;

@Before
public void init() {
Mockito.when(serviceAccountConfig.getAccountId()).thenReturn(ACCOUNT_ID);
Mockito.when(serviceAccountConfig.getPrivateKey()).thenReturn(PRIVATE_KEY);
}

@Test
public void construction() throws Exception {
SecretBytes bytes = SecretBytes.fromBytes(PK_BYTES);
JsonServiceAccountConfig serviceAccountConfig = new JsonServiceAccountConfig();
serviceAccountConfig.setSecretJsonKey(bytes);
assertNotNull(serviceAccountConfig.getAccountId());
// Create a credential
Credentials c =
(Credentials) new GoogleRobotPrivateKeyCredentials(ACCOUNT_ID, serviceAccountConfig, null);
Expand All @@ -112,8 +82,9 @@ public void construction() throws Exception {
assertEquals(CLOUD_NAME, cloud.getDisplayName());
Assert.assertNotEquals(CLOUD_NAME, cloud.name);

// Ensure ComputeClient is created
assertNotNull("ComputeClient was not initialized", cloud.getClient());
// Ensure correct exception is thrown
assertThrows(
GoogleRobotPrivateKeyCredentials.PrivateKeyNotSetException.class, cloud::getClient);

// Ensure transient properties were initialized
for (InstanceConfiguration ic : ics) {
Expand Down Expand Up @@ -175,6 +146,10 @@ public void getConfigurationsByLabelMulti() throws Exception {
@Test
public void descriptorFillCredentials() throws Exception {
// Create a credential
SecretBytes bytes = SecretBytes.fromBytes(PK_BYTES);
JsonServiceAccountConfig serviceAccountConfig = new JsonServiceAccountConfig();
serviceAccountConfig.setSecretJsonKey(bytes);
assertNotNull(serviceAccountConfig.getAccountId());
Credentials c =
(Credentials) new GoogleRobotPrivateKeyCredentials(ACCOUNT_ID, serviceAccountConfig, null);
CredentialsStore store = new SystemCredentialsProvider.ProviderImpl().getStore(r.jenkins);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,28 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.ArgumentMatchers.any;
import static org.junit.Assert.assertThrows;

import com.cloudbees.plugins.credentials.Credentials;
import com.cloudbees.plugins.credentials.CredentialsStore;
import com.cloudbees.plugins.credentials.SecretBytes;
import com.cloudbees.plugins.credentials.SystemCredentialsProvider;
import com.cloudbees.plugins.credentials.domains.Domain;
import com.google.api.client.auth.oauth2.Credential;
import com.google.jenkins.plugins.credentials.oauth.GoogleRobotCredentials;
import com.google.jenkins.plugins.credentials.oauth.GoogleRobotPrivateKeyCredentials;
import com.google.jenkins.plugins.credentials.oauth.JsonServiceAccountConfig;
import hudson.model.Node;
import io.jenkins.plugins.casc.misc.ConfiguredWithCode;
import io.jenkins.plugins.casc.misc.JenkinsConfiguredWithCodeRule;
import java.nio.charset.StandardCharsets;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
import org.mockito.Mockito;

public class ConfigAsCodeTest {

private static final byte[] PK_BYTES =
"{\"client_email\": \"[email protected]\"}".getBytes(StandardCharsets.UTF_8);

@Rule public JenkinsRule jenkinsRule = new JenkinsConfiguredWithCodeRule();

@Test
Expand Down Expand Up @@ -59,9 +64,12 @@ public void shouldCreateCloudInstanceFromCode() {
@Test
@ConfiguredWithCode("configuration-as-code.yml")
public void shouldCreateGCEClientFromCode() throws Exception {
GoogleRobotCredentials credentials = Mockito.mock(GoogleRobotCredentials.class);
Mockito.when(credentials.getId()).thenReturn("gce-jenkins");
Mockito.when(credentials.getGoogleCredential(any())).thenReturn(Mockito.mock(Credential.class));
SecretBytes bytes = SecretBytes.fromBytes(PK_BYTES);
JsonServiceAccountConfig serviceAccountConfig = new JsonServiceAccountConfig();
serviceAccountConfig.setSecretJsonKey(bytes);
assertNotNull(serviceAccountConfig.getAccountId());
Credentials credentials =
new GoogleRobotPrivateKeyCredentials("gce-jenkins", serviceAccountConfig, null);

CredentialsStore store =
new SystemCredentialsProvider.ProviderImpl().getStore(jenkinsRule.jenkins);
Expand All @@ -70,6 +78,8 @@ public void shouldCreateGCEClientFromCode() throws Exception {
ComputeEngineCloud cloud =
(ComputeEngineCloud) jenkinsRule.jenkins.clouds.getByName("gce-jenkins-build");
assertNotNull("Cloud by name not found", cloud);
assertNotNull("GCE client not created", cloud.getClient());
// Ensure correct exception is thrown
assertThrows(
GoogleRobotPrivateKeyCredentials.PrivateKeyNotSetException.class, cloud::getClient);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.anyString;

import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.google.api.services.compute.model.AcceleratorType;
import com.google.api.services.compute.model.DiskType;
import com.google.api.services.compute.model.Image;
Expand All @@ -44,6 +43,7 @@
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.htmlunit.html.HtmlPage;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
Expand Down

0 comments on commit 8c8a38a

Please sign in to comment.