Skip to content

Commit

Permalink
Improve tests (#581)
Browse files Browse the repository at this point in the history
Improve tests
  • Loading branch information
mPokornyETM authored Nov 24, 2023
1 parent 71f6e7c commit ce79091
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,15 @@ public synchronized LockableResource fromName(String resourceName) {
return null;
}

// ---------------------------------------------------------------------------
/** Checks if given resource exist. */
@NonNull
@Restricted(NoExternalUse.class)
public boolean resourceExist(@CheckForNull String resourceName) {
return this.fromName(resourceName) != null;
}

// ---------------------------------------------------------------------------
public synchronized boolean queue(List<LockableResource> resources, long queueItemId, String queueProjectName) {
for (LockableResource r : resources) {
if (r.isReserved() || r.isQueued(queueItemId) || r.isLocked()) {
Expand Down Expand Up @@ -1256,11 +1265,11 @@ public static LockableResourcesManager get() {
@Override
public synchronized void save() {
if (enableSave == -1) {
// read system property and chache it.
// read system property and cache it.
enableSave = SystemProperties.getBoolean(Constants.SYSTEM_PROPERTY_DISABLE_SAVE) ? 0 : 1;
}

if (enableSave == 0) return; // savinig is disabled
if (enableSave == 0) return; // saving is disabled

if (BulkChange.contains(this)) return; // no change detected

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,20 @@
import io.jenkins.plugins.casc.misc.Util;
import io.jenkins.plugins.casc.model.CNode;
import java.util.List;
import org.jenkins.plugins.lockableresources.util.Constants;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;

public class ConfigurationAsCodeTest {

// ---------------------------------------------------------------------------
@Before
public void setUp() {
// to speed up the test
System.setProperty(Constants.SYSTEM_PROPERTY_DISABLE_SAVE, "true");
}

@ClassRule
@ConfiguredWithCode("configuration-as-code.yml")
public static JenkinsConfiguredWithCodeRule r = new JenkinsConfiguredWithCodeRule();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,24 @@

import com.google.common.base.Joiner;
import hudson.model.Result;
import org.jenkins.plugins.lockableresources.util.Constants;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

public class DeclarativePipelineTest {

// ---------------------------------------------------------------------------
@Before
public void setUp() {
// to speed up the test
System.setProperty(Constants.SYSTEM_PROPERTY_DISABLE_SAVE, "true");
}

@Rule
public JenkinsRule j = new JenkinsRule();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@
import jenkins.model.Jenkins;
import org.jenkins.plugins.lockableresources.actions.LockableResourcesRootAction;
import org.jenkins.plugins.lockableresources.queue.LockableResourcesQueueTaskDispatcher;
import org.jenkins.plugins.lockableresources.util.Constants;
import org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SecureGroovyScript;
import org.jenkinsci.plugins.scriptsecurity.scripts.ApprovalContext;
import org.jenkinsci.plugins.scriptsecurity.scripts.ScriptApproval;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.Issue;
Expand All @@ -45,6 +47,13 @@ public class FreeStyleProjectTest {
@Rule
public JenkinsRule j = new JenkinsRule();

// ---------------------------------------------------------------------------
@Before
public void setUp() {
// to speed up the test
System.setProperty(Constants.SYSTEM_PROPERTY_DISABLE_SAVE, "true");
}

@Test
@Issue("JENKINS-34853")
public void security170fix() throws Exception {
Expand Down Expand Up @@ -209,7 +218,9 @@ public void approvalRequired() throws Exception {
@Test
public void autoCreateResource() throws IOException, InterruptedException, ExecutionException {
FreeStyleProject f = j.createFreeStyleProject("f");
assertNull(LockableResourcesManager.get().fromName("resource1"));
f.addProperty(new RequiredResourcesProperty("resource1", null, null, null, null));
assertNull(LockableResourcesManager.get().fromName("resource1"));

FreeStyleBuild fb1 = f.scheduleBuild2(0).waitForStart();
j.waitForMessage("acquired lock on [resource1]", fb1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,25 @@
import hudson.model.FreeStyleBuild;
import hudson.model.FreeStyleProject;
import java.util.concurrent.Semaphore;
import org.jenkins.plugins.lockableresources.util.Constants;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.TestBuilder;

public class InteroperabilityTest extends LockStepTestBase {

// ---------------------------------------------------------------------------
@Before
public void setUp() {
// to speed up the test
System.setProperty(Constants.SYSTEM_PROPERTY_DISABLE_SAVE, "true");
}

@Rule
public JenkinsRule j = new JenkinsRule();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class LockStepHardKillTest extends LockStepTestBase {
@Issue("JENKINS-36479")
@Test
public void hardKillNewBuildClearsLock() throws Exception {
LockableResourcesManager.get().createResource("resource1");
LockableResourcesManager.get().createResourceWithLabel("resource1", "label");

WorkflowJob p1 = j.jenkins.createProject(WorkflowJob.class, "p1");
p1.setDefinition(new CpsFlowDefinition(
Expand Down Expand Up @@ -105,6 +105,7 @@ public void hardKillWithWaitingRuns() throws Exception {
LockableResourcesManager.get().createResource("resource1");
WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition(
/// FIXME why we need retry here, when you know it, write the comment here or remove it
"retry(99) {\n" + " lock('resource1') {\n" + " semaphore('wait-inside')\n" + " }\n" + "}",
true));

Expand Down Expand Up @@ -134,6 +135,7 @@ public void hardKillWithWaitingRunsOnLabel() throws Exception {
LockableResourcesManager.get().createResourceWithLabel("resource2", "label1");
WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition(
/// FIXME why we need retry here, when you know it, write the comment here or remove it
"retry(99) {\n"
+ " lock(label: 'label1', quantity: 1) {\n"
+ " semaphore('wait-inside')\n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,22 @@

import hudson.security.FullControlOnceLoggedInAuthorizationStrategy;
import org.htmlunit.FailingHttpStatusCodeException;
import org.jenkins.plugins.lockableresources.util.Constants;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;

public class LockableResourceApiTest {

// ---------------------------------------------------------------------------
@Before
public void setUp() {
// to speed up the test
System.setProperty(Constants.SYSTEM_PROPERTY_DISABLE_SAVE, "true");
}

@Rule
public JenkinsRule j = new JenkinsRule();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

import java.util.logging.Logger;
import org.jenkins.plugins.lockableresources.util.Constants;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
Expand All @@ -15,21 +16,31 @@

public class NodesMirrorTest {

private static final Logger LOGGER = Logger.getLogger(NodesMirror.class.getName());

@Rule
public final JenkinsRule j = new JenkinsRule();

@Test
public void mirror_few_nodes() throws Exception {
System.setProperty(Constants.SYSTEM_PROPERTY_ENABLE_NODE_MIRROR, "true");

LOGGER.info("add agent: FirstAgent");
j.createSlave("FirstAgent", "label label2", null);
LOGGER.info("add agent: SecondAgent");
j.createSlave("SecondAgent", null, null);

// this is asynchronous operation, so wait until resources are created.
for (int i = 1; LockableResourcesManager.get().fromName("SecondAgent") != null && i <= 10; i++) {
LOGGER.info("wait for resources");
for (int i = 1;
!LockableResourcesManager.get().resourceExist("FirstAgent")
&& !LockableResourcesManager.get().resourceExist("SecondAgent")
&& i <= 10;
i++) {
Thread.sleep(100);
}

LOGGER.info("check agent: FirstAgent");
LockableResource firstAgent = LockableResourcesManager.get().fromName("FirstAgent");

assertEquals("FirstAgent", firstAgent.getName());
Expand Down

0 comments on commit ce79091

Please sign in to comment.