Skip to content

Commit

Permalink
Avoid CLICommandInvoker.authorizedTo in QuietDownCommandTest (#8754)
Browse files Browse the repository at this point in the history
  • Loading branch information
jglick committed Dec 8, 2023
1 parent 8fbe0d3 commit 6f8c3fe
Showing 1 changed file with 27 additions and 19 deletions.
46 changes: 27 additions & 19 deletions test/src/test/java/hudson/cli/QuietDownCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import org.jvnet.hudson.test.BuildWatcher;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.LoggerRule;
import org.jvnet.hudson.test.MockAuthorizationStrategy;

/**
* @author pjanouse
Expand All @@ -66,6 +67,9 @@ public class QuietDownCommandTest {
= new QueueTest.TestFlyweightTask(new AtomicInteger(), null);
private static final String TEST_REASON = "test reason";

private static final String VIEWER = "viewer";
private static final String ADMIN = "admin";

@ClassRule
public static final BuildWatcher buildWatcher = new BuildWatcher();

Expand All @@ -78,22 +82,26 @@ public class QuietDownCommandTest {
@Before
public void setUp() {
command = new CLICommandInvoker(j, "quiet-down");
j.jenkins.setSecurityRealm(j.createDummySecurityRealm());
j.jenkins.setAuthorizationStrategy(new MockAuthorizationStrategy().
grant(Jenkins.ADMINISTER).everywhere().to(ADMIN).
grant(Jenkins.READ).everywhere().to(VIEWER));
}

@Test
public void quietDownShouldFailWithoutAdministerPermission() {
final CLICommandInvoker.Result result = command
.authorizedTo(Jenkins.READ)
.asUser(VIEWER)
.invoke();
assertThat(result, failedWith(6));
assertThat(result, hasNoStandardOutput());
assertThat(result.stderr(), containsString("ERROR: user is missing the Overall/Administer permission"));
assertThat(result.stderr(), containsString("ERROR: " + VIEWER + " is missing the Overall/Administer permission"));
}

@Test
public void quietDownShouldSuccess() {
final CLICommandInvoker.Result result = command
.authorizedTo(Jenkins.READ, Jenkins.ADMINISTER)
.asUser(ADMIN)
.invoke();
assertThat(result, succeededSilently());
assertJenkinsInQuietMode();
Expand All @@ -102,7 +110,7 @@ public void quietDownShouldSuccess() {
@Test
public void quietDownShouldSuccessWithBlock() {
final CLICommandInvoker.Result result = command
.authorizedTo(Jenkins.READ, Jenkins.ADMINISTER)
.asUser(ADMIN)
.invokeWithArgs("-block");
assertThat(result, succeededSilently());
assertJenkinsInQuietMode();
Expand All @@ -111,7 +119,7 @@ public void quietDownShouldSuccessWithBlock() {
@Test
public void quietDownShouldSuccessWithTimeout() {
final CLICommandInvoker.Result result = command
.authorizedTo(Jenkins.READ, Jenkins.ADMINISTER)
.asUser(ADMIN)
.invokeWithArgs("-timeout", "0");
assertThat(result, succeededSilently());
assertJenkinsInQuietMode();
Expand All @@ -120,7 +128,7 @@ public void quietDownShouldSuccessWithTimeout() {
@Test
public void quietDownShouldSuccessWithReason() {
final CLICommandInvoker.Result result = command
.authorizedTo(Jenkins.READ, Jenkins.ADMINISTER)
.asUser(ADMIN)
.invokeWithArgs("-reason", TEST_REASON);
assertThat(result, succeededSilently());
assertJenkinsInQuietMode();
Expand All @@ -130,7 +138,7 @@ public void quietDownShouldSuccessWithReason() {
@Test
public void quietDownShouldSuccessWithBlockAndTimeout() {
final CLICommandInvoker.Result result = command
.authorizedTo(Jenkins.READ, Jenkins.ADMINISTER)
.asUser(ADMIN)
.invokeWithArgs("-block", "-timeout", "0");
assertThat(result, succeededSilently());
assertJenkinsInQuietMode();
Expand All @@ -139,7 +147,7 @@ public void quietDownShouldSuccessWithBlockAndTimeout() {
@Test
public void quietDownShouldSuccessWithBlockAndTimeoutAndReason() {
final CLICommandInvoker.Result result = command
.authorizedTo(Jenkins.READ, Jenkins.ADMINISTER)
.asUser(ADMIN)
.invokeWithArgs("-block", "-timeout", "0", "-reason", TEST_REASON);
assertThat(result, succeededSilently());
assertJenkinsInQuietMode();
Expand All @@ -149,7 +157,7 @@ public void quietDownShouldSuccessWithBlockAndTimeoutAndReason() {
@Test
public void quietDownShouldFailWithEmptyTimeout() {
final CLICommandInvoker.Result result = command
.authorizedTo(Jenkins.READ, Jenkins.ADMINISTER)
.asUser(ADMIN)
.invokeWithArgs("-timeout");
assertThat(result, failedWith(2));
assertThat(result, hasNoStandardOutput());
Expand All @@ -161,7 +169,7 @@ public void quietDownShouldSuccessOnAlreadyQuietDownedJenkins() {
j.jenkins.doQuietDown();
assertJenkinsInQuietMode();
final CLICommandInvoker.Result result = command
.authorizedTo(Jenkins.READ, Jenkins.ADMINISTER)
.asUser(ADMIN)
.invoke();
assertThat(result, succeededSilently());
assertJenkinsInQuietMode();
Expand All @@ -172,7 +180,7 @@ public void quietDownShouldSuccessWithBlockOnAlreadyQuietDownedJenkins() throws
j.jenkins.doQuietDown(true, 0, null, false);
assertJenkinsInQuietMode();
final CLICommandInvoker.Result result = command
.authorizedTo(Jenkins.READ, Jenkins.ADMINISTER)
.asUser(ADMIN)
.invokeWithArgs("-block");
assertThat(result, succeededSilently());
assertJenkinsInQuietMode();
Expand All @@ -184,7 +192,7 @@ public void quietDownShouldSuccessWithBlockAndTimeoutOnAlreadyQuietDownedJenkins
assertJenkinsInQuietMode();
final long time_before = System.currentTimeMillis();
final CLICommandInvoker.Result result = command
.authorizedTo(Jenkins.READ, Jenkins.ADMINISTER)
.asUser(ADMIN)
.invokeWithArgs("-block", "-timeout", "20000");
assertThat(result, succeededSilently());
assertThat(System.currentTimeMillis() < time_before + 20000, equalTo(true));
Expand All @@ -202,7 +210,7 @@ public void quietDownShouldSuccessAndRunningExecutor() throws Exception {
final FreeStyleBuild build = OnlineNodeCommandTest.startBlockingAndFinishingBuild(project, finish);

final CLICommandInvoker.Result result = command
.authorizedTo(Jenkins.READ, Jenkins.ADMINISTER)
.asUser(ADMIN)
.invoke();
assertThat(result, succeededSilently());
assertJenkinsInQuietMode();
Expand Down Expand Up @@ -230,7 +238,7 @@ public void quietDownShouldSuccessWithBlockAndRunningExecutor() throws Exception
assertJenkinsNotInQuietMode();
beforeCli.signal();
final CLICommandInvoker.Result result = command
.authorizedTo(Jenkins.READ, Jenkins.ADMINISTER)
.asUser(ADMIN)
.invokeWithArgs("-block");
fail("Should never return from previous CLI call!");
return null;
Expand Down Expand Up @@ -271,7 +279,7 @@ public void quietDownShouldSuccessWithBlockAndZeroTimeoutAndRunningExecutor() th
assertJenkinsNotInQuietMode();
beforeCli.signal();
final CLICommandInvoker.Result result = command
.authorizedTo(Jenkins.READ, Jenkins.ADMINISTER)
.asUser(ADMIN)
.invokeWithArgs("-block", "-timeout", "0");
fail("Should never return from previous CLI call!");
return null;
Expand Down Expand Up @@ -313,7 +321,7 @@ public void quietDownShouldSuccessWithBlockPlusExpiredTimeoutAndRunningExecutor(
final long time_before = System.currentTimeMillis();
beforeCli.signal();
final CLICommandInvoker.Result result = command
.authorizedTo(Jenkins.READ, Jenkins.ADMINISTER)
.asUser(ADMIN)
.invokeWithArgs("-block", "-timeout", Integer.toString(TIMEOUT));
assertThat(result, succeededSilently());
assertThat(System.currentTimeMillis() > time_before + TIMEOUT, equalTo(true));
Expand Down Expand Up @@ -353,7 +361,7 @@ public void quietDownShouldSuccessWithBlockPlusNonExpiredTimeoutAndRunningExecut
assertJenkinsNotInQuietMode();
beforeCli.signal();
final CLICommandInvoker.Result result = command
.authorizedTo(Jenkins.READ, Jenkins.ADMINISTER)
.asUser(ADMIN)
.invokeWithArgs("-block", "-timeout", Integer.toString(2 * TIMEOUT));
fail("Blocking call shouldn't finish, should be killed by called thread!");
return null;
Expand Down Expand Up @@ -397,7 +405,7 @@ public void quietDownShouldSuccessWithBlockAndFinishingExecutor() throws Excepti
final long time_before = System.currentTimeMillis();
beforeCli.signal();
final CLICommandInvoker.Result result = command
.authorizedTo(Jenkins.READ, Jenkins.ADMINISTER)
.asUser(ADMIN)
.invokeWithArgs("-block");
assertThat(result, succeededSilently());
assertThat(System.currentTimeMillis() > time_before + 1000, equalTo(true));
Expand Down Expand Up @@ -437,7 +445,7 @@ public void quietDownShouldSuccessWithBlockAndNonExpiredTimeoutAndFinishingExecu
final long time_before = System.currentTimeMillis();
beforeCli.signal();
final CLICommandInvoker.Result result = command
.authorizedTo(Jenkins.READ, Jenkins.ADMINISTER)
.asUser(ADMIN)
.invokeWithArgs("-block", "-timeout", Integer.toString(TIMEOUT));
assertThat(result, succeededSilently());
assertThat(System.currentTimeMillis() > time_before + 1000, equalTo(true));
Expand Down

0 comments on commit 6f8c3fe

Please sign in to comment.