Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
xinnjie committed Dec 17, 2024
1 parent e44529b commit 3cccaa0
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 53 deletions.
11 changes: 9 additions & 2 deletions pkg/cli/cmd/pipelinerun/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,25 @@ List all PipelineRuns in 'default' namespace:
}

func pipelineRunFromRecord(record *pb.Record) (*pipelinev1.PipelineRun, error) {
if record.Data == nil {
return nil, fmt.Errorf("record data is nil")
}
pr := &pipelinev1.PipelineRun{}
if record.Data.GetType() == "tekton.dev/v1beta1.PipelineRun" {
switch record.Data.GetType() {
case "tekton.dev/v1beta1.PipelineRun":
prV1beta1 := &pipelinev1beta1.PipelineRun{}
if err := json.Unmarshal(record.Data.Value, prV1beta1); err != nil {
return nil, fmt.Errorf("failed to unmarshal PipelineRun data: %v", err)
}
if err := pr.ConvertFrom(context.TODO(), prV1beta1); err != nil {
return nil, fmt.Errorf("failed to convert v1beta1 PipelineRun to v1: %v", err)
}
} else {
case "tekton.dev/v1.PipelineRun":
if err := json.Unmarshal(record.Data.Value, pr); err != nil {
return nil, fmt.Errorf("failed to unmarshal PipelineRun data: %v", err)
}
default:
return nil, fmt.Errorf("unsupported PipelineRun type: %s", record.Data.GetType())
}
return pr, nil
}
Expand All @@ -98,6 +104,7 @@ func printFormatted(out io.Writer, records []*pb.Record, c clockwork.Clock) erro
}

for _, record := range records {
fmt.Println(record.String())
if pr, err := pipelineRunFromRecord(record); err != nil {
continue
} else {
Expand Down
136 changes: 96 additions & 40 deletions pkg/cli/cmd/pipelinerun/list_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pipelinerun

import (
"encoding/json"
"testing"
"time"

Expand All @@ -12,14 +13,19 @@ import (
"github.com/jonboulle/clockwork"

"github.com/spf13/cobra"
pipelinev1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
pb "github.com/tektoncd/results/proto/v1alpha2/results_go_proto"
"google.golang.org/protobuf/types/known/timestamppb"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"knative.dev/pkg/apis"
duckv1 "knative.dev/pkg/apis/duck/v1"
)

func TestListPipelineRuns_empty(t *testing.T) {
results := []*pb.Result{}
records := []*pb.Record{}
now := time.Now()
cmd := command(results, now)
cmd := command(records, now)

output, err := test.ExecuteCommand(cmd, "list")
if err != nil {
Expand All @@ -34,57 +40,107 @@ func TestListPipelineRuns(t *testing.T) {
updateTime := clock.Now().Add(time.Duration(-2) * time.Minute)
startTime := clock.Now().Add(time.Duration(-3) * time.Minute)
endTime := clock.Now().Add(time.Duration(-1) * time.Minute)
results := []*pb.Result{
{
Name: "default/results/e6b4b2e3-d876-4bbe-a927-95c691b6fdc7",
Uid: "949eebd9-1cf7-478f-a547-9ee313035f10",
CreateTime: timestamppb.New(createTime),
UpdateTime: timestamppb.New(updateTime),
Annotations: map[string]string{
"object.metadata.name": "hello-goodbye-run-vfsxn",
"tekton.dev/pipeline": "hello-goodbye",
},
Summary: &pb.RecordSummary{
Record: "default/results/e6b4b2e3-d876-4bbe-a927-95c691b6fdc7/records/e6b4b2e3-d876-4bbe-a927-95c691b6fdc7",
Type: "tekton.dev/v1.PipelineRun",
StartTime: timestamppb.New(startTime),
EndTime: timestamppb.New(endTime),
Status: pb.RecordSummary_SUCCESS,
},
},
{
Name: "default/results/3dacd30b-ce42-476c-be7e-84b0f664df55",
Uid: "c8d4cd50-06e8-4325-9ba2-044e6cc45235",
CreateTime: timestamppb.New(createTime),
UpdateTime: timestamppb.New(updateTime),
Annotations: map[string]string{
"object.metadata.name": "hello-goodbye-run-xtw2j",
},
Summary: &pb.RecordSummary{
Record: "default/results/3dacd30b-ce42-476c-be7e-84b0f664df55/records/3dacd30b-ce42-476c-be7e-84b0f664df55",
Type: "tekton.dev/v1.PipelineRun",
},
},
}
cmd := command(results, clock.Now())
records := testDataSuccessfullPipelineRun(t, createTime, updateTime, startTime, endTime)
cmd := command(records, clock.Now())
output, err := test.ExecuteCommand(cmd, "list")
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
test.AssertOutput(t, `NAMESPACE UID STARTED DURATION STATUS
default 949eebd9-1cf7-478f-a547-9ee313035f10 3 minutes ago 2m0s Succeeded
default c8d4cd50-06e8-4325-9ba2-044e6cc45235 --- --- Unknown`, output)
test.AssertOutput(t, `NAMESPACE UID STARTED DURATION STATUS
default hello-goodbye-run-xgkf8 3 minutes ago 2m0s Succeeded`, output)
}

func command(results []*pb.Result, now time.Time) *cobra.Command {
func command(records []*pb.Record, now time.Time) *cobra.Command {
clock := clockwork.NewFakeClockAt(now)

param := &flags.Params{
ResultsClient: fake.NewResultsClient(results),
ResultsClient: fake.NewResultsClient(nil, records),
LogsClient: nil,
PluginLogsClient: nil,
Clock: clock,
}
cmd := Command(param)
return cmd
}

func testDataSuccessfullPipelineRun(t *testing.T, createTime, updateTime, startTime, endTime time.Time) []*pb.Record {
pipelineRun := &pipelinev1.PipelineRun{
ObjectMeta: metav1.ObjectMeta{
Name: "hello-goodbye-run-xgkf8",
Namespace: "default",
UID: "d2c19786-5fb7-4577-84a4-10d43c157c5c",
ResourceVersion: "4320960",
Generation: 1,
Labels: map[string]string{
"tekton.dev/pipeline": "hello-goodbye",
},
Annotations: map[string]string{
"results.tekton.dev/log": "default/results/d2c19786-5fb7-4577-84a4-10d43c157c5c/logs/d05a16ce-f7a4-3370-8c3a-88c30067680a",
"results.tekton.dev/record": "default/results/d2c19786-5fb7-4577-84a4-10d43c157c5c/records/d2c19786-5fb7-4577-84a4-10d43c157c5c",
"results.tekton.dev/result": "default/results/d2c19786-5fb7-4577-84a4-10d43c157c5c",
},
Finalizers: []string{"results.tekton.dev/pipelinerun"},
},
Spec: pipelinev1.PipelineRunSpec{
PipelineRef: &pipelinev1.PipelineRef{
Name: "hello-goodbye",
},
Params: []pipelinev1.Param{
{
Name: "username",
Value: pipelinev1.ParamValue{
Type: pipelinev1.ParamTypeString,
StringVal: "Tekton",
},
},
},
Timeouts: &pipelinev1.TimeoutFields{
Pipeline: &metav1.Duration{Duration: time.Hour},
},
},
Status: pipelinev1.PipelineRunStatus{
PipelineRunStatusFields: pipelinev1.PipelineRunStatusFields{
StartTime: &metav1.Time{Time: startTime},
CompletionTime: &metav1.Time{Time: endTime},
ChildReferences: []pipelinev1.ChildStatusReference{
{
Name: "hello-goodbye-run-xgkf8-hello",
PipelineTaskName: "hello",
},
{
Name: "hello-goodbye-run-xgkf8-goodbye",
PipelineTaskName: "goodbye",
},
},
},
Status: duckv1.Status{
Conditions: []apis.Condition{
{
Type: apis.ConditionSucceeded,
Status: corev1.ConditionTrue,
Reason: "Succeeded",
Message: "Tasks Completed: 2 (Failed: 0, Cancelled 0), Skipped: 0",
LastTransitionTime: apis.VolatileTime{Inner: metav1.Time{Time: updateTime}},
},
},
},
},
}
prBytes, err := json.Marshal(pipelineRun)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
records := []*pb.Record{
{
Name: "default/results/e6b4b2e3-d876-4bbe-a927-95c691b6fdc7/records/e6b4b2e3-d876-4bbe-a927-95c691b6fdc7",
Uid: "095a449f-691a-4be7-9bcb-3a52bba3bc6d",
Data: &pb.Any{
Type: "tekton.dev/v1.PipelineRun",
Value: prBytes,
},
CreateTime: timestamppb.New(createTime),
UpdateTime: timestamppb.New(updateTime),
},
}
return records
}
2 changes: 1 addition & 1 deletion pkg/cli/cmd/result/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestDescribeResult(t *testing.T) {
}}

param := &flags.Params{
ResultsClient: fake.NewResultsClient(results),
ResultsClient: fake.NewResultsClient(results, nil),
LogsClient: nil,
PluginLogsClient: nil,
Clock: clock,
Expand Down
32 changes: 22 additions & 10 deletions pkg/test/fake/results.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,26 @@ import (
type ResultsClient struct {
// Map of result name to Result for GetResult and ListResults
results map[string]*pb.Result

// Map of record name to Record for GetRecord and ListRecords
records map[string]*pb.Record
}

// NewResultsClient creates a new fake ResultsClient
func NewResultsClient(testData []*pb.Result) *ResultsClient {
func NewResultsClient(testResults []*pb.Result, testRecords []*pb.Record) *ResultsClient {
r := &ResultsClient{
results: make(map[string]*pb.Result),
records: make(map[string]*pb.Record),
}
for _, result := range testData {
for _, result := range testResults {
r.results[result.Name] = result
}
for _, record := range testRecords {
r.records[record.Name] = record
}
return r
}

// AddResult adds a Result to the fake client's data store
func (c *ResultsClient) AddResult(name string, result *pb.Result) {
c.results[name] = result
}

// GetResult implements ResultsClient.GetResult
func (c *ResultsClient) GetResult(_ context.Context, in *pb.GetResultRequest, _ ...grpc.CallOption) (*pb.Result, error) {
result, exists := c.results[in.Name]
Expand Down Expand Up @@ -78,13 +80,23 @@ func (c *ResultsClient) UpdateRecord(_ context.Context, _ *pb.UpdateRecordReques
}

// GetRecord is unimplemented
func (c *ResultsClient) GetRecord(_ context.Context, _ *pb.GetRecordRequest, _ ...grpc.CallOption) (*pb.Record, error) {
return nil, fmt.Errorf("unimplemented")
func (c *ResultsClient) GetRecord(_ context.Context, in *pb.GetRecordRequest, _ ...grpc.CallOption) (*pb.Record, error) {
record, exists := c.records[in.Name]
if !exists {
return nil, fmt.Errorf("record not found: %s", in.Name)
}
return record, nil
}

// ListRecords is unimplemented
func (c *ResultsClient) ListRecords(_ context.Context, _ *pb.ListRecordsRequest, _ ...grpc.CallOption) (*pb.ListRecordsResponse, error) {
return nil, fmt.Errorf("unimplemented")
records := make([]*pb.Record, 0, len(c.records))
for _, record := range c.records {
records = append(records, record)
}
return &pb.ListRecordsResponse{
Records: records,
}, nil
}

// DeleteRecord is unimplemented
Expand Down

0 comments on commit 3cccaa0

Please sign in to comment.