Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JENKINS-26797 - Zombie instance created by the EC2 plugins
* fixing error code for provisionOndemand - InvalidInstanceID.NotFound * refactoring updateRemoteTags - no code duplication * no exception throwing if remote tagging fails - only logging ** otherwise we got zombie instances which are not shown * adding unit testing
- Loading branch information
Showing
3 changed files
with
170 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 139 additions & 0 deletions
139
src/test/java/hudson/plugins/ec2/SlaveTemplateUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
package hudson.plugins.ec2; | ||
|
||
import com.amazonaws.AmazonServiceException; | ||
import com.amazonaws.services.ec2.AmazonEC2; | ||
import com.amazonaws.services.ec2.AmazonEC2Client; | ||
import com.amazonaws.services.ec2.model.InstanceType; | ||
import com.amazonaws.services.ec2.model.Tag; | ||
import hudson.model.Node; | ||
import junit.framework.Assert; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import java.util.*; | ||
import java.util.logging.Handler; | ||
import java.util.logging.LogRecord; | ||
import java.util.logging.Logger; | ||
|
||
/** | ||
* Created by christophbeckmann on 3/17/15. | ||
*/ | ||
public class SlaveTemplateUnitTest { | ||
|
||
Logger logger; | ||
TestHandler handler; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
AmazonEC2Cloud.testMode = true; | ||
|
||
handler = new TestHandler(); | ||
logger = Logger.getLogger(SlaveTemplate.class.getName()); | ||
logger.addHandler(handler); | ||
} | ||
|
||
@After | ||
public void tearDown() throws Exception { | ||
AmazonEC2Cloud.testMode = false; | ||
} | ||
|
||
@Test | ||
public void testUpdateRemoteTags() throws Exception { | ||
AmazonEC2 ec2 = new AmazonEC2Client() { | ||
@Override | ||
public void createTags(com.amazonaws.services.ec2.model.CreateTagsRequest createTagsRequest) { | ||
|
||
} | ||
}; | ||
|
||
String ami = "ami1"; | ||
String description = "foo ami"; | ||
|
||
EC2Tag tag1 = new EC2Tag("name1", "value1"); | ||
EC2Tag tag2 = new EC2Tag("name2", "value2"); | ||
List<EC2Tag> tags = new ArrayList<EC2Tag>(); | ||
tags.add(tag1); | ||
tags.add(tag2); | ||
String instanceId = "123"; | ||
|
||
SlaveTemplate orig = new SlaveTemplate(ami, EC2AbstractSlave.TEST_ZONE, null, "default", "foo", InstanceType.M1Large, "ttt", Node.Mode.NORMAL, description, "bar", "bbb", "aaa", "10", "fff", null, "-Xmx1g", false, "subnet 456", tags, null, false, null, "", true, false, "", false, "") { | ||
@Override | ||
protected Object readResolve() { | ||
return null; | ||
} | ||
}; | ||
|
||
ArrayList<Tag> awsTags = new ArrayList<Tag>(); | ||
awsTags.add(new Tag(EC2Tag.TAG_NAME_JENKINS_SLAVE_TYPE, "value1")); | ||
awsTags.add(new Tag(EC2Tag.TAG_NAME_JENKINS_SLAVE_TYPE, "value2")); | ||
|
||
orig.updateRemoteTags(ec2, awsTags, "InvalidInstanceRequestID.NotFound", instanceId); | ||
Assert.assertEquals(0, handler.getRecords().size()); | ||
} | ||
|
||
@Test | ||
public void testUpdateRemoteTagsInstanceNotFound() throws Exception { | ||
AmazonEC2 ec2 = new AmazonEC2Client() { | ||
@Override | ||
public void createTags(com.amazonaws.services.ec2.model.CreateTagsRequest createTagsRequest) { | ||
AmazonServiceException e = new AmazonServiceException("Instance not found - InvalidInstanceRequestID.NotFound"); | ||
e.setErrorCode("InvalidInstanceRequestID.NotFound"); | ||
throw e; | ||
} | ||
}; | ||
|
||
String ami = "ami1"; | ||
String description = "foo ami"; | ||
|
||
EC2Tag tag1 = new EC2Tag("name1", "value1"); | ||
EC2Tag tag2 = new EC2Tag("name2", "value2"); | ||
List<EC2Tag> tags = new ArrayList<EC2Tag>(); | ||
tags.add(tag1); | ||
tags.add(tag2); | ||
String instanceId = "123"; | ||
|
||
SlaveTemplate orig = new SlaveTemplate(ami, EC2AbstractSlave.TEST_ZONE, null, "default", "foo", InstanceType.M1Large, "ttt", Node.Mode.NORMAL, description, "bar", "bbb", "aaa", "10", "fff", null, "-Xmx1g", false, "subnet 456", tags, null, false, null, "", true, false, "", false, "") { | ||
@Override | ||
protected Object readResolve() { | ||
return null; | ||
} | ||
}; | ||
|
||
ArrayList<Tag> awsTags = new ArrayList<Tag>(); | ||
awsTags.add(new Tag(EC2Tag.TAG_NAME_JENKINS_SLAVE_TYPE, "value1")); | ||
awsTags.add(new Tag(EC2Tag.TAG_NAME_JENKINS_SLAVE_TYPE, "value2")); | ||
|
||
orig.updateRemoteTags(ec2, awsTags, "InvalidSpotInstanceRequestID.NotFound", instanceId); | ||
Assert.assertEquals(5, handler.getRecords().size()); | ||
|
||
Iterator<LogRecord> logs = handler.getRecords().iterator(); | ||
while (logs.hasNext()) { | ||
String log = logs.next().getMessage(); | ||
Assert.assertTrue(log.contains("Instance not found - InvalidInstanceRequestID.NotFound")); | ||
} | ||
|
||
} | ||
} | ||
|
||
class TestHandler extends Handler { | ||
private List<LogRecord> records = new LinkedList<LogRecord>(); | ||
|
||
@Override | ||
public void close() throws SecurityException { | ||
} | ||
|
||
@Override | ||
public void flush() { | ||
} | ||
|
||
@Override | ||
public void publish(LogRecord record) { | ||
records.add(record); | ||
} | ||
|
||
public List<LogRecord> getRecords() { | ||
return records; | ||
} | ||
} | ||
|