Search in sources :

Example 6 with DumbSlave

use of org.jenkinsci.test.acceptance.po.DumbSlave in project acceptance-test-harness by jenkinsci.

the class DeclarativePipelineTest method basicDeclarativeTests.

@WithPlugins("pipeline-model-definition")
@Test
public void basicDeclarativeTests() throws Exception {
    WorkflowJob helloWorldJob = jenkins.jobs.create(WorkflowJob.class);
    helloWorldJob.script.set("pipeline {\n" + "  agent none\n" + "  stages {\n" + "    stage('foo') {\n" + "      steps {\n" + "        echo 'Hello world'\n" + "      }\n" + "    }\n" + "  }\n" + "}\n");
    helloWorldJob.sandbox.check();
    helloWorldJob.save();
    Build helloWorldBuild = helloWorldJob.startBuild().shouldSucceed();
    assertThat(helloWorldBuild.getConsole(), containsRegexp("Hello world", Pattern.MULTILINE));
    MavenInstallation.installMaven(jenkins, "M3", "3.1.0");
    final DumbSlave slave = (DumbSlave) slaveController.install(jenkins).get();
    slave.configure(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            slave.labels.set("remote");
            return null;
        }
    });
    WorkflowJob toolsEnvAgentJob = jenkins.jobs.create(WorkflowJob.class);
    toolsEnvAgentJob.script.set("pipeline {\n" + "  agent { label 'remote' }\n" + "  tools {\n" + "    maven 'M3'\n" + "  }\n" + "  environment {\n" + "    FOO = 'BAR'\n" + "  }\n" + "  stages {\n" + "    stage('first') {\n" + "      steps {\n" + "        sh 'mvn --version'\n" + "      }\n" + "    }\n" + "    stage('second') {\n" + "      steps {\n" + "        echo \"FOO is ${env.FOO}\"\n" + "      }\n" + "    }\n" + "  }\n" + "}\n");
    toolsEnvAgentJob.sandbox.check();
    toolsEnvAgentJob.save();
    Build toolsEnvAgentBuild = toolsEnvAgentJob.startBuild().shouldSucceed();
    String toolsEnvAgentConsole = toolsEnvAgentBuild.getConsole();
    assertThat(toolsEnvAgentConsole, containsRegexp("\\(first\\)", Pattern.MULTILINE));
    assertThat(toolsEnvAgentConsole, containsRegexp("Apache Maven 3\\.1\\.0", Pattern.MULTILINE));
    assertThat(toolsEnvAgentConsole, containsRegexp("\\(second\\)", Pattern.MULTILINE));
    assertThat(toolsEnvAgentConsole, containsRegexp("FOO is BAR", Pattern.MULTILINE));
    WorkflowJob missingAgentJob = jenkins.jobs.create(WorkflowJob.class);
    missingAgentJob.script.set("pipeline {\n" + "  stages {\n" + "    stage('foo') {\n" + "      steps {\n" + "        echo 'Hello world'\n" + "      }\n" + "    }\n" + "  }\n" + "}\n");
    missingAgentJob.sandbox.check();
    missingAgentJob.save();
    Build missingAgentBuild = missingAgentJob.startBuild().shouldFail();
    String missingAgentConsole = missingAgentBuild.getConsole();
    assertThat(missingAgentConsole, containsRegexp("Missing required section ['\"]agent['\"]"));
    assertThat(missingAgentConsole, not(containsRegexp("Hello world")));
    WorkflowJob missingToolVersionJob = jenkins.jobs.create(WorkflowJob.class);
    missingToolVersionJob.script.set("pipeline {\n" + "  agent { label 'remote' }\n" + "  tools {\n" + "    maven 'some-other-version'\n" + "  }\n" + "  environment {\n" + "    FOO = 'BAR'\n" + "  }\n" + "  stages {\n" + "    stage('first') {\n" + "      steps {\n" + "        sh 'mvn --version'\n" + "      }\n" + "    }\n" + "    stage('second') {\n" + "      steps {\n" + "        echo \"FOO is ${env.FOO}\"\n" + "      }\n" + "    }\n" + "  }\n" + "}\n");
    missingToolVersionJob.sandbox.check();
    missingToolVersionJob.save();
    Build missingToolVersionBuild = missingToolVersionJob.startBuild().shouldFail();
    String missingToolVersionConsole = missingToolVersionBuild.getConsole();
    assertThat(missingToolVersionConsole, containsRegexp("Tool type ['\"]maven['\"] does not have an install of ['\"]some-other-version['\"] configured"));
    assertThat(missingToolVersionConsole, not(containsRegexp("FOO is BAR")));
}
Also used : Build(org.jenkinsci.test.acceptance.po.Build) WorkflowJob(org.jenkinsci.test.acceptance.po.WorkflowJob) DumbSlave(org.jenkinsci.test.acceptance.po.DumbSlave) AbstractJUnitTest(org.jenkinsci.test.acceptance.junit.AbstractJUnitTest) Test(org.junit.Test) WithPlugins(org.jenkinsci.test.acceptance.junit.WithPlugins)

Example 7 with DumbSlave

use of org.jenkinsci.test.acceptance.po.DumbSlave in project acceptance-test-harness by jenkinsci.

the class LocalSlaveController method install.

@Override
public Future<Slave> install(Jenkins jenkins) {
    // Just to make sure the dumb slave is set up properly, we should seed it
    // with a FS root and executors
    final DumbSlave s = jenkins.slaves.create(DumbSlave.class);
    s.asLocal();
    s.save();
    s.waitUntilOnline();
    BasicFuture<Slave> b = new BasicFuture<>(null);
    b.completed(s);
    return b;
}
Also used : DumbSlave(org.jenkinsci.test.acceptance.po.DumbSlave) Slave(org.jenkinsci.test.acceptance.po.Slave) BasicFuture(org.apache.http.concurrent.BasicFuture) DumbSlave(org.jenkinsci.test.acceptance.po.DumbSlave)

Example 8 with DumbSlave

use of org.jenkinsci.test.acceptance.po.DumbSlave in project acceptance-test-harness by jenkinsci.

the class XvncSlaveContainer method connect.

/**
 * Attaches the slave to Jenkins.
 * @param j the server
 * @return return a configured slave; call {@link Slave#save} after any other customizations to complete; use {@link Slave#waitUntilOnline} if desired
 */
public Slave connect(Jenkins j) {
    // Some code from SshSlaveController could be applicable here, but looks too tricky to reuse.
    DumbSlave s = j.slaves.create(DumbSlave.class);
    SshSlaveLauncher launcher = s.setLauncher(SshSlaveLauncher.class);
    launcher.host.set(ipBound(22));
    launcher.port(port(22));
    launcher.pwdCredentials("test", "test");
    launcher.setSshHostKeyVerificationStrategy(SshSlaveLauncher.NonVerifyingKeyVerificationStrategy.class);
    s.remoteFS.set("/home/test");
    s.setExecutors(1);
    return s;
}
Also used : SshSlaveLauncher(org.jenkinsci.test.acceptance.plugins.ssh_slaves.SshSlaveLauncher) DumbSlave(org.jenkinsci.test.acceptance.po.DumbSlave)

Example 9 with DumbSlave

use of org.jenkinsci.test.acceptance.po.DumbSlave in project acceptance-test-harness by jenkinsci.

the class WorkflowPluginTest method sshGitInsideDocker.

@Category(DockerTest.class)
@WithDocker
@WithPlugins({ "workflow-cps@2.0", "workflow-job@2.0", "workflow-durable-task-step@2.0", "docker-workflow@1.5", "git", "ssh-agent@1.10", "ssh-slaves@1.11" })
@WithCredentials(credentialType = WithCredentials.SSH_USERNAME_PRIVATE_KEY, values = { "git", "/org/jenkinsci/test/acceptance/docker/fixtures/GitContainer/unsafe" }, id = "gitcreds")
@Issue("JENKINS-27152")
@Test
public void sshGitInsideDocker() throws Exception {
    GitContainer gitContainer = gitServer.get();
    try (GitRepo repo = new GitRepo()) {
        repo.changeAndCommitFoo("Initial commit");
        repo.transferToDockerContainer(gitContainer.host(), gitContainer.port());
        DumbSlave slave = jenkins.slaves.create(DumbSlave.class);
        slave.setExecutors(1);
        // TODO perhaps should be a constant in SshdContainer
        slave.remoteFS.set("/home/test");
        SshSlaveLauncher launcher = slave.setLauncher(SshSlaveLauncher.class);
        Process proc = new ProcessBuilder("stat", "-c", "%g", "/var/run/docker.sock").start();
        String group = IOUtils.toString(proc.getInputStream()).trim();
        Assume.assumeThat("docker.sock can be statted", proc.waitFor(), is(0));
        try {
            Integer.parseInt(group);
        } catch (NumberFormatException x) {
            Assume.assumeNoException("unexpected output from stat on docker.sock", x);
        }
        // Note that we need to link to the Git container both on the agent, for the benefit of JGit (TODO pending JENKINS-30600), and in the build container, for the benefit of git-pull:
        DockerAgentContainer agentContainer = agent.starter().withOptions(new CommandBuilder("--volume=/var/run/docker.sock:/var/run/docker.sock", "--env=DOCKER_GROUP=" + group, "--link=" + gitContainer.getCid() + ":git")).start();
        launcher.host.set(agentContainer.ipBound(22));
        launcher.port(agentContainer.port(22));
        launcher.pwdCredentials("test", "test");
        launcher.setSshHostKeyVerificationStrategy(SshSlaveLauncher.NonVerifyingKeyVerificationStrategy.class);
        slave.save();
        {
            // TODO JENKINS-30600 workaround
            JGitInstallation.addJGit(jenkins);
            find(by.button("Save")).click();
        }
        WorkflowJob job = jenkins.jobs.create(WorkflowJob.class);
        String networkOptions = StringUtils.isNotBlank(System.getenv("DOCKER_FIXTURES_NETWORK")) ? " --network=" + System.getenv("DOCKER_FIXTURES_NETWORK") : "";
        String options = "--link=" + gitContainer.getCid() + ":git" + networkOptions;
        String repoUrl = StringUtils.isNotBlank(System.getenv("DOCKER_FIXTURES_NETWORK")) ? gitContainer.getRepoUrlInsideDocker() : gitContainer.getRepoUrlInsideDocker("git");
        job.script.set("node('" + slave.getName() + "') {\n" + "  docker.image('cloudbees/java-build-tools').inside('" + options + "') {\n" + // TODO JENKINS-30600: "    git url: '" + gitContainer.getRepoUrlInsideDocker("git") + "', credentialsId: 'gitcreds'\n" +
        "    checkout([$class: 'GitSCM', userRemoteConfigs: [[url: '" + repoUrl + "', credentialsId: 'gitcreds']], gitTool: 'jgit'])\n" + "    sh 'mkdir ~/.ssh && echo StrictHostKeyChecking no > ~/.ssh/config'\n" + "    sshagent(['gitcreds']) {sh 'ls -l $SSH_AUTH_SOCK && git pull origin master'}\n" + "  }\n" + "}");
        job.sandbox.check();
        job.save();
        assertThat(job.startBuild().shouldSucceed().getConsole(), containsString("-> FETCH_HEAD"));
    }
}
Also used : SshSlaveLauncher(org.jenkinsci.test.acceptance.plugins.ssh_slaves.SshSlaveLauncher) GitContainer(org.jenkinsci.test.acceptance.docker.fixtures.GitContainer) GitRepo(org.jenkinsci.test.acceptance.plugins.git.GitRepo) DockerAgentContainer(org.jenkinsci.test.acceptance.docker.fixtures.DockerAgentContainer) Matchers.containsString(org.hamcrest.Matchers.containsString) DumbSlave(org.jenkinsci.test.acceptance.po.DumbSlave) CommandBuilder(org.jenkinsci.utils.process.CommandBuilder) WorkflowJob(org.jenkinsci.test.acceptance.po.WorkflowJob) Category(org.junit.experimental.categories.Category) WithCredentials(org.jenkinsci.test.acceptance.junit.WithCredentials) Issue(org.jvnet.hudson.test.Issue) AbstractJUnitTest(org.jenkinsci.test.acceptance.junit.AbstractJUnitTest) Test(org.junit.Test) DockerTest(org.jenkinsci.test.acceptance.junit.DockerTest) WithPlugins(org.jenkinsci.test.acceptance.junit.WithPlugins) WithDocker(org.jenkinsci.test.acceptance.junit.WithDocker)

Example 10 with DumbSlave

use of org.jenkinsci.test.acceptance.po.DumbSlave in project acceptance-test-harness by jenkinsci.

the class WorkflowPluginTest method linearFlow.

@WithPlugins({ "workflow-job", "workflow-cps@2.10", "workflow-basic-steps@2.1", "workflow-durable-task-step", "pipeline-input-step", "junit@1.18", "git@2.3" })
@Test
public void linearFlow() throws Exception {
    assumeTrue("This test requires a restartable Jenkins", jenkins.canRestart());
    MavenInstallation.installMaven(jenkins, "M3", "3.5.0");
    final DumbSlave slave = (DumbSlave) slaveController.install(jenkins).get();
    slave.configure(() -> slave.labels.set("remote"));
    WorkflowJob job = jenkins.jobs.create(WorkflowJob.class);
    job.script.set("node('remote') {\n" + "  git 'https://github.com/jglick/simple-maven-project-with-tests.git'\n" + "  def v = version()\n" + "  if (v) {\n" + "    echo(/Building version $v/)\n" + "  }\n" + "  def mvnHome = tool 'M3'\n" + "  withEnv([\"PATH+MAVEN=$mvnHome/bin\", \"M2_HOME=$mvnHome\"]) {\n" + "    sh 'mvn -B -Dmaven.test.failure.ignore verify'\n" + "  }\n" + "  input 'Ready to go?'\n" + "  archiveArtifacts artifacts: '**/target/*.jar', fingerprint: true\n" + "  junit '**/target/surefire-reports/TEST-*.xml'\n" + "}\n" + "def version() {\n" + "  def matcher = readFile('pom.xml') =~ '<version>(.+)</version>'\n" + "  matcher ? matcher[0][1] : null\n" + "}");
    job.sandbox.check();
    job.save();
    final Build build = job.startBuild();
    waitFor().until(new Wait.Predicate<Boolean>() {

        @Override
        public Boolean apply() throws Exception {
            return build.getConsole().contains("Ready to go?");
        }

        @Override
        public String diagnose(Throwable lastException, String message) {
            return "Console output:\n" + build.getConsole() + "\n";
        }
    });
    assertThat(build.getConsole(), containsString("Building version 1.0-SNAPSHOT"));
    jenkins.restart();
    // Default 120s timeout of Build.waitUntilFinished sometimes expires waiting for RetentionStrategy.Always to tick (after initial failure of CommandLauncher.launch: EOFException: unexpected stream termination):
    // TODO rather wait for build output: "Ready to run"
    slave.waitUntilOnline();
    visit(build.getConsoleUrl());
    clickLink("Proceed");
    try {
        build.shouldSucceed();
    } catch (AssertionError x) {
        // Tests in this project are currently designed to fail at random, so either status is OK.
        // TODO if resultIs were public and there were a disjunction combinator for Matcher we could use it here.
        build.shouldBeUnstable();
    }
    new Artifact(build, "target/simple-maven-project-with-tests-1.0-SNAPSHOT.jar").assertThatExists(true);
    build.open();
    clickLink("Test Result");
    assertThat(driver, hasContent("All Tests"));
}
Also used : Build(org.jenkinsci.test.acceptance.po.Build) Matchers.containsString(org.hamcrest.Matchers.containsString) DumbSlave(org.jenkinsci.test.acceptance.po.DumbSlave) WorkflowJob(org.jenkinsci.test.acceptance.po.WorkflowJob) Wait(org.jenkinsci.test.acceptance.junit.Wait) IOException(java.io.IOException) Artifact(org.jenkinsci.test.acceptance.po.Artifact) AbstractJUnitTest(org.jenkinsci.test.acceptance.junit.AbstractJUnitTest) Test(org.junit.Test) DockerTest(org.jenkinsci.test.acceptance.junit.DockerTest) WithPlugins(org.jenkinsci.test.acceptance.junit.WithPlugins)

Aggregations

DumbSlave (org.jenkinsci.test.acceptance.po.DumbSlave)17 Test (org.junit.Test)12 AbstractJUnitTest (org.jenkinsci.test.acceptance.junit.AbstractJUnitTest)8 WithPlugins (org.jenkinsci.test.acceptance.junit.WithPlugins)8 SshSlaveLauncher (org.jenkinsci.test.acceptance.plugins.ssh_slaves.SshSlaveLauncher)7 Matchers.containsString (org.hamcrest.Matchers.containsString)6 WithCredentials (org.jenkinsci.test.acceptance.junit.WithCredentials)5 WithDocker (org.jenkinsci.test.acceptance.junit.WithDocker)5 Build (org.jenkinsci.test.acceptance.po.Build)5 FreeStyleJob (org.jenkinsci.test.acceptance.po.FreeStyleJob)5 DockerTest (org.jenkinsci.test.acceptance.junit.DockerTest)4 SshPrivateKeyCredential (org.jenkinsci.test.acceptance.plugins.ssh_credentials.SshPrivateKeyCredential)4 WarningsPublisher (org.jenkinsci.test.acceptance.plugins.warnings.WarningsPublisher)3 WorkflowJob (org.jenkinsci.test.acceptance.po.WorkflowJob)3 Issue (org.jvnet.hudson.test.Issue)3 IOException (java.io.IOException)2 Since (org.jenkinsci.test.acceptance.junit.Since)2 CredentialsPage (org.jenkinsci.test.acceptance.plugins.credentials.CredentialsPage)2 ManagedCredentials (org.jenkinsci.test.acceptance.plugins.credentials.ManagedCredentials)2 SshCredentialDialog (org.jenkinsci.test.acceptance.plugins.ssh_credentials.SshCredentialDialog)2