...

Source file src/edge-infra.dev/pkg/edge/k8objectsutils/status/status_test.go

Documentation: edge-infra.dev/pkg/edge/k8objectsutils/status

     1  package status
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  	corev1 "k8s.io/api/core/v1"
     8  )
     9  
    10  func TestIsPodReady(t *testing.T) {
    11  	testCases := []struct {
    12  		name     string
    13  		status   corev1.PodStatus
    14  		expected bool
    15  	}{
    16  		{
    17  			name: "Pod is running and has ready condition true",
    18  			status: corev1.PodStatus{
    19  				Phase: corev1.PodRunning,
    20  				Conditions: []corev1.PodCondition{
    21  					{
    22  						Type:   corev1.PodReady,
    23  						Status: corev1.ConditionTrue,
    24  					},
    25  				},
    26  			},
    27  			expected: true,
    28  		},
    29  		{
    30  			name: "Pod is running but has ready condition false",
    31  			status: corev1.PodStatus{
    32  				Phase: corev1.PodRunning,
    33  				Conditions: []corev1.PodCondition{
    34  					{
    35  						Type:   corev1.PodReady,
    36  						Status: corev1.ConditionFalse,
    37  					},
    38  				},
    39  			},
    40  			expected: false,
    41  		},
    42  		{
    43  			name: "Pod is not running but has ready condition true",
    44  			status: corev1.PodStatus{
    45  				Phase: corev1.PodPending,
    46  				Conditions: []corev1.PodCondition{
    47  					{
    48  						Type:   corev1.PodReady,
    49  						Status: corev1.ConditionTrue,
    50  					},
    51  				},
    52  			},
    53  			expected: false,
    54  		},
    55  		{
    56  			name: "Pod is not running and has ready condition false",
    57  			status: corev1.PodStatus{
    58  				Phase: corev1.PodFailed,
    59  				Conditions: []corev1.PodCondition{
    60  					{
    61  						Type:   corev1.PodReady,
    62  						Status: corev1.ConditionFalse,
    63  					},
    64  				},
    65  			},
    66  			expected: false,
    67  		},
    68  		{
    69  			name: "Pod is not running and has no ready condition",
    70  			status: corev1.PodStatus{
    71  				Phase:      corev1.PodUnknown,
    72  				Conditions: []corev1.PodCondition{},
    73  			},
    74  			expected: false,
    75  		},
    76  		{
    77  			name: "Pod is running but has no ready condition",
    78  			status: corev1.PodStatus{
    79  				Phase:      corev1.PodRunning,
    80  				Conditions: []corev1.PodCondition{},
    81  			},
    82  			expected: false,
    83  		},
    84  		{
    85  			name: "Pod is running and has ready condition true (among other conditions)",
    86  			status: corev1.PodStatus{
    87  				Phase: corev1.PodRunning,
    88  				Conditions: []corev1.PodCondition{
    89  					{
    90  						Type:   corev1.PodInitialized,
    91  						Status: corev1.ConditionTrue,
    92  					},
    93  					{
    94  						Type:   corev1.PodReady,
    95  						Status: corev1.ConditionTrue,
    96  					},
    97  					{
    98  						Type:   corev1.PodScheduled,
    99  						Status: corev1.ConditionTrue,
   100  					},
   101  				},
   102  			},
   103  			expected: true,
   104  		},
   105  	}
   106  	for _, tc := range testCases {
   107  		t.Run(tc.name, func(t *testing.T) {
   108  			actual := IsPodReady(&corev1.Pod{Status: tc.status})
   109  			require.Equal(t, tc.expected, actual)
   110  		})
   111  	}
   112  }
   113  
   114  func TestIsContainerCLBO(t *testing.T) {
   115  	testCases := []struct {
   116  		name      string
   117  		status    corev1.ContainerStatus
   118  		threshold uint16
   119  		expected  bool
   120  	}{
   121  		{
   122  			name: "Not ready - Has waiting state CLBO - Over threshold",
   123  			status: corev1.ContainerStatus{
   124  				Ready: false,
   125  				State: corev1.ContainerState{
   126  					Waiting: &corev1.ContainerStateWaiting{
   127  						Reason: "CrashLoopBackOff",
   128  					},
   129  				},
   130  				RestartCount: 101,
   131  			},
   132  			threshold: 100,
   133  			expected:  true,
   134  		},
   135  		{
   136  			name: "Not ready - Has waiting state CLBO - Under threshold",
   137  			status: corev1.ContainerStatus{
   138  				Ready: false,
   139  				State: corev1.ContainerState{
   140  					Waiting: &corev1.ContainerStateWaiting{
   141  						Reason: "CrashLoopBackOff",
   142  					},
   143  				},
   144  				RestartCount: 0,
   145  			},
   146  			threshold: 1,
   147  			expected:  false,
   148  		},
   149  		{
   150  			name: "Not ready - Has waiting state CLBO - Equal to threshold",
   151  			status: corev1.ContainerStatus{
   152  				Ready: false,
   153  				State: corev1.ContainerState{
   154  					Waiting: &corev1.ContainerStateWaiting{
   155  						Reason: "CrashLoopBackOff",
   156  					},
   157  				},
   158  				RestartCount: 100,
   159  			},
   160  			threshold: 100,
   161  			expected:  false,
   162  		},
   163  		{
   164  			name: "Not ready - Has non-CLBO waiting state - Over threshold",
   165  			status: corev1.ContainerStatus{
   166  				Ready: false,
   167  				State: corev1.ContainerState{
   168  					Waiting: &corev1.ContainerStateWaiting{
   169  						Reason: "Waiting",
   170  					},
   171  				},
   172  				RestartCount: 300,
   173  			},
   174  			threshold: 100,
   175  			expected:  false,
   176  		},
   177  		{
   178  			name: "Ready - Has waiting state CLBO - Over threshold",
   179  			status: corev1.ContainerStatus{
   180  				Ready: true,
   181  				State: corev1.ContainerState{
   182  					Waiting: &corev1.ContainerStateWaiting{
   183  						Reason: "CrashLoopBackOff",
   184  					},
   185  				},
   186  				RestartCount: 200,
   187  			},
   188  			threshold: 100,
   189  			expected:  false,
   190  		},
   191  		{
   192  			name: "Ready - Has no waiting state - Over threshold",
   193  			status: corev1.ContainerStatus{
   194  				Ready:        true,
   195  				State:        corev1.ContainerState{},
   196  				RestartCount: 200,
   197  			},
   198  			threshold: 100,
   199  			expected:  false,
   200  		},
   201  		{
   202  			name: "Ready - Has no waiting state - Under threshold",
   203  			status: corev1.ContainerStatus{
   204  				Ready:        true,
   205  				State:        corev1.ContainerState{},
   206  				RestartCount: 0,
   207  			},
   208  			threshold: 100,
   209  			expected:  false,
   210  		},
   211  	}
   212  	for _, tc := range testCases {
   213  		t.Run(tc.name, func(t *testing.T) {
   214  			actual := IsContainerCLBO(tc.status, tc.threshold)
   215  			require.Equal(t, tc.expected, actual)
   216  		})
   217  	}
   218  }
   219  

View as plain text