|
| 1 | +// Copyright The Conforma Contributors |
| 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 | +// SPDX-License-Identifier: Apache-2.0 |
| 16 | + |
| 17 | +package kind |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "os" |
| 23 | + "path/filepath" |
| 24 | + "time" |
| 25 | + |
| 26 | + "github.com/tektoncd/cli/pkg/formatted" |
| 27 | + pipeline "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1" |
| 28 | + tekton "github.com/tektoncd/pipeline/pkg/client/clientset/versioned/typed/pipeline/v1" |
| 29 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 30 | + "k8s.io/apimachinery/pkg/util/wait" |
| 31 | + "sigs.k8s.io/yaml" |
| 32 | + |
| 33 | + "github.com/conforma/cli/acceptance/kubernetes/types" |
| 34 | + "github.com/conforma/cli/acceptance/testenv" |
| 35 | +) |
| 36 | + |
| 37 | +// localPipeline loads the checked-out definition, changing only bundle locations |
| 38 | +// to use the tasks built from this checkout by buildTaskBundleImage. |
| 39 | +func localPipeline(version, name, bundle string) (*pipeline.Pipeline, error) { |
| 40 | + content, err := os.ReadFile(filepath.Join("pipelines", name, version, name+".yaml")) |
| 41 | + if err != nil { |
| 42 | + return nil, err |
| 43 | + } |
| 44 | + var definition pipeline.Pipeline |
| 45 | + if err := yaml.UnmarshalStrict(content, &definition); err != nil { |
| 46 | + return nil, err |
| 47 | + } |
| 48 | + for i := range definition.Spec.Tasks { |
| 49 | + task := &definition.Spec.Tasks[i] |
| 50 | + if task.TaskRef == nil || task.TaskRef.Resolver != "bundles" { |
| 51 | + return nil, fmt.Errorf("pipeline task %q must use a bundle resolver", task.Name) |
| 52 | + } |
| 53 | + found := false |
| 54 | + for j := range task.TaskRef.Params { |
| 55 | + param := &task.TaskRef.Params[j] |
| 56 | + if param.Name == "bundle" { |
| 57 | + param.Value = pipeline.ParamValue{Type: pipeline.ParamTypeString, StringVal: bundle} |
| 58 | + found = true |
| 59 | + } |
| 60 | + } |
| 61 | + if !found { |
| 62 | + return nil, fmt.Errorf("pipeline task %q has no bundle parameter", task.Name) |
| 63 | + } |
| 64 | + } |
| 65 | + return &definition, nil |
| 66 | +} |
| 67 | + |
| 68 | +// RunPipeline runs the repository pipeline against the local task bundles. |
| 69 | +func (k *kindCluster) RunPipeline(ctx context.Context, version, name string, params map[string]string) error { |
| 70 | + t := testenv.FetchState[testState](ctx) |
| 71 | + bundle := fmt.Sprintf("registry.image-registry.svc.cluster.local:%d/ec-task-bundle:%s", k.registryPort, version) |
| 72 | + definition, err := localPipeline(version, name, bundle) |
| 73 | + if err != nil { |
| 74 | + return err |
| 75 | + } |
| 76 | + tkn, err := tekton.NewForConfig(k.config) |
| 77 | + if err != nil { |
| 78 | + return err |
| 79 | + } |
| 80 | + tknParams := make([]pipeline.Param, 0, len(params)) |
| 81 | + for n, v := range params { |
| 82 | + tknParams = append(tknParams, stringParam(ctx, n, v, t)) |
| 83 | + } |
| 84 | + pr, err := tkn.PipelineRuns(t.namespace).Create(ctx, &pipeline.PipelineRun{ |
| 85 | + ObjectMeta: metav1.ObjectMeta{GenerateName: "acceptance-pipelinerun-"}, |
| 86 | + Spec: pipeline.PipelineRunSpec{ |
| 87 | + PipelineSpec: &definition.Spec, |
| 88 | + Params: tknParams, |
| 89 | + TaskRunTemplate: pipeline.PipelineTaskRunTemplate{ServiceAccountName: "default"}, |
| 90 | + Timeouts: &pipeline.TimeoutFields{Pipeline: &metav1.Duration{Duration: 10 * time.Minute}}, |
| 91 | + }, |
| 92 | + }, metav1.CreateOptions{}) |
| 93 | + if err != nil { |
| 94 | + return err |
| 95 | + } |
| 96 | + t.pipelineRun = pr.Name |
| 97 | + return nil |
| 98 | +} |
| 99 | + |
| 100 | +// AwaitUntilPipelineIsDone polls with a deadline. A timeout or API failure is |
| 101 | +// always an error, never an expected negative validation result. |
| 102 | +func (k *kindCluster) AwaitUntilPipelineIsDone(ctx context.Context) (*types.PipelineInfo, error) { |
| 103 | + t := testenv.FetchState[testState](ctx) |
| 104 | + tkn, err := tekton.NewForConfig(k.config) |
| 105 | + if err != nil { |
| 106 | + return nil, err |
| 107 | + } |
| 108 | + var pr *pipeline.PipelineRun |
| 109 | + err = wait.PollUntilContextTimeout(ctx, time.Second, 11*time.Minute, true, func(ctx context.Context) (bool, error) { |
| 110 | + var err error |
| 111 | + pr, err = tkn.PipelineRuns(t.namespace).Get(ctx, t.pipelineRun, metav1.GetOptions{}) |
| 112 | + if err != nil { |
| 113 | + return false, err |
| 114 | + } |
| 115 | + return pr.IsDone(), nil |
| 116 | + }) |
| 117 | + if err != nil { |
| 118 | + return nil, fmt.Errorf("waiting for PipelineRun %s/%s: %w", t.namespace, t.pipelineRun, err) |
| 119 | + } |
| 120 | + results := map[string]any{} |
| 121 | + for _, result := range pr.Status.Results { |
| 122 | + results[result.Name] = paramValue(result.Value) |
| 123 | + } |
| 124 | + // Preserve condition messages (including resolution/validation failures) in |
| 125 | + // assertion errors instead of reporting just a boolean status. |
| 126 | + return &types.PipelineInfo{ |
| 127 | + Name: pr.Name, |
| 128 | + Status: fmt.Sprintf("%s: %v", formatted.Condition(pr.Status.Conditions), pr.Status.Conditions), |
| 129 | + Successful: pr.IsSuccessful(), |
| 130 | + Results: results, |
| 131 | + }, nil |
| 132 | +} |
0 commit comments