|
| 1 | +// +build conformance |
| 2 | + |
| 3 | +/* |
| 4 | +Copyright 2020 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 | + "context" |
| 23 | + "testing" |
| 24 | + |
| 25 | + "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" |
| 26 | + corev1 "k8s.io/api/core/v1" |
| 27 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 28 | + "knative.dev/pkg/apis" |
| 29 | + knativetest "knative.dev/pkg/test" |
| 30 | +) |
| 31 | + |
| 32 | +func TestTaskRun(t *testing.T) { |
| 33 | + ctx := context.Background() |
| 34 | + ctx, cancel := context.WithCancel(ctx) |
| 35 | + defer cancel() |
| 36 | + c, namespace := setup(ctx, t) |
| 37 | + |
| 38 | + knativetest.CleanupOnInterrupt(func() { tearDown(ctx, t, c, namespace) }, t.Logf) |
| 39 | + defer tearDown(ctx, t, c, namespace) |
| 40 | + |
| 41 | + trName := "echo-hello-task-run" |
| 42 | + tr := &v1beta1.TaskRun{ |
| 43 | + ObjectMeta: metav1.ObjectMeta{Name: trName, Namespace: namespace}, |
| 44 | + Spec: v1beta1.TaskRunSpec{ |
| 45 | + TaskSpec: &v1beta1.TaskSpec{ |
| 46 | + Steps: []v1beta1.Step{{Container: corev1.Container{ |
| 47 | + Image: "busybox", |
| 48 | + Command: []string{"echo", "\"hello\""}, |
| 49 | + }}}, |
| 50 | + }, |
| 51 | + }, |
| 52 | + } |
| 53 | + |
| 54 | + t.Logf("Creating TaskRun %s", trName) |
| 55 | + if _, err := c.TaskRunClient.Create(ctx, tr, metav1.CreateOptions{}); err != nil { |
| 56 | + t.Fatalf("Failed to create TaskRun `%s`: %s", trName, err) |
| 57 | + } |
| 58 | + |
| 59 | + if err := WaitForTaskRunState(ctx, c, trName, TaskRunSucceed(trName), "WaitTaskRunDone"); err != nil { |
| 60 | + t.Errorf("Error waiting for TaskRun to finish: %s", err) |
| 61 | + return |
| 62 | + } |
| 63 | + tr, err := c.TaskRunClient.Get(ctx, trName, metav1.GetOptions{}) |
| 64 | + if err != nil { |
| 65 | + t.Fatalf("Failed to get TaskRun `%s`: %s", trName, err) |
| 66 | + } |
| 67 | + |
| 68 | + // check required fields in TaskRun ObjectMeta |
| 69 | + if tr.ObjectMeta.Name == "" { |
| 70 | + t.Errorf("TaskRun ObjectMeta doesn't have the Name.") |
| 71 | + } |
| 72 | + if len(tr.ObjectMeta.Labels) == 0 { |
| 73 | + t.Errorf("TaskRun ObjectMeta doesn't have the Labels.") |
| 74 | + } |
| 75 | + if len(tr.ObjectMeta.Annotations) == 0 { |
| 76 | + t.Errorf("TaskRun ObjectMeta doesn't have the Annotations.") |
| 77 | + } |
| 78 | + if tr.ObjectMeta.CreationTimestamp.IsZero() { |
| 79 | + t.Errorf("TaskRun ObjectMeta doesn't have the CreationTimestamp.") |
| 80 | + } |
| 81 | + |
| 82 | + // check required fields in TaskRun Status |
| 83 | + if len(tr.Status.Conditions) == 0 { |
| 84 | + t.Errorf("TaskRun Status doesn't have the Conditions.") |
| 85 | + } |
| 86 | + if tr.Status.StartTime.IsZero() { |
| 87 | + t.Errorf("TaskRun Status doesn't have the StartTime.") |
| 88 | + } |
| 89 | + if tr.Status.CompletionTime.IsZero() { |
| 90 | + t.Errorf("TaskRun Status doesn't have the CompletionTime.") |
| 91 | + } |
| 92 | + if len(tr.Status.Steps) == 0 { |
| 93 | + t.Errorf("TaskRun Status doesn't have the Steps.") |
| 94 | + } |
| 95 | + |
| 96 | + condition := tr.Status.GetCondition(apis.ConditionSucceeded) |
| 97 | + if condition == nil { |
| 98 | + t.Errorf("Expected a succeeded Condition but got nil.") |
| 99 | + } |
| 100 | + if condition.Status != corev1.ConditionTrue { |
| 101 | + t.Errorf("TaskRun Status Condition doesn't have the right Status.") |
| 102 | + } |
| 103 | + if condition.Reason == "" { |
| 104 | + t.Errorf("TaskRun Status Condition doesn't have the Reason.") |
| 105 | + } |
| 106 | + if condition.Message == "" { |
| 107 | + t.Errorf("TaskRun Status Condition doesn't have the Message.") |
| 108 | + } |
| 109 | + if condition.Severity != apis.ConditionSeverityError && condition.Severity != apis.ConditionSeverityWarning && condition.Severity != apis.ConditionSeverityInfo { |
| 110 | + t.Errorf("TaskRun Status Condition doesn't have the right Severity.") |
| 111 | + } |
| 112 | +} |
0 commit comments