github.com/kubeshop/testkube@v1.17.23/contrib/executor/jmeterd/pkg/slaves/utils_test.go (about) 1 package slaves 2 3 import ( 4 "context" 5 "testing" 6 "time" 7 8 "github.com/stretchr/testify/assert" 9 v1 "k8s.io/api/core/v1" 10 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 11 "k8s.io/apimachinery/pkg/util/wait" 12 "k8s.io/client-go/kubernetes/fake" 13 14 "github.com/kubeshop/testkube/pkg/api/v1/testkube" 15 ) 16 17 func TestGetSlavesCount(t *testing.T) { 18 t.Parallel() 19 20 tests := []struct { 21 name string 22 input map[string]testkube.Variable 23 want int 24 wantErr bool 25 }{ 26 { 27 name: "Empty Value", 28 want: defaultSlavesCount, 29 wantErr: false, 30 }, 31 { 32 name: "Valid Value", 33 input: map[string]testkube.Variable{"SLAVES_COUNT": {Value: "10"}}, 34 want: 10, 35 wantErr: false, 36 }, 37 { 38 name: "Invalid Value", 39 input: map[string]testkube.Variable{"SLAVES_COUNT": {Value: "abc"}}, 40 want: 0, 41 wantErr: true, 42 }, 43 } 44 45 for i := range tests { 46 tt := tests[i] 47 t.Run(tt.name, func(t *testing.T) { 48 t.Parallel() 49 50 got, err := GetSlavesCount(tt.input) 51 if (err != nil) != tt.wantErr { 52 t.Errorf("GetSlavesCount() error = %v, wantErr %v", err, tt.wantErr) 53 return 54 } 55 assert.Equal(t, tt.want, got) 56 }) 57 } 58 } 59 60 func TestValidateAndGetSlavePodName(t *testing.T) { 61 t.Parallel() 62 63 tests := []struct { 64 testName string 65 executionId string 66 currentSlaveCount int 67 expectedOutput string 68 }{ 69 { 70 testName: "aVeryLongTestNameThatExceedsTheLimitWhenConcatenated", 71 executionId: "exec123", 72 currentSlaveCount: 5, 73 expectedOutput: "aVeryLongTestNameTha-slave-5-exec123", 74 }, 75 { 76 testName: "shortName", 77 executionId: "exec123", 78 currentSlaveCount: 5, 79 expectedOutput: "shortName-slave-5-exec123", 80 }, 81 } 82 83 for _, tt := range tests { 84 t.Run(tt.testName, func(t *testing.T) { 85 actualOutput := validateAndGetSlavePodName(tt.testName, tt.executionId, tt.currentSlaveCount) 86 if actualOutput != tt.expectedOutput { 87 t.Errorf("expected %v, got %v", tt.expectedOutput, actualOutput) 88 } 89 }) 90 } 91 } 92 93 func TestIsPodReady(t *testing.T) { 94 t.Parallel() 95 96 ctx := context.Background() 97 98 t.Run("poll until pod is ready", func(t *testing.T) { 99 t.Parallel() 100 101 clientset := fake.NewSimpleClientset() 102 103 pod := &v1.Pod{ 104 ObjectMeta: metav1.ObjectMeta{ 105 Name: "test-pod", 106 Namespace: "default", 107 }, 108 Status: v1.PodStatus{ 109 Phase: v1.PodRunning, 110 Conditions: []v1.PodCondition{ 111 { 112 Type: v1.PodReady, 113 Status: v1.ConditionTrue, 114 }, 115 }, 116 PodIP: "192.168.1.1", 117 }, 118 } 119 120 _, err := clientset.CoreV1().Pods("default").Create(ctx, pod, metav1.CreateOptions{}) 121 if err != nil { 122 t.Fatalf("error injecting pod add: %v", err) 123 } 124 125 conditionFunc := isPodReady(clientset, "test-pod", "default") 126 127 // Use PollImmediate to repeatedly evaluate condition 128 err = wait.PollUntilContextTimeout(ctx, time.Millisecond*5, time.Second*3, true, conditionFunc) 129 if err != nil { 130 t.Fatalf("error waiting for pod to be ready: %v", err) 131 } 132 }) 133 134 t.Run("poll times out", func(t *testing.T) { 135 t.Parallel() 136 137 clientset := fake.NewSimpleClientset() 138 139 pod := &v1.Pod{ 140 ObjectMeta: metav1.ObjectMeta{ 141 Name: "test-pod", 142 Namespace: "default", 143 }, 144 Status: v1.PodStatus{ 145 Phase: v1.PodRunning, 146 Conditions: []v1.PodCondition{ 147 { 148 Type: v1.PodInitialized, 149 Status: v1.ConditionFalse, 150 }, 151 }, 152 PodIP: "192.168.1.1", 153 }, 154 } 155 156 _, err := clientset.CoreV1().Pods("default").Create(ctx, pod, metav1.CreateOptions{}) 157 if err != nil { 158 t.Fatalf("error injecting pod add: %v", err) 159 } 160 161 conditionFunc := isPodReady(clientset, "test-pod", "default") 162 163 // Use PollImmediate to repeatedly evaluate condition 164 err = wait.PollUntilContextTimeout(ctx, time.Millisecond*50, time.Millisecond*160, true, conditionFunc) 165 assert.ErrorContains(t, err, "context deadline exceeded") 166 }) 167 168 }