|
| 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 | +} |
0 commit comments