github.com/kubeshop/testkube@v1.17.23/pkg/executor/common_test.go (about)

     1  package executor
     2  
     3  import (
     4  	"context"
     5  	"reflect"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  	corev1 "k8s.io/api/core/v1"
    10  	"k8s.io/client-go/kubernetes"
    11  
    12  	"k8s.io/client-go/kubernetes/fake"
    13  )
    14  
    15  func TestPodHasError(t *testing.T) {
    16  
    17  	t.Run("succeded pod return no error ", func(t *testing.T) {
    18  		// given
    19  		pod := succeededPod()
    20  
    21  		// when
    22  		err := IsPodFailed(pod)
    23  
    24  		//then
    25  		assert.NoError(t, err)
    26  	})
    27  
    28  	t.Run("failed pod returns error", func(t *testing.T) {
    29  		// given
    30  		pod := failedPod()
    31  
    32  		// when
    33  		err := IsPodFailed(pod)
    34  
    35  		//then
    36  		assert.EqualError(t, err, "pod failed")
    37  	})
    38  
    39  	t.Run("failed pod with pending init container", func(t *testing.T) {
    40  		// given
    41  		pod := failedInitContainer()
    42  
    43  		// when
    44  		err := IsPodFailed(pod)
    45  
    46  		//then
    47  		assert.EqualError(t, err, "secret nonexistingsecret not found")
    48  	})
    49  }
    50  
    51  func succeededPod() *corev1.Pod {
    52  	return &corev1.Pod{
    53  		Status: corev1.PodStatus{Phase: corev1.PodSucceeded},
    54  	}
    55  }
    56  
    57  func failedPod() *corev1.Pod {
    58  	return &corev1.Pod{
    59  		Status: corev1.PodStatus{Phase: corev1.PodFailed, Message: "pod failed"},
    60  	}
    61  }
    62  
    63  func failedInitContainer() *corev1.Pod {
    64  	return &corev1.Pod{
    65  		Status: corev1.PodStatus{
    66  			Phase: corev1.PodPending,
    67  			InitContainerStatuses: []corev1.ContainerStatus{
    68  				{
    69  					State: corev1.ContainerState{
    70  						Waiting: &corev1.ContainerStateWaiting{
    71  							Reason:  "CreateContainerConfigError",
    72  							Message: "secret nonexistingsecret not found",
    73  						},
    74  					},
    75  				},
    76  			}},
    77  	}
    78  }
    79  
    80  func TestGetPodLogs(t *testing.T) {
    81  	type args struct {
    82  		c             kubernetes.Interface
    83  		namespace     string
    84  		pod           corev1.Pod
    85  		logLinesCount []int64
    86  	}
    87  	tests := []struct {
    88  		name     string
    89  		args     args
    90  		wantLogs []byte
    91  		wantErr  bool
    92  	}{
    93  		{
    94  			name: "pod with multiple containers",
    95  			args: args{
    96  				c:         fake.NewSimpleClientset(),
    97  				namespace: "testkube_test",
    98  				pod: corev1.Pod{
    99  					Spec: corev1.PodSpec{
   100  						InitContainers: []corev1.Container{
   101  							{
   102  								Name: "init_container",
   103  							},
   104  						},
   105  						Containers: []corev1.Container{
   106  							{
   107  								Name: "first_container",
   108  							},
   109  							{
   110  								Name: "second_container",
   111  							},
   112  						},
   113  					},
   114  				},
   115  			},
   116  			wantLogs: []byte("fake logsfake logsfake logs"),
   117  			wantErr:  false,
   118  		},
   119  	}
   120  	for _, tt := range tests {
   121  		t.Run(tt.name, func(t *testing.T) {
   122  			gotLogs, err := GetPodLogs(context.Background(), tt.args.c, tt.args.namespace, tt.args.pod, tt.args.logLinesCount...)
   123  			if (err != nil) != tt.wantErr {
   124  				t.Errorf("GetPodLogs() error = %v, wantErr %v", err, tt.wantErr)
   125  				return
   126  			}
   127  			if !reflect.DeepEqual(gotLogs, tt.wantLogs) {
   128  				t.Errorf("GetPodLogs() = %v, want %v", gotLogs, tt.wantLogs)
   129  			}
   130  		})
   131  	}
   132  }