-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathassert.go
82 lines (74 loc) · 2.61 KB
/
assert.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package assert provides utility functions for asserting conditions in tests.
package assert
import (
"context"
"strings"
"testing"
"github.com/gcb-catalog-testing-bot/catalog-infra/pkg/resourcemanager"
v1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
"github.com/tektoncd/pipeline/pkg/client/clientset/versioned"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// AssertStepResultNotEmpty asserts that a step result in the Tekton TaskRun is not empty
func AssertStepResultNotEmpty(t *testing.T, tektonClient *versioned.Clientset, tektonRun resourcemanager.TektonRun, stepName, resultName, namespace string) {
t.Helper()
var steps []v1.StepState
switch strings.ToLower(tektonRun.Kind) {
case "taskrun":
taskRun, err := tektonClient.TektonV1().TaskRuns(namespace).Get(context.TODO(), tektonRun.Name, metav1.GetOptions{})
if err != nil {
t.Errorf("failed to get TaskRun: %v", err)
}
steps = taskRun.Status.Steps
case "pipelinerun":
t.Error("PipelineRun not supported for verifying step-level results")
default:
t.Errorf("unsupported Tekton Run kind: %s", tektonRun.Kind)
}
checkStepResults(t, steps, stepName, resultName)
}
// checkStepResults checks that a step result in the Tekton TaskRun is not empty
func checkStepResults(t *testing.T, steps []v1.StepState, stepName, resultName string) {
t.Helper()
for _, step := range steps {
if step.Name != stepName {
continue
}
for _, result := range step.Results {
if result.Name != resultName {
continue
}
switch result.Type {
case v1.ResultsTypeString:
if result.Value.StringVal != "" {
return
}
case v1.ResultsTypeArray:
if len(result.Value.ArrayVal) > 0 {
return
}
case v1.ResultsTypeObject:
if result.Value.ObjectVal != nil && len(result.Value.ObjectVal) > 0 {
return
}
default:
t.Errorf("unsupported result type for '%s': %v", resultName, result.Type)
}
t.Errorf("Step result '%s' in step '%s' is empty", resultName, step.Name)
}
}
t.Errorf("Step result '%s' not found in Step '%s'", resultName, stepName)
}