Skip to content

Commit

Permalink
Use BuildWatcher instead of TailLog for simple
Browse files Browse the repository at this point in the history
  • Loading branch information
gbhat618 committed Jan 2, 2025
1 parent 12658e1 commit 35bd816
Show file tree
Hide file tree
Showing 14 changed files with 92 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
public class ComputeClientIT {
private static final Logger log = Logger.getLogger(ComputeClientIT.class.getName());

private static Map<String, String> label = getLabel(ComputeClientIT.class);
private static final Map<String, String> label = getLabel(ComputeClientIT.class);
private static ComputeClient client;

@ClassRule
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
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;
Expand All @@ -31,7 +30,6 @@
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;
Expand Down Expand Up @@ -61,15 +59,13 @@ public class ComputeEngineCloud1WorkerCreatedFor2ExecutorsIT {
private static final String MULTIPLE_NUM_EXECUTORS = "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();

private static ComputeClient client;
private static Map<String, String> label = getLabel(ComputeEngineCloud1WorkerCreatedFor2ExecutorsIT.class);
private static Collection<PlannedNode> planned;
private static Instance instance;
private static final Map<String, String> label = getLabel(ComputeEngineCloud1WorkerCreatedFor2ExecutorsIT.class);

@BeforeClass
public static void init() throws Exception {
Expand All @@ -87,10 +83,8 @@ public static void init() throws Exception {
.googleLabels(label)
.build()));

planned = cloud.provision(new LabelAtom(LABEL), 2);
Collection<PlannedNode> planned = cloud.provision(new LabelAtom(LABEL), 2);
planned.iterator().next().future.get();

instance = client.getInstance(PROJECT_ID, ZONE, planned.iterator().next().displayName);
}

@AfterClass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import hudson.ProxyConfiguration;
import hudson.model.FreeStyleBuild;
import hudson.model.FreeStyleProject;
import hudson.model.Result;
import hudson.model.labels.LabelAtom;
import hudson.tasks.Builder;
import java.io.IOException;
Expand All @@ -51,24 +52,27 @@
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.rules.Timeout;
import org.jvnet.hudson.test.BuildWatcher;
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
* is set to true, connections to GCE VMs bypass the proxy, and when set to false the proxy is used.
*/
@Log
public class ComputeEngineCloudIgnoreProxyIT {

@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);

@ClassRule
public static JenkinsRule jenkinsRule = new JenkinsRule();

@ClassRule
public static BuildWatcher bw = new BuildWatcher();

private static ComputeClient client;
private static Map<String, String> label = getLabel(ComputeEngineCloudIgnoreProxyIT.class);
private static final Map<String, String> label = getLabel(ComputeEngineCloudIgnoreProxyIT.class);
private static ComputeEngineCloud cloud;

@BeforeClass
Expand Down Expand Up @@ -120,12 +124,10 @@ public void testWorkerIgnoringProxy() throws Exception {
.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();
}
Future<FreeStyleBuild> build = project.scheduleBuild2(0);
ComputeEngineInstance node = (ComputeEngineInstance) build.get().getBuiltOn();
assertTrue(node.isIgnoreProxy());
jenkinsRule.assertBuildStatus(Result.SUCCESS, build.get());
}

// Due to the proxy configured for Jenkins, the build fails because it is not able to connect to
Expand All @@ -141,9 +143,7 @@ public void testWorkerNotIgnoringProxyFails() throws Exception {
.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);
}
Future<FreeStyleBuild> build = project.scheduleBuild2(0);
build.get(300, TimeUnit.SECONDS);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,18 @@
* with multiple Jenkins labels and that these labels are properly provisioned.
*/
public class ComputeEngineCloudMultipleLabelsIT {
private static Logger log = Logger.getLogger(ComputeEngineCloudMultipleLabelsIT.class.getName());
private static final Logger log = Logger.getLogger(ComputeEngineCloudMultipleLabelsIT.class.getName());

private static final String MULTIPLE_LABEL = "integration test";

@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();

private static ComputeClient client;
private static Map<String, String> label = getLabel(ComputeEngineCloudMultipleLabelsIT.class);
private static final Map<String, String> label = getLabel(ComputeEngineCloudMultipleLabelsIT.class);
private static String name;

@BeforeClass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@
* with multiple matching {@link InstanceConfiguration} and that these instances are provisioned in round-robin fashion.
*/
public class ComputeEngineCloudMultipleMatchingConfigurationsIT {
private static Logger log = Logger.getLogger(ComputeEngineCloudMultipleMatchingConfigurationsIT.class.getName());
private static final Logger log =
Logger.getLogger(ComputeEngineCloudMultipleMatchingConfigurationsIT.class.getName());

private static final String DESC_1 = "type_1";
private static final String DESC_2 = "type_2";
Expand All @@ -69,7 +70,7 @@ public class ComputeEngineCloudMultipleMatchingConfigurationsIT {
public static JenkinsRule jenkinsRule = new JenkinsRule();

private static ComputeClient client;
private static Map<String, String> label = getLabel(ComputeEngineCloudMultipleMatchingConfigurationsIT.class);
private static final Map<String, String> label = getLabel(ComputeEngineCloudMultipleMatchingConfigurationsIT.class);
private static Collection<PlannedNode> planned;

@BeforeClass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.rules.Timeout;
import org.jvnet.hudson.test.BuildWatcher;
import org.jvnet.hudson.test.JenkinsRule;

/**
Expand All @@ -62,13 +63,16 @@ public class ComputeEngineCloudNoSnapshotCreatedIT {
private static final Logger log = Logger.getLogger(ComputeEngineCloudNoSnapshotCreatedIT.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();

@ClassRule
public static BuildWatcher bw = new BuildWatcher();

private static ComputeClient client;
private static Map<String, String> label = getLabel(ComputeEngineCloudNoSnapshotCreatedIT.class);
private static final Map<String, String> label = getLabel(ComputeEngineCloudNoSnapshotCreatedIT.class);
private static String name;

@BeforeClass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,8 @@
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.rules.Timeout;
import org.jvnet.hudson.test.BuildWatcher;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.PrefixedOutputStream;
import org.jvnet.hudson.test.TailLog;

/**
* Integration test suite for {@link ComputeEngineCloudNonStandardJavaIT}. Verifies that instances
Expand All @@ -70,6 +69,9 @@ public class ComputeEngineCloudNonStandardJavaIT {
@ClassRule
public static JenkinsRule jenkinsRule = new JenkinsRule();

@ClassRule
public static BuildWatcher bw = new BuildWatcher();

private static ComputeClient client;
private static final Map<String, String> label = getLabel(ComputeEngineCloudNonStandardJavaIT.class);

Expand Down Expand Up @@ -102,16 +104,14 @@ public static void teardown() throws IOException {
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())));
}
var r = jenkinsRule.buildAndAssertSuccess(p);
assertEquals(1, jenkinsRule.jenkins.getNodes().size());
var instance = client.getInstance(
PROJECT_ID, ZONE, jenkinsRule.jenkins.getNodes().get(0).getNodeName());
assertThat(
"Build did not run on GCP agent",
JenkinsRule.getLog(r),
is(containsString("Running on " + instance.getName())));
jenkinsRule.waitUntilNoActivity();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.rules.Timeout;
import org.jvnet.hudson.test.BuildWatcher;
import org.jvnet.hudson.test.JenkinsRule;

/**
Expand All @@ -64,13 +65,16 @@ public class ComputeEngineCloudOneShotInstanceIT {
private static final int QUIET_PERIOD_SECS = 30;

@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);

@ClassRule
public static JenkinsRule jenkinsRule = new JenkinsRule();

@ClassRule
public static BuildWatcher bw = new BuildWatcher();

private static ComputeClient client;
private static Map<String, String> label = getLabel(ComputeEngineCloudOneShotInstanceIT.class);
private static final Map<String, String> label = getLabel(ComputeEngineCloudOneShotInstanceIT.class);
private static FreeStyleBuild build;
private static String nodeName;
private static Future<FreeStyleBuild> otherBuildFuture;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,8 @@
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.rules.Timeout;
import org.jvnet.hudson.test.BuildWatcher;
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 the build is rescheduled and
Expand All @@ -77,8 +76,11 @@ public class ComputeEngineCloudRestartPreemptedIT {
@ClassRule
public static JenkinsRule jenkinsRule = new JenkinsRule();

@ClassRule
public static BuildWatcher bw = new BuildWatcher();

private static ComputeClient client;
private static Map<String, String> label = getLabel(ComputeEngineCloudRestartPreemptedIT.class);
private static final Map<String, String> label = getLabel(ComputeEngineCloudRestartPreemptedIT.class);
private static ComputeEngineCloud cloud;

@BeforeClass
Expand Down Expand Up @@ -138,28 +140,21 @@ public void testIfNodeWasPreempted() throws Exception {
project.setAssignedLabel(new LabelAtom(LABEL));
FreeStyleBuild freeStyleBuild;
// build1 that gets failed due to preemption
try (var tailLog = new TailLog(jenkinsRule, "p", 1).withColor(PrefixedOutputStream.Color.MAGENTA)) {
QueueTaskFuture<FreeStyleBuild> taskFuture = project.scheduleBuild2(0);

Awaitility.await().timeout(7, TimeUnit.MINUTES).until(() -> computer.getLog()
.contains("listening to metadata for preemption event"));
QueueTaskFuture<FreeStyleBuild> taskFuture = project.scheduleBuild2(0);
Awaitility.await().timeout(7, TimeUnit.MINUTES).until(() -> computer.getLog()
.contains("listening to metadata for preemption event"));

client.simulateMaintenanceEvent(PROJECT_ID, ZONE, name);
Awaitility.await().timeout(8, TimeUnit.MINUTES).until(computer::getPreempted);
client.simulateMaintenanceEvent(PROJECT_ID, ZONE, name);
Awaitility.await().timeout(8, TimeUnit.MINUTES).until(computer::getPreempted);

freeStyleBuild = taskFuture.get();
assertEquals(FAILURE, freeStyleBuild.getResult());
tailLog.waitForCompletion();
}
freeStyleBuild = taskFuture.get();
assertEquals(FAILURE, freeStyleBuild.getResult());

Awaitility.await().timeout(5, TimeUnit.MINUTES).until(() -> freeStyleBuild.getNextBuild() != null);

// build2 gets automatically scheduled and succeeds
try (var tailLog = new TailLog(jenkinsRule, "p", 2).withColor(PrefixedOutputStream.Color.YELLOW)) {
FreeStyleBuild nextBuild = freeStyleBuild.getNextBuild();
Awaitility.await().timeout(5, TimeUnit.MINUTES).until(() -> nextBuild.getResult() != null);
assertEquals(SUCCESS, nextBuild.getResult());
tailLog.waitForCompletion();
}
FreeStyleBuild nextBuild = freeStyleBuild.getNextBuild();
Awaitility.await().timeout(5, TimeUnit.MINUTES).until(() -> nextBuild.getResult() != null);
assertEquals(SUCCESS, nextBuild.getResult());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.rules.Timeout;
import org.jvnet.hudson.test.BuildWatcher;
import org.jvnet.hudson.test.JenkinsRule;

/**
Expand All @@ -70,8 +71,11 @@ public class ComputeEngineCloudSnapshotCreatedIT {
@ClassRule
public static JenkinsRule jenkinsRule = new JenkinsRule();

@ClassRule
public static BuildWatcher bw = new BuildWatcher();

private static ComputeClient client;
private static Map<String, String> label = getLabel(ComputeEngineCloudSnapshotCreatedIT.class);
private static final Map<String, String> label = getLabel(ComputeEngineCloudSnapshotCreatedIT.class);
private static Snapshot createdSnapshot = null;

@BeforeClass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,18 @@
* still present on the created instance.
*/
public class ComputeEngineCloudTemplateNoGoogleLabelsIT {
private static Logger log = Logger.getLogger(ComputeEngineCloudTemplateNoGoogleLabelsIT.class.getName());
private static final Logger log = Logger.getLogger(ComputeEngineCloudTemplateNoGoogleLabelsIT.class.getName());

private static final String TEMPLATE = format("projects/%s/global/instanceTemplates/test-template-no-labels");

@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();

private static ComputeClient client;
private static Map<String, String> label = getLabel(ComputeEngineCloudTemplateNoGoogleLabelsIT.class);
private static final Map<String, String> label = getLabel(ComputeEngineCloudTemplateNoGoogleLabelsIT.class);
private static ComputeEngineCloud cloud;
private static Instance instance;

Expand Down
Loading

0 comments on commit 35bd816

Please sign in to comment.