From 0a79d1e437e0303b6ee591b9d376e354bb23b35a Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Thu, 21 Sep 2023 02:42:06 -0700 Subject: [PATCH] Refresh plugin for August 2023 (Upgrade to a minimum core version 2.361.3 and minimum Java version 11) (#348) * Refresh plugin for August 2023 * Remove usage of Mockito to allow test to pass on Java 17 --- Jenkinsfile | 7 +- pom.xml | 18 ++-- .../k8sengine/CredentialsUtilTest.java | 86 +++++-------------- 3 files changed, 35 insertions(+), 76 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index e73a81fd..9ceab421 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -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], ]) diff --git a/pom.xml b/pom.xml index ad22eb68..019c2448 100644 --- a/pom.xml +++ b/pom.xml @@ -18,7 +18,7 @@ org.jenkins-ci.plugins plugin - 4.45 + 4.72 @@ -66,7 +66,7 @@ 0 999999-SNAPSHOT jenkinsci/google-kubernetes-engine-plugin - 2.346.1 + 2.361.4 32.1.2-jre 1.25.0 1.42.2 @@ -78,10 +78,15 @@ + + com.google.code.gson + gson + 2.10.1 + io.jenkins.tools.bom - bom-2.346.x - 1643.v1cffef51df73 + bom-2.361.x + 2102.v854b_fec19c92 pom import @@ -114,9 +119,8 @@ apache-httpcomponents-client-4-api - com.fasterxml.jackson.core - jackson-core - 2.13.3 + org.jenkins-ci.plugins + jackson2-api org.jenkins-ci.plugins.workflow diff --git a/src/test/java/com/google/jenkins/plugins/k8sengine/CredentialsUtilTest.java b/src/test/java/com/google/jenkins/plugins/k8sengine/CredentialsUtilTest.java index acb18638..12b36bf7 100644 --- a/src/test/java/com/google/jenkins/plugins/k8sengine/CredentialsUtilTest.java +++ b/src/test/java/com/google/jenkins/plugins/k8sengine/CredentialsUtilTest.java @@ -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.of(), TEST_CREDENTIALS_ID)); - } @Test(expected = AbortException.class) public void testGetRobotCredentialsInvalidCredentialsIdAbortException() throws AbortException { CredentialsUtil.getRobotCredentials( - jenkins.get(), ImmutableList.of(), TEST_INVALID_CREDENTIALS_ID); + r.jenkins, ImmutableList.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( @@ -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.of(), null); + CredentialsUtil.getRobotCredentials(r.jenkins, ImmutableList.of(), null); } @Test(expected = IllegalArgumentException.class) public void testGetRobotCredentialsWithEmptyCredentialsId() throws AbortException { - CredentialsUtil.getRobotCredentials(jenkins.get(), ImmutableList.of(), ""); + CredentialsUtil.getRobotCredentials(r.jenkins, ImmutableList.of(), ""); } @Test(expected = IllegalArgumentException.class)