Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refresh plugin for August 2023 #405

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
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.2</version>
<version>1.7</version>
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Latest version at the time of this writing.

</extension>
</extensions>
5 changes: 4 additions & 1 deletion Jenkinsfile
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
// https://github.com/jenkins-infra/pipeline-library
buildPlugin(failFast: false)
buildPlugin(useContainerAgent: true, configurations: [
[platform: 'linux', jdk: 21],
[platform: 'linux', jdk: 17],
])
64 changes: 32 additions & 32 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>4.31</version>
<version>4.72</version>
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Latest version at the time of this writing. Supports Java 11 source level.

<relativePath />
</parent>

Expand All @@ -17,13 +17,10 @@
<properties>
<revision>3.2.1</revision>
<changelist>-SNAPSHOT</changelist>
<jenkins.version>2.277.2</jenkins.version>
<java.level>8</java.level>
<jenkins.version>2.387.3</jenkins.version>
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<gitHubRepo>jenkinsci/${project.artifactId}-plugin</gitHubRepo>
<aws-sdk.version>1.12.101-300.vc09c7be9cb57</aws-sdk.version>
<aws-credentials.version>1.32</aws-credentials.version>
<mockito.version>3.12.4</mockito.version>
<powermock.version>2.0.5</powermock.version>
<aws-sdk.version>1.12.529-406.vdeff15e5817d</aws-sdk.version>
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Latest version at the time of this writing.

<aws-credentials.version>191.vcb_f183ce58b_9</aws-credentials.version>
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Latest version at the time of this writing.

</properties>

<name>EC2 Fleet Jenkins Plugin</name>
Expand Down Expand Up @@ -65,8 +62,8 @@
</developers>

<scm>
<connection>scm:git:https://github.com/${gitHubRepo}</connection>
<developerConnection>scm:git:https://github.com/${gitHubRepo}</developerConnection>
<connection>scm:git:https://github.com/${gitHubRepo}.git</connection>
<developerConnection>scm:git:git@github.com:${gitHubRepo}.git</developerConnection>
<url>https://github.com/${gitHubRepo}</url>
<tag>${scmTag}</tag>
</scm>
Expand All @@ -89,8 +86,8 @@
<dependencies>
<dependency>
<groupId>io.jenkins.tools.bom</groupId>
<artifactId>bom-2.249.x</artifactId>
<version>984.vb5eaac999a7e</version>
<artifactId>bom-2.387.x</artifactId>
<version>2357.v1043f8578392</version>
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Latest version at the time of this writing.

<scope>import</scope>
<type>pom</type>
</dependency>
Expand Down Expand Up @@ -123,15 +120,8 @@

<!-- tests -->
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>${powermock.version}</version>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand All @@ -143,17 +133,6 @@
<groupId>io.jenkins.configuration-as-code</groupId>
<artifactId>test-harness</artifactId>
<scope>test</scope>
<!--
PCT fails on maven-enforcer-plugin:enforce
test-harness:1.46 puts joda-time:2.10.2
aws-java-sdk:1.11.821 puts 2.8.1
-->
<exclusions>
<exclusion>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
</exclusion>
</exclusions>
</dependency>

</dependencies>
Expand All @@ -166,10 +145,31 @@
<extensions>true</extensions>
<configuration>
<compatibleSinceVersion>1.45</compatibleSinceVersion>
<minimumJavaVersion>8</minimumJavaVersion>
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Long deprecated.

</configuration>
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>jdk17</id>
<activation>
<jdk>[17,)</jdk>
</activation>
<properties />
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>-Xms768M -Xmx768M -XX:+HeapDumpOnOutOfMemoryError -XX:+TieredCompilation -XX:TieredStopAtLevel=1 @{jenkins.addOpens} @{jenkins.insaneHook} --add-opens java.base/java.util.concurrent=ALL-UNNAMED --add-opens java.base/java.util.concurrent.locks=ALL-UNNAMED</argLine>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</profile>
</profiles>
Comment on lines +153 to +174
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code also appears in the jira-plugin build, to allow the use of Mockito in integration tests.

</project>
6 changes: 4 additions & 2 deletions src/main/java/com/amazon/jenkins/ec2fleet/CloudNanny.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.amazon.jenkins.ec2fleet;

import com.google.common.annotations.VisibleForTesting;
import hudson.Extension;
import hudson.model.PeriodicWork;
import hudson.slaves.Cloud;
Expand Down Expand Up @@ -32,7 +33,7 @@ public long getRecurrencePeriod() {
}

/**
* <h2>Exceptions</h2>
* <p><strong>Exceptions</strong>
* This method will be executed by {@link PeriodicWork} inside {@link java.util.concurrent.ScheduledExecutorService}
* by default it stops execution if task throws exception, however {@link PeriodicWork} fix that
* by catch any exception and just log it, so we safe to throw exception here.
Expand Down Expand Up @@ -68,7 +69,8 @@ protected void doRun() {
*
* @return basic java list
*/
private static Jenkins.CloudList getClouds() {
@VisibleForTesting
static Jenkins.CloudList getClouds() {
return Jenkins.get().clouds;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.amazon.jenkins.ec2fleet;

import com.google.common.annotations.VisibleForTesting;
import hudson.Extension;
import hudson.model.PeriodicWork;
import hudson.slaves.Cloud;
Expand All @@ -23,7 +24,7 @@ public long getRecurrencePeriod() {
}

/**
* <h2>Exceptions</h2>
* <p><strong>Exceptions</strong>
* This method will be executed by {@link PeriodicWork} inside {@link java.util.concurrent.ScheduledExecutorService}
* by default it stops execution if task throws exception, however {@link PeriodicWork} fix that
* by catch any exception and just log it, so we safe to throw exception here.
Expand Down Expand Up @@ -53,7 +54,8 @@ protected void doRun() {
*
* @return widgets
*/
private static List<Widget> getWidgets() {
@VisibleForTesting
static List<Widget> getWidgets() {
Comment on lines +57 to +58
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needed for the switch from PowerMock to Mockito's mockStatic.

return Jenkins.get().getWidgets();
}

Expand All @@ -63,7 +65,8 @@ private static List<Widget> getWidgets() {
*
* @return basic java list
*/
private static List<Cloud> getClouds() {
@VisibleForTesting
static List<Cloud> getClouds() {
Comment on lines +68 to +69
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needed for the switch from PowerMock to Mockito's mockStatic.

return Jenkins.get().clouds;
}

Expand Down
18 changes: 13 additions & 5 deletions src/main/java/com/amazon/jenkins/ec2fleet/aws/RegionInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,36 @@ public enum RegionInfo {
US_EAST_2("us-east-2", "US East (Ohio)"),
US_WEST_1("us-west-1", "US West (N. California)"),
US_WEST_2("us-west-2", "US West (Oregon)"),
US_ISO_EAST_1("us-iso-east-1", "US ISO East"),
US_ISOB_EAST_1("us-isob-east-1", "US ISOB East (Ohio)"),
US_ISO_WEST_1("us-iso-west-1", "US ISO West"),
EU_WEST_1("eu-west-1", "EU (Ireland)"),
EU_WEST_2("eu-west-2", "EU (London)"),
EU_WEST_3("eu-west-3", "EU (Paris)"),
EU_CENTRAL_1("eu-central-1", "EU (Frankfurt)"),
EU_CENTRAL_2("eu-central-2", "EU (Zurich)"),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needed to upgrade to the latest AWS Java SDK Jenkins plugin.

EU_NORTH_1("eu-north-1", "EU (Stockholm)"),
EU_SOUTH_1("eu-south-1", "EU (Milan)"),
AF_SOUTH_1("af-south-1", "Africa (Cape Town)"),
EU_SOUTH_2("eu-south-2", "EU (Spain)"),
AP_EAST_1("ap-east-1", "Asia Pacific (Hong Kong)"),
AP_SOUTH_1("ap-south-1", "Asia Pacific (Mumbai)"),
AP_SOUTH_2("ap-south-2", "Asia Pacific (Hyderabad)"),
AP_SOUTHEAST_1("ap-southeast-1", "Asia Pacific (Singapore)"),
AP_SOUTHEAST_2("ap-southeast-2", "Asia Pacific (Sydney)"),
AP_SOUTHEAST_3("ap-southeast-3", "Asia Pacific (Jakarta)"),
AP_SOUTHEAST_4("ap-southeast-4", "Asia Pacific (Melbourne)"),
AP_NORTHEAST_1("ap-northeast-1", "Asia Pacific (Tokyo)"),
AP_NORTHEAST_2("ap-northeast-2", "Asia Pacific (Seoul)"),
AP_NORTHEAST_3("ap-northeast-3", "Asia Pacific (Osaka)"),

SA_EAST_1("sa-east-1", "South America (Sao Paulo)"),
CN_NORTH_1("cn-north-1", "China (Beijing)"),
CN_NORTHWEST_1("cn-northwest-1", "China (Ningxia)"),
CA_CENTRAL_1("ca-central-1", "Canada (Central)"),
ME_CENTRAL_1("me-central-1", "Middle East (UAE)"),
ME_SOUTH_1("me-south-1", "Middle East (Bahrain)"),
AP_NORTHEAST_3("ap-northeast-3", "Asia Pacific (Osaka)");
AF_SOUTH_1("af-south-1", "Africa (Cape Town)"),
US_ISO_EAST_1("us-iso-east-1", "US ISO East"),
US_ISOB_EAST_1("us-isob-east-1", "US ISOB East (Ohio)"),
US_ISO_WEST_1("us-iso-west-1", "US ISO West"),
IL_CENTRAL_1("il-central-1", "Israel (Tel Aviv)");

private final String name;
private final String description;
Expand Down
61 changes: 42 additions & 19 deletions src/test/java/com/amazon/jenkins/ec2fleet/CloudNannyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@
import com.amazon.jenkins.ec2fleet.fleet.EC2Fleets;
import hudson.slaves.Cloud;
import jenkins.model.Jenkins;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;

import java.lang.reflect.Field;
import java.util.Collections;
import java.util.Map;
import java.util.WeakHashMap;
Expand All @@ -21,19 +22,24 @@
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.*;

@RunWith(PowerMockRunner.class)
@PrepareForTest({CloudNanny.class, Jenkins.class, EC2Fleets.class})
@RunWith(MockitoJUnitRunner.class)
public class CloudNannyTest {
@Mock
private Jenkins jenkins;

@Mock
@Mock(strictness = Mock.Strictness.LENIENT)
private EC2Fleet ec2Fleet;

@Mock
private MockedStatic<CloudNanny> mockedCloudNanny;

private MockedStatic<Jenkins> mockedJenkins;

private MockedStatic<EC2Fleets> mockedEc2Fleets;

@Mock(strictness = Mock.Strictness.LENIENT)
private EC2FleetCloud cloud1;

@Mock
@Mock(strictness = Mock.Strictness.LENIENT)
private EC2FleetCloud cloud2;

private Jenkins.CloudList clouds = new Jenkins.CloudList();
Expand All @@ -53,15 +59,15 @@ public class CloudNannyTest {

@Before
public void before() throws Exception {
PowerMockito.mockStatic(CloudNanny.class);
PowerMockito.when(CloudNanny.class, "getClouds").thenReturn(clouds);
mockedCloudNanny = Mockito.mockStatic(CloudNanny.class);
mockedCloudNanny.when(CloudNanny::getClouds).thenReturn(clouds);

PowerMockito.mockStatic(Jenkins.class);
PowerMockito.when(Jenkins.get()).thenReturn(jenkins);
mockedJenkins = Mockito.mockStatic(Jenkins.class);
mockedJenkins.when(Jenkins::get).thenReturn(jenkins);

PowerMockito.mockStatic(EC2Fleets.class);
when(EC2Fleets.get(anyString())).thenReturn(ec2Fleet);
PowerMockito.when(ec2Fleet.getState(anyString(), anyString(), anyString(), anyString()))
mockedEc2Fleets = Mockito.mockStatic(EC2Fleets.class);
mockedEc2Fleets.when(() -> EC2Fleets.get(anyString())).thenReturn(ec2Fleet);
Mockito.when(ec2Fleet.getState(anyString(), anyString(), anyString(), anyString()))
.thenReturn(new FleetStateStats("", 0, FleetStateStats.State.active(),
Collections.<String>emptySet(), Collections.<String, Double>emptyMap()));

Expand All @@ -80,18 +86,35 @@ public void before() throws Exception {
recurrenceCounters.put(cloud2, recurrenceCounter2);
}

@After
public void after() {
mockedEc2Fleets.close();
mockedJenkins.close();
mockedCloudNanny.close();
}

private CloudNanny getMockCloudNannyInstance() {
CloudNanny cloudNanny = Whitebox.newInstance(CloudNanny.class);
CloudNanny cloudNanny = new CloudNanny();

// next execution should trigger running the status check.
recurrenceCounter1.set(1);
recurrenceCounter2.set(1);

Whitebox.setInternalState(cloudNanny, "recurrenceCounters", recurrenceCounters);
setInternalState(cloudNanny, "recurrenceCounters", recurrenceCounters);

return cloudNanny;
}

private static void setInternalState(Object obj, String fieldName, Object newValue) {
try {
Field field = obj.getClass().getDeclaredField(fieldName);
field.setAccessible(true);
field.set(obj, newValue);
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new AssertionError(e);
}
}

@Test
public void shouldDoNothingIfNoCloudsAndWidgets() {
getMockCloudNannyInstance().doRun();
Expand All @@ -115,7 +138,7 @@ public void shouldIgnoreNonEC2FleetClouds() {
getMockCloudNannyInstance().doRun();

verify(cloud1).update();
verifyZeroInteractions(nonEc2FleetCloud);
verifyNoInteractions(nonEc2FleetCloud);
}

@Test
Expand Down
Loading
Loading