hudson.tasks.BatchFile Java Examples

The following examples show how to use hudson.tasks.BatchFile. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BuildWrapperOrderCredentialsBindingTest.java    From credentials-binding-plugin with MIT License 6 votes vote down vote up
@Issue("JENKINS-37871")
@Test public void secretBuildWrapperRunsBeforeNormalWrapper() throws Exception {
    StringCredentialsImpl firstCreds = new StringCredentialsImpl(CredentialsScope.GLOBAL, credentialsId, "sample1", Secret.fromString(password));

    CredentialsProvider.lookupStores(r.jenkins).iterator().next().addCredentials(Domain.global(), firstCreds);

    SecretBuildWrapper wrapper = new SecretBuildWrapper(Arrays.asList(new StringBinding(bindingKey, credentialsId)));

    FreeStyleProject f = r.createFreeStyleProject("buildWrapperOrder");

    f.getBuildersList().add(Functions.isWindows() ? new BatchFile("echo %PASS_1%") : new Shell("echo $PASS_1"));
    f.getBuildWrappersList().add(new BuildWrapperOrder());
    f.getBuildWrappersList().add(wrapper);

    // configRoundtrip makes sure the ordinal of SecretBuildWrapper extension is applied correctly.
    r.configRoundtrip(f);

    FreeStyleBuild b = r.buildAndAssertSuccess(f);
    r.assertLogContains("Secret found!", b);
}
 
Example #2
Source File: UsernamePasswordBindingTest.java    From credentials-binding-plugin with MIT License 6 votes vote down vote up
@Test public void basics() throws Exception {
    String username = "bob";
    String password = "s3cr3t";
    UsernamePasswordCredentialsImpl c = new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, null, "sample", username, password);
    CredentialsProvider.lookupStores(r.jenkins).iterator().next().addCredentials(Domain.global(), c);
    FreeStyleProject p = r.createFreeStyleProject();
    p.getBuildWrappersList().add(new SecretBuildWrapper(Collections.<Binding<?>>singletonList(new UsernamePasswordBinding("AUTH", c.getId()))));
    p.getBuildersList().add(Functions.isWindows() ? new BatchFile("echo %AUTH% > auth.txt") : new Shell("echo $AUTH > auth.txt"));
    r.configRoundtrip(p);
    SecretBuildWrapper wrapper = p.getBuildWrappersList().get(SecretBuildWrapper.class);
    assertNotNull(wrapper);
    List<? extends MultiBinding<?>> bindings = wrapper.getBindings();
    assertEquals(1, bindings.size());
    MultiBinding<?> binding = bindings.get(0);
    assertEquals(c.getId(), binding.getCredentialsId());
    assertEquals(UsernamePasswordBinding.class, binding.getClass());
    assertEquals("AUTH", ((UsernamePasswordBinding) binding).getVariable());
    FreeStyleBuild b = r.buildAndAssertSuccess(p);
    r.assertLogNotContains(password, b);
    assertEquals(username + ':' + password, b.getWorkspace().child("auth.txt").readToString().trim());
    assertEquals("[AUTH]", b.getSensitiveBuildVariables().toString());
}
 
Example #3
Source File: MarathonBuilder.java    From marathonv5 with Apache License 2.0 5 votes vote down vote up
@Override public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener)
        throws InterruptedException, IOException {
    if (OS.indexOf("win") >= 0) {
        return new BatchFile(marathonHome + "marathon.bat " + getCommand()).perform(build, launcher, listener);
    } else if (OS.indexOf("mac") >= 0 || OS.indexOf("nix") >= 0 || OS.indexOf("nux") >= 0 || OS.indexOf("aix") > 0) {
        return new Shell(marathonHome + "marathon " + getCommand()).perform(build, launcher, listener);
    } else {
        listener.getLogger().println("Your system is not supported");
        return false;
    }

}
 
Example #4
Source File: IntegrationTest.java    From ec2-spot-jenkins-plugin with Apache License 2.0 5 votes vote down vote up
protected List<QueueTaskFuture> getQueueTaskFutures(int count) throws IOException {
    final LabelAtom label = new LabelAtom("momo");

    final List<QueueTaskFuture> rs = new ArrayList<>();
    for (int i = 0; i < count; i++) {
        final FreeStyleProject project = j.createFreeStyleProject();
        project.setAssignedLabel(label);
        project.getBuildersList().add(Functions.isWindows() ? new BatchFile("Ping -n " + JOB_SLEEP_TIME + " 127.0.0.1 > nul") : new Shell("sleep " + JOB_SLEEP_TIME));
        rs.add(project.scheduleBuild2(0));
    }
    return rs;
}
 
Example #5
Source File: AppTest.java    From jenkins-test-harness with MIT License 5 votes vote down vote up
private void meat() throws IOException, InterruptedException, ExecutionException {
    FreeStyleProject project = j.createFreeStyleProject();
    if(System.getProperty("os.name").contains("Windows")) {
        project.getBuildersList().add(new BatchFile("echo hello"));
    } else {
        project.getBuildersList().add(new Shell("echo hello"));
    }

    FreeStyleBuild build = project.scheduleBuild2(0).get();
    System.out.println(build.getDisplayName()+" completed");

    String s = FileUtils.readFileToString(build.getLogFile());
    assertTrue(s,s.contains("echo hello"));
}
 
Example #6
Source File: AggregatedTestResultPublisherTest.java    From junit-plugin with MIT License 5 votes vote down vote up
private void addFingerprinterToProject(FreeStyleProject project, String[] contents, String[] files) throws Exception {
    StringBuilder targets = new StringBuilder();
    for (int i = 0; i < contents.length; i++) {
        String command = "echo $BUILD_NUMBER " + contents[i] + " > " + files[i];
        project.getBuildersList().add(Functions.isWindows() ? new BatchFile(command) : new Shell(command));
        targets.append(files[i]).append(',');
    }

    project.getPublishersList().add(new Fingerprinter(targets.toString()));
}
 
Example #7
Source File: DollarSecretPatternFactoryTest.java    From credentials-binding-plugin with MIT License 5 votes vote down vote up
@Issue("JENKINS-24805")
@Test
public void maskingFreeStyleSecrets() throws Exception {
    String firstCredentialsId = "creds_1";
    String firstPassword = "a$build";
    StringCredentialsImpl firstCreds = new StringCredentialsImpl(CredentialsScope.GLOBAL, firstCredentialsId, "sample1", Secret.fromString(firstPassword));

    CredentialsProvider.lookupStores(r.jenkins).iterator().next().addCredentials(Domain.global(), firstCreds);

    String secondCredentialsId = "creds_2";
    String secondPassword = "a$$b";
    StringCredentialsImpl secondCreds = new StringCredentialsImpl(CredentialsScope.GLOBAL, secondCredentialsId, "sample2", Secret.fromString(secondPassword));

    CredentialsProvider.lookupStores(r.jenkins).iterator().next().addCredentials(Domain.global(), secondCreds);

    SecretBuildWrapper wrapper = new SecretBuildWrapper(Arrays.asList(new StringBinding("PASS_1", firstCredentialsId),
            new StringBinding("PASS_2", secondCredentialsId)));

    FreeStyleProject project = r.createFreeStyleProject();

    project.setConcurrentBuild(true);
    project.getBuildersList().add(Functions.isWindows() ? new BatchFile("echo %PASS_1%") : new Shell("echo \"$PASS_1\""));
    project.getBuildersList().add(Functions.isWindows() ? new BatchFile("echo %PASS_2%") : new Shell("echo \"$PASS_2\""));
    project.getBuildersList().add(new Maven("$PASS_1 $PASS_2", "default"));
    project.getBuildWrappersList().add(wrapper);

    r.configRoundtrip((Item)project);

    QueueTaskFuture<FreeStyleBuild> future = project.scheduleBuild2(0);
    FreeStyleBuild build = future.get();
    r.assertLogNotContains(firstPassword, build);
    r.assertLogNotContains(firstPassword.replace("$", "$$"), build);
    r.assertLogNotContains(secondPassword, build);
    r.assertLogNotContains(secondPassword.replace("$", "$$"), build);
    r.assertLogContains("****", build);
}
 
Example #8
Source File: UsernamePasswordMultiBindingTest.java    From credentials-binding-plugin with MIT License 5 votes vote down vote up
@Test public void basics() throws Exception {
    String username = "bob";
    String password = "s3cr3t";
    UsernamePasswordCredentialsImpl c = new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, null, "sample", username, password);
    CredentialsProvider.lookupStores(r.jenkins).iterator().next().addCredentials(Domain.global(), c);
    FreeStyleProject p = r.createFreeStyleProject();
    p.getBuildWrappersList().add(new SecretBuildWrapper(Collections.<MultiBinding<?>>singletonList(new UsernamePasswordMultiBinding("userid", "pass", c.getId()))));
    if (Functions.isWindows()) {
        p.getBuildersList().add(new BatchFile("@echo off\necho %userid%/%pass% > auth.txt"));
    } else {
        p.getBuildersList().add(new Shell("set +x\necho $userid/$pass > auth.txt"));
    }
    r.configRoundtrip((Item)p);
    SecretBuildWrapper wrapper = p.getBuildWrappersList().get(SecretBuildWrapper.class);
    assertNotNull(wrapper);
    List<? extends MultiBinding<?>> bindings = wrapper.getBindings();
    assertEquals(1, bindings.size());
    MultiBinding<?> binding = bindings.get(0);
    assertEquals(c.getId(), binding.getCredentialsId());
    assertEquals(UsernamePasswordMultiBinding.class, binding.getClass());
    assertEquals("userid", ((UsernamePasswordMultiBinding) binding).getUsernameVariable());
    assertEquals("pass", ((UsernamePasswordMultiBinding) binding).getPasswordVariable());
    FreeStyleBuild b = r.buildAndAssertSuccess(p);
    r.assertLogNotContains(password, b);
    assertEquals(username + '/' + password, b.getWorkspace().child("auth.txt").readToString().trim());
    assertEquals("[pass, userid]", new TreeSet<String>(b.getSensitiveBuildVariables()).toString());
}
 
Example #9
Source File: SecretBuildWrapperTest.java    From credentials-binding-plugin with MIT License 5 votes vote down vote up
@Issue("JENKINS-24805")
@Test public void maskingFreeStyleSecrets() throws Exception {
    String firstCredentialsId = "creds_1";
    String firstPassword = "p4$$";
    StringCredentialsImpl firstCreds = new StringCredentialsImpl(CredentialsScope.GLOBAL, firstCredentialsId, "sample1", Secret.fromString(firstPassword));

    CredentialsProvider.lookupStores(r.jenkins).iterator().next().addCredentials(Domain.global(), firstCreds);

    String secondCredentialsId = "creds_2";
    String secondPassword = "p4$$" + "someMoreStuff";
    StringCredentialsImpl secondCreds = new StringCredentialsImpl(CredentialsScope.GLOBAL, secondCredentialsId, "sample2", Secret.fromString(secondPassword));

    CredentialsProvider.lookupStores(r.jenkins).iterator().next().addCredentials(Domain.global(), secondCreds);

    SecretBuildWrapper wrapper = new SecretBuildWrapper(Arrays.asList(new StringBinding("PASS_1", firstCredentialsId),
            new StringBinding("PASS_2", secondCredentialsId)));

    FreeStyleProject f = r.createFreeStyleProject();

    f.setConcurrentBuild(true);
    f.getBuildersList().add(Functions.isWindows() ? new BatchFile("echo %PASS_1%") : new Shell("echo \"$PASS_1\""));
    f.getBuildersList().add(Functions.isWindows() ? new BatchFile("echo %PASS_2%") : new Shell("echo \"$PASS_2\""));
    f.getBuildWrappersList().add(wrapper);

    r.configRoundtrip((Item)f);

    FreeStyleBuild b = r.buildAndAssertSuccess(f);
    r.assertLogNotContains(firstPassword, b);
    r.assertLogNotContains(secondPassword, b);
    r.assertLogContains("****", b);
}
 
Example #10
Source File: SecretBuildWrapperTest.java    From credentials-binding-plugin with MIT License 5 votes vote down vote up
@Issue("JENKINS-24805")
@Test public void emptySecretsList() throws Exception {
    SecretBuildWrapper wrapper = new SecretBuildWrapper(new ArrayList<MultiBinding<?>>());

    FreeStyleProject f = r.createFreeStyleProject();

    f.setConcurrentBuild(true);
    f.getBuildersList().add(Functions.isWindows() ? new BatchFile("echo PASSES") : new Shell("echo PASSES"));
    f.getBuildWrappersList().add(wrapper);

    r.configRoundtrip((Item)f);

    FreeStyleBuild b = r.buildAndAssertSuccess(f);
    r.assertLogContains("PASSES", b);
}
 
Example #11
Source File: SecretBuildWrapperTest.java    From credentials-binding-plugin with MIT License 5 votes vote down vote up
@Issue("JENKINS-41760")
@Test public void emptySecret() throws Exception {
    CredentialsProvider.lookupStores(r.jenkins).iterator().next().addCredentials(Domain.global(), new StringCredentialsImpl(CredentialsScope.GLOBAL, "creds", null, Secret.fromString("")));
    FreeStyleProject p = r.createFreeStyleProject();
    p.getBuildWrappersList().add(new SecretBuildWrapper(Collections.singletonList(new StringBinding("SECRET", "creds"))));
    p.getBuildersList().add(Functions.isWindows() ? new BatchFile("echo PASSES") : new Shell("echo PASSES"));
    r.assertLogContains("PASSES", r.buildAndAssertSuccess(p));
}
 
Example #12
Source File: AutoResubmitIntegrationTest.java    From ec2-spot-jenkins-plugin with Apache License 2.0 4 votes vote down vote up
@Test
public void should_successfully_resubmit_parametrized_task() throws Exception {
    EC2FleetCloud cloud = new EC2FleetCloud(null, null, "credId", null, "region",
            null, "fId", "momo", null, new LocalComputerConnector(j), false, false,
            0, 0, 10, 1, false, false,
            false, 0, 0, false,
            10, false);
    j.jenkins.clouds.add(cloud);

    List<QueueTaskFuture> rs = new ArrayList<>();
    final FreeStyleProject project = j.createFreeStyleProject();
    project.setAssignedLabel(new LabelAtom("momo"));
    project.addProperty(new ParametersDefinitionProperty(new StringParameterDefinition("number", "opa")));
    /*
    example of actions for project

    actions = {CopyOnWriteArrayList@14845}  size = 2
        0 = {ParametersAction@14853}
        safeParameters = {TreeSet@14855}  size = 0
        parameters = {ArrayList@14856}  size = 1
        0 = {StringParameterValue@14862} "(StringParameterValue) number='1'"
        value = "1"
        name = "number"
        description = ""
        parameterDefinitionNames = {ArrayList@14857}  size = 1
        0 = "number"
        build = null
        run = {FreeStyleBuild@14834} "parameter #14"
     */
    project.getBuildersList().add(Functions.isWindows() ? new BatchFile("Ping -n %number% 127.0.0.1 > nul") : new Shell("sleep ${number}"));

    rs.add(project.scheduleBuild2(0, new ParametersAction(new StringParameterValue("number", "30"))));

    System.out.println("check if zero nodes!");
    Assert.assertEquals(0, j.jenkins.getNodes().size());

    assertAtLeastOneNode();

    final Node node = j.jenkins.getNodes().get(0);
    assertQueueIsEmpty();

    System.out.println("disconnect node");
    node.toComputer().disconnect(new OfflineCause.ChannelTermination(new UnsupportedOperationException("Test")));

    assertLastBuildResult(Result.FAILURE, Result.ABORTED);

    node.toComputer().connect(true);
    assertNodeIsOnline(node);
    assertQueueAndNodesIdle(node);

    Assert.assertEquals(1, j.jenkins.getProjects().size());
    Assert.assertEquals(Result.SUCCESS, j.jenkins.getProjects().get(0).getLastBuild().getResult());
    Assert.assertEquals(2, j.jenkins.getProjects().get(0).getBuilds().size());

    cancelTasks(rs);
}
 
Example #13
Source File: CertificateMultiBindingTest.java    From credentials-binding-plugin with MIT License 4 votes vote down vote up
@Test
public void basics() throws Exception {
	String alias = "androiddebugkey";
	String password = "android";
	StandardCertificateCredentials c = new CertificateCredentialsImpl(CredentialsScope.GLOBAL, null, alias,
			password, new CertificateCredentialsImpl.FileOnMasterKeyStoreSource(certificate.getAbsolutePath()));
	CredentialsProvider.lookupStores(r.jenkins).iterator().next().addCredentials(Domain.global(), c);
	FreeStyleProject p = r.createFreeStyleProject();
	CertificateMultiBinding binding = new CertificateMultiBinding("keystore", c.getId());
	binding.setAliasVariable("alias");
	binding.setPasswordVariable("password");
	p.getBuildWrappersList().add(new SecretBuildWrapper(Collections
			.<MultiBinding<?>> singletonList(binding)));
	if (Functions.isWindows()) {
		p.getBuildersList().add(new BatchFile(
				  "echo | set /p=\"%alias%/%password%/\" > secrets.txt\r\n"
				+ "IF EXIST \"%keystore%\" (\r\n"
				+ "echo | set /p=\"exists\" >> secrets.txt\r\n"
				+ ") ELSE (\r\n"
				+ "echo | set /p=\"missing\" >> secrets.txt\r\n"
				+ ")\r\n"
                   + "exit 0"));
	} else {
		p.getBuildersList().add(new Shell(
				  "printf $alias/$password/ > secrets.txt\n"
				+ "if [ -f \"$keystore\" ]\n"
				+ "then\n"
				+ "printf exists >> secrets.txt\n"
				+ "else\n"
				+ "printf missing >> secrets.txt\n"
				+ "fi"));
	}
	r.configRoundtrip((Item) p);
	SecretBuildWrapper wrapper = p.getBuildWrappersList().get(SecretBuildWrapper.class);
	assertNotNull(wrapper);
	List<? extends MultiBinding<?>> bindings = wrapper.getBindings();
	assertEquals(1, bindings.size());
	MultiBinding<?> testBinding = bindings.get(0);
	assertEquals(c.getId(), testBinding.getCredentialsId());
	assertEquals(CertificateMultiBinding.class, testBinding.getClass());
	assertEquals("password", ((CertificateMultiBinding) testBinding).getPasswordVariable());
	assertEquals("alias", ((CertificateMultiBinding) testBinding).getAliasVariable());
	assertEquals("keystore", ((CertificateMultiBinding) testBinding).getKeystoreVariable());
	FreeStyleBuild b = r.buildAndAssertSuccess(p);
	r.assertLogNotContains(password, b);
	assertEquals(alias + '/' + password + "/exists", b.getWorkspace().child("secrets.txt").readToString().trim());
	assertThat(b.getSensitiveBuildVariables(), containsInAnyOrder("keystore", "password", "alias"));
}