Skip to content

Commit

Permalink
update some its for better test and assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
gbhat618 committed Dec 23, 2024
1 parent 05b4e95 commit 4a49441
Show file tree
Hide file tree
Showing 11 changed files with 150 additions and 148 deletions.
5 changes: 3 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
<changelist>999999-SNAPSHOT</changelist>
<gitHubRepo>jenkinsci/${project.artifactId}-plugin</gitHubRepo>
<jenkins.version>2.452.3</jenkins.version>
<jenkinsRuleTimeout>0</jenkinsRuleTimeout>
<skip.surefire.tests>${skipTests}</skip.surefire.tests>
<skipITs>true</skipITs>
<it.windows>false</it.windows>
Expand Down Expand Up @@ -379,7 +378,6 @@
<excludes>
<exclude>**/ITUtil.java</exclude>
</excludes>
<argLine>-Djenkins.test.timeout=${jenkinsRuleTimeout}</argLine>
<runOrder>${it.runOrder}</runOrder>
<reuseForks>false</reuseForks>
</configuration>
Expand All @@ -398,6 +396,9 @@
<skipTests>${skip.surefire.tests}</skipTests>
<runOrder>balanced</runOrder>
<reuseForks>false</reuseForks>
<systemPropertyVariables>
<jenkins.test.timeout>600</jenkins.test.timeout>
</systemPropertyVariables>
</configuration>
</plugin>
<plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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";

Expand All @@ -64,14 +65,13 @@ public class ComputeEngineCloud1WorkerCreatedFor2ExecutorsIT {

private static ComputeClient client;
private static Map<String, String> label = getLabel(ComputeEngineCloud1WorkerCreatedFor2ExecutorsIT.class);
private static Collection<PlannedNode> 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()
Expand All @@ -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
Expand All @@ -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"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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
Expand All @@ -70,60 +70,80 @@ public class ComputeEngineCloudIgnoreProxyIT {
private static ComputeClient client;
private static Map<String, String> label = getLabel(ComputeEngineCloudIgnoreProxyIT.class);
private static ComputeEngineCloud cloud;
private static FreeStyleProject project;
private static Node worker;

@BeforeClass
public static void init() throws Exception {
log.info("init");
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
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<FreeStyleBuild> 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<FreeStyleBuild> 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<FreeStyleBuild> build = project.scheduleBuild2(0);
build.get(300, TimeUnit.SECONDS);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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<PlannedNode> iterator = planned.iterator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<String, String> label = getLabel(ComputeEngineCloudNonStandardJavaIT.class);
private static Collection<PlannedNode> planned;
private static Instance instance;

@BeforeClass
public static void init() throws Exception {
Expand All @@ -85,28 +90,28 @@ 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
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())));
}
}
}
Loading

0 comments on commit 4a49441

Please sign in to comment.