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
…61.3 and minimum Java version 11) (#348)

* Refresh plugin for August 2023

* Remove usage of Mockito to allow test to pass on Java 17
  • Loading branch information
basil committed Sep 21, 2023
1 parent 4bec5b5 commit 0a79d1e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 76 deletions.
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],
[platform: 'linux', jdk: 11],
[platform: 'windows', jdk: 8],
buildPlugin(useContainerAgent: true, configurations: [
[platform: 'linux', jdk: 17],
[platform: 'windows', jdk: 11],
])
18 changes: 11 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>4.45</version>
<version>4.72</version>
<relativePath/>
</parent>

Expand Down Expand Up @@ -66,7 +66,7 @@
<revision>0</revision>
<changelist>999999-SNAPSHOT</changelist>
<gitHubRepo>jenkinsci/google-kubernetes-engine-plugin</gitHubRepo>
<jenkins.version>2.346.1</jenkins.version>
<jenkins.version>2.361.4</jenkins.version>
<google.guava.version>32.1.2-jre</google.guava.version>
<google.api.version>1.25.0</google.api.version>
<google.http.version>1.42.2</google.http.version>
Expand All @@ -78,10 +78,15 @@

<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.10.1</version>
</dependency>
<dependency>
<groupId>io.jenkins.tools.bom</groupId>
<artifactId>bom-2.346.x</artifactId>
<version>1643.v1cffef51df73</version>
<artifactId>bom-2.361.x</artifactId>
<version>2102.v854b_fec19c92</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Expand Down Expand Up @@ -114,9 +119,8 @@
<artifactId>apache-httpcomponents-client-4-api</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.13.3</version>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>jackson2-api</artifactId>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,96 +1,52 @@
package com.google.jenkins.plugins.k8sengine;

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

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.cloudbees.plugins.credentials.domains.DomainRequirement;
import com.google.api.client.auth.oauth2.Credential;
import com.google.common.collect.ImmutableList;
import com.google.jenkins.plugins.credentials.oauth.GoogleRobotCredentials;
import com.google.jenkins.plugins.k8sengine.client.ContainerScopeRequirement;
import com.google.jenkins.plugins.credentials.oauth.GoogleRobotPrivateKeyCredentials;
import com.google.jenkins.plugins.credentials.oauth.JsonServiceAccountConfig;
import hudson.AbortException;
import java.io.IOException;
import java.security.GeneralSecurityException;
import jenkins.model.Jenkins;
import org.junit.BeforeClass;
import java.nio.charset.StandardCharsets;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.jvnet.hudson.test.JenkinsRule;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class CredentialsUtilTest {
private static final String TEST_CREDENTIALS_ID = "test-credentials-id";
private static final String TEST_INVALID_CREDENTIALS_ID = "test-invalid-credentials-id";
private static final String TEST_ACCESS_TOKEN = "test-access-token";
@ClassRule public static JenkinsRule r = new JenkinsRule();
public static Jenkins jenkins;

@BeforeClass
public static void init() throws IOException {
jenkins = r.jenkins;

CredentialsStore store = new SystemCredentialsProvider.ProviderImpl().getStore(jenkins);
GoogleRobotCredentials credentials = Mockito.mock(GoogleRobotCredentials.class);
Mockito.when(credentials.getId()).thenReturn(TEST_CREDENTIALS_ID);
store.addCredentials(Domain.global(), credentials);
}

@Test
public void testGetRobotCredentialsReturnsFirstCredential() throws IOException {
assertNotNull(
CredentialsUtil.getRobotCredentials(
jenkins.get(), ImmutableList.<DomainRequirement>of(), TEST_CREDENTIALS_ID));
}

@Test(expected = AbortException.class)
public void testGetRobotCredentialsInvalidCredentialsIdAbortException() throws AbortException {
CredentialsUtil.getRobotCredentials(
jenkins.get(), ImmutableList.<DomainRequirement>of(), TEST_INVALID_CREDENTIALS_ID);
r.jenkins, ImmutableList.<DomainRequirement>of(), TEST_INVALID_CREDENTIALS_ID);
}

@Test(expected = AbortException.class)
public void testGetGoogleCredentialAbortException()
throws GeneralSecurityException, AbortException {
GoogleRobotCredentials robotCreds = Mockito.mock(GoogleRobotCredentials.class);
Mockito.when(robotCreds.getGoogleCredential(any(ContainerScopeRequirement.class)))
.thenThrow(new GeneralSecurityException());
@Test(expected = GoogleRobotPrivateKeyCredentials.PrivateKeyNotSetException.class)
public void testGetGoogleCredentialAbortException() throws Exception {
SecretBytes bytes =
SecretBytes.fromBytes(
"{\"client_email\": \"example@example.com\"}".getBytes(StandardCharsets.UTF_8));
JsonServiceAccountConfig serviceAccountConfig = new JsonServiceAccountConfig();
serviceAccountConfig.setSecretJsonKey(bytes);
assertNotNull(serviceAccountConfig.getAccountId());
GoogleRobotCredentials robotCreds =
new GoogleRobotPrivateKeyCredentials(
TEST_INVALID_CREDENTIALS_ID, serviceAccountConfig, null);
CredentialsStore store = new SystemCredentialsProvider.ProviderImpl().getStore(r.jenkins);
store.addCredentials(Domain.global(), robotCreds);
CredentialsUtil.getGoogleCredential(robotCreds);
}

@Test
public void testGetGoogleCredentialReturnsCredential()
throws GeneralSecurityException, AbortException {
GoogleRobotCredentials robotCreds = Mockito.mock(GoogleRobotCredentials.class);
Credential credential = Mockito.mock(Credential.class);
Mockito.when(robotCreds.getGoogleCredential(any(ContainerScopeRequirement.class)))
.thenReturn(credential);
assertNotNull(CredentialsUtil.getGoogleCredential(robotCreds));
}

@Test(expected = IOException.class)
public void testGetAccessTokenIOException() throws IOException {
Credential googleCredential = Mockito.mock(Credential.class);
Mockito.when(googleCredential.refreshToken()).thenThrow(IOException.class);
CredentialsUtil.getAccessToken(googleCredential);
}

@Test
public void testGetAccessTokenReturnsToken() throws IOException {
Credential googleCredential = Mockito.mock(Credential.class);
Mockito.when(googleCredential.refreshToken()).thenReturn(true);
Mockito.when(googleCredential.getAccessToken()).thenReturn(TEST_ACCESS_TOKEN);
String accessToken = CredentialsUtil.getAccessToken(googleCredential);
assertNotNull(accessToken);
assertEquals(TEST_ACCESS_TOKEN, accessToken);
}

@Test(expected = NullPointerException.class)
public void testGetRobotCredentialsWithEmptyItemGroup() throws AbortException {
CredentialsUtil.getRobotCredentials(
Expand All @@ -99,17 +55,17 @@ public void testGetRobotCredentialsWithEmptyItemGroup() throws AbortException {

@Test(expected = NullPointerException.class)
public void testGetRobotCredentialsWithEmptyDomainRequirements() throws AbortException {
CredentialsUtil.getRobotCredentials(jenkins.get(), null, TEST_CREDENTIALS_ID);
CredentialsUtil.getRobotCredentials(r.jenkins, null, TEST_CREDENTIALS_ID);
}

@Test(expected = IllegalArgumentException.class)
public void testGetRobotCredentialsWithNullCredentialsId() throws AbortException {
CredentialsUtil.getRobotCredentials(jenkins.get(), ImmutableList.<DomainRequirement>of(), null);
CredentialsUtil.getRobotCredentials(r.jenkins, ImmutableList.<DomainRequirement>of(), null);
}

@Test(expected = IllegalArgumentException.class)
public void testGetRobotCredentialsWithEmptyCredentialsId() throws AbortException {
CredentialsUtil.getRobotCredentials(jenkins.get(), ImmutableList.<DomainRequirement>of(), "");
CredentialsUtil.getRobotCredentials(r.jenkins, ImmutableList.<DomainRequirement>of(), "");
}

@Test(expected = IllegalArgumentException.class)
Expand Down

0 comments on commit 0a79d1e

Please sign in to comment.