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 <[email protected]>
  • Loading branch information
adshmh committed Sep 23, 2020
1 parent 026677f commit fdbd822
Showing 1 changed file with 52 additions and 117 deletions.
169 changes: 52 additions & 117 deletions pkg/pod/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,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 +81,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 +115,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 +144,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 +172,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 +203,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 +227,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 +251,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 +272,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 +297,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 +322,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 +335,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 +345,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 +361,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 +380,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 +405,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 +449,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 +497,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 @@ -979,14 +888,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 +1192,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 fdbd822

Please sign in to comment.