Skip to content
Permalink
Browse files

Merge pull request #3 from ikedam/feature/JENKINS-30135_LostLabel

[JENKINS-30135] Consistent behavior even when the label instance replaced.
  • Loading branch information...
ikedam committed Sep 13, 2015
2 parents 92df9f2 + aa54184 commit 0ec2c9c5ad9b6de7ab572f1cfc84e16f450c88e8
@@ -23,6 +23,7 @@
*/
package jp.ikedam.jenkins.plugins.groovy_label_assignment;

import jenkins.model.Jenkins;
import hudson.model.Label;
import hudson.model.labels.LabelAssignmentAction;
import hudson.model.queue.SubTask;
@@ -32,16 +33,39 @@
*/
public class GroovyLabelAssignmentAction implements LabelAssignmentAction
{
private Label label;
@Deprecated
transient private Label label;

private final String labelString;

/**
* Constructor
*
* @param label assigned label.
* @param labelString assigned label expression.
* @since 1.1.1
*/
public GroovyLabelAssignmentAction(String labelString)
{
this.labelString = labelString;
}

/**
* @param label
* @deprecated use {@link GroovyLabelAssignmentAction#GroovyLabelAssignmentAction(String)}
*/
@Deprecated
public GroovyLabelAssignmentAction(Label label)
{
this.label = label;
this(label.getExpression());
}

private Object readResolve()
{
if(label != null)
{
return new GroovyLabelAssignmentAction(label.getExpression());
}
return this;
}

/**
@@ -89,7 +113,7 @@ public String getUrlName()
@Override
public Label getAssignedLabel(SubTask task)
{
return label;
return getAssignedLabel();
}


@@ -102,6 +126,15 @@ public Label getAssignedLabel(SubTask task)
*/
public Label getAssignedLabel()
{
return label;
return Jenkins.getInstance().getLabel(getLabelString());
}

/**
* @return the expression string for the assigned label.
* @since 1.1.1
*/
public String getLabelString()
{
return labelString;
}
}
@@ -139,18 +139,17 @@ public boolean assignLabel(AbstractProject<?, ?> project, List<Action> actions)
return true;
}

Label label;
try
{
label = LabelExpression.parseExpression(labelString);
LabelExpression.parseExpression(labelString);
}
catch(ANTLRException e)
{
LOGGER.log(Level.SEVERE, String.format("%s: Invalid label string: %s", project.getName(), labelString), e);
return false;
}

LabelAssignmentAction labelAction = new GroovyLabelAssignmentAction(label);
LabelAssignmentAction labelAction = new GroovyLabelAssignmentAction(labelString);
actions.add(0, labelAction);

LOGGER.info(String.format("%s: label is modified to %s", project.getName(), labelString));
@@ -29,7 +29,7 @@ THE SOFTWARE.
<t:summary icon="computer.png">
<l:pane title="${%Assigned Label}" width="3">
<f:block>
${it.assignedLabel.expression}
${it.labelString}
</f:block>
</l:pane>
</t:summary>
@@ -24,8 +24,10 @@
package jp.ikedam.jenkins.plugins.groovy_label_assignment;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
@@ -59,6 +61,7 @@
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.Bug;
import org.jvnet.hudson.test.JenkinsRule.WebClient;
import org.jvnet.hudson.test.CaptureEnvironmentBuilder;

@@ -602,4 +605,42 @@ public void testCurrentJobWithMatrixProject() throws Exception {
}
}
}

@Bug(30135)
@Test
public void testLabelIsOnceRemoved() throws Exception
{
slave1.getComputer().cliOffline("for testing");
assertTrue(slave1.getComputer().isOffline());

FreeStyleProject p = j.createFreeStyleProject();
p.addProperty(new GroovyLabelAssignmentProperty("return \"test1\""));
Future<FreeStyleBuild> b = p.scheduleBuild2(0);

try {
b.get(500, TimeUnit.MILLISECONDS);
fail("Build should not run as the node is offline.");
} catch(TimeoutException e) {
// ok
}

// remove "test1" from slave1
// Unfortunately, there's no easy way to do this...
// Copied from hudson.model.Computer#replaceBy(Node)
synchronized (j.jenkins)
{
List<Node> nodes = new ArrayList<Node>(j.jenkins.getNodes());
int i = nodes.indexOf(slave1);
if(i<0)
{
throw new IOException("This slave appears to be removed while you were editing the configuration");
}
slave1 = j.createSlave(slave1.getNodeName(), "", null);
nodes.set(i, slave1);
j.jenkins.setNodes(nodes);
}

j.createOnlineSlave("test1");
assertNotNull(b.get(500, TimeUnit.MILLISECONDS));
}
}

0 comments on commit 0ec2c9c

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