Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[JENKINS-28041] Extended delete-node CLI command to accept multiple n…
…ames to delete
- Loading branch information
Showing
21 changed files
with
255 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,90 @@ | |||
/* | |||
* The MIT License | |||
* | |||
* Copyright (c) 2015 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 hudson.Extension; | |||
import hudson.model.Computer; | |||
import hudson.model.Node; | |||
import jenkins.model.Jenkins; | |||
import org.kohsuke.args4j.Argument; | |||
|
|||
import java.util.HashSet; | |||
import java.util.List; | |||
|
|||
/** | |||
* @author pjanouse | |||
* @since TODO | |||
*/ | |||
@Extension | |||
public class DeleteNodeCommand extends CLICommand { | |||
|
|||
@Argument(usage="Nodes name to delete", required=true, multiValued=true) | |||
private List<String> nodes; | |||
|
|||
@Override | |||
public String getShortDescription() { | |||
|
|||
return Messages.DeleteNodeCommand_ShortDescription(); | |||
} | |||
|
|||
@Override | |||
protected int run() throws Exception { | |||
|
|||
boolean errorOccurred = false; | |||
|
|||
HashSet<String> hs = new HashSet<String>(); | |||
hs.addAll(nodes); | |||
|
|||
for (String node_s : hs) { | |||
|
|||
Node node = Jenkins.getInstance().getNode(node_s); | |||
|
|||
if(node == null) { | |||
stderr.format("No such node '%s'\n", node_s); | |||
errorOccurred = true; | |||
continue; | |||
} | |||
|
|||
try { | |||
node.checkPermission(Computer.DELETE); | |||
} catch (Exception e) { | |||
stderr.println(e.getMessage()); | |||
errorOccurred = true; | |||
continue; | |||
} | |||
|
|||
try { | |||
Jenkins.getInstance().removeNode(node); | |||
} catch (Exception e) { | |||
stderr.format("Unexpected exception occurred during deletion of node '%s': %s\n", | |||
node.getDisplayName(), | |||
e.getMessage() | |||
); | |||
errorOccurred = true; | |||
continue; | |||
} | |||
} | |||
return errorOccurred ? -1 : 0; | |||
} | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1 @@ | |||
DeleteNodeCommand.ShortDescription=Sletter en node |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1 @@ | |||
DeleteNodeCommand.ShortDescription=Knoten l\u00f6schen. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1 @@ | |||
DeleteNodeCommand.ShortDescription=Cancella un nodo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1 @@ | |||
DeleteNodeCommand.ShortDescription=Deletes a node |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
test/src/test/java/hudson/cli/DeleteNodeCommandTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,147 @@ | |||
/* | |||
* The MIT License | |||
* | |||
* Copyright 2015 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. | |||
*/ | |||
|
|||
/** | |||
* @author pjanouse | |||
* @since TODO | |||
*/ | |||
|
|||
package hudson.cli; | |||
|
|||
import hudson.model.Computer; | |||
import hudson.slaves.DumbSlave; | |||
import hudson.slaves.NodeProperty; | |||
import jenkins.model.Jenkins; | |||
import org.junit.Before; | |||
import org.junit.Ignore; | |||
import org.junit.Rule; | |||
import org.junit.Test; | |||
import org.jvnet.hudson.test.JenkinsRule; | |||
|
|||
import java.util.ArrayList; | |||
|
|||
import static hudson.cli.CLICommandInvoker.Matcher.*; | |||
import static org.hamcrest.MatcherAssert.assertThat; | |||
import static org.hamcrest.Matchers.containsString; | |||
import static org.hamcrest.Matchers.nullValue; | |||
import static org.hamcrest.Matchers.notNullValue; | |||
|
|||
public class DeleteNodeCommandTest { | |||
|
|||
private CLICommandInvoker command; | |||
|
|||
@Rule public final JenkinsRule j = new JenkinsRule(); | |||
|
|||
@Before public void setUp() { | |||
|
|||
command = new CLICommandInvoker(j, new DeleteNodeCommand()); | |||
} | |||
|
|||
@Test public void deleteNodeShouldFailWithoutNodeDeletePermission() throws Exception { | |||
|
|||
j.createSlave("aNode", "", null); | |||
|
|||
final CLICommandInvoker.Result result = command | |||
.authorizedTo(Jenkins.READ) | |||
.invokeWithArgs("aNode") | |||
; | |||
|
|||
assertThat(result, failedWith(-1)); | |||
assertThat(result, hasNoStandardOutput()); | |||
assertThat(result.stderr(), containsString("user is missing the Slave/Delete permission")); | |||
} | |||
|
|||
@Test public void deleteNodeShouldSucceed() throws Exception { | |||
|
|||
j.createSlave("aNode", "", null); | |||
|
|||
final CLICommandInvoker.Result result = command | |||
.authorizedTo(Computer.DELETE, Jenkins.READ) | |||
.invokeWithArgs("aNode") | |||
; | |||
|
|||
assertThat(result, succeededSilently()); | |||
assertThat(j.jenkins.getNode("aNode"), nullValue()); | |||
} | |||
|
|||
@Test public void deleteNodeShouldFailIfNodeDoesNotExist() { | |||
|
|||
final CLICommandInvoker.Result result = command | |||
.authorizedTo(Computer.DELETE, Jenkins.READ) | |||
.invokeWithArgs("never_created") | |||
; | |||
|
|||
assertThat(result, failedWith(-1)); | |||
assertThat(result, hasNoStandardOutput()); | |||
assertThat(result.stderr(), containsString("No such node 'never_created'")); | |||
} | |||
|
|||
@Test public void deleteNodeManyShouldSucceed() throws Exception { | |||
|
|||
j.createSlave("aNode1", "", null); | |||
j.createSlave("aNode2", "", null); | |||
j.createSlave("aNode3", "", null); | |||
|
|||
final CLICommandInvoker.Result result = command | |||
.authorizedTo(Computer.DELETE, Jenkins.READ) | |||
.invokeWithArgs("aNode1", "aNode2", "aNode3"); | |||
|
|||
assertThat(result, succeededSilently()); | |||
assertThat(j.jenkins.getView("aNode1"), nullValue()); | |||
assertThat(j.jenkins.getView("aNode2"), nullValue()); | |||
assertThat(j.jenkins.getView("aNode3"), nullValue()); | |||
} | |||
|
|||
@Test public void deleteNodeManyShouldFailIfANodeDoesNotExist() throws Exception { | |||
|
|||
j.createSlave("aNode1", "", null); | |||
j.createSlave("aNode2", "", null); | |||
|
|||
final CLICommandInvoker.Result result = command | |||
.authorizedTo(Computer.DELETE, Jenkins.READ) | |||
.invokeWithArgs("aNode1", "aNode2", "never_created"); | |||
|
|||
assertThat(result, failedWith(-1)); | |||
assertThat(result, hasNoStandardOutput()); | |||
assertThat(result.stderr(), containsString("No such node 'never_created'")); | |||
|
|||
assertThat(j.jenkins.getView("aNode1"), nullValue()); | |||
assertThat(j.jenkins.getView("aNode2"), nullValue()); | |||
assertThat(j.jenkins.getView("never_created"), nullValue()); | |||
} | |||
|
|||
@Test public void deleteNodeManyShouldSucceedEvenANodeIsSpecifiedTwice() throws Exception { | |||
|
|||
j.createSlave("aNode1", "", null); | |||
j.createSlave("aNode2", "", null); | |||
|
|||
final CLICommandInvoker.Result result = command | |||
.authorizedTo(Computer.DELETE, Jenkins.READ) | |||
.invokeWithArgs("aNode1", "aNode2", "aNode1"); | |||
|
|||
assertThat(result, succeededSilently()); | |||
assertThat(j.jenkins.getView("aNode1"), nullValue()); | |||
assertThat(j.jenkins.getView("aNode2"), nullValue()); | |||
} | |||
} |