Skip to content

Commit

Permalink
JENKINS-26797 - Zombie instance created by the EC2 plugins
Browse files Browse the repository at this point in the history
* changed updateRemoteTags back to private
* introduced powermock-module-junit4 for invokeMethod
  • Loading branch information
cbek committed Mar 25, 2015
1 parent 141236b commit 6b46558
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 11 deletions.
16 changes: 16 additions & 0 deletions pom.xml
Expand Up @@ -51,7 +51,23 @@ THE SOFTWARE.
<tag>HEAD</tag>
</scm>

<properties>
<powermock.version>1.6.2</powermock.version>
</properties>

<dependencies>
<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-mockito</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/hudson/plugins/ec2/SlaveTemplate.java
Expand Up @@ -711,7 +711,7 @@ private KeyPair getKeyPair(AmazonEC2 ec2) throws IOException, AmazonClientExcept
* @param params
* @throws InterruptedException
*/
protected void updateRemoteTags(AmazonEC2 ec2, Collection<Tag> inst_tags, String catchErrorCode, String... params) throws InterruptedException {
private void updateRemoteTags(AmazonEC2 ec2, Collection<Tag> inst_tags, String catchErrorCode, String... params) throws InterruptedException {
for (int i = 0; i < 5; i++) {
try {
CreateTagsRequest tag_request = new CreateTagsRequest();
Expand Down
27 changes: 17 additions & 10 deletions src/test/java/hudson/plugins/ec2/SlaveTemplateUnitTest.java
Expand Up @@ -6,19 +6,23 @@
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 org.powermock.reflect.Whitebox;

import java.util.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Handler;
import java.util.logging.LogRecord;
import java.util.logging.Logger;

/**
* Created by christophbeckmann on 3/17/15.
*/
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;


public class SlaveTemplateUnitTest {

Logger logger;
Expand Down Expand Up @@ -68,8 +72,9 @@ protected Object readResolve() {
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());
final Object params[] = {ec2, awsTags, "InvalidInstanceRequestID.NotFound", instanceId};
Whitebox.invokeMethod(orig, "updateRemoteTags", params);
assertEquals(0, handler.getRecords().size());
}

@Test
Expand Down Expand Up @@ -104,13 +109,15 @@ protected Object readResolve() {
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());
final Object params[] = {ec2, awsTags, "InvalidSpotInstanceRequestID.NotFound", instanceId};
Whitebox.invokeMethod(orig, "updateRemoteTags", params);

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"));
assertTrue(log.contains("Instance not found - InvalidInstanceRequestID.NotFound"));
}

}
Expand Down

0 comments on commit 6b46558

Please sign in to comment.