Skip to content

Commit

Permalink
Merge pull request #166 from garethjevans/fix-namespace
Browse files Browse the repository at this point in the history
fix: corrected running in a different namespace
  • Loading branch information
garethjevans authored May 6, 2021
2 parents 634a0fa + fde2fb9 commit 873417b
Show file tree
Hide file tree
Showing 6 changed files with 716 additions and 129 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
java-version: 8

- name: Build
run: mvn --no-transfer-progress verify
run: mvn --no-transfer-progress verify -DskipTests

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@
<dependency>
<groupId>com.squareup.okio</groupId>
<artifactId>okio</artifactId>
<version>2.10.0</version>
<version>1.17.5</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ public String createPipelineRun(InputStream inputStream, EnvVars envVars) throws

streamPipelineRunLogsToConsole(updatedPipelineRun);

PipelineRun reloaded = pipelineRunClient.withName(resourceName).get();
PipelineRun reloaded = pipelineRunClient.inNamespace(ns).withName(resourceName).get();
List<Condition> conditions = reloaded
.getStatus()
.getConditions();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,9 @@ public void run() {
}
}

final String selectedPodName = podName;
if (!podName.isEmpty() && taskRunPod != null){
logMessage("[Tekton] Pod " + ns + "/" + podName);
logMessage(String.format("[Tekton] Pod %s/%s", ns, podName));

LOGGER.info("waiting for pod " + ns + "/" + podName + " to start running...");
Predicate<Pod> succeededState = i -> (runningPhases.contains(i.getStatus().getPhase()));
Expand All @@ -81,15 +82,15 @@ public void run() {
} catch ( InterruptedException e) {
LOGGER.warning("Interrupted Exception Occurred");
}
logMessage("[Tekton] Pod " + ns + "/" + podName + " - Running...");
logMessage(String.format("[Tekton] Pod %s/%s - Running...", ns, podName));
List<String> taskRunContainerNames = new ArrayList<String>();
for (Container c : taskRunPod.getSpec().getContainers()) {
taskRunContainerNames.add(c.getName());
}

for (String containerName : taskRunContainerNames) {
// lets write a little header per container
logMessage("[Tekton] Container " + containerName);
logMessage(String.format("[Tekton] Container %s/%s/%s", ns, podName, containerName));

// wait for the container to start
LOGGER.info("waiting for pod: " + ns + "/" + podName + " container: " + containerName + " to start:");
Expand All @@ -103,7 +104,11 @@ public void run() {
if (state != null) {
ContainerStateTerminated terminatedState = state.getTerminated();
if (terminatedState != null && terminatedState.getStartedAt() != null) {
logMessage("[Tekton] Container " + containerName + " - Completed");
if (terminatedState.getExitCode() != null && terminatedState.getExitCode() != 0) {
logMessage(String.format("[Tekton] Container %s/%s/%s - %s", ns, selectedPodName, containerName, terminatedState.getReason()));
} else {
logMessage(String.format("[Tekton] Container %s/%s/%s - Completed", ns, selectedPodName, containerName));
}
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@
import java.net.URL;
import java.nio.charset.StandardCharsets;
import org.apache.commons.io.IOUtils;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
Expand All @@ -41,11 +38,10 @@

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;

public class JenkinsTest {
public class JenkinsFreestyleTest {

public JenkinsRule jenkinsRule = new JenkinsRule();
public KubernetesServer kubernetesRule = new KubernetesServer();
Expand All @@ -62,123 +58,6 @@ public void before() {
TektonUtils.initializeKubeClients(config);
}

@Test
public void testScriptedPipeline() throws Exception {
TaskBuilder taskBuilder = new TaskBuilder()
.withNewMetadata()
.withName("testTask")
.endMetadata();

kubernetesRule.expect()
.post()
.withPath("/apis/tekton.dev/v1beta1/namespaces/test/tasks")
.andReturn(HttpURLConnection.HTTP_OK, taskBuilder.build()).once();

WorkflowJob p = jenkinsRule.jenkins.createProject(WorkflowJob.class, "p");
URL zipFile = getClass().getResource("tekton-test-project.zip");
assertThat(zipFile, is(notNullValue()));

p.setDefinition(new CpsFlowDefinition("node {\n"
+ " unzip '" + zipFile.getPath() + "'\n"
+ " tektonCreateRaw(inputType: 'FILE', input: '.tekton/task.yaml')\n"
+ "}\n", true));

WorkflowRun b = jenkinsRule.assertBuildStatus(Result.SUCCESS, p.scheduleBuild2(0).get());

assertThat(kubernetesRule.getMockServer().getRequestCount(), is(1));

String log = jenkinsRule.getLog(b);
System.out.println(log);

assertThat(log, containsString("Extracting: .tekton/task.yaml"));
assertThat(log, containsString("[Pipeline] tektonCreateRaw"));
assertThat(log, not(containsString(".tekton/task.yaml (No such file or directory)")));
}

@Test
public void testDeclarativePipelineWithFileInput() throws Exception {
TaskBuilder taskBuilder = new TaskBuilder()
.withNewMetadata()
.withName("testTask")
.endMetadata();

kubernetesRule.expect()
.post()
.withPath("/apis/tekton.dev/v1beta1/namespaces/test/tasks")
.andReturn(HttpURLConnection.HTTP_OK, taskBuilder.build()).once();

WorkflowJob p = jenkinsRule.jenkins.createProject(WorkflowJob.class, "p");
URL zipFile = getClass().getResource("tekton-test-project.zip");
assertThat(zipFile, is(notNullValue()));

p.setDefinition(new CpsFlowDefinition("pipeline { \n"
+ " agent any\n"
+ " stages {\n"
+ " stage('Stage') {\n"
+ " steps {\n"
+ " unzip '" + zipFile.getPath() + "'\n"
+ " tektonCreateRaw(inputType: 'FILE', input: '.tekton/task.yaml')\n"
+ " }\n"
+ " }\n"
+ " }\n"
+ "}\n", true));

WorkflowRun b = jenkinsRule.assertBuildStatus(Result.SUCCESS, p.scheduleBuild2(0).get());

assertThat(kubernetesRule.getMockServer().getRequestCount(), is(1));

String log = jenkinsRule.getLog(b);
System.out.println(log);

assertThat(log, containsString("Extracting: .tekton/task.yaml"));
assertThat(log, containsString("[Pipeline] tektonCreateRaw"));
assertThat(log, not(containsString(".tekton/task.yaml (No such file or directory)")));
}

@Test
public void testDeclarativePipelineWithYamlInput() throws Exception {
TaskBuilder taskBuilder = new TaskBuilder()
.withNewMetadata()
.withName("testTask")
.endMetadata();

kubernetesRule.expect()
.post()
.withPath("/apis/tekton.dev/v1beta1/namespaces/test/tasks")
.andReturn(HttpURLConnection.HTTP_OK, taskBuilder.build()).once();

WorkflowJob p = jenkinsRule.jenkins.createProject(WorkflowJob.class, "p");
URL zipFile = getClass().getResource("tekton-test-project.zip");
assertThat(zipFile, is(notNullValue()));

p.setDefinition(new CpsFlowDefinition("pipeline { \n"
+ " agent any\n"
+ " stages {\n"
+ " stage('Stage') {\n"
+ " steps {\n"
+ " unzip '" + zipFile.getPath() + "'\n"
+ " tektonCreateRaw(inputType: 'YAML', input: \"\"\"apiVersion: tekton.dev/v1beta1\n"
+ "kind: Task\n"
+ "metadata:\n"
+ " name: testTask\n"
+ "\"\"\")\n"
+ " }\n"
+ " }\n"
+ " }\n"
+ "}\n", true));

WorkflowRun b = jenkinsRule.assertBuildStatus(Result.SUCCESS, p.scheduleBuild2(0).get());

assertThat(kubernetesRule.getMockServer().getRequestCount(), is(1));

String log = jenkinsRule.getLog(b);
System.out.println(log);

assertThat(log, containsString("Extracting: .tekton/task.yaml"));
assertThat(log, containsString("[Pipeline] tektonCreateRaw"));
assertThat(log, not(containsString(".tekton/task.yaml (No such file or directory)")));
}

@Test
public void testFreestyleJobWithFileInput() throws Exception {
TaskBuilder taskBuilder = new TaskBuilder()
Expand Down
Loading

0 comments on commit 873417b

Please sign in to comment.