From 4a4944131e89f0b1a7ce17d8f7e008007b6dec01 Mon Sep 17 00:00:00 2001 From: guruprasad Date: Mon, 23 Dec 2024 16:45:15 +0530 Subject: [PATCH] update some its for better test and assertions --- pom.xml | 5 +- ...ineCloud1WorkerCreatedFor2ExecutorsIT.java | 45 ++++++---- .../ComputeEngineCloudIgnoreProxyIT.java | 86 ++++++++++++------- ...CloudMultipleMatchingConfigurationsIT.java | 4 +- .../ComputeEngineCloudNonStandardJavaIT.java | 49 ++++++----- .../ComputeEngineCloudRestartPreemptedIT.java | 7 +- .../ComputeEngineCloudWorkerCreatedIT.java | 66 ++++++-------- .../ComputeEngineCloudWorkerFailedIT.java | 2 +- .../ConfigAsCodeNonStandardJavaIT.java | 2 +- .../integration/ConfigAsCodeTestIT.java | 4 +- .../computeengine/integration/ITUtil.java | 28 +----- 11 files changed, 150 insertions(+), 148 deletions(-) diff --git a/pom.xml b/pom.xml index 5c1e0450..64e67735 100644 --- a/pom.xml +++ b/pom.xml @@ -66,7 +66,6 @@ 999999-SNAPSHOT jenkinsci/${project.artifactId}-plugin 2.452.3 - 0 ${skipTests} true false @@ -379,7 +378,6 @@ **/ITUtil.java - -Djenkins.test.timeout=${jenkinsRuleTimeout} ${it.runOrder} false @@ -398,6 +396,9 @@ ${skip.surefire.tests} balanced false + + 600 + diff --git a/src/test/java/com/google/jenkins/plugins/computeengine/integration/ComputeEngineCloud1WorkerCreatedFor2ExecutorsIT.java b/src/test/java/com/google/jenkins/plugins/computeengine/integration/ComputeEngineCloud1WorkerCreatedFor2ExecutorsIT.java index 4ecbbd3f..fd17f70a 100644 --- a/src/test/java/com/google/jenkins/plugins/computeengine/integration/ComputeEngineCloud1WorkerCreatedFor2ExecutorsIT.java +++ b/src/test/java/com/google/jenkins/plugins/computeengine/integration/ComputeEngineCloud1WorkerCreatedFor2ExecutorsIT.java @@ -20,24 +20,25 @@ import static com.google.jenkins.plugins.computeengine.integration.ITUtil.NULL_TEMPLATE; import static com.google.jenkins.plugins.computeengine.integration.ITUtil.PROJECT_ID; import static com.google.jenkins.plugins.computeengine.integration.ITUtil.TEST_TIMEOUT_MULTIPLIER; -import static com.google.jenkins.plugins.computeengine.integration.ITUtil.ZONE; import static com.google.jenkins.plugins.computeengine.integration.ITUtil.getLabel; import static com.google.jenkins.plugins.computeengine.integration.ITUtil.initClient; import static com.google.jenkins.plugins.computeengine.integration.ITUtil.initCloud; import static com.google.jenkins.plugins.computeengine.integration.ITUtil.initCredentials; import static com.google.jenkins.plugins.computeengine.integration.ITUtil.instanceConfigurationBuilder; import static com.google.jenkins.plugins.computeengine.integration.ITUtil.teardownResources; +import static org.awaitility.Awaitility.await; +import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertEquals; -import com.google.api.services.compute.model.Instance; import com.google.cloud.graphite.platforms.plugin.client.ComputeClient; import com.google.common.collect.ImmutableList; import com.google.jenkins.plugins.computeengine.ComputeEngineCloud; import hudson.model.labels.LabelAtom; -import hudson.slaves.NodeProvisioner.PlannedNode; import java.io.IOException; -import java.util.Collection; +import java.time.Duration; +import java.util.ArrayList; import java.util.Map; +import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import java.util.logging.Logger; import org.junit.AfterClass; @@ -52,7 +53,7 @@ * when configured to support multiple executors if the second executor is not required. */ public class ComputeEngineCloud1WorkerCreatedFor2ExecutorsIT { - private static Logger log = Logger.getLogger(ComputeEngineCloud1WorkerCreatedFor2ExecutorsIT.class.getName()); + private static final Logger log = Logger.getLogger(ComputeEngineCloud1WorkerCreatedFor2ExecutorsIT.class.getName()); private static final String MULTIPLE_NUM_EXECUTORS = "2"; @@ -64,14 +65,13 @@ public class ComputeEngineCloud1WorkerCreatedFor2ExecutorsIT { private static ComputeClient client; private static Map label = getLabel(ComputeEngineCloud1WorkerCreatedFor2ExecutorsIT.class); - private static Collection planned; - private static Instance instance; + private static ComputeEngineCloud cloud; @BeforeClass public static void init() throws Exception { log.info("init"); initCredentials(jenkinsRule); - ComputeEngineCloud cloud = initCloud(jenkinsRule); + cloud = initCloud(jenkinsRule); client = initClient(jenkinsRule, label, log); cloud.setConfigurations(ImmutableList.of(instanceConfigurationBuilder() @@ -82,11 +82,6 @@ public static void init() throws Exception { .template(NULL_TEMPLATE) .googleLabels(label) .build())); - - planned = cloud.provision(new LabelAtom(LABEL), 2); - planned.iterator().next().future.get(); - - instance = client.getInstance(PROJECT_ID, ZONE, planned.iterator().next().displayName); } @AfterClass @@ -95,12 +90,24 @@ public static void teardown() throws IOException { } @Test - public void test1WorkerCreatedFor2ExecutorsStatusRunning() { - assertEquals("RUNNING", instance.getStatus()); - } - - @Test - public void test1WorkerCreatedFor2ExecutorsOnly1Planned() { + public void test1WorkerCreatedFor2ExecutorsStatusRunning() + throws ExecutionException, InterruptedException, IOException { + var planned = cloud.provision(new LabelAtom(LABEL), 2); assertEquals(1, planned.size()); + + planned.iterator().next().future.get(); + + // assert on jenkins side + assertEquals(1, jenkinsRule.jenkins.getNodes().size()); + assertEquals(2, jenkinsRule.jenkins.getNodes().get(0).getNumExecutors()); + + // assert on gcp side + assertEquals(1, new ArrayList<>(client.listInstancesWithLabel(PROJECT_ID, label)).size()); + await().timeout(Duration.ofMinutes(2)) + .until( + () -> client.listInstancesWithLabel(PROJECT_ID, label) + .get(0) + .getStatus(), + is("RUNNING")); } } diff --git a/src/test/java/com/google/jenkins/plugins/computeengine/integration/ComputeEngineCloudIgnoreProxyIT.java b/src/test/java/com/google/jenkins/plugins/computeengine/integration/ComputeEngineCloudIgnoreProxyIT.java index cb601ebf..04836535 100644 --- a/src/test/java/com/google/jenkins/plugins/computeengine/integration/ComputeEngineCloudIgnoreProxyIT.java +++ b/src/test/java/com/google/jenkins/plugins/computeengine/integration/ComputeEngineCloudIgnoreProxyIT.java @@ -16,7 +16,6 @@ package com.google.jenkins.plugins.computeengine.integration; -import static com.google.jenkins.plugins.computeengine.integration.ITUtil.LABEL; import static com.google.jenkins.plugins.computeengine.integration.ITUtil.NULL_TEMPLATE; import static com.google.jenkins.plugins.computeengine.integration.ITUtil.NUM_EXECUTORS; import static com.google.jenkins.plugins.computeengine.integration.ITUtil.TEST_TIMEOUT_MULTIPLIER; @@ -27,7 +26,7 @@ import static com.google.jenkins.plugins.computeengine.integration.ITUtil.initCredentials; import static com.google.jenkins.plugins.computeengine.integration.ITUtil.instanceConfigurationBuilder; import static com.google.jenkins.plugins.computeengine.integration.ITUtil.teardownResources; -import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import com.google.cloud.graphite.platforms.plugin.client.ComputeClient; @@ -38,7 +37,6 @@ import hudson.ProxyConfiguration; import hudson.model.FreeStyleBuild; import hudson.model.FreeStyleProject; -import hudson.model.Node; import hudson.model.labels.LabelAtom; import hudson.tasks.Builder; import java.io.IOException; @@ -54,6 +52,8 @@ import org.junit.Test; import org.junit.rules.Timeout; import org.jvnet.hudson.test.JenkinsRule; +import org.jvnet.hudson.test.PrefixedOutputStream; +import org.jvnet.hudson.test.TailLog; /** * Integration test suite for {@link ComputeEngineCloud}. Verifies that when the ignore proxy flag @@ -70,8 +70,6 @@ public class ComputeEngineCloudIgnoreProxyIT { private static ComputeClient client; private static Map label = getLabel(ComputeEngineCloudIgnoreProxyIT.class); private static ComputeEngineCloud cloud; - private static FreeStyleProject project; - private static Node worker; @BeforeClass public static void init() throws Exception { @@ -79,32 +77,11 @@ public static void init() throws Exception { initCredentials(jenkinsRule); cloud = initCloud(jenkinsRule); client = initClient(jenkinsRule, label, log); - - InstanceConfiguration instanceConfiguration = instanceConfigurationBuilder() - .numExecutorsStr(NUM_EXECUTORS) - .labels(LABEL) - .template(NULL_TEMPLATE) - .googleLabels(label) - .oneShot(true) - .ignoreProxy(true) - .build(); - Jenkins jenkins = jenkinsRule.getInstance(); jenkins.proxy = new ProxyConfiguration("127.0.0.1", 8080); jenkins.proxy.save(); jenkins.save(); - cloud.setConfigurations(ImmutableList.of(instanceConfiguration)); - assertTrue(cloud.getInstanceConfigurationByDescription(instanceConfiguration.getDescription()) - .isIgnoreProxy()); - - project = jenkinsRule.createFreeStyleProject(); - Builder step = execute(Commands.ECHO, "works"); - project.getBuildersList().add(step); - project.setAssignedLabel(new LabelAtom(LABEL)); - - FreeStyleBuild build = jenkinsRule.buildAndAssertSuccess(project); - worker = build.getBuiltOn(); - assertNotNull(worker); + log.info("init completed"); } @AfterClass @@ -112,18 +89,61 @@ public static void teardown() throws IOException { teardownResources(client, label, log); } + private static InstanceConfiguration getConfiguration(boolean ignoreProxy, String proxyLabel) { + return instanceConfigurationBuilder() + .description("IgnoreProxy=" + ignoreProxy) + .numExecutorsStr(NUM_EXECUTORS) + .labels(proxyLabel) + .template(NULL_TEMPLATE) + .googleLabels(label) + .oneShot(true) + .ignoreProxy(ignoreProxy) + .build(); + } + + private static FreeStyleProject createProject(String nodeLabel) throws IOException { + var project = jenkinsRule.createFreeStyleProject(nodeLabel); + Builder step = execute(Commands.ECHO, "works"); + project.getBuildersList().add(step); + project.setAssignedLabel(new LabelAtom(nodeLabel)); + return project; + } + @Test - public void testWorkerIgnoringProxy() { - ComputeEngineInstance node = (ComputeEngineInstance) worker; - assertTrue(node.isIgnoreProxy()); + public void testWorkerIgnoringProxy() throws Exception { + String identifier = "ignoreProxy"; + var instanceConfig = getConfiguration(true, identifier); + cloud.setConfigurations(ImmutableList.of(instanceConfig)); + jenkinsRule.getInstance().save(); + + assertTrue(cloud.getInstanceConfigurationByDescription(instanceConfig.getDescription()) + .isIgnoreProxy()); + + var project = createProject(identifier); + try (var tailLog = new TailLog(jenkinsRule, identifier, 1).withColor(PrefixedOutputStream.Color.MAGENTA)) { + Future build = project.scheduleBuild2(0); + ComputeEngineInstance node = (ComputeEngineInstance) build.get().getBuiltOn(); + assertTrue(node.isIgnoreProxy()); + tailLog.waitForCompletion(); + } } // Due to the proxy configured for Jenkins, the build fails because it is not able to connect to // the node through the proxy. @Test(expected = TimeoutException.class) public void testWorkerNotIgnoringProxyFails() throws Exception { - cloud.getConfigurations().get(0).setIgnoreProxy(false); - Future build = project.scheduleBuild2(0); - build.get(100, TimeUnit.SECONDS); + String identifier = "notIgnoreProxy"; + var instanceConfig = getConfiguration(false, identifier); + cloud.setConfigurations(ImmutableList.of(instanceConfig)); + jenkinsRule.getInstance().save(); + + assertFalse(cloud.getInstanceConfigurationByDescription(instanceConfig.getDescription()) + .isIgnoreProxy()); + + var project = createProject(identifier); + try (var tailLog = new TailLog(jenkinsRule, identifier, 1).withColor(PrefixedOutputStream.Color.YELLOW)) { + Future build = project.scheduleBuild2(0); + build.get(300, TimeUnit.SECONDS); + } } } diff --git a/src/test/java/com/google/jenkins/plugins/computeengine/integration/ComputeEngineCloudMultipleMatchingConfigurationsIT.java b/src/test/java/com/google/jenkins/plugins/computeengine/integration/ComputeEngineCloudMultipleMatchingConfigurationsIT.java index 5fa2159d..caac3511 100644 --- a/src/test/java/com/google/jenkins/plugins/computeengine/integration/ComputeEngineCloudMultipleMatchingConfigurationsIT.java +++ b/src/test/java/com/google/jenkins/plugins/computeengine/integration/ComputeEngineCloudMultipleMatchingConfigurationsIT.java @@ -63,7 +63,7 @@ public class ComputeEngineCloudMultipleMatchingConfigurationsIT { private static final String DESC_2 = "type_2"; @ClassRule - public static Timeout timeout = new Timeout(5 * TEST_TIMEOUT_MULTIPLIER, TimeUnit.MINUTES); + public static Timeout timeout = new Timeout(5L * TEST_TIMEOUT_MULTIPLIER, TimeUnit.MINUTES); @ClassRule public static JenkinsRule jenkinsRule = new JenkinsRule(); @@ -106,7 +106,7 @@ public static void teardown() throws IOException { } @Test - public void testMultipleLabelsProvisionedWithLabels() throws IOException { + public void testRoundRobinProvisioningWhenMultipleMatchingConfigurations() throws IOException { assertEquals(2, planned.size()); final Iterator iterator = planned.iterator(); diff --git a/src/test/java/com/google/jenkins/plugins/computeengine/integration/ComputeEngineCloudNonStandardJavaIT.java b/src/test/java/com/google/jenkins/plugins/computeengine/integration/ComputeEngineCloudNonStandardJavaIT.java index fd8cf6b3..76e73e10 100644 --- a/src/test/java/com/google/jenkins/plugins/computeengine/integration/ComputeEngineCloudNonStandardJavaIT.java +++ b/src/test/java/com/google/jenkins/plugins/computeengine/integration/ComputeEngineCloudNonStandardJavaIT.java @@ -29,26 +29,30 @@ import static com.google.jenkins.plugins.computeengine.integration.ITUtil.instanceConfigurationBuilder; import static com.google.jenkins.plugins.computeengine.integration.ITUtil.teardownResources; import static com.google.jenkins.plugins.computeengine.integration.ITUtil.windows; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertEquals; import static org.junit.Assume.assumeFalse; -import com.google.api.services.compute.model.Instance; import com.google.cloud.graphite.platforms.plugin.client.ComputeClient; import com.google.common.collect.ImmutableList; import com.google.jenkins.plugins.computeengine.ComputeEngineCloud; import com.google.jenkins.plugins.computeengine.InstanceConfiguration; -import hudson.model.labels.LabelAtom; -import hudson.slaves.NodeProvisioner.PlannedNode; import java.io.IOException; -import java.util.Collection; import java.util.Map; +import java.util.concurrent.TimeUnit; import java.util.logging.Logger; +import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition; +import org.jenkinsci.plugins.workflow.job.WorkflowJob; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.ClassRule; import org.junit.Test; +import org.junit.rules.Timeout; import org.jvnet.hudson.test.JenkinsRule; -import org.jvnet.hudson.test.recipes.WithTimeout; +import org.jvnet.hudson.test.PrefixedOutputStream; +import org.jvnet.hudson.test.TailLog; /** * Integration test suite for {@link ComputeEngineCloudNonStandardJavaIT}. Verifies that instances @@ -58,15 +62,16 @@ public class ComputeEngineCloudNonStandardJavaIT { private static final String NON_STANDARD_JAVA_PATH = "/usr/bin/non-standard-java"; - private static Logger log = Logger.getLogger(ComputeEngineCloudNonStandardJavaIT.class.getName()); + private static final Logger log = Logger.getLogger(ComputeEngineCloudNonStandardJavaIT.class.getName()); + + @ClassRule + public static Timeout timeout = new Timeout(5, TimeUnit.MINUTES); @ClassRule public static JenkinsRule jenkinsRule = new JenkinsRule(); private static ComputeClient client; private static Map label = getLabel(ComputeEngineCloudNonStandardJavaIT.class); - private static Collection planned; - private static Instance instance; @BeforeClass public static void init() throws Exception { @@ -85,12 +90,7 @@ public static void init() throws Exception { .javaExecPath(NON_STANDARD_JAVA_PATH) .googleLabels(label) .build(); - cloud.setConfigurations(ImmutableList.of(instanceConfiguration)); - planned = cloud.provision(new LabelAtom(LABEL), 1); - planned.iterator().next().future.get(); - instance = cloud.getClient() - .getInstance(PROJECT_ID, ZONE, planned.iterator().next().displayName); } @AfterClass @@ -98,15 +98,20 @@ public static void teardown() throws IOException { teardownResources(client, label, log); } - @WithTimeout(300) - @Test - public void testWorkerCreatedOnePlannedNode() { - assertEquals(1, planned.size()); - } - - @WithTimeout(300) @Test - public void testInstanceStatusRunning() { - assertEquals("RUNNING", instance.getStatus()); + public void testBuildOnNonStandardJavaAgent() throws Exception { + var p = jenkinsRule.createProject(WorkflowJob.class, "p"); + p.setDefinition(new CpsFlowDefinition("node('" + LABEL + "') { sh 'date' }", true)); + try (var tailLog = new TailLog(jenkinsRule, "p", 1).withColor(PrefixedOutputStream.Color.MAGENTA)) { + var r = jenkinsRule.buildAndAssertSuccess(p); + assertEquals(1, jenkinsRule.jenkins.getNodes().size()); + var instance = client.getInstance( + PROJECT_ID, ZONE, jenkinsRule.jenkins.getNodes().get(0).getNodeName()); + tailLog.waitForCompletion(); + assertThat( + "Build did not run on GCP agent", + JenkinsRule.getLog(r), + is(containsString("Running on " + instance.getName()))); + } } } diff --git a/src/test/java/com/google/jenkins/plugins/computeengine/integration/ComputeEngineCloudRestartPreemptedIT.java b/src/test/java/com/google/jenkins/plugins/computeengine/integration/ComputeEngineCloudRestartPreemptedIT.java index 73974688..7177ac50 100644 --- a/src/test/java/com/google/jenkins/plugins/computeengine/integration/ComputeEngineCloudRestartPreemptedIT.java +++ b/src/test/java/com/google/jenkins/plugins/computeengine/integration/ComputeEngineCloudRestartPreemptedIT.java @@ -20,6 +20,7 @@ import static com.google.jenkins.plugins.computeengine.integration.ITUtil.NULL_TEMPLATE; import static com.google.jenkins.plugins.computeengine.integration.ITUtil.NUM_EXECUTORS; import static com.google.jenkins.plugins.computeengine.integration.ITUtil.PROJECT_ID; +import static com.google.jenkins.plugins.computeengine.integration.ITUtil.TEST_TIMEOUT_MULTIPLIER; import static com.google.jenkins.plugins.computeengine.integration.ITUtil.ZONE; import static com.google.jenkins.plugins.computeengine.integration.ITUtil.execute; import static com.google.jenkins.plugins.computeengine.integration.ITUtil.getLabel; @@ -56,10 +57,10 @@ import org.junit.BeforeClass; import org.junit.ClassRule; import org.junit.Test; +import org.junit.rules.Timeout; import org.jvnet.hudson.test.JenkinsRule; import org.jvnet.hudson.test.PrefixedOutputStream; import org.jvnet.hudson.test.TailLog; -import org.jvnet.hudson.test.recipes.WithTimeout; /** * Integration test suite for {@link ComputeEngineCloud}. Verifies that the build is rescheduled and @@ -70,6 +71,9 @@ @Log public class ComputeEngineCloudRestartPreemptedIT { + @ClassRule + public static Timeout timeout = new Timeout(10L * TEST_TIMEOUT_MULTIPLIER, TimeUnit.MINUTES); + @ClassRule public static JenkinsRule jenkinsRule = new JenkinsRule(); @@ -117,7 +121,6 @@ public static void teardown() throws IOException { * It is just that in the test logs, the logs get mixed up and may seem confusing. */ @Test - @WithTimeout(1200) public void testIfNodeWasPreempted() throws Exception { Collection planned = cloud.provision(new LabelAtom(LABEL), 1); Iterator iterator = planned.iterator(); diff --git a/src/test/java/com/google/jenkins/plugins/computeengine/integration/ComputeEngineCloudWorkerCreatedIT.java b/src/test/java/com/google/jenkins/plugins/computeengine/integration/ComputeEngineCloudWorkerCreatedIT.java index ae586c48..5e63cf7d 100644 --- a/src/test/java/com/google/jenkins/plugins/computeengine/integration/ComputeEngineCloudWorkerCreatedIT.java +++ b/src/test/java/com/google/jenkins/plugins/computeengine/integration/ComputeEngineCloudWorkerCreatedIT.java @@ -34,17 +34,17 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; -import com.google.api.services.compute.model.Instance; +import com.google.api.services.compute.model.Metadata; import com.google.cloud.graphite.platforms.plugin.client.ComputeClient; import com.google.common.collect.ImmutableList; import com.google.jenkins.plugins.computeengine.ComputeEngineCloud; import com.google.jenkins.plugins.computeengine.InstanceConfiguration; +import hudson.model.Node; import hudson.model.labels.LabelAtom; -import hudson.slaves.NodeProvisioner.PlannedNode; import java.io.IOException; -import java.util.Collection; import java.util.Map; import java.util.Optional; +import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import java.util.logging.Logger; import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition; @@ -57,7 +57,6 @@ import org.jvnet.hudson.test.JenkinsRule; import org.jvnet.hudson.test.PrefixedOutputStream; import org.jvnet.hudson.test.TailLog; -import org.jvnet.hudson.test.recipes.WithTimeout; /** * Integration test suite for {@link ComputeEngineCloud}. This verifies the default case for an @@ -68,7 +67,7 @@ public class ComputeEngineCloudWorkerCreatedIT { private static Logger log = Logger.getLogger(ComputeEngineCloudWorkerCreatedIT.class.getName()); @ClassRule - public static Timeout timeout = new Timeout(5 * TEST_TIMEOUT_MULTIPLIER, TimeUnit.MINUTES); + public static Timeout timeout = new Timeout(10L * TEST_TIMEOUT_MULTIPLIER, TimeUnit.MINUTES); @ClassRule public static JenkinsRule jenkinsRule = new JenkinsRule(); @@ -77,8 +76,6 @@ public class ComputeEngineCloudWorkerCreatedIT { private static ComputeEngineCloud cloud; private static Map label = getLabel(ComputeEngineCloudWorkerCreatedIT.class); private static InstanceConfiguration instanceConfiguration; - private static Collection planned; - private static Instance instance; @BeforeClass public static void init() throws Exception { @@ -98,12 +95,6 @@ public static void init() throws Exception { .build(); cloud.setConfigurations(ImmutableList.of(instanceConfiguration)); - planned = cloud.provision(new LabelAtom(LABEL), 1); - - planned.iterator().next().future.get(); - - instance = cloud.getClient() - .getInstance(PROJECT_ID, ZONE, planned.iterator().next().displayName); } @AfterClass @@ -112,48 +103,43 @@ public static void teardown() throws IOException { } @Test - public void testWorkerCreatedOnePlannedNode() { - assertEquals(1, planned.size()); - } - - @Test - public void testWorkerCreatedNumberOfLabels() { - assertEquals(3, instance.getLabels().size()); - } + public void smokes() throws IOException, ExecutionException, InterruptedException { + var planned = cloud.provision(new LabelAtom(LABEL), 1); + planned.iterator().next().future.get(); + var instance = cloud.getClient() + .getInstance(PROJECT_ID, ZONE, planned.iterator().next().displayName); - @Test - public void testWorkerCreatedConfigLabelKeyAndValue() { + assertEquals("one instance should be provisioned", 1, planned.size()); + assertEquals("GCP VM should have 3 labels", 3, instance.getLabels().size()); assertEquals( - instanceConfiguration.getNamePrefix(), instance.getLabels().get(ComputeEngineCloud.CONFIG_LABEL_KEY)); - } - - @Test - public void testWorkerCreatedCloudIdKeyAndValue() { - assertEquals(cloud.getInstanceId(), instance.getLabels().get(ComputeEngineCloud.CLOUD_ID_LABEL_KEY)); - } - - @Test - public void testWorkerCreatedStatusRunning() { - assertEquals("RUNNING", instance.getStatus()); - } + "GCP VM name starts with the prefix configured", + instanceConfiguration.getNamePrefix(), + instance.getLabels().get(ComputeEngineCloud.CONFIG_LABEL_KEY)); + // This is an important label to be present on the VM as it helps in `CleanLostNodesWork` + assertEquals( + "GCP VM label has the cloud instanceId as value", + cloud.getInstanceId(), + instance.getLabels().get(ComputeEngineCloud.CLOUD_ID_LABEL_KEY)); + assertEquals("GCP VM is running ", "RUNNING", instance.getStatus()); - @Test - public void testGuestAttributesEnabled() { + // Verify that the guest-attributes are enabled on the VM Optional guestAttributes = instance.getMetadata().getItems().stream() .filter(item -> item.getKey().equals(InstanceConfiguration.GUEST_ATTRIBUTES_METADATA_KEY)) - .map(item -> item.getValue()) + .map(Metadata.Items::getValue) .findFirst(); assertTrue(guestAttributes.isPresent()); - assertEquals(guestAttributes.get(), "TRUE"); + assertEquals("TRUE", guestAttributes.get()); } - @WithTimeout(300) @Test public void testWorkerCanExecuteBuild() throws Exception { var p = jenkinsRule.createProject(WorkflowJob.class, "p"); p.setDefinition(new CpsFlowDefinition("node('" + LABEL + "') { sh 'date' }", true)); try (var tailLog = new TailLog(jenkinsRule, "p", 1).withColor(PrefixedOutputStream.Color.MAGENTA)) { var r = jenkinsRule.buildAndAssertSuccess(p); + assertEquals(1, jenkinsRule.jenkins.getNodes().size()); + Node node = jenkinsRule.jenkins.getNodes().get(0); + var instance = client.getInstance(PROJECT_ID, ZONE, node.getNodeName()); tailLog.waitForCompletion(); assertThat( "Build did not run on GCP agent", diff --git a/src/test/java/com/google/jenkins/plugins/computeengine/integration/ComputeEngineCloudWorkerFailedIT.java b/src/test/java/com/google/jenkins/plugins/computeengine/integration/ComputeEngineCloudWorkerFailedIT.java index 6a4833d5..d1a63af9 100644 --- a/src/test/java/com/google/jenkins/plugins/computeengine/integration/ComputeEngineCloudWorkerFailedIT.java +++ b/src/test/java/com/google/jenkins/plugins/computeengine/integration/ComputeEngineCloudWorkerFailedIT.java @@ -55,7 +55,7 @@ public class ComputeEngineCloudWorkerFailedIT { private static Logger log = Logger.getLogger(ComputeEngineCloudWorkerFailedIT.class.getName()); @ClassRule - public static Timeout timeout = new Timeout(7 * TEST_TIMEOUT_MULTIPLIER, TimeUnit.MINUTES); + public static Timeout timeout = new Timeout(7L * TEST_TIMEOUT_MULTIPLIER, TimeUnit.MINUTES); @ClassRule public static JenkinsRule jenkinsRule = new JenkinsRule(); diff --git a/src/test/java/com/google/jenkins/plugins/computeengine/integration/ConfigAsCodeNonStandardJavaIT.java b/src/test/java/com/google/jenkins/plugins/computeengine/integration/ConfigAsCodeNonStandardJavaIT.java index b11ec1af..d12c3154 100644 --- a/src/test/java/com/google/jenkins/plugins/computeengine/integration/ConfigAsCodeNonStandardJavaIT.java +++ b/src/test/java/com/google/jenkins/plugins/computeengine/integration/ConfigAsCodeNonStandardJavaIT.java @@ -44,7 +44,7 @@ public class ConfigAsCodeNonStandardJavaIT { public static JenkinsRule jenkinsRule = new JenkinsRule(); @ClassRule - public static Timeout timeout = new Timeout(5 * TEST_TIMEOUT_MULTIPLIER, TimeUnit.MINUTES); + public static Timeout timeout = new Timeout(5L * TEST_TIMEOUT_MULTIPLIER, TimeUnit.MINUTES); private static ComputeClient client; private static Map label = getLabel(ConfigAsCodeNonStandardJavaIT.class); diff --git a/src/test/java/com/google/jenkins/plugins/computeengine/integration/ConfigAsCodeTestIT.java b/src/test/java/com/google/jenkins/plugins/computeengine/integration/ConfigAsCodeTestIT.java index bccbd037..da21a917 100644 --- a/src/test/java/com/google/jenkins/plugins/computeengine/integration/ConfigAsCodeTestIT.java +++ b/src/test/java/com/google/jenkins/plugins/computeengine/integration/ConfigAsCodeTestIT.java @@ -37,11 +37,11 @@ public class ConfigAsCodeTestIT { public static JenkinsRule jenkinsRule = new JenkinsRule(); @ClassRule - public static Timeout timeout = new Timeout(10 * TEST_TIMEOUT_MULTIPLIER, TimeUnit.MINUTES); + public static Timeout timeout = new Timeout(10L * TEST_TIMEOUT_MULTIPLIER, TimeUnit.MINUTES); private static ComputeClient client; private static Map label = getLabel(ConfigAsCodeTestIT.class); - private static String DISABLE_NO_DELAY_SYSTEM_PROPERTY = + private static final String DISABLE_NO_DELAY_SYSTEM_PROPERTY = "com.google.jenkins.plugins.computeengine.disableNoDelayProvisioning"; @BeforeClass diff --git a/src/test/java/com/google/jenkins/plugins/computeengine/integration/ITUtil.java b/src/test/java/com/google/jenkins/plugins/computeengine/integration/ITUtil.java index 451ec380..24e119c4 100644 --- a/src/test/java/com/google/jenkins/plugins/computeengine/integration/ITUtil.java +++ b/src/test/java/com/google/jenkins/plugins/computeengine/integration/ITUtil.java @@ -63,7 +63,6 @@ import com.google.jenkins.plugins.computeengine.ssh.GoogleKeyPair; import com.google.jenkins.plugins.credentials.oauth.GoogleRobotPrivateKeyCredentials; import com.google.jenkins.plugins.credentials.oauth.JsonServiceAccountConfig; -import hudson.model.FreeStyleBuild; import hudson.model.Node; import hudson.plugins.powershell.PowerShell; import hudson.tasks.Builder; @@ -79,7 +78,6 @@ import jenkins.util.SystemProperties; import lombok.extern.java.Log; import org.apache.commons.lang.SystemUtils; -import org.jenkinsci.plugins.workflow.job.WorkflowRun; import org.jvnet.hudson.test.JenkinsRule; /** Common logic and constants used throughout the integration tests. */ @@ -364,6 +362,8 @@ static Map getLabel(Class testClass) { private static void deleteIntegrationInstances( boolean waitForCompletion, ComputeClient client, Map label, Logger log) throws IOException { List instances = client.listInstancesWithLabel(PROJECT_ID, label); + log.info("Cleaning up old instances; found " + instances.size() + " instances to delete, it may take a while " + + "for this deletion"); for (Instance i : instances) { safeDelete(i.getName(), waitForCompletion, client, log); } @@ -371,34 +371,14 @@ private static void deleteIntegrationInstances( private static void safeDelete(String instanceId, boolean waitForCompletion, ComputeClient client, Logger log) { try { + log.info("deleting instance: " + instanceId); Operation operation = client.terminateInstanceAsync(PROJECT_ID, ZONE, instanceId); if (waitForCompletion) { client.waitForOperationCompletion(PROJECT_ID, operation.getName(), operation.getZone(), 3 * 60 * 1000); + log.info("deletion complete"); } } catch (Exception e) { log.warning(String.format("Error deleting instance %s: %s", instanceId, e.getMessage())); } } - - public static String printLogsAndGetAgentName(Object build) throws IOException { - List logs; - if (build instanceof FreeStyleBuild) { - logs = ((FreeStyleBuild) build).getLog(1000); - } else if (build instanceof WorkflowRun) { - logs = ((WorkflowRun) build).getLog(1000); - } else { - throw new IllegalArgumentException("Unsupported build type"); - } - - String agentName = null; - for (String line : logs) { - if (line.contains("Building remotely on")) { - agentName = line.split(" ")[3]; - } else if (line.contains("Running on")) { - agentName = line.split(" ")[2]; - } - log.info(line); - } - return agentName; - } }