From 240796a47812bc7d6a35512c05fb8cd8d1976346 Mon Sep 17 00:00:00 2001 From: Mohamed Mansour Date: Tue, 7 Jan 2025 13:21:58 -0800 Subject: [PATCH] Log EC2 errors while calling RunInstances for reserved capacity --- .../hudson/plugins/ec2/SlaveTemplate.java | 7 ++- .../hudson/plugins/ec2/SlaveTemplateTest.java | 54 +++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/src/main/java/hudson/plugins/ec2/SlaveTemplate.java b/src/main/java/hudson/plugins/ec2/SlaveTemplate.java index adaa017f9..59d984060 100644 --- a/src/main/java/hudson/plugins/ec2/SlaveTemplate.java +++ b/src/main/java/hudson/plugins/ec2/SlaveTemplate.java @@ -2029,7 +2029,12 @@ private List provisionOndemand( } } } else { - newInstances = ec2.runInstances(riRequest).getReservation().getInstances(); + try { + newInstances = ec2.runInstances(riRequest).getReservation().getInstances(); + } catch (AmazonEC2Exception e) { + logProvisionInfo("Error while trying to run instances for reserved capacity: " + e.getMessage()); + throw e; + } } // Have to create a new instance diff --git a/src/test/java/hudson/plugins/ec2/SlaveTemplateTest.java b/src/test/java/hudson/plugins/ec2/SlaveTemplateTest.java index 9ec683015..f4fa96673 100644 --- a/src/test/java/hudson/plugins/ec2/SlaveTemplateTest.java +++ b/src/test/java/hudson/plugins/ec2/SlaveTemplateTest.java @@ -1440,6 +1440,60 @@ public void provisionOnDemandSetsMetadataDefaultOptions() throws Exception { assertEquals(metadataOptionsRequest.getHttpPutResponseHopLimit(), Integer.valueOf(1)); } + @Test(expected = AmazonEC2Exception.class) + public void provisionOnDemandSetsMetadataDefaultOptionsWithEC2Exception() throws Exception { + SlaveTemplate template = new SlaveTemplate( + TEST_AMI, + TEST_ZONE, + TEST_SPOT_CFG, + TEST_SEC_GROUPS, + TEST_REMOTE_FS, + TEST_INSTANCE_TYPE, + TEST_EBSO, + TEST_LABEL, + Node.Mode.NORMAL, + "", + "bar", + "bbb", + "aaa", + "10", + "fff", + null, + "java", + "-Xmx1g", + false, + "subnet 456", + null, + null, + 0, + 0, + null, + "", + true, + false, + "", + false, + "", + true, + false, + false, + ConnectionStrategy.PUBLIC_IP, + -1, + Collections.emptyList(), + null, + Tenancy.Default, + EbsEncryptRootVolume.DEFAULT, + null, + true, + null, + true); + + AmazonEC2 mockedEC2 = setupTestForProvisioning(template); + when(mockedEC2.runInstances(any(RunInstancesRequest.class))).thenThrow(new AmazonEC2Exception("InsufficientInstanceCapacity")); + + template.provision(2, EnumSet.noneOf(ProvisionOptions.class)); + } + private HtmlForm getConfigForm(EC2Cloud ac) throws IOException, SAXException { return r.createWebClient().goTo(ac.getUrl() + "configure").getFormByName("config"); }