Skip to content

Commit

Permalink
Refactoring constants after PR jenkinsci#660 has been merged
Browse files Browse the repository at this point in the history
  • Loading branch information
eaglerainbow committed Jun 10, 2018
1 parent dc18f4f commit fa99486
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,13 @@ public final class DockerContainerLabelKeys {
*/
@Restricted(NoExternalUse.class)
static final String TEMPLATE_NAME = PLUGIN_LABEL_KEY_NAMESPACE + "JenkinsTemplateName";

/**
* Name of the Docker "label" that we'll put into every container we start,
* setting its value to our {@link Node#isRemoveVolumes()}, so that we
* can recognize our own containers later.
*/
@Restricted(NoExternalUse.class)
static final String REMOVE_VOLUMES = PLUGIN_LABEL_KEY_NAMESPACE + "JenkinsRemoveVolumes";

}
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ private ContainerNodeNameMap retrieveContainers(DockerClient client) {
*/
Map<String, String> labelFilter = new HashMap<>();

labelFilter.put(DockerTemplateBase.CONTAINER_LABEL_JENKINS_INSTANCE_ID, getJenkinsInstanceId());
labelFilter.put(DockerContainerLabelKeys.JENKINS_INSTANCE_ID, getJenkinsInstanceId());

List<Container> containerList = client.listContainersCmd()
.withShowAll(true)
Expand All @@ -217,7 +217,7 @@ private ContainerNodeNameMap retrieveContainers(DockerClient client) {
* requested it to have (see "withLabelFilter")
*/

String containerNodeName = containerLabels.get(DockerTemplate.CONTAINER_LABEL_NODE_NAME);
String containerNodeName = containerLabels.get(DockerContainerLabelKeys.NODE_NAME);
if (containerNodeName == null) {
LOGGER.warn("Container {} is said to be created by this Jenkins instance, but does not have any node name label assigned; manual cleanup is required for this", containerId);
continue;
Expand Down Expand Up @@ -334,7 +334,7 @@ private void terminateContainerGracefully(DockerCloud dc, Container container) t
* would not have had the JenkinsId label, which is required for us to end up here.
*/

String removeVolumesString = containerLabels.get(DockerTemplate.CONTAINER_LABEL_REMOVE_VOLUMES);
String removeVolumesString = containerLabels.get(DockerContainerLabelKeys.REMOVE_VOLUMES);
if (removeVolumesString == null) {
throw new ContainerIsTaintedException("The removeVolumes label is missing; thus the container must have been "
+ "mangled unexpectedly; not cleaning up at all anymore, as another tool must have touched it");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,6 @@ public class DockerTemplate implements Describable<DockerTemplate> {

private static final UniqueIdGenerator ID_GENERATOR = new UniqueIdGenerator(36);

/**
* Name of the Docker "label" that we'll put into every container we start,
* setting its value to our {@link Node#isRemoveVolumes()}, so that we
* can recognize our own containers later.
*/
@Restricted(NoExternalUse.class)
static String CONTAINER_LABEL_REMOVE_VOLUMES = "JenkinsRemoveVolumes";

/** Default value for {@link #getName()} if {@link #name} is null. */
private static final String DEFAULT_NAME = "docker";

Expand Down Expand Up @@ -236,7 +228,7 @@ public CreateContainerCmd fillContainerConfig(CreateContainerCmd containerConfig
final String templateName = getName();

Map<String, String> labels = result.getLabels();
labels.put(CONTAINER_LABEL_REMOVE_VOLUMES, Boolean.toString(this.isRemoveVolumes()));
labels.put(DockerContainerLabelKeys.REMOVE_VOLUMES, Boolean.toString(this.isRemoveVolumes()));
labels.put(DockerContainerLabelKeys.TEMPLATE_NAME, templateName);

final String nodeName = calcUnusedNodeName(templateName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ public void testSimpleEnvironmentNothingTodo() throws IOException, InterruptedEx
List<DockerCloud> listOfCloud = new LinkedList<DockerCloud>();

Map<String, String> labelMap = new HashMap<>();
labelMap.put(DockerTemplate.CONTAINER_LABEL_NODE_NAME, nodeName);
labelMap.put(DockerTemplate.CONTAINER_LABEL_REMOVE_VOLUMES, "false");
labelMap.put(DockerContainerLabelKeys.NODE_NAME, nodeName);
labelMap.put(DockerContainerLabelKeys.REMOVE_VOLUMES, "false");

List<Container> containerList = new LinkedList<Container>();
Container c = TestableDockerContainerWatchdog.createMockedContainer(containerId, "Running", 0L, labelMap);
Expand Down Expand Up @@ -83,9 +83,9 @@ public void testContainerExistsButSlaveIsMissing() throws IOException, Interrupt
List<DockerCloud> listOfCloud = new LinkedList<DockerCloud>();

Map<String, String> labelMap = new HashMap<>();
labelMap.put(DockerTemplate.CONTAINER_LABEL_NODE_NAME, nodeName);
labelMap.put(DockerTemplate.CONTAINER_LABEL_TEMPLATE_NAME, "unittesttemplate");
labelMap.put(DockerTemplate.CONTAINER_LABEL_REMOVE_VOLUMES, "false");
labelMap.put(DockerContainerLabelKeys.NODE_NAME, nodeName);
labelMap.put(DockerContainerLabelKeys.TEMPLATE_NAME, "unittesttemplate");
labelMap.put(DockerContainerLabelKeys.REMOVE_VOLUMES, "false");

List<Container> containerList = new LinkedList<Container>();
Container c = TestableDockerContainerWatchdog.createMockedContainer(containerId, "Running", 0L, labelMap);
Expand Down Expand Up @@ -121,9 +121,9 @@ public void testContainerExistsButSlaveIsMissingWrongNodeNameIsIgnored() throws
List<DockerCloud> listOfCloud = new LinkedList<DockerCloud>();

Map<String, String> labelMap = new HashMap<>();
labelMap.put(DockerTemplate.CONTAINER_LABEL_NODE_NAME, nodeName);
labelMap.put(DockerTemplate.CONTAINER_LABEL_TEMPLATE_NAME, "unittesttemplate");
labelMap.put(DockerTemplate.CONTAINER_LABEL_REMOVE_VOLUMES, "false");
labelMap.put(DockerContainerLabelKeys.NODE_NAME, nodeName);
labelMap.put(DockerContainerLabelKeys.TEMPLATE_NAME, "unittesttemplate");
labelMap.put(DockerContainerLabelKeys.REMOVE_VOLUMES, "false");

List<Container> containerList = new LinkedList<Container>();
Container c = TestableDockerContainerWatchdog.createMockedContainer(containerId, "Running", 0L, labelMap);
Expand Down Expand Up @@ -168,17 +168,17 @@ public void testContainerExistsButSlaveIsMissingTwoClouds() throws IOException,
List<Container> containerList = new LinkedList<Container>();

Map<String, String> labelMap = new HashMap<>();
labelMap.put(DockerTemplate.CONTAINER_LABEL_NODE_NAME, nodeName1);
labelMap.put(DockerTemplate.CONTAINER_LABEL_TEMPLATE_NAME, "unittestTemplate");
labelMap.put(DockerTemplate.CONTAINER_LABEL_REMOVE_VOLUMES, "false");
labelMap.put(DockerContainerLabelKeys.NODE_NAME, nodeName1);
labelMap.put(DockerContainerLabelKeys.TEMPLATE_NAME, "unittestTemplate");
labelMap.put(DockerContainerLabelKeys.REMOVE_VOLUMES, "false");

Container c = TestableDockerContainerWatchdog.createMockedContainer(containerId1, "Running", 0L, labelMap);
containerList.add(c);

labelMap = new HashMap<>();
labelMap.put(DockerTemplate.CONTAINER_LABEL_NODE_NAME, nodeName2);
labelMap.put(DockerTemplate.CONTAINER_LABEL_TEMPLATE_NAME, "unittestTemplate");
labelMap.put(DockerTemplate.CONTAINER_LABEL_REMOVE_VOLUMES, "false");
labelMap.put(DockerContainerLabelKeys.NODE_NAME, nodeName2);
labelMap.put(DockerContainerLabelKeys.TEMPLATE_NAME, "unittestTemplate");
labelMap.put(DockerContainerLabelKeys.REMOVE_VOLUMES, "false");

c = TestableDockerContainerWatchdog.createMockedContainer(containerId2, "Running", 0L, labelMap);
containerList.add(c);
Expand Down Expand Up @@ -238,9 +238,9 @@ public void testContainerExistsButSlaveIsMissingWithTemplate() throws IOExceptio
List<DockerCloud> listOfCloud = new LinkedList<DockerCloud>();

Map<String, String> labelMap = new HashMap<>();
labelMap.put(DockerTemplate.CONTAINER_LABEL_NODE_NAME, nodeName);
labelMap.put(DockerTemplate.CONTAINER_LABEL_TEMPLATE_NAME, "unittesttemplate");
labelMap.put(DockerTemplate.CONTAINER_LABEL_REMOVE_VOLUMES, "false");
labelMap.put(DockerContainerLabelKeys.NODE_NAME, nodeName);
labelMap.put(DockerContainerLabelKeys.TEMPLATE_NAME, "unittesttemplate");
labelMap.put(DockerContainerLabelKeys.REMOVE_VOLUMES, "false");

List<Container> containerList = new LinkedList<Container>();
Container c = TestableDockerContainerWatchdog.createMockedContainer(containerId, "Running", 0L, labelMap);
Expand Down Expand Up @@ -287,9 +287,9 @@ public void testContainerExistsButSlaveIsMissingTooEarly() throws IOException, I
List<DockerCloud> listOfCloud = new LinkedList<DockerCloud>();

Map<String, String> labelMap = new HashMap<>();
labelMap.put(DockerTemplate.CONTAINER_LABEL_NODE_NAME, nodeName);
labelMap.put(DockerTemplate.CONTAINER_LABEL_TEMPLATE_NAME, "unittesttemplate");
labelMap.put(DockerTemplate.CONTAINER_LABEL_REMOVE_VOLUMES, "false");
labelMap.put(DockerContainerLabelKeys.NODE_NAME, nodeName);
labelMap.put(DockerContainerLabelKeys.TEMPLATE_NAME, "unittesttemplate");
labelMap.put(DockerContainerLabelKeys.REMOVE_VOLUMES, "false");

List<Container> containerList = new LinkedList<Container>();
Container c = TestableDockerContainerWatchdog.createMockedContainer(containerId, "Running", clock.instant().toEpochMilli() / 1000, labelMap);
Expand Down Expand Up @@ -335,8 +335,8 @@ public void testContainerExistsButSlaveIsMissingRemoveVolumesLabelMissing() thro
List<DockerCloud> listOfCloud = new LinkedList<DockerCloud>();

Map<String, String> labelMap = new HashMap<>();
labelMap.put(DockerTemplate.CONTAINER_LABEL_NODE_NAME, nodeName);
labelMap.put(DockerTemplate.CONTAINER_LABEL_TEMPLATE_NAME, "unittesttemplate");
labelMap.put(DockerContainerLabelKeys.NODE_NAME, nodeName);
labelMap.put(DockerContainerLabelKeys.TEMPLATE_NAME, "unittesttemplate");
// NB The removeVolumes label is not set here

List<Container> containerList = new LinkedList<Container>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public static DockerAPI createMockedDockerAPI(List<Container> containerList) {
@Override
public ListContainersCmd answer(InvocationOnMock invocation) throws Throwable {
Map<String, String> arg = (Map<String, String>) invocation.getArgumentAt(0, Map.class);
String jenkinsInstanceIdInFilter = arg.get(DockerTemplateBase.CONTAINER_LABEL_JENKINS_INSTANCE_ID);
String jenkinsInstanceIdInFilter = arg.get(DockerContainerLabelKeys.JENKINS_INSTANCE_ID);
Assert.assertEquals(UNITTEST_JENKINS_ID, jenkinsInstanceIdInFilter);

return listContainerCmd;
Expand Down

0 comments on commit fa99486

Please sign in to comment.