@@ -3,44 +3,43 @@ package oncluster
33import (
44 "context"
55 "fmt"
6- "strings"
76 "testing"
87
98 v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
109 "knative.dev/func/pkg/k8s"
1110 "knative.dev/func/pkg/pipelines/tekton"
1211)
1312
14- // TektonPipelineExists verifies pipeline with a given prefix exists on cluster
15- func TektonPipelineExists (t * testing.T , pipelinePrefix string ) bool {
13+ // TektonPipelineExists verifies pipeline with a given function name exists on cluster
14+ func TektonPipelineExists (t * testing.T , functionName string ) bool {
1615 ns , _ , _ := k8s .GetClientConfig ().Namespace ()
1716 client , _ := tekton .NewTektonClient (ns )
18- pipelines , err := client .Pipelines (ns ).List (context .Background (), v1.ListOptions {})
17+ // Look for pipelines with the function.knative.dev/name label matching the function name
18+ pipelines , err := client .Pipelines (ns ).List (context .Background (), v1.ListOptions {
19+ LabelSelector : "function.knative.dev/name=" + functionName ,
20+ })
1921 if err != nil {
2022 t .Error (err .Error ())
23+ return false
2124 }
22- for _ , pipeline := range pipelines .Items {
23- if strings .HasPrefix (pipeline .Name , pipelinePrefix ) && strings .HasSuffix (pipeline .Name , "-pipeline" ) {
24- return true
25- }
26- }
27- return false
25+ // If any pipeline exists with this label, it means the pipeline was created for this function
26+ return len (pipelines .Items ) > 0
2827}
2928
30- // TektonPipelineRunExists verifies pipelinerun with a given prefix exists on cluster
31- func TektonPipelineRunExists (t * testing.T , pipelineRunPrefix string ) bool {
29+ // TektonPipelineRunExists verifies pipelinerun with a given function name exists on cluster
30+ func TektonPipelineRunExists (t * testing.T , functionName string ) bool {
3231 ns , _ , _ := k8s .GetClientConfig ().Namespace ()
3332 client , _ := tekton .NewTektonClient (ns )
34- pipelineRuns , err := client .PipelineRuns (ns ).List (context .Background (), v1.ListOptions {})
33+ // Look for pipeline runs with the function.knative.dev/name label matching the function name
34+ pipelineRuns , err := client .PipelineRuns (ns ).List (context .Background (), v1.ListOptions {
35+ LabelSelector : "function.knative.dev/name=" + functionName ,
36+ })
3537 if err != nil {
3638 t .Error (err .Error ())
39+ return false
3740 }
38- for _ , run := range pipelineRuns .Items {
39- if strings .HasPrefix (run .Name , pipelineRunPrefix ) {
40- return true
41- }
42- }
43- return false
41+ // If any pipeline run exists with this label, it means a pipeline run was created for this function
42+ return len (pipelineRuns .Items ) > 0
4443}
4544
4645type PipelineRunSummary struct {
@@ -67,33 +66,39 @@ func (p *PipelineRunSummary) IsSucceed() bool {
6766
6867// TektonPipelineLastRunSummary gather information about a pipeline run such as
6968// list of tasks executed and status of each task execution. It is meant to be used on assertions
70- func TektonPipelineLastRunSummary (t * testing.T , pipelinePrefix string ) * PipelineRunSummary {
69+ func TektonPipelineLastRunSummary (t * testing.T , functionName string ) * PipelineRunSummary {
7170 ns , _ , _ := k8s .GetClientConfig ().Namespace ()
7271 client , _ := tekton .NewTektonClient (ns )
73- pipelineRuns , err := client .PipelineRuns (ns ).List (context .Background (), v1.ListOptions {})
72+ // Look for pipeline runs with the function.knative.dev/name label matching the function name
73+ pipelineRuns , err := client .PipelineRuns (ns ).List (context .Background (), v1.ListOptions {
74+ LabelSelector : "function.knative.dev/name=" + functionName ,
75+ })
7476 if err != nil {
7577 t .Error (err .Error ())
78+ return & PipelineRunSummary {}
7679 }
80+
7781 lr := PipelineRunSummary {}
78- for _ , run := range pipelineRuns .Items {
79- if strings .HasPrefix (run .Name , pipelinePrefix ) {
80- lr .PipelineRunName = run .Name
81- if len (run .Status .Conditions ) > 0 {
82- lr .PipelineRunStatus = run .Status .Conditions [0 ].Reason
82+ // Get the most recent pipeline run (they're sorted by creation time)
83+ if len (pipelineRuns .Items ) > 0 {
84+ // Take the first one (most recent) from the filtered list
85+ run := pipelineRuns .Items [0 ]
86+ lr .PipelineRunName = run .Name
87+ if len (run .Status .Conditions ) > 0 {
88+ lr .PipelineRunStatus = run .Status .Conditions [0 ].Reason
89+ }
90+ lr .TasksRunSummary = []PipelineTaskRunSummary {}
91+ for _ , ref := range run .Status .ChildReferences {
92+ r := PipelineTaskRunSummary {}
93+ r .TaskName = ref .PipelineTaskName
94+ taskRun , err := client .TaskRuns (ns ).Get (context .Background (), ref .Name , v1.GetOptions {})
95+ if err != nil {
96+ t .Error (err .Error ())
8397 }
84- lr .TasksRunSummary = []PipelineTaskRunSummary {}
85- for _ , ref := range run .Status .ChildReferences {
86- r := PipelineTaskRunSummary {}
87- r .TaskName = ref .PipelineTaskName
88- taskRun , err := client .TaskRuns (ns ).Get (context .Background (), ref .Name , v1.GetOptions {})
89- if err != nil {
90- t .Error (err .Error ())
91- }
92- if len (taskRun .Status .Conditions ) > 0 {
93- r .TaskStatus = taskRun .Status .Conditions [0 ].Reason
94- }
95- lr .TasksRunSummary = append (lr .TasksRunSummary , r )
98+ if len (taskRun .Status .Conditions ) > 0 {
99+ r .TaskStatus = taskRun .Status .Conditions [0 ].Reason
96100 }
101+ lr .TasksRunSummary = append (lr .TasksRunSummary , r )
97102 }
98103 }
99104 return & lr
0 commit comments