Skip to content

Commit

Permalink
Fix bug when stats is null for termination (#247)
Browse files Browse the repository at this point in the history
  • Loading branch information
imuqtadir authored Jan 11, 2021
1 parent 05d0cd6 commit cd49256
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/main/java/com/amazon/jenkins/ec2fleet/EC2FleetCloud.java
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,9 @@ public EC2FleetCloud(final String name,
this.noDelayProvision = noDelayProvision;

if (StringUtils.isNotEmpty(oldId)) {
id.setValue(oldId);
this.stats = EC2Fleets.get(fleet).getState(
getAwsCredentialsId(), region, endpoint, getFleet());
// existent cloud was modified, let's re-assign all dependencies of old cloud instance
// to new one
EC2FleetCloudAwareUtils.reassign(oldId, this);
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/amazon/jenkins/ec2fleet/LazyUuid.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@ public synchronized String getValue() {
return value;
}

public synchronized void setValue(String value) {
this.value = value;
}
}
26 changes: 26 additions & 0 deletions src/test/java/com/amazon/jenkins/ec2fleet/UiIntegrationTest.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
package com.amazon.jenkins.ec2fleet;

import com.amazon.jenkins.ec2fleet.fleet.EC2Fleet;
import com.amazon.jenkins.ec2fleet.fleet.EC2Fleets;
import com.amazonaws.services.ec2.AmazonEC2;

import com.gargoylesoftware.htmlunit.html.DomElement;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlFormUtil;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
import com.google.common.collect.ImmutableSet;
import hudson.PluginWrapper;
import hudson.model.Node;
import hudson.slaves.Cloud;
import hudson.slaves.NodeProperty;
import org.apache.commons.lang.StringUtils;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.BuildWatcher;
import org.jvnet.hudson.test.JenkinsRule;
import org.mockito.Mockito;
import org.xml.sax.SAXException;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static org.junit.Assert.assertEquals;
Expand All @@ -27,6 +35,11 @@
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.nullable;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;

/**
* Detailed guides https://jenkins.io/doc/developer/testing/
Expand All @@ -40,6 +53,19 @@ public class UiIntegrationTest {
@ClassRule
public static BuildWatcher bw = new BuildWatcher();

@Before
public void before() {
final EC2Fleet ec2Fleet = mock(EC2Fleet.class);
EC2Fleets.setGet(ec2Fleet);
final EC2Api ec2Api = spy(EC2Api.class);
Registry.setEc2Api(ec2Api);
final AmazonEC2 amazonEC2 = mock(AmazonEC2.class);

when(ec2Fleet.getState(anyString(), anyString(), nullable(String.class), anyString()))
.thenReturn(new FleetStateStats("", 2, FleetStateStats.State.active(), ImmutableSet.of("i-1", "i-2"), Collections.emptyMap()));
when(ec2Api.connect(anyString(), anyString(), Mockito.nullable(String.class))).thenReturn(amazonEC2);
}

@Test
public void shouldFindThePluginByShortName() {
PluginWrapper wrapper = j.getPluginManager().getPlugin("ec2-fleet");
Expand Down

0 comments on commit cd49256

Please sign in to comment.