Skip to content

Commit

Permalink
use status helper functions in pod status unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Arash Deshmeh <adeshmeh@ca.ibm.com>
  • Loading branch information
adshmh committed Sep 23, 2020
1 parent 026677f commit 4406fe6
Showing 1 changed file with 58 additions and 148 deletions.
206 changes: 58 additions & 148 deletions pkg/pod/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,6 @@ import (

var ignoreVolatileTime = cmp.Comparer(func(_, _ apis.VolatileTime) bool { return true })

var conditionRunning apis.Condition = apis.Condition{
Type: apis.ConditionSucceeded,
Status: corev1.ConditionUnknown,
Reason: v1beta1.TaskRunReasonRunning.String(),
Message: "Not all Steps in the Task have finished executing",
}
var conditionSucceeded apis.Condition = apis.Condition{
Type: apis.ConditionSucceeded,
Status: corev1.ConditionTrue,
Reason: v1beta1.TaskRunReasonSuccessful.String(),
Message: "All Steps have completed executing",
}

func TestMakeTaskRunStatus(t *testing.T) {
for _, c := range []struct {
desc string
Expand All @@ -57,9 +44,7 @@ func TestMakeTaskRunStatus(t *testing.T) {
podStatus: corev1.PodStatus{},

want: v1beta1.TaskRunStatus{
Status: duckv1beta1.Status{
Conditions: []apis.Condition{conditionRunning},
},
Status: statusRunning(t),
TaskRunStatusFields: v1beta1.TaskRunStatusFields{
Steps: []v1beta1.StepState{},
Sidecars: []v1beta1.SidecarState{},
Expand All @@ -83,9 +68,7 @@ func TestMakeTaskRunStatus(t *testing.T) {
}},
},
want: v1beta1.TaskRunStatus{
Status: duckv1beta1.Status{
Conditions: []apis.Condition{conditionRunning},
},
Status: statusRunning(t),
TaskRunStatusFields: v1beta1.TaskRunStatusFields{
Steps: []v1beta1.StepState{{
ContainerState: corev1.ContainerState{
Expand Down Expand Up @@ -119,9 +102,7 @@ func TestMakeTaskRunStatus(t *testing.T) {
}},
},
want: v1beta1.TaskRunStatus{
Status: duckv1beta1.Status{
Conditions: []apis.Condition{conditionRunning},
},
Status: statusRunning(t),
TaskRunStatusFields: v1beta1.TaskRunStatusFields{
Steps: []v1beta1.StepState{{
ContainerState: corev1.ContainerState{
Expand Down Expand Up @@ -150,14 +131,7 @@ func TestMakeTaskRunStatus(t *testing.T) {
}},
},
want: v1beta1.TaskRunStatus{
Status: duckv1beta1.Status{
Conditions: []apis.Condition{{
Type: apis.ConditionSucceeded,
Status: corev1.ConditionTrue,
Reason: v1beta1.TaskRunReasonSuccessful.String(),
Message: "All Steps have completed executing",
}},
},
Status: statusSuccess(t),
TaskRunStatusFields: v1beta1.TaskRunStatusFields{
Steps: []v1beta1.StepState{{
ContainerState: corev1.ContainerState{
Expand Down Expand Up @@ -185,9 +159,7 @@ func TestMakeTaskRunStatus(t *testing.T) {
}},
},
want: v1beta1.TaskRunStatus{
Status: duckv1beta1.Status{
Conditions: []apis.Condition{conditionRunning},
},
Status: statusRunning(t),
TaskRunStatusFields: v1beta1.TaskRunStatusFields{
Steps: []v1beta1.StepState{{
ContainerState: corev1.ContainerState{
Expand Down Expand Up @@ -218,14 +190,7 @@ func TestMakeTaskRunStatus(t *testing.T) {
}},
},
want: v1beta1.TaskRunStatus{
Status: duckv1beta1.Status{
Conditions: []apis.Condition{{
Type: apis.ConditionSucceeded,
Status: corev1.ConditionFalse,
Reason: v1beta1.TaskRunReasonFailed.String(),
Message: "\"step-failure\" exited with code 123 (image: \"image-id\"); for logs run: kubectl -n foo logs pod -c step-failure\n",
}},
},
Status: statusFailure(t, "\"step-failure\" exited with code 123 (image: \"image-id\"); for logs run: kubectl -n foo logs pod -c step-failure\n"),
TaskRunStatusFields: v1beta1.TaskRunStatusFields{
Steps: []v1beta1.StepState{{
ContainerState: corev1.ContainerState{
Expand All @@ -249,14 +214,7 @@ func TestMakeTaskRunStatus(t *testing.T) {
Message: "boom",
},
want: v1beta1.TaskRunStatus{
Status: duckv1beta1.Status{
Conditions: []apis.Condition{{
Type: apis.ConditionSucceeded,
Status: corev1.ConditionFalse,
Reason: v1beta1.TaskRunReasonFailed.String(),
Message: "boom",
}},
},
Status: statusFailure(t, "boom"),
TaskRunStatusFields: v1beta1.TaskRunStatusFields{
Steps: []v1beta1.StepState{},
Sidecars: []v1beta1.SidecarState{},
Expand All @@ -280,14 +238,7 @@ func TestMakeTaskRunStatus(t *testing.T) {
}},
},
want: v1beta1.TaskRunStatus{
Status: duckv1beta1.Status{
Conditions: []apis.Condition{{
Type: apis.ConditionSucceeded,
Status: corev1.ConditionFalse,
Reason: v1beta1.TaskRunReasonFailed.String(),
Message: "OOMKilled",
}},
},
Status: statusFailure(t, "OOMKilled"),
TaskRunStatusFields: v1beta1.TaskRunStatusFields{
Steps: []v1beta1.StepState{{
ContainerState: corev1.ContainerState{
Expand All @@ -308,14 +259,7 @@ func TestMakeTaskRunStatus(t *testing.T) {
desc: "failure-unspecified",
podStatus: corev1.PodStatus{Phase: corev1.PodFailed},
want: v1beta1.TaskRunStatus{
Status: duckv1beta1.Status{
Conditions: []apis.Condition{{
Type: apis.ConditionSucceeded,
Status: corev1.ConditionFalse,
Reason: v1beta1.TaskRunReasonFailed.String(),
Message: "build failed for unspecified reasons.",
}},
},
Status: statusFailure(t, "build failed for unspecified reasons."),
TaskRunStatusFields: v1beta1.TaskRunStatusFields{
Steps: []v1beta1.StepState{},
Sidecars: []v1beta1.SidecarState{},
Expand All @@ -340,14 +284,7 @@ func TestMakeTaskRunStatus(t *testing.T) {
}},
},
want: v1beta1.TaskRunStatus{
Status: duckv1beta1.Status{
Conditions: []apis.Condition{{
Type: apis.ConditionSucceeded,
Status: corev1.ConditionUnknown,
Reason: "Pending",
Message: `build step "step-status-name" is pending with reason "i'm pending"`,
}},
},
Status: statusPending(t, "Pending", `build step "step-status-name" is pending with reason "i'm pending"`),
TaskRunStatusFields: v1beta1.TaskRunStatusFields{
Steps: []v1beta1.StepState{{
ContainerState: corev1.ContainerState{
Expand All @@ -372,14 +309,7 @@ func TestMakeTaskRunStatus(t *testing.T) {
}},
},
want: v1beta1.TaskRunStatus{
Status: duckv1beta1.Status{
Conditions: []apis.Condition{{
Type: apis.ConditionSucceeded,
Status: corev1.ConditionUnknown,
Reason: "Pending",
Message: `pod status "the type":"Unknown"; message: "the message"`,
}},
},
Status: statusPending(t, "Pending", `pod status "the type":"Unknown"; message: "the message"`),
TaskRunStatusFields: v1beta1.TaskRunStatusFields{
Steps: []v1beta1.StepState{},
Sidecars: []v1beta1.SidecarState{},
Expand All @@ -392,14 +322,7 @@ func TestMakeTaskRunStatus(t *testing.T) {
Message: "pod status message",
},
want: v1beta1.TaskRunStatus{
Status: duckv1beta1.Status{
Conditions: []apis.Condition{{
Type: apis.ConditionSucceeded,
Status: corev1.ConditionUnknown,
Reason: "Pending",
Message: "pod status message",
}},
},
Status: statusPending(t, "Pending", "pod status message"),
TaskRunStatusFields: v1beta1.TaskRunStatusFields{
Steps: []v1beta1.StepState{},
Sidecars: []v1beta1.SidecarState{},
Expand All @@ -409,14 +332,7 @@ func TestMakeTaskRunStatus(t *testing.T) {
desc: "pending-no-message",
podStatus: corev1.PodStatus{Phase: corev1.PodPending},
want: v1beta1.TaskRunStatus{
Status: duckv1beta1.Status{
Conditions: []apis.Condition{{
Type: apis.ConditionSucceeded,
Status: corev1.ConditionUnknown,
Reason: "Pending",
Message: "Pending",
}},
},
Status: statusPending(t, "Pending", "Pending"),
TaskRunStatusFields: v1beta1.TaskRunStatusFields{
Steps: []v1beta1.StepState{},
Sidecars: []v1beta1.SidecarState{},
Expand All @@ -432,14 +348,7 @@ func TestMakeTaskRunStatus(t *testing.T) {
}},
},
want: v1beta1.TaskRunStatus{
Status: duckv1beta1.Status{
Conditions: []apis.Condition{{
Type: apis.ConditionSucceeded,
Status: corev1.ConditionUnknown,
Reason: ReasonExceededNodeResources,
Message: "TaskRun Pod exceeded available resources",
}},
},
Status: statusPending(t, ReasonExceededNodeResources, "TaskRun Pod exceeded available resources"),
TaskRunStatusFields: v1beta1.TaskRunStatusFields{
Steps: []v1beta1.StepState{},
Sidecars: []v1beta1.SidecarState{},
Expand All @@ -458,14 +367,7 @@ func TestMakeTaskRunStatus(t *testing.T) {
}},
},
want: v1beta1.TaskRunStatus{
Status: duckv1beta1.Status{
Conditions: []apis.Condition{{
Type: apis.ConditionSucceeded,
Status: corev1.ConditionUnknown,
Reason: ReasonCreateContainerConfigError,
Message: "Pending",
}},
},
Status: statusPending(t, ReasonCreateContainerConfigError, "Pending"),
TaskRunStatusFields: v1beta1.TaskRunStatusFields{
Steps: []v1beta1.StepState{},
Sidecars: []v1beta1.SidecarState{},
Expand All @@ -490,9 +392,7 @@ func TestMakeTaskRunStatus(t *testing.T) {
}},
},
want: v1beta1.TaskRunStatus{
Status: duckv1beta1.Status{
Conditions: []apis.Condition{conditionRunning},
},
Status: statusRunning(t),
TaskRunStatusFields: v1beta1.TaskRunStatusFields{
Steps: []v1beta1.StepState{{
ContainerState: corev1.ContainerState{
Expand Down Expand Up @@ -536,9 +436,7 @@ func TestMakeTaskRunStatus(t *testing.T) {
}},
},
want: v1beta1.TaskRunStatus{
Status: duckv1beta1.Status{
Conditions: []apis.Condition{conditionRunning},
},
Status: statusRunning(t),
TaskRunStatusFields: v1beta1.TaskRunStatusFields{
Steps: []v1beta1.StepState{{
ContainerState: corev1.ContainerState{
Expand Down Expand Up @@ -586,9 +484,7 @@ func TestMakeTaskRunStatus(t *testing.T) {
}},
},
want: v1beta1.TaskRunStatus{
Status: duckv1beta1.Status{
Conditions: []apis.Condition{conditionRunning},
},
Status: statusRunning(t),
TaskRunStatusFields: v1beta1.TaskRunStatusFields{
Steps: []v1beta1.StepState{{
ContainerState: corev1.ContainerState{
Expand Down Expand Up @@ -625,9 +521,7 @@ func TestMakeTaskRunStatus(t *testing.T) {
}},
},
want: v1beta1.TaskRunStatus{
Status: duckv1beta1.Status{
Conditions: []apis.Condition{conditionSucceeded},
},
Status: statusSuccess(t),
TaskRunStatusFields: v1beta1.TaskRunStatusFields{
Steps: []v1beta1.StepState{{
ContainerState: corev1.ContainerState{
Expand Down Expand Up @@ -661,9 +555,7 @@ func TestMakeTaskRunStatus(t *testing.T) {
}},
},
want: v1beta1.TaskRunStatus{
Status: duckv1beta1.Status{
Conditions: []apis.Condition{conditionSucceeded},
},
Status: statusSuccess(t),
TaskRunStatusFields: v1beta1.TaskRunStatusFields{
Steps: []v1beta1.StepState{{
ContainerState: corev1.ContainerState{
Expand Down Expand Up @@ -701,9 +593,7 @@ func TestMakeTaskRunStatus(t *testing.T) {
}},
},
want: v1beta1.TaskRunStatus{
Status: duckv1beta1.Status{
Conditions: []apis.Condition{conditionSucceeded},
},
Status: statusSuccess(t),
TaskRunStatusFields: v1beta1.TaskRunStatusFields{
Steps: []v1beta1.StepState{{
ContainerState: corev1.ContainerState{
Expand Down Expand Up @@ -748,9 +638,7 @@ func TestMakeTaskRunStatus(t *testing.T) {
}},
},
want: v1beta1.TaskRunStatus{
Status: duckv1beta1.Status{
Conditions: []apis.Condition{conditionSucceeded},
},
Status: statusSuccess(t),
TaskRunStatusFields: v1beta1.TaskRunStatusFields{
Steps: []v1beta1.StepState{{
ContainerState: corev1.ContainerState{
Expand Down Expand Up @@ -823,9 +711,7 @@ func TestMakeTaskRunStatus(t *testing.T) {
}},
},
want: v1beta1.TaskRunStatus{
Status: duckv1beta1.Status{
Conditions: []apis.Condition{conditionSucceeded},
},
Status: statusSuccess(t),
TaskRunStatusFields: v1beta1.TaskRunStatusFields{
Steps: []v1beta1.StepState{{
ContainerState: corev1.ContainerState{
Expand All @@ -852,9 +738,7 @@ func TestMakeTaskRunStatus(t *testing.T) {
}},
},
want: v1beta1.TaskRunStatus{
Status: duckv1beta1.Status{
Conditions: []apis.Condition{conditionSucceeded},
},
Status: statusSuccess(t),
TaskRunStatusFields: v1beta1.TaskRunStatusFields{
Steps: []v1beta1.StepState{{
ContainerState: corev1.ContainerState{
Expand Down Expand Up @@ -979,14 +863,7 @@ func TestMakeRunStatusErrors(t *testing.T) {
}},
},
want: v1beta1.TaskRunStatus{
Status: duckv1beta1.Status{
Conditions: []apis.Condition{{
Type: apis.ConditionSucceeded,
Status: corev1.ConditionFalse,
Reason: v1beta1.TaskRunReasonFailed.String(),
Message: "\"step-non-json\" exited with code 1 (image: \"image\"); for logs run: kubectl -n foo logs pod -c step-non-json\n",
}},
},
Status: statusFailure(t, "\"step-non-json\" exited with code 1 (image: \"image\"); for logs run: kubectl -n foo logs pod -c step-non-json\n"),
TaskRunStatusFields: v1beta1.TaskRunStatusFields{
Steps: []v1beta1.StepState{{
ContainerState: corev1.ContainerState{
Expand Down Expand Up @@ -1290,3 +1167,36 @@ func TestMarkStatusSuccess(t *testing.T) {
t.Errorf("Unexpected status: %s", diff.PrintWantGot(d))
}
}

func statusRunning(t *testing.T) duckv1beta1.Status {
t.Helper()
var trs v1beta1.TaskRunStatus
MarkStatusRunning(&trs, v1beta1.TaskRunReasonRunning.String(), "Not all Steps in the Task have finished executing")
return trs.Status
}

func statusFailure(t *testing.T, message string) duckv1beta1.Status {
t.Helper()
var trs v1beta1.TaskRunStatus
MarkStatusFailure(&trs, message)
return trs.Status
}

func statusSuccess(t *testing.T) duckv1beta1.Status {
t.Helper()
var trs v1beta1.TaskRunStatus
MarkStatusSuccess(&trs)
return trs.Status
}

func statusPending(t *testing.T, reason, message string) duckv1beta1.Status {
t.Helper()
return duckv1beta1.Status{
Conditions: []apis.Condition{{
Type: apis.ConditionSucceeded,
Status: corev1.ConditionUnknown,
Reason: reason,
Message: message,
}},
}
}

0 comments on commit 4406fe6

Please sign in to comment.