github.com/tilt-dev/tilt@v0.33.15-0.20240515162809-0a22ed45d8a0/internal/store/k8sconv/pod_test.go (about)

     1  package k8sconv
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	v1 "k8s.io/api/core/v1"
     8  
     9  	"github.com/tilt-dev/tilt/pkg/apis/core/v1alpha1"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func TestContainerStatusToRuntimeState(t *testing.T) {
    15  	cases := []struct {
    16  		Name   string
    17  		Status v1alpha1.Container
    18  		Result v1alpha1.RuntimeStatus
    19  	}{
    20  		{
    21  			"ok-running", v1alpha1.Container{
    22  				State: v1alpha1.ContainerState{Running: &v1alpha1.ContainerStateRunning{}},
    23  			}, v1alpha1.RuntimeStatusOK,
    24  		},
    25  		{
    26  			"ok-terminated", v1alpha1.Container{
    27  				State: v1alpha1.ContainerState{Terminated: &v1alpha1.ContainerStateTerminated{}},
    28  			}, v1alpha1.RuntimeStatusOK,
    29  		},
    30  		{
    31  			"error-terminated", v1alpha1.Container{
    32  				State: v1alpha1.ContainerState{Terminated: &v1alpha1.ContainerStateTerminated{ExitCode: 1}},
    33  			}, v1alpha1.RuntimeStatusError,
    34  		},
    35  		{
    36  			"error-waiting", v1alpha1.Container{
    37  				State: v1alpha1.ContainerState{Waiting: &v1alpha1.ContainerStateWaiting{Reason: "CrashLoopBackOff"}},
    38  			}, v1alpha1.RuntimeStatusError,
    39  		},
    40  		{
    41  			"pending-waiting", v1alpha1.Container{
    42  				State: v1alpha1.ContainerState{Waiting: &v1alpha1.ContainerStateWaiting{Reason: "Initializing"}},
    43  			}, v1alpha1.RuntimeStatusPending,
    44  		},
    45  	}
    46  
    47  	for _, c := range cases {
    48  		t.Run(c.Name, func(t *testing.T) {
    49  			assert.Equal(t, c.Result, ContainerStatusToRuntimeState(c.Status))
    50  		})
    51  	}
    52  }
    53  
    54  func TestPodStatus(t *testing.T) {
    55  	type tc struct {
    56  		pod      v1.PodStatus
    57  		status   string
    58  		messages []string
    59  	}
    60  
    61  	cases := []tc{
    62  		{
    63  			pod: v1.PodStatus{
    64  				ContainerStatuses: []v1.ContainerStatus{
    65  					{
    66  						LastTerminationState: v1.ContainerState{
    67  							Terminated: &v1.ContainerStateTerminated{
    68  								ExitCode: 128,
    69  								Message:  "failed to create containerd task: OCI runtime create failed: container_linux.go:345: starting container process caused \"exec: \\\"/hello\\\": stat /hello: no such file or directory\": unknown",
    70  								Reason:   "StartError",
    71  							},
    72  						},
    73  						Ready: false,
    74  						State: v1.ContainerState{
    75  							Waiting: &v1.ContainerStateWaiting{
    76  								Message: "Back-off 40s restarting failed container=my-app pod=my-app-7bb79c789d-8h6n9_default(31369f71-df65-4352-b6bd-6d704a862699)",
    77  								Reason:  "CrashLoopBackOff",
    78  							},
    79  						},
    80  					},
    81  				},
    82  			},
    83  			status: "CrashLoopBackOff",
    84  			messages: []string{
    85  				"failed to create containerd task: OCI runtime create failed: container_linux.go:345: starting container process caused \"exec: \\\"/hello\\\": stat /hello: no such file or directory\": unknown",
    86  				"Back-off 40s restarting failed container=my-app pod=my-app-7bb79c789d-8h6n9_default(31369f71-df65-4352-b6bd-6d704a862699)",
    87  			},
    88  		},
    89  	}
    90  
    91  	for i, c := range cases {
    92  		t.Run(fmt.Sprintf("case%d", i), func(t *testing.T) {
    93  			pod := v1.Pod{Status: c.pod}
    94  			status := PodStatusToString(pod)
    95  			assert.Equal(t, c.status, status)
    96  
    97  			messages := PodStatusErrorMessages(pod)
    98  			assert.Equal(t, c.messages, messages)
    99  		})
   100  	}
   101  }