Skip to content

Commit 39155d2

Browse files
committed
Add an integration test checking the workingdirs
We didn't check previously if workingdir really worked properly, let's add tests for this. Signed-off-by: Chmouel Boudjnah <[email protected]>
1 parent e228f11 commit 39155d2

File tree

2 files changed

+139
-0
lines changed

2 files changed

+139
-0
lines changed

examples/taskruns/workingdir.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ spec:
1212
- '-c'
1313
- '[[ $PWD == /workspace ]]'
1414

15+
- name: implicit
16+
image: ubuntu
17+
command: ['bash']
18+
workingDir: "foo" # implicitly mkdir's /workspace/foo
19+
args:
20+
- '-c'
21+
- '[[ $PWD == /workspace/foo ]]'
22+
1523
- name: override
1624
image: ubuntu
1725
command: ['bash']

test/workingdir_test.go

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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+
p, err := c.KubeClient.Kube.CoreV1().Pods(namespace).Get(tr.Status.PodName, metav1.GetOptions{})
69+
if err != nil {
70+
t.Fatalf("Error getting pod `%s` in namespace `%s`", tr.Status.PodName, namespace)
71+
}
72+
for _, stat := range p.Status.ContainerStatuses {
73+
if strings.HasPrefix(stat.Name, "working-dir-initializer") {
74+
if stat.State.Terminated != nil {
75+
req := c.KubeClient.Kube.CoreV1().Pods(namespace).GetLogs(p.Name, &corev1.PodLogOptions{Container: stat.Name})
76+
logContent, err := req.Do().Raw()
77+
if err != nil {
78+
t.Fatalf("Error getting pod logs for pod `%s` and container `%s` in namespace `%s`", tr.Status.PodName, stat.Name, namespace)
79+
}
80+
if string(logContent) != "" {
81+
t.Logf("Found some content in workingdir pod: %s, `%s` when it should be empty", tr.Status.PodName, logContent)
82+
}
83+
}
84+
}
85+
}
86+
}
87+
}
88+
89+
func TestWorkingDirIgnoredNonSlashWorkspace(t *testing.T) {
90+
c, namespace := setup(t)
91+
t.Parallel()
92+
93+
knativetest.CleanupOnInterrupt(func() { tearDown(t, c, namespace) }, t.Logf)
94+
defer tearDown(t, c, namespace)
95+
96+
task := tb.Task(wdTaskName, namespace, tb.TaskSpec(
97+
tb.Step("step1", "ubuntu", tb.StepWorkingDir("/HELLOMOTO"), tb.StepArgs("-c", "echo YES")),
98+
))
99+
if _, err := c.TaskClient.Create(task); err != nil {
100+
t.Fatalf("Failed to create Task: %s", err)
101+
}
102+
103+
t.Logf("Creating TaskRun namespace %s", namespace)
104+
taskRun := tb.TaskRun(wdTaskRunName, namespace, tb.TaskRunSpec(
105+
tb.TaskRunTaskRef(wdTaskName), tb.TaskRunServiceAccountName("default"),
106+
))
107+
if _, err := c.TaskRunClient.Create(taskRun); err != nil {
108+
t.Fatalf("Failed to create TaskRun: %s", err)
109+
}
110+
111+
t.Logf("Waiting for TaskRun in namespace %s to finish successfully", namespace)
112+
if err := WaitForTaskRunState(c, wdTaskRunName, TaskRunSucceed(wdTaskRunName), "TaskRunSuccess"); err != nil {
113+
t.Errorf("Error waiting for TaskRun to finish successfully: %s", err)
114+
}
115+
116+
tr, err := c.TaskRunClient.Get(wdTaskRunName, metav1.GetOptions{})
117+
if err != nil {
118+
t.Errorf("Error retrieving taskrun: %s", err)
119+
}
120+
if tr.Status.PodName != "" {
121+
p, err := c.KubeClient.Kube.CoreV1().Pods(namespace).Get(tr.Status.PodName, metav1.GetOptions{})
122+
if err != nil {
123+
t.Fatalf("Error getting pod `%s` in namespace `%s`", tr.Status.PodName, namespace)
124+
}
125+
for _, stat := range p.Status.ContainerStatuses {
126+
if strings.HasPrefix(stat.Name, "working-dir-initializer") {
127+
t.Logf("Found a working dir container called `%s` in `%s` when it should have been excluded:", stat.Name, tr.Status.PodName)
128+
}
129+
}
130+
}
131+
}

0 commit comments

Comments
 (0)