Skip to content

Commit 095983a

Browse files
committed
mirror podtemplate
1 parent aabe9e6 commit 095983a

File tree

6 files changed

+197
-17
lines changed

6 files changed

+197
-17
lines changed

pkg/pods/pod_template.go

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919

2020
"github.com/tektoncd/cli/pkg/file"
2121
"github.com/tektoncd/pipeline/pkg/apis/pipeline/pod"
22-
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
2322
"sigs.k8s.io/yaml"
2423
)
2524

@@ -36,19 +35,3 @@ func ParsePodTemplate(httpClient http.Client, podTemplateLocation string, valida
3635

3736
return podTemplate, nil
3837
}
39-
40-
type TaskRunSpec = []v1beta1.PipelineTaskRunSpec
41-
42-
func ParseTaskRunSpec(httpClient http.Client, taskRunSpecLocation string, validate file.TypeValidator, errorMsg error) (TaskRunSpec, error) {
43-
taskRunSpec := TaskRunSpec{}
44-
b, err := file.LoadFileContent(httpClient, taskRunSpecLocation, validate, errorMsg)
45-
if err != nil {
46-
return taskRunSpec, err
47-
}
48-
49-
if err := yaml.UnmarshalStrict(b, &taskRunSpec); err != nil {
50-
return taskRunSpec, err
51-
}
52-
53-
return taskRunSpec, nil
54-
}

pkg/pods/task_run_spec.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright © 2020 The Tekton Authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package pods
16+
17+
import (
18+
"net/http"
19+
20+
"github.com/tektoncd/cli/pkg/file"
21+
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
22+
"sigs.k8s.io/yaml"
23+
)
24+
25+
type TaskRunSpec = []v1beta1.PipelineTaskRunSpec
26+
27+
func ParseTaskRunSpec(httpClient http.Client, taskRunSpecLocation string, validate file.TypeValidator, errorMsg error) (TaskRunSpec, error) {
28+
taskRunSpec := TaskRunSpec{}
29+
b, err := file.LoadFileContent(httpClient, taskRunSpecLocation, validate, errorMsg)
30+
if err != nil {
31+
return taskRunSpec, err
32+
}
33+
34+
if err := yaml.UnmarshalStrict(b, &taskRunSpec); err != nil {
35+
return taskRunSpec, err
36+
}
37+
38+
return taskRunSpec, nil
39+
}

pkg/pods/task_run_spec_test.go

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// Copyright © 2020 The Tekton Authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package pods
16+
17+
import (
18+
"fmt"
19+
"net/http"
20+
"testing"
21+
22+
"github.com/tektoncd/cli/pkg/file"
23+
"github.com/tektoncd/cli/pkg/test"
24+
"github.com/tektoncd/pipeline/pkg/apis/pipeline/pod"
25+
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
26+
corev1 "k8s.io/api/core/v1"
27+
)
28+
29+
func getTestTaskRunSpec() TaskRunSpec {
30+
runAsNonRoot := true
31+
runAsUser := int64(1001)
32+
33+
return TaskRunSpec{
34+
v1beta1.PipelineTaskRunSpec{
35+
PipelineTaskName: "first-create-file",
36+
TaskPodTemplate: &pod.PodTemplate{
37+
ImagePullSecrets: nil,
38+
HostNetwork: false,
39+
SchedulerName: "SchedulerName",
40+
SecurityContext: &corev1.PodSecurityContext{
41+
RunAsNonRoot: &runAsNonRoot,
42+
RunAsUser: &runAsUser,
43+
},
44+
},
45+
},
46+
}
47+
}
48+
49+
func TestTaskRunSpec_Local_File(t *testing.T) {
50+
httpClient := *http.DefaultClient
51+
podTemplateLocation := "./testdata/taskrunspec.yaml"
52+
53+
podTemplate, err := ParseTaskRunSpec(httpClient, podTemplateLocation, file.IsYamlFile(), fmt.Errorf("invalid file format for %s: .yaml or .yml file extension and format required", podTemplateLocation))
54+
if err != nil {
55+
t.Errorf("Unexpected error: %v", err)
56+
}
57+
58+
test.AssertOutput(t, getTestTaskRunSpec(), podTemplate)
59+
}
60+
61+
func TestTaskRunSpec_Local_File_Typo(t *testing.T) {
62+
httpClient := *http.DefaultClient
63+
podTemplateLocation := "./testdata/taskrunspec-typo.yaml"
64+
65+
_, err := ParseTaskRunSpec(httpClient, podTemplateLocation, file.IsYamlFile(), fmt.Errorf("invalid file format for %s: .yaml or .yml file extension and format required", podTemplateLocation))
66+
if err == nil {
67+
t.Fatalf("Expected error for local file typo, but error was nil")
68+
}
69+
70+
expected := `error unmarshaling JSON: while decoding JSON: json: unknown field "ecurityContext"`
71+
test.AssertOutput(t, expected, err.Error())
72+
}
73+
74+
func TestTaskRunSpec_Local_File_Not_YAML(t *testing.T) {
75+
httpClient := *http.DefaultClient
76+
podTemplateLocation := "./testdata/taskrunspec-not-yaml"
77+
78+
_, err := ParseTaskRunSpec(httpClient, podTemplateLocation, file.IsYamlFile(), fmt.Errorf("invalid file format for %s: .yaml or .yml file extension and format required", podTemplateLocation))
79+
if err == nil {
80+
t.Fatalf("Expected error for local file typo, but error was nil")
81+
}
82+
83+
expected := "invalid file format for ./testdata/taskrunspec-not-yaml: .yaml or .yml file extension and format required"
84+
test.AssertOutput(t, expected, err.Error())
85+
}
86+
87+
func TestTaskRunSpec_Local_File_Not_Found(t *testing.T) {
88+
httpClient := *http.DefaultClient
89+
podTemplateLocation := "./testdata/not-exist.yaml"
90+
91+
_, err := ParseTaskRunSpec(httpClient, podTemplateLocation, file.IsYamlFile(), fmt.Errorf("invalid file format for %s: .yaml or .yml file extension and format required", podTemplateLocation))
92+
if err == nil {
93+
t.Fatalf("Expected error for local file typo, but error was nil")
94+
}
95+
96+
expected := "open ./testdata/not-exist.yaml: no such file or directory"
97+
test.AssertOutput(t, expected, err.Error())
98+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Copyright 2020 The Tekton Authors.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
- pipelineTaskName: first-create-file
16+
taskPodTemplate:
17+
schedulerName: SchedulerName
18+
securityContext:
19+
runAsNonRoot: true
20+
runAsUser: 1001
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Copyright 2020 The Tekton Authors.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
- pipelineTaskName: first-create-file
16+
taskPodTemplate:
17+
schedulerName: SchedulerName
18+
ecurityContext:
19+
runAsNonRoot: true
20+
runAsUser: 1001

pkg/pods/testdata/taskrunspec.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Copyright 2020 The Tekton Authors.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
- pipelineTaskName: first-create-file
16+
taskPodTemplate:
17+
schedulerName: SchedulerName
18+
securityContext:
19+
runAsNonRoot: true
20+
runAsUser: 1001

0 commit comments

Comments
 (0)