From 6bfebb09208d8333001946cc4d3296aa1d9d1de9 Mon Sep 17 00:00:00 2001 From: Timothy Bingaman Date: Tue, 5 Jul 2016 14:47:39 -0400 Subject: [PATCH] Add tests highlighting problem with retention logic --- .../plugins/ec2/EC2RetentionStrategyTest.java | 114 ++++++++++++++++++ 1 file changed, 114 insertions(+) diff --git a/src/test/java/hudson/plugins/ec2/EC2RetentionStrategyTest.java b/src/test/java/hudson/plugins/ec2/EC2RetentionStrategyTest.java index 770f81d64..5102e849e 100644 --- a/src/test/java/hudson/plugins/ec2/EC2RetentionStrategyTest.java +++ b/src/test/java/hudson/plugins/ec2/EC2RetentionStrategyTest.java @@ -4,21 +4,33 @@ import com.amazonaws.services.ec2.model.InstanceType; import hudson.model.Executor; import hudson.model.Node; +import hudson.model.Label; +import hudson.model.Queue; +import hudson.model.ResourceList; +import hudson.model.Queue.Executable; +import hudson.model.Queue.Task; +import hudson.model.queue.CauseOfBlockage; import hudson.plugins.ec2.util.AmazonEC2FactoryMockImpl; import hudson.plugins.ec2.util.MinimumInstanceChecker; import hudson.plugins.ec2.util.MinimumNumberOfInstancesTimeRangeConfig; import hudson.plugins.ec2.util.PrivateKeyHelper; import hudson.plugins.ec2.util.SSHCredentialHelper; +import hudson.security.ACL; +import hudson.security.AccessControlled; +import hudson.security.Permission;import hudson.model.Executor; import hudson.slaves.NodeProperty; import hudson.slaves.OfflineCause; import jenkins.util.NonLocalizable; +import jenkins.model.Jenkins; import net.sf.json.JSONObject; +import org.acegisecurity.Authentication; import org.junit.Rule; import org.junit.Test; import org.jvnet.hudson.test.JenkinsRule; import org.testcontainers.shaded.org.bouncycastle.jce.provider.BouncyCastleProvider; import org.jvnet.hudson.test.LoggerRule; +import javax.annotation.Nonnull; import java.security.Security; import java.time.Clock; import java.time.Duration; @@ -26,6 +38,7 @@ import java.time.LocalDateTime; import java.time.Month; import java.time.ZoneId; +import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -40,6 +53,7 @@ import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.hasItem; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; public class EC2RetentionStrategyTest { @@ -80,6 +94,106 @@ public void testOnBillingHourRetention() throws Exception { } } + @Test + public void testRetentionWhenQueueHasWaitingItemForThisNode() throws Exception { + EC2RetentionStrategy rs = new EC2RetentionStrategy("-2"); + EC2Computer computer = computerWithIdleTime(59, 0); + final Label selfLabel = computer.getNode().getSelfLabel(); + final Queue queue = Jenkins.get().getQueue(); + final Task task = taskForLabel(selfLabel, false); + queue.schedule(task, 500); + checkRetentionStrategy(rs, computer); + assertFalse("Expected computer to be left running", idleTimeoutCalled.get()); + queue.cancel(task); + EC2RetentionStrategy rs2 = new EC2RetentionStrategy("-2"); + checkRetentionStrategy(rs2, computer); + assertTrue("Expected computer to be idled", idleTimeoutCalled.get()); + } + + @Test + public void testRetentionWhenQueueHasBlockedItemForThisNode() throws Exception { + EC2RetentionStrategy rs = new EC2RetentionStrategy("-2"); + EC2Computer computer = computerWithIdleTime(59, 0); + final Label selfLabel = computer.getNode().getSelfLabel(); + final Queue queue = Jenkins.get().getQueue(); + final Task task = taskForLabel(selfLabel, true); + queue.schedule(task, 0); + checkRetentionStrategy(rs, computer); + assertFalse("Expected computer to be left running", idleTimeoutCalled.get()); + queue.cancel(task); + EC2RetentionStrategy rs2 = new EC2RetentionStrategy("-2"); + checkRetentionStrategy(rs2, computer); + assertTrue("Expected computer to be idled", idleTimeoutCalled.get()); + } + + private interface AccessControlledTask extends Queue.Task, AccessControlled { + } + + private Queue.Task taskForLabel(final Label label, boolean blocked) { + final CauseOfBlockage cob = blocked ? new CauseOfBlockage() { + @Override + public String getShortDescription() { + return "Blocked"; + } + } : null; + return new AccessControlledTask() { + @Nonnull + public ACL getACL() { + return new ACL() { + public boolean hasPermission(@Nonnull Authentication a, @Nonnull Permission permission) { + return true; + } + }; + } + public ResourceList getResourceList() { + return null; + } + + public Node getLastBuiltOn() { + return null; + } + + public long getEstimatedDuration() { + return -1; + } + + public Label getAssignedLabel() { + return label; + } + + public Executable createExecutable() { + return null; + } + + public String getDisplayName() { + return null; + } + + @Override + public CauseOfBlockage getCauseOfBlockage() { + return cob; + } + + public boolean hasAbortPermission() { + return false; + } + + public String getUrl() { + return null; + } + + public String getName() { + return null; + } + + public String getFullDisplayName() { + return null; + } + + public void checkAbortPermission() { + } + }; + } private EC2Computer computerWithIdleTime(final int minutes, final int seconds) throws Exception { return computerWithIdleTime(minutes, seconds, false, null); }