@Override public Boolean call() throws Throwable { final Jenkins jenkins = Jenkins.getInstance(); // prepare job final FreeStyleProject project = jenkins.createProject(FreeStyleProject.class, "freestyle-project"); final Shell env = new Shell("env"); project.getBuildersList().add(env); project.setAssignedLabel(new LabelAtom(DOCKER_CLOUD_LABEL)); project.save(); LOG.trace("trace test."); project.scheduleBuild(new TestCause()); // image pull may take time waitUntilNoActivityUpTo(jenkins, 10 * 60 * 1000); final FreeStyleBuild lastBuild = project.getLastBuild(); assertThat(lastBuild, not(nullValue())); assertThat(lastBuild.getResult(), is(Result.SUCCESS)); assertThat(getLog(lastBuild), Matchers.containsString(TEST_VALUE)); assertThat(getLog(lastBuild), Matchers.containsString(CLOUD_ID + "=" + DOCKER_CLOUD_NAME)); return true; }
@Ignore("Unit test fails when performing a release. The queue has a race condition" + "which is resolved in 1.607+ (TODO).") @Test public void verifyMinimumBuildQueue() throws Exception { // Given QueueTaskFuture<FreeStyleBuild> build; String assignedLabel = "foo"; FreeStyleProject p = j.createFreeStyleProject("bar"); p.setAssignedLabel(new LabelAtom(assignedLabel)); BuildQueue queue = new BuildQueue(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); // When build = p.scheduleBuild2(0); queue.addContents(createContainer(baos)); // Then build.cancel(true); List<String> output = new ArrayList<String>(Arrays.asList(baos.toString().split("\n"))); assertContains(output, "1 item"); assertContains(output, p.getDisplayName()); assertContains(output, "Waiting for next available executor"); }
@Override @Before public void setUp() throws Exception { super.setUp(); slave = createOnlineSlave(new LabelAtom("oneiric-10")); // poll to make sure test slave is online before continuing long timeoutExpiredMs = System.currentTimeMillis() + 3000; while (true) { if (slave.getChannel() != null) { break; } this.wait(timeoutExpiredMs - System.currentTimeMillis()); if (System.currentTimeMillis() >= timeoutExpiredMs) { fail("Could not start test slave"); } } slave.setLabelString("ubuntu gcc python-2.4 linux"); }
@Test public void testRegisterJobs_ProjectSingleLabel() throws Exception { Project<?, ?> lemon = createFreeStyleProject("lemon"); lemon.setAssignedLabel(new LabelAtom("linux")); AbstractWorkerThread oneiric = new ExecutorWorkerThread("GearmanServer", 4730, "MyWorker", slave.toComputer(), "master", new NoopAvailabilityMonitor()); oneiric.testInitWorker(); oneiric.registerJobs(); Set<String> functions = oneiric.worker.getRegisteredFunctions(); assertEquals(2, functions.size()); assertTrue(functions.contains("build:lemon")); assertTrue(functions.contains("build:lemon:linux")); }
@Test public void testRegisterJobs_ProjectNoLabel_Exclusive() throws Exception { Project<?, ?> lemon = createFreeStyleProject("lemon"); DumbSlave exclusive_slave = createOnlineSlave(new LabelAtom("foo")); exclusive_slave.setMode(Mode.EXCLUSIVE); AbstractWorkerThread oneiric = new ExecutorWorkerThread( "GearmanServer", 4730, "MyWorker", exclusive_slave.toComputer(), "master", new NoopAvailabilityMonitor()); oneiric.testInitWorker(); oneiric.registerJobs(); Set<String> functions = oneiric.worker.getRegisteredFunctions(); assertEquals(0, functions.size()); }
@Test public void testRegisterJobs_SlaveOffline() throws Exception { DumbSlave offlineSlave = createSlave(new LabelAtom("oneiric-10")); offlineSlave.setLabelString("ubuntu gcc python-2.4 linux"); Project<?, ?> lemon = createFreeStyleProject("lemon"); lemon.setAssignedLabel(new LabelAtom("linux")); AbstractWorkerThread oneiric = new ExecutorWorkerThread("GearmanServer", 4730, "MyWorker", offlineSlave.toComputer(), "master", new NoopAvailabilityMonitor()); oneiric.testInitWorker(); oneiric.registerJobs(); Set<String> functions = oneiric.worker.getRegisteredFunctions(); assertEquals(0, functions.size()); }
@Test public void testRegisterJobs_MavenProject() throws Exception { MavenModuleSet lemon = createMavenProject("lemon"); lemon.setAssignedLabel(new LabelAtom("linux")); AbstractWorkerThread oneiric = new ExecutorWorkerThread("GearmanServer", 4730, "MyWorker", slave.toComputer(), "master", new NoopAvailabilityMonitor()); oneiric.testInitWorker(); oneiric.registerJobs(); Set<String> functions = oneiric.worker.getRegisteredFunctions(); assertEquals(2, functions.size()); assertTrue(functions.contains("build:lemon")); assertTrue(functions.contains("build:lemon:linux")); }
@Before public void setup() { Set<LabelAtom> labels = Collections.singleton(label); Mockito.when(label.matches(Mockito.anyCollectionOf(LabelAtom.class))).thenReturn(true); Mockito.when(slaveTemplate.createSlaveName()).thenReturn("slave-1", "slave-2", "slave-3"); Mockito.when(slaveTemplate.getNumExecutors()).thenReturn(1); Mockito.when(slaveTemplate.getLabelSet()).thenReturn(labels); }
public void testLabelAtom() throws Exception{ setUpCloud(LABEL1 + " " + LABEL2); assertEquals(true, ac.canProvision(new LabelAtom(LABEL1))); assertEquals(true, ac.canProvision(new LabelAtom(LABEL2))); assertEquals(false, ac.canProvision(new LabelAtom("aaa"))); assertEquals(true, ac.canProvision(null)); }
public void testExclusiveMode() throws Exception{ setUpCloud(LABEL1 + " " + LABEL2, Node.Mode.EXCLUSIVE); assertEquals(true, ac.canProvision(new LabelAtom(LABEL1))); assertEquals(true, ac.canProvision(new LabelAtom(LABEL2))); assertEquals(false, ac.canProvision(new LabelAtom("aaa"))); assertEquals(false, ac.canProvision(null)); }
Map<String, String> getLabelsMap(Set<LabelAtom> labelSet) { ImmutableMap.Builder<String, String> builder = ImmutableMap.<String, String> builder(); builder.putAll(getLabels()); if (!labelSet.isEmpty()) { for (LabelAtom label: labelSet) { builder.put(getIdForLabel(label), "true"); } } return builder.build(); }
public Set<LabelAtom> getLabels() { if (_labels == null) { // Do not do this in readResolve as it can result in a recursive dependency load that // makes jenkins startup slow and unstable. _labels = Label.parse(this.labelString); } return _labels; }
public DockerJobSlave(@Nonnull String nodeName, String nodeDescription, String jobName, String remoteFS, Set<LabelAtom> labels, DockerJobComputerLauncher launcher) throws Descriptor.FormException, IOException { super(nodeName, nodeDescription, remoteFS, 1, Mode.NORMAL, LABEL_JOINER.join(labels), launcher, new DockerJobRetentionStrategy(), ImmutableList.<NodeProperty<?>>of()); this.jobName = jobName; }
@Override public TagCloud<LabelAtom> getLabelCloud() { initPython(); if (pexec.isImplemented(3)) { return (TagCloud) pexec.execPython("get_label_cloud"); } else { return super.getLabelCloud(); } }
@Override @Exported public Set<LabelAtom> getAssignedLabels() { initPython(); if (pexec.isImplemented(4)) { return (Set) pexec.execPython("get_assigned_labels"); } else { return super.getAssignedLabels(); } }
@Override @WithBridgeMethods(Label.class) public LabelAtom getSelfLabel() { initPython(); if (pexec.isImplemented(6)) { return (LabelAtom) pexec.execPython("get_self_label"); } else { return super.getSelfLabel(); } }
private String buildStatusData(AbstractBuild<?, ?> build) { Hudson hudson = Hudson.getInstance(); AbstractProject<?, ?> project = build.getProject(); Map data = new HashMap<String, String>(); data.put("name", project.getName()); data.put("number", build.getNumber()); data.put("manager", masterName); data.put("worker", this.worker.getWorkerID()); String rootUrl = Hudson.getInstance().getRootUrl(); if (rootUrl != null) { data.put("url", rootUrl + build.getUrl()); } Result result = build.getResult(); if (result != null) { data.put("result", result.toString()); } ArrayList<String> nodeLabels = new ArrayList<String>(); Node node = build.getBuiltOn(); if (node != null) { Set<LabelAtom> nodeLabelAtoms = node.getAssignedLabels(); for (LabelAtom labelAtom : nodeLabelAtoms) { nodeLabels.add(labelAtom.getDisplayName()); } } data.put("node_labels", nodeLabels); data.put("node_name", node.getNodeName()); Gson gson = new Gson(); return gson.toJson(data); }
@Test public void testRegisterJobs_ProjectInvalidLabel() throws Exception { Project<?, ?> lemon = createFreeStyleProject("lemon"); lemon.setAssignedLabel(new LabelAtom("bogus")); AbstractWorkerThread oneiric = new ExecutorWorkerThread("GearmanServer", 4730, "MyWorker", slave.toComputer(), "master", new NoopAvailabilityMonitor()); oneiric.testInitWorker(); oneiric.registerJobs(); Set<String> functions = oneiric.worker.getRegisteredFunctions(); assertEquals(0, functions.size()); }
@Test public void testRegisterJobs_ProjectDisabled() throws Exception { Project<?, ?> lemon = createFreeStyleProject("lemon"); lemon.setAssignedLabel(new LabelAtom("linux")); lemon.disable(); AbstractWorkerThread oneiric = new ExecutorWorkerThread("GearmanServer", 4730, "MyWorker", slave.toComputer(), "master", new NoopAvailabilityMonitor()); oneiric.testInitWorker(); oneiric.registerJobs(); Set<String> functions = oneiric.worker.getRegisteredFunctions(); assertEquals(0, functions.size()); }
@Test public void testRegisterJobs_ProjectNotLabel() throws Exception { Project<?, ?> lemon = createFreeStyleProject("lemon"); lemon.setAssignedLabel(new LabelAtom("!linux")); AbstractWorkerThread oneiric = new ExecutorWorkerThread("GearmanServer", 4730, "MyWorker", slave.toComputer(), "master", new NoopAvailabilityMonitor()); oneiric.testInitWorker(); oneiric.registerJobs(); Set<String> functions = oneiric.worker.getRegisteredFunctions(); assertEquals(0, functions.size()); }
public Set<LabelAtom> getLabelSet() { return labelSet; }
public Set<LabelAtom> getLabelSet() { return Label.parse(label); }
@Nonnull public Set<LabelAtom> getLabelSet() { return labelSet != null ? labelSet : Collections.emptySet(); }
public Set<LabelAtom> getLabelSet(){ return labelSet; }
public ProvisionResult provisionJob(final String jobName, AbstractProject job, MappingWorksheet.WorkChunk task) throws Exception { JobValidationResult result = validateJob(task.assignedLabel).orNull(); if (result == null) { return ProvisionResult.NOT_SUPPORTED; } DockerJobProperty jobConfig = (DockerJobProperty) job.getProperty(DockerJobProperty.class); final String imageName = getImageName(jobConfig, result); boolean resetJob = false; Map<String, String> jobEnv = result.environment; if (jobConfig != null) { resetJob = jobConfig.resetJobEnabled(); Map<String, String> newEnv = newHashMap(jobEnv); newEnv.putAll(jobConfig.getEnvironmentVars()); jobEnv = ImmutableMap.copyOf(newEnv); } if (isNullOrEmpty(imageName)) { throw new RuntimeException(format("Unable to find docker image for job %s", jobName)); } if (availableCapacity() <= 0) { return ProvisionResult.NO_CAPACITY; } // Provision DockerJobSlave SlaveOptions options = new SlaveOptions(jobName, imageName); options.setCleanEnvironment(resetJob); options.setEnvironment(jobEnv); options.setDirectoryMappings(_directoryMappings); final DockerJobSlave slave = new DockerJobSlave( jobName + '-' + RandomStringUtils.random(6, true, true), "Job running in docker container", jobName, "/", ImmutableSet.<LabelAtom>builder() .addAll(result.labels) .add(new LabelAtom("image/" + imageName)) .build(), new DockerJobComputerLauncher(getDisplayName(), options)); _jenkins.addNode(slave); Computer.threadPoolForRemoting.submit(new Runnable() { @Override public void run() { try { Computer slaveComputer = slave.toComputer(); slaveComputer.connect(false).get(); } catch (Exception ex) { LOG.log(SEVERE, format("Error provisioning docker slave: job=%s image=%s", jobName, imageName), ex); throw Throwables.propagate(ex); } } }); return ProvisionResult.SUCCESS; }
private Optional<JobValidationResult> validateJob(Label label) { Set<LabelAtom> allLabels = Sets.union(_requiredLabels, _labels); if (label == null) { if (_requiredLabels.size() > 0) { LOG.log(FINE, "Condition does not include required labels: condition={0} cloud={1} required={2}", new Object[]{label, getDisplayName(), _requiredLabels}); return Optional.absent(); } else { LOG.log(FINE, "Condition matched cloud labels: condition={0} cloud={1} labels={2}", new Object[]{label, getDisplayName(), allLabels}); return Optional.of(new JobValidationResult(allLabels, null, ImmutableMap.<String, String>of())); } } // Check that the condition includes all the required atoms if (!label.listAtoms().containsAll(_requiredLabels)) { LOG.log(FINE, "Condition does not include required labels: condition={0} cloud={1} required={2}", new Object[]{label, getDisplayName(), _requiredLabels}); return Optional.absent(); } // Check if the condition matches the cloud's labels if (label.matches(allLabels)) { LOG.log(FINE, "Condition matched cloud labels: condition={0} cloud={1} labels={2}", new Object[]{label, getDisplayName(), allLabels}); return Optional.of(new JobValidationResult(allLabels, null, ImmutableMap.<String, String>of())); } // Check if the condition matches the cloud's labels combined with a specific image DockerJobGlobalConfiguration config = DockerJobGlobalConfiguration.get(); for (LabeledDockerImage image : config.getLabeledImages()) { Set<LabelAtom> imageLabels = Sets.union(image.getLabels(), allLabels); if (label.matches(imageLabels)) { LOG.log(FINE, "Condition matched cloud+image labels: condition={0} cloud={1} image={2} labels={3}", new Object[]{label, getDisplayName(), image.imageName, imageLabels}); return Optional.of(new JobValidationResult(imageLabels, image.imageName, image.getEnvironmentVars())); } else { LOG.log(FINE, "Condition does not match cloud+image labels: condition={0} cloud={1} image={2} labels={3}", new Object[]{label, getDisplayName(), image.imageName, imageLabels}); } } LOG.log(FINE, "Condition does not match cloud: condition={0} cloud={1} labels={2} images={3}", new Object[]{label, getDisplayName(), allLabels, config.getLabeledImages().size()}); return Optional.absent(); }
public JobValidationResult(Set<LabelAtom> labels, String imageName, Map<String, String> environment) { this.labels = labels; this.imageName = imageName; this.environment = environment; }
public Set<LabelAtom> getLabelSet(){ return this.labelSet; }
@Exported public Set<LabelAtom> getAssignedLabels() { Set<LabelAtom> r = new TreeSet<LabelAtom>(super.getAssignedLabels()); r.add(new LabelAtom(NODE_LABEL_STRING)); return Collections.unmodifiableSet(r); }
public Label getAssignedLabel() { return new LabelAtom(DeployNowSlave.NODE_LABEL_STRING); }
@Override public Label getAssignedLabel(@Nonnull SubTask task) { return new LabelAtom(uuid); }
@Override public Collection<LabelAtom> findLabels(Node node) { initPython(); return (Collection) pexec.execPython("find_labels", node); }
public TagCloud<LabelAtom> superGetLabelCloud() { return super.getLabelCloud(); }
public Set<LabelAtom> superGetAssignedLabels() { return super.getAssignedLabels(); }
public LabelAtom superGetSelfLabel() { return super.getSelfLabel(); }
@Test public void testLabelCaching() throws IOException, InterruptedException { printInfo("testLabelCaching()"); //Fetch the jenkins instance; which is a valid build host Jenkins j = Jenkins.getInstance(); //Set its label to a test value j.setLabelString("test:foo"); //Create a simple job XmlProject A = new XmlProject("A"); //Assign the test:foo label A.project.setAssignedLabel(new LabelAtom("test:foo")); //Build the job; should work try { QueueTaskFuture<InheritanceBuild> future = A.project.scheduleBuild2(0); InheritanceBuild b = future.get(5, TimeUnit.SECONDS); jRule.assertBuildStatusSuccess(b); //Then, change the label on the Jenkins instance j.setLabelString("test:bar"); //Refresh the node information, to force label re-association j.setNodes(j.getNodes()); //Nuke the buffers, to force label regeneration InheritanceProject.clearBuffers(null); //Build the job again. It must not be able to start, as the labels are wrong future = A.project.scheduleBuild2(0); Thread.sleep(5*1000); Assert.assertFalse( "Job A should not have run, as no host with a correct label is online", future.isDone() ); //Now, change the label and wait for at most 15 seconds for the //label cache to clear and the job to finish j.setLabelString("test:foo"); //Refresh the node information, to force label re-association j.setNodes(j.getNodes()); b = future.get(15, TimeUnit.SECONDS); jRule.assertBuildStatusSuccess(b); } catch (Exception ex) { Assert.fail(String.format( "Got exception during execution of project 'A':\n%s", ex.getMessage() )); } }
private static Label getLabel(String name) { Iterator<LabelAtom> i = Label.parse(name).iterator(); return i.hasNext() ? i.next() : null; }