// TestUpdateTaskRunStateWithConditionChecks runs "getTaskRunsStatus" and verifies how it updates a PipelineRun status
// from several TaskRun with Conditions associated to the PipelineRun
TestUpdateTaskRunStateWithConditionChecks runs "getTaskRunsStatus" and verifies how it updates a PipelineRun status
from several TaskRun with Conditions associated to the PipelineRun
// TestUpdateTaskRunStateWithConditionChecks runs " getTaskRunsStatus " and verifies how it updates a PipelineRun status // from several TaskRun with Conditions associated to the PipelineRun
func TestUpdateTaskRunStateWithConditionChecks(t *testing.T) {
	taskrunName := "task-run"
	successConditionCheckName := "success-condition"
	failingConditionCheckName := "fail-condition"
	successCondition := parse.MustParseCondition(t, `
metadata:
  name: cond-1
  namespace: foo
`)
	failingCondition := parse.MustParseCondition(t, `
metadata:
  name: cond-2
  namespace: foo
`)
	pipelineTask := v1beta1.PipelineTask{
		TaskRef: &v1beta1.TaskRef{Name: "unit-test-task"},
		Conditions: []v1beta1.PipelineTaskCondition{{
			ConditionRef: successCondition.Name,
		}, {
			ConditionRef: failingCondition.Name,
		}},
	}
	successConditionCheck := parse.MustParseTaskRun(t, fmt.Sprintf(`
metadata:
  name: %s
  namespace: foo
spec:
status:
  conditions:
    - type: Succeeded
      status: "True"
  steps:
    - container:
      terminated:
        exitCode: 0
`, successConditionCheckName))
	failingConditionCheck := parse.MustParseTaskRun(t, fmt.Sprintf(`
metadata:
  name: %s
  namespace: foo
spec:
status:
  conditions:
    - type: Succeeded
      status: "False"
  steps:
    - container:
      terminated:
        exitCode: 127
`, failingConditionCheckName))
	successrcc := ResolvedConditionCheck{
		ConditionRegisterName: successCondition.Name + "-0",
		ConditionCheckName:    successConditionCheckName,
		Condition:             successCondition,
		ConditionCheck:        conditionCheckFromTaskRun(successConditionCheck),
	}
	failingrcc := ResolvedConditionCheck{
		ConditionRegisterName: failingCondition.Name + "-0",
		ConditionCheckName:    failingConditionCheckName,
		Condition:             failingCondition,
		ConditionCheck:        conditionCheckFromTaskRun(failingConditionCheck),
	}
	successConditionCheckStatus := &v1beta1.PipelineRunConditionCheckStatus{
		ConditionName: successrcc.ConditionRegisterName,
		Status: &v1beta1.ConditionCheckStatus{
			ConditionCheckStatusFields: v1beta1.ConditionCheckStatusFields{
				Check: corev1.ContainerState{
					Terminated: &corev1.ContainerStateTerminated{ExitCode: 0},
				},
			},
			Status: duckv1beta1.Status{
				Conditions: []apis.Condition{{Type: apis.ConditionSucceeded, Status: corev1.ConditionTrue}},
			},
		},
	}
	failingConditionCheckStatus := &v1beta1.PipelineRunConditionCheckStatus{
		ConditionName: failingrcc.ConditionRegisterName,
		Status: &v1beta1.ConditionCheckStatus{
			ConditionCheckStatusFields: v1beta1.ConditionCheckStatusFields{
				Check: corev1.ContainerState{
					Terminated: &corev1.ContainerStateTerminated{ExitCode: 127},
				},
			},
			Status: duckv1beta1.Status{
				Conditions: []apis.Condition{{Type: apis.ConditionSucceeded, Status: corev1.ConditionFalse}},
			},
		},
	}
	failedTaskRunStatus := v1beta1.TaskRunStatus{
		Status: duckv1beta1.Status{
			Conditions: []apis.Condition{{
				Type:    apis.ConditionSucceeded,
				Status:  corev1.ConditionFalse,
				Reason:  ReasonConditionCheckFailed,
				Message: fmt.Sprintf("ConditionChecks failed for Task %s in PipelineRun %s", taskrunName, "test-pipeline-run"),
			}},
		},
	}
	tcs := []struct {
		name           string
		rcc            TaskConditionCheckState
		expectedStatus v1beta1.PipelineRunTaskRunStatus
	}{{
		name: "success-condition-checks",
		rcc:  TaskConditionCheckState{&successrcc},
		expectedStatus: v1beta1.PipelineRunTaskRunStatus{
			ConditionChecks: map[string]*v1beta1.PipelineRunConditionCheckStatus{
				successrcc.ConditionCheck.Name: successConditionCheckStatus,
			},
		},
	}, {
		name: "failing-condition-checks",
		rcc:  TaskConditionCheckState{&failingrcc},
		expectedStatus: v1beta1.PipelineRunTaskRunStatus{
			Status: &failedTaskRunStatus,
			ConditionChecks: map[string]*v1beta1.PipelineRunConditionCheckStatus{
				failingrcc.ConditionCheck.Name: failingConditionCheckStatus,
			},
		},
	}, {
		name: "multiple-condition-checks",
		rcc:  TaskConditionCheckState{&successrcc, &failingrcc},
		expectedStatus: v1beta1.PipelineRunTaskRunStatus{
			Status: &failedTaskRunStatus,
			ConditionChecks: map[string]*v1beta1.PipelineRunConditionCheckStatus{
				successrcc.ConditionCheck.Name: successConditionCheckStatus,
				failingrcc.ConditionCheck.Name: failingConditionCheckStatus,
			},
		},
	}}
	for _, tc := range tcs {
		t.Run(tc.name, func(t *testing.T) {
			pr := parse.MustParsePipelineRun(t, `
metadata:
  name: test-pipeline-run
  namespace: foo
spec:
  pipelineRef:
    name: test-pipeline
`)
			state := PipelineRunState{{
				PipelineTask:            &pipelineTask,
				TaskRunName:             taskrunName,
				ResolvedConditionChecks: tc.rcc,
			}}
			pr.Status.InitializeConditions(testClock)
			status := state.GetTaskRunsStatus(pr)
			expected := map[string]*v1beta1.PipelineRunTaskRunStatus{
				taskrunName: &tc.expectedStatus,
			}
			if d := cmp.Diff(status, expected, cmpopts.IgnoreFields(apis.Condition{}, "LastTransitionTime.Inner.Time")); d != "" {
				t.Fatalf("Did not get expected status for %s %s", tc.name, diff.PrintWantGot(d))
			}
		})
	}
}
func TestUpdateTaskRunStateWithConditionChecks ( t * testing . T ) { taskrunName := " " successConditionCheckName := " " failingConditionCheckName := " " successCondition := parse . MustParseCondition ( t , `
metadata:
  name: cond-1
  namespace: foo
` ) failingCondition := parse . MustParseCondition ( t , `
metadata:
  name: cond-2
  namespace: foo
` ) pipelineTask := v1beta1 . PipelineTask { TaskRef : & v1beta1 . TaskRef { Name : " " } , Conditions : [ ] v1beta1 . PipelineTaskCondition { { ConditionRef : successCondition . Name , } , { ConditionRef : failingCondition . Name , } } , } successConditionCheck := parse . MustParseTaskRun ( t , fmt . Sprintf ( `
metadata:
  name: %s
  namespace: foo
spec:
status:
  conditions:
    - type: Succeeded
      status: "True"
  steps:
    - container:
      terminated:
        exitCode: 0
` , successConditionCheckName ) ) failingConditionCheck := parse . MustParseTaskRun ( t , fmt . Sprintf ( `
metadata:
  name: %s
  namespace: foo
spec:
status:
  conditions:
    - type: Succeeded
      status: "False"
  steps:
    - container:
      terminated:
        exitCode: 127
` , failingConditionCheckName ) ) successrcc := ResolvedConditionCheck { ConditionRegisterName : successCondition . Name + " " , ConditionCheckName : successConditionCheckName , Condition : successCondition , ConditionCheck : conditionCheckFromTaskRun ( successConditionCheck ) , } failingrcc := ResolvedConditionCheck { ConditionRegisterName : failingCondition . Name + " " , ConditionCheckName : failingConditionCheckName , Condition : failingCondition , ConditionCheck : conditionCheckFromTaskRun ( failingConditionCheck ) , } successConditionCheckStatus := & v1beta1 . PipelineRunConditionCheckStatus { ConditionName : successrcc . ConditionRegisterName , Status : & v1beta1 . ConditionCheckStatus { ConditionCheckStatusFields : v1beta1 . ConditionCheckStatusFields { Check : corev1 . ContainerState { Terminated : & corev1 . ContainerStateTerminated { ExitCode : 0 } , } , } , Status : duckv1beta1 . Status { Conditions : [ ] apis . Condition { { Type : apis . ConditionSucceeded , Status : corev1 . ConditionTrue } } , } , } , } failingConditionCheckStatus := & v1beta1 . PipelineRunConditionCheckStatus { ConditionName : failingrcc . ConditionRegisterName , Status : & v1beta1 . ConditionCheckStatus { ConditionCheckStatusFields : v1beta1 . ConditionCheckStatusFields { Check : corev1 . ContainerState { Terminated : & corev1 . ContainerStateTerminated { ExitCode : 127 } , } , } , Status : duckv1beta1 . Status { Conditions : [ ] apis . Condition { { Type : apis . ConditionSucceeded , Status : corev1 . ConditionFalse } } , } , } , } failedTaskRunStatus := v1beta1 . TaskRunStatus { Status : duckv1beta1 . Status { Conditions : [ ] apis . Condition { { Type : apis . ConditionSucceeded , Status : corev1 . ConditionFalse , Reason : ReasonConditionCheckFailed , Message : fmt . Sprintf ( " " , taskrunName , " " ) , } } , } , } tcs := [ ] struct { name string rcc TaskConditionCheckState expectedStatus v1beta1 . PipelineRunTaskRunStatus } { { name : " " , rcc : TaskConditionCheckState { & successrcc } , expectedStatus : v1beta1 . PipelineRunTaskRunStatus { ConditionChecks : map [ string ] * v1beta1 . PipelineRunConditionCheckStatus { successrcc . ConditionCheck . Name : successConditionCheckStatus , } , } , } , { name : " " , rcc : TaskConditionCheckState { & failingrcc } , expectedStatus : v1beta1 . PipelineRunTaskRunStatus { Status : & failedTaskRunStatus , ConditionChecks : map [ string ] * v1beta1 . PipelineRunConditionCheckStatus { failingrcc . ConditionCheck . Name : failingConditionCheckStatus , } , } , } , { name : " " , rcc : TaskConditionCheckState { & successrcc , & failingrcc } , expectedStatus : v1beta1 . PipelineRunTaskRunStatus { Status : & failedTaskRunStatus , ConditionChecks : map [ string ] * v1beta1 . PipelineRunConditionCheckStatus { successrcc . ConditionCheck . Name : successConditionCheckStatus , failingrcc . ConditionCheck . Name : failingConditionCheckStatus , } , } , } } for _ , tc := range tcs { t . Run ( tc . name , func ( t * testing . T ) { pr := parse . MustParsePipelineRun ( t , `
metadata:
  name: test-pipeline-run
  namespace: foo
spec:
  pipelineRef:
    name: test-pipeline
` ) state := PipelineRunState { { PipelineTask : & pipelineTask , TaskRunName : taskrunName , ResolvedConditionChecks : tc . rcc , } } pr . Status . InitializeConditions ( testClock ) status := state . GetTaskRunsStatus ( pr ) expected := map [ string ] * v1beta1 . PipelineRunTaskRunStatus { taskrunName : & tc . expectedStatus , } if d := cmp . Diff ( status , expected , cmpopts . IgnoreFields ( apis . Condition { } , " " ) ) ; d != " " { t . Fatalf ( " " , tc . name , diff . PrintWantGot ( d ) ) } } ) } }
TestUpdateTaskRunStateWithConditionChecks runs "getTaskRunsStatus" and verifies how it updates a PipelineRun status
from several TaskRun with Conditions associated to the PipelineRun
TestUpdateTaskRunStateWithConditionChecks runs " getTaskRunsStatus " and verifies how it updates a PipelineRun status from several TaskRun with Conditions associated to the PipelineRun