|
15 | 15 | package pod_test |
16 | 16 |
|
17 | 17 | import ( |
| 18 | + "context" |
18 | 19 | "testing" |
| 20 | + "time" |
| 21 | + |
| 22 | + "github.com/NVIDIA/aicr/pkg/k8s/pod" |
| 23 | + batchv1 "k8s.io/api/batch/v1" |
| 24 | + corev1 "k8s.io/api/core/v1" |
| 25 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 26 | + "k8s.io/apimachinery/pkg/watch" |
| 27 | + "k8s.io/client-go/kubernetes/fake" |
| 28 | + k8stesting "k8s.io/client-go/testing" |
19 | 29 | ) |
20 | 30 |
|
21 | | -// Tests for Job operations will be added as needed. |
22 | | -// The implementation is validated through integration tests in k8s/agent and validator/agent. |
23 | | -func TestJobPackage(t *testing.T) { |
24 | | - // Placeholder test to satisfy go test |
25 | | - t.Log("pkg/k8s/pod/job package loaded successfully") |
| 31 | +func TestWaitForJobCompletion_Success(t *testing.T) { |
| 32 | + job := &batchv1.Job{ |
| 33 | + ObjectMeta: metav1.ObjectMeta{Name: "test-job", Namespace: "default"}, |
| 34 | + } |
| 35 | + //nolint:staticcheck // SA1019: fake.NewSimpleClientset is sufficient for tests |
| 36 | + client := fake.NewSimpleClientset(job) |
| 37 | + |
| 38 | + watcher := watch.NewFake() |
| 39 | + client.PrependWatchReactor("jobs", k8stesting.DefaultWatchReactor(watcher, nil)) |
| 40 | + |
| 41 | + go func() { |
| 42 | + time.Sleep(50 * time.Millisecond) |
| 43 | + completedJob := job.DeepCopy() |
| 44 | + completedJob.Status.Conditions = []batchv1.JobCondition{ |
| 45 | + {Type: batchv1.JobComplete, Status: corev1.ConditionTrue}, |
| 46 | + } |
| 47 | + watcher.Modify(completedJob) |
| 48 | + }() |
| 49 | + |
| 50 | + err := pod.WaitForJobCompletion(context.Background(), client, "default", "test-job", 5*time.Second) |
| 51 | + if err != nil { |
| 52 | + t.Errorf("expected no error, got: %v", err) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func TestWaitForJobCompletion_Failure(t *testing.T) { |
| 57 | + job := &batchv1.Job{ |
| 58 | + ObjectMeta: metav1.ObjectMeta{Name: "test-job", Namespace: "default"}, |
| 59 | + } |
| 60 | + //nolint:staticcheck // SA1019: fake.NewSimpleClientset is sufficient for tests |
| 61 | + client := fake.NewSimpleClientset(job) |
| 62 | + |
| 63 | + watcher := watch.NewFake() |
| 64 | + client.PrependWatchReactor("jobs", k8stesting.DefaultWatchReactor(watcher, nil)) |
| 65 | + |
| 66 | + go func() { |
| 67 | + time.Sleep(50 * time.Millisecond) |
| 68 | + failedJob := job.DeepCopy() |
| 69 | + failedJob.Status.Conditions = []batchv1.JobCondition{ |
| 70 | + {Type: batchv1.JobFailed, Status: corev1.ConditionTrue, Reason: "BackoffLimitExceeded"}, |
| 71 | + } |
| 72 | + watcher.Modify(failedJob) |
| 73 | + }() |
| 74 | + |
| 75 | + err := pod.WaitForJobCompletion(context.Background(), client, "default", "test-job", 5*time.Second) |
| 76 | + if err == nil { |
| 77 | + t.Error("expected error for failed job") |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +func TestWaitForJobCompletion_Timeout(t *testing.T) { |
| 82 | + job := &batchv1.Job{ |
| 83 | + ObjectMeta: metav1.ObjectMeta{Name: "test-job", Namespace: "default"}, |
| 84 | + } |
| 85 | + //nolint:staticcheck // SA1019: fake.NewSimpleClientset is sufficient for tests |
| 86 | + client := fake.NewSimpleClientset(job) |
| 87 | + |
| 88 | + watcher := watch.NewFake() |
| 89 | + client.PrependWatchReactor("jobs", k8stesting.DefaultWatchReactor(watcher, nil)) |
| 90 | + |
| 91 | + err := pod.WaitForJobCompletion(context.Background(), client, "default", "test-job", 100*time.Millisecond) |
| 92 | + if err == nil { |
| 93 | + t.Error("expected timeout error") |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +func TestWaitForJobCompletion_ContextCancelled(t *testing.T) { |
| 98 | + //nolint:staticcheck // SA1019: fake.NewSimpleClientset is sufficient for tests |
| 99 | + client := fake.NewSimpleClientset() |
| 100 | + |
| 101 | + watcher := watch.NewFake() |
| 102 | + client.PrependWatchReactor("jobs", k8stesting.DefaultWatchReactor(watcher, nil)) |
| 103 | + |
| 104 | + ctx, cancel := context.WithCancel(context.Background()) |
| 105 | + cancel() |
| 106 | + |
| 107 | + err := pod.WaitForJobCompletion(ctx, client, "default", "test-job", 5*time.Second) |
| 108 | + if err == nil { |
| 109 | + t.Error("expected error for cancelled context") |
| 110 | + } |
26 | 111 | } |
0 commit comments