Skip to content
Permalink
Browse files

[JENKINS-22969] Extract keep-build

  • Loading branch information...
olivergondza committed May 12, 2014
1 parent 3d9c174 commit cca046f618b404fe575ac128ca93da218a1e33ac
@@ -0,0 +1,61 @@
/*
* The MIT License
*
* Copyright (c) 2014 Red Hat, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package hudson.cli;

import org.kohsuke.args4j.Argument;
import org.kohsuke.args4j.CmdLineException;

import hudson.Extension;
import hudson.model.AbstractProject;
import hudson.model.Run;

/**
* @author ogondza
* @since TODO
*/
@Extension
public class KeepBuildCommand extends CLICommand {

@Argument(metaVar="JOB", usage="Name of the job", required=true, index=0)
public transient AbstractProject<?, ?> job;

@Argument(metaVar="BUILD#", usage="Number of the build", required=true, index=1)
public int number;

@Override
public String getShortDescription() {
return Messages.KeepBuildCommand_ShortDescription();
}

@Override
protected int run() throws Exception{
Run<?, ?> build = job.getBuildByNumber(number);
if (build == null) throw new CmdLineException(
null, "No such build '#" + number + "' exists"
);

build.keepLog();
return 0;
}
}
@@ -2118,7 +2118,6 @@ public void doToggleLogKeep( StaplerRequest req, StaplerResponse rsp ) throws IO
/**
* Marks this build to keep the log.
*/
@CLIMethod(name="keep-build")
public final void keepLog() throws IOException {
keepLog(true);
}
@@ -34,6 +34,8 @@ InstallPluginCommand.ShortDescription=\
Installs a plugin either from a file, an URL, or from update center.
InstallToolCommand.ShortDescription=\
Performs automatic tool installation, and print its location to stdout. Can be only called from inside a build.
KeepBuildCommand.ShortDescription=\
Mark the build to keep the build forever.
ListChangesCommand.ShortDescription=\
Dumps the changelog for the specified build(s).
ListJobsCommand.ShortDescription=\
@@ -345,7 +345,6 @@ MyViewsProperty.ViewExistsCheck.AlreadyExists=A view with name {0} already exist

CLI.restart.shortDescription=Restart Jenkins
CLI.safe-restart.shortDescription=Safely restart Jenkins
CLI.keep-build.shortDescription=Mark the build to keep the build forever.
CLI.quiet-down.shortDescription=Quiet down Jenkins, in preparation for a restart. Don\u2019t start any builds.
CLI.cancel-quiet-down.shortDescription=Cancel the effect of the "quiet-down" command.
CLI.reload-configuration.shortDescription=Discard all the loaded data in memory and reload everything from file system. Useful when you modified config files directly on disk.
@@ -0,0 +1,85 @@
/*
* The MIT License
*
* Copyright (c) 2014 Red Hat, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package hudson.cli;

import static hudson.cli.CLICommandInvoker.Matcher.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.CoreMatchers.is;
import jenkins.model.Jenkins;
import hudson.cli.CLICommandInvoker;
import hudson.cli.CLICommandInvoker.Result;
import hudson.model.FreeStyleBuild;
import hudson.model.FreeStyleProject;
import hudson.model.Job;
import hudson.model.Run;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

public class KeepBuildCommandTest {

@Rule public JenkinsRule j = new JenkinsRule();

private FreeStyleProject job;
private FreeStyleBuild build;

private CLICommandInvoker invoker;
private Result res;

@Before public void setUp() throws Exception {
invoker = new CLICommandInvoker(j, "keep-build");

job = j.createFreeStyleProject("a_project");
build = job.scheduleBuild2(0).get();
assertThat(build.isKeepLog(), is(false));
}

@Test public void keepBuildCli() {
res = invoker.invokeWithArgs("a_project", "1");
assertThat(res, succeededSilently());
assertThat(build.isKeepLog(), is(true));
}

@Test public void failIfDoesNotExist() {
res = invoker.invokeWithArgs("no_such_project", "1");
assertThat(res, failedWith("No such job"));
assertThat(build.isKeepLog(), is(false));

res = invoker.invokeWithArgs("a_project", "42");
assertThat(res, failedWith("No such build"));
assertThat(build.isKeepLog(), is(false));
}

@Test public void failWithoutPermisssion() {
res = invoker.authorizedTo(Jenkins.READ, Run.UPDATE).invokeWithArgs("a_project", "1");
assertThat(res, failedWith("No such job"));
assertThat(build.isKeepLog(), is(false));

res = invoker.authorizedTo(Jenkins.READ, Job.READ).invokeWithArgs("a_project", "1");
assertThat(res, failedWith("user is missing the Run/Update permission"));
assertThat(build.isKeepLog(), is(false));
}
}

0 comments on commit cca046f

Please sign in to comment.
You can’t perform that action at this time.