|
| 1 | +// +build e2e |
| 2 | + |
| 3 | +/* |
| 4 | +Copyright 2019 The Tekton Authors |
| 5 | +
|
| 6 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +you may not use this file except in compliance with the License. |
| 8 | +You may obtain a copy of the License at |
| 9 | +
|
| 10 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +
|
| 12 | +Unless required by applicable law or agreed to in writing, software |
| 13 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +See the License for the specific language governing permissions and |
| 16 | +limitations under the License. |
| 17 | +*/ |
| 18 | + |
| 19 | +package test |
| 20 | + |
| 21 | +import ( |
| 22 | + "strings" |
| 23 | + "testing" |
| 24 | + |
| 25 | + tb "github.com/tektoncd/pipeline/test/builder" |
| 26 | + corev1 "k8s.io/api/core/v1" |
| 27 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 28 | + knativetest "knative.dev/pkg/test" |
| 29 | +) |
| 30 | + |
| 31 | +const ( |
| 32 | + wdTaskName = "wd-task" |
| 33 | + wdTaskRunName = "wd-task-run" |
| 34 | +) |
| 35 | + |
| 36 | +func TestWorkingDirCreated(t *testing.T) { |
| 37 | + c, namespace := setup(t) |
| 38 | + t.Parallel() |
| 39 | + |
| 40 | + knativetest.CleanupOnInterrupt(func() { tearDown(t, c, namespace) }, t.Logf) |
| 41 | + defer tearDown(t, c, namespace) |
| 42 | + |
| 43 | + task := tb.Task(wdTaskName, namespace, tb.TaskSpec( |
| 44 | + tb.Step("step1", "ubuntu", tb.StepWorkingDir("/workspace/HELLOMOTO"), tb.StepArgs("-c", "echo YES")), |
| 45 | + )) |
| 46 | + if _, err := c.TaskClient.Create(task); err != nil { |
| 47 | + t.Fatalf("Failed to create Task: %s", err) |
| 48 | + } |
| 49 | + |
| 50 | + t.Logf("Creating TaskRun namespace %s", namespace) |
| 51 | + taskRun := tb.TaskRun(wdTaskRunName, namespace, tb.TaskRunSpec( |
| 52 | + tb.TaskRunTaskRef(wdTaskName), tb.TaskRunServiceAccountName("default"), |
| 53 | + )) |
| 54 | + if _, err := c.TaskRunClient.Create(taskRun); err != nil { |
| 55 | + t.Fatalf("Failed to create TaskRun: %s", err) |
| 56 | + } |
| 57 | + |
| 58 | + t.Logf("Waiting for TaskRun in namespace %s to finish successfully", namespace) |
| 59 | + if err := WaitForTaskRunState(c, wdTaskRunName, TaskRunSucceed(wdTaskRunName), "TaskRunSuccess"); err != nil { |
| 60 | + t.Errorf("Error waiting for TaskRun to finish successfully: %s", err) |
| 61 | + } |
| 62 | + |
| 63 | + tr, err := c.TaskRunClient.Get(wdTaskRunName, metav1.GetOptions{}) |
| 64 | + if err != nil { |
| 65 | + t.Errorf("Error retrieving taskrun: %s", err) |
| 66 | + } |
| 67 | + if tr.Status.PodName == "" { |
| 68 | + t.Fatal("Error getting a PodName (empty)") |
| 69 | + } |
| 70 | + p, err := c.KubeClient.Kube.CoreV1().Pods(namespace).Get(tr.Status.PodName, metav1.GetOptions{}) |
| 71 | + if err != nil { |
| 72 | + t.Fatalf("Error getting pod `%s` in namespace `%s`", tr.Status.PodName, namespace) |
| 73 | + } |
| 74 | + for _, stat := range p.Status.ContainerStatuses { |
| 75 | + if strings.HasPrefix(stat.Name, "working-dir-initializer") { |
| 76 | + if stat.State.Terminated != nil { |
| 77 | + req := c.KubeClient.Kube.CoreV1().Pods(namespace).GetLogs(p.Name, &corev1.PodLogOptions{Container: stat.Name}) |
| 78 | + logContent, err := req.Do().Raw() |
| 79 | + if err != nil { |
| 80 | + t.Fatalf("Error getting pod logs for pod `%s` and container `%s` in namespace `%s`", tr.Status.PodName, stat.Name, namespace) |
| 81 | + } |
| 82 | + if string(logContent) != "" { |
| 83 | + t.Logf("Found some content in workingdir pod: %s, `%s` when it should be empty", tr.Status.PodName, logContent) |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +func TestWorkingDirIgnoredNonSlashWorkspace(t *testing.T) { |
| 91 | + c, namespace := setup(t) |
| 92 | + t.Parallel() |
| 93 | + |
| 94 | + knativetest.CleanupOnInterrupt(func() { tearDown(t, c, namespace) }, t.Logf) |
| 95 | + defer tearDown(t, c, namespace) |
| 96 | + |
| 97 | + task := tb.Task(wdTaskName, namespace, tb.TaskSpec( |
| 98 | + tb.Step("step1", "ubuntu", tb.StepWorkingDir("/HELLOMOTO"), tb.StepArgs("-c", "echo YES")), |
| 99 | + )) |
| 100 | + if _, err := c.TaskClient.Create(task); err != nil { |
| 101 | + t.Fatalf("Failed to create Task: %s", err) |
| 102 | + } |
| 103 | + |
| 104 | + t.Logf("Creating TaskRun namespace %s", namespace) |
| 105 | + taskRun := tb.TaskRun(wdTaskRunName, namespace, tb.TaskRunSpec( |
| 106 | + tb.TaskRunTaskRef(wdTaskName), tb.TaskRunServiceAccountName("default"), |
| 107 | + )) |
| 108 | + if _, err := c.TaskRunClient.Create(taskRun); err != nil { |
| 109 | + t.Fatalf("Failed to create TaskRun: %s", err) |
| 110 | + } |
| 111 | + |
| 112 | + t.Logf("Waiting for TaskRun in namespace %s to finish successfully", namespace) |
| 113 | + if err := WaitForTaskRunState(c, wdTaskRunName, TaskRunSucceed(wdTaskRunName), "TaskRunSuccess"); err != nil { |
| 114 | + t.Errorf("Error waiting for TaskRun to finish successfully: %s", err) |
| 115 | + } |
| 116 | + |
| 117 | + tr, err := c.TaskRunClient.Get(wdTaskRunName, metav1.GetOptions{}) |
| 118 | + if err != nil { |
| 119 | + t.Errorf("Error retrieving taskrun: %s", err) |
| 120 | + } |
| 121 | + |
| 122 | + p, err := c.KubeClient.Kube.CoreV1().Pods(namespace).Get(tr.Status.PodName, metav1.GetOptions{}) |
| 123 | + if err != nil { |
| 124 | + t.Fatalf("Error getting pod `%s` in namespace `%s`", tr.Status.PodName, namespace) |
| 125 | + } |
| 126 | + for _, stat := range p.Status.ContainerStatuses { |
| 127 | + if strings.HasPrefix(stat.Name, "working-dir-initializer") { |
| 128 | + t.Logf("Found a working dir container called `%s` in `%s` when it should have been excluded:", stat.Name, tr.Status.PodName) |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | +} |
0 commit comments