|
| 1 | +// Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package main |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + stderrors "errors" |
| 20 | + "sync/atomic" |
| 21 | + "testing" |
| 22 | + "time" |
| 23 | + |
| 24 | + "github.com/NVIDIA/aicr/pkg/errors" |
| 25 | + "github.com/NVIDIA/aicr/validators" |
| 26 | + appsv1 "k8s.io/api/apps/v1" |
| 27 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 28 | + "k8s.io/apimachinery/pkg/runtime" |
| 29 | + k8sfake "k8s.io/client-go/kubernetes/fake" |
| 30 | + k8stesting "k8s.io/client-go/testing" |
| 31 | +) |
| 32 | + |
| 33 | +//nolint:unparam // name is a meaningful fixture input even though current cases all use "admission". |
| 34 | +func deployWithAvailable(name string, avail int32) *appsv1.Deployment { |
| 35 | + r := int32(1) |
| 36 | + return &appsv1.Deployment{ |
| 37 | + ObjectMeta: metav1.ObjectMeta{Name: name, Namespace: "kai-scheduler"}, |
| 38 | + Spec: appsv1.DeploymentSpec{Replicas: &r}, |
| 39 | + Status: appsv1.DeploymentStatus{AvailableReplicas: avail}, |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +// TestWaitForDeploymentAvailable_ImmediatelyReady returns without waiting when |
| 44 | +// the deployment is already Available. |
| 45 | +func TestWaitForDeploymentAvailable_ImmediatelyReady(t *testing.T) { |
| 46 | + t.Parallel() |
| 47 | + client := k8sfake.NewClientset(deployWithAvailable("admission", 1)) |
| 48 | + vctx := &validators.Context{Ctx: context.Background(), Clientset: client} |
| 49 | + |
| 50 | + got, err := waitForDeploymentAvailable(vctx, "kai-scheduler", "admission", 5*time.Second) |
| 51 | + if err != nil { |
| 52 | + t.Fatalf("unexpected error: %v", err) |
| 53 | + } |
| 54 | + if got == nil || got.Status.AvailableReplicas != 1 { |
| 55 | + t.Fatalf("expected available deployment, got %+v", got) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +// TestWaitForDeploymentAvailable_TransientBlip is the regression test for the |
| 60 | +// gang-scheduling flake: a deployment that reports 0/1 on the first sample and |
| 61 | +// becomes Available shortly after must pass, not fail closed. |
| 62 | +func TestWaitForDeploymentAvailable_TransientBlip(t *testing.T) { |
| 63 | + t.Parallel() |
| 64 | + client := k8sfake.NewClientset(deployWithAvailable("admission", 0)) |
| 65 | + |
| 66 | + var calls int32 |
| 67 | + client.PrependReactor("get", "deployments", func(k8stesting.Action) (bool, runtime.Object, error) { |
| 68 | + n := atomic.AddInt32(&calls, 1) |
| 69 | + avail := int32(0) |
| 70 | + if n >= 2 { // available from the second poll onward |
| 71 | + avail = 1 |
| 72 | + } |
| 73 | + return true, deployWithAvailable("admission", avail), nil |
| 74 | + }) |
| 75 | + |
| 76 | + vctx := &validators.Context{Ctx: context.Background(), Clientset: client} |
| 77 | + got, err := waitForDeploymentAvailable(vctx, "kai-scheduler", "admission", 5*time.Second) |
| 78 | + if err != nil { |
| 79 | + t.Fatalf("expected success after transient 0/1, got error: %v", err) |
| 80 | + } |
| 81 | + if got == nil || got.Status.AvailableReplicas != 1 { |
| 82 | + t.Fatalf("expected available deployment, got %+v", got) |
| 83 | + } |
| 84 | + if atomic.LoadInt32(&calls) < 2 { |
| 85 | + t.Errorf("expected the helper to re-poll past the initial 0/1 (calls=%d)", calls) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +// TestWaitForDeploymentAvailable_NeverReady fails closed (after the bound) when |
| 90 | +// the deployment stays unavailable. |
| 91 | +func TestWaitForDeploymentAvailable_NeverReady(t *testing.T) { |
| 92 | + t.Parallel() |
| 93 | + client := k8sfake.NewClientset(deployWithAvailable("admission", 0)) |
| 94 | + vctx := &validators.Context{Ctx: context.Background(), Clientset: client} |
| 95 | + |
| 96 | + _, err := waitForDeploymentAvailable(vctx, "kai-scheduler", "admission", 50*time.Millisecond) |
| 97 | + if err == nil { |
| 98 | + t.Fatal("expected error for never-ready deployment") |
| 99 | + } |
| 100 | + if !stderrors.Is(err, errors.New(errors.ErrCodeInternal, "")) { |
| 101 | + t.Errorf("expected ErrCodeInternal, got %v", err) |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +// TestWaitForDeploymentAvailable_ParentCanceled ensures an external abort |
| 106 | +// (parent context canceled) is propagated as a transient timeout, not |
| 107 | +// misclassified as a deployment readiness failure. |
| 108 | +func TestWaitForDeploymentAvailable_ParentCanceled(t *testing.T) { |
| 109 | + t.Parallel() |
| 110 | + client := k8sfake.NewClientset(deployWithAvailable("admission", 0)) |
| 111 | + parent, cancel := context.WithCancel(context.Background()) |
| 112 | + cancel() // external abort before the wait runs |
| 113 | + vctx := &validators.Context{Ctx: parent, Clientset: client} |
| 114 | + |
| 115 | + // Generous bound so any failure must come from the parent cancellation, |
| 116 | + // not our own deadline. |
| 117 | + _, err := waitForDeploymentAvailable(vctx, "kai-scheduler", "admission", 5*time.Second) |
| 118 | + if err == nil { |
| 119 | + t.Fatal("expected error on canceled parent context") |
| 120 | + } |
| 121 | + if !stderrors.Is(err, errors.New(errors.ErrCodeTimeout, "")) { |
| 122 | + t.Errorf("expected ErrCodeTimeout for cancellation, got %v", err) |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +// TestWaitForDeploymentAvailable_NotFound surfaces a missing deployment as |
| 127 | +// NotFound after the bound (not a generic internal error). |
| 128 | +func TestWaitForDeploymentAvailable_NotFound(t *testing.T) { |
| 129 | + t.Parallel() |
| 130 | + client := k8sfake.NewClientset() // no deployments |
| 131 | + vctx := &validators.Context{Ctx: context.Background(), Clientset: client} |
| 132 | + |
| 133 | + _, err := waitForDeploymentAvailable(vctx, "kai-scheduler", "admission", 50*time.Millisecond) |
| 134 | + if err == nil { |
| 135 | + t.Fatal("expected error for missing deployment") |
| 136 | + } |
| 137 | + if !stderrors.Is(err, errors.New(errors.ErrCodeNotFound, "")) { |
| 138 | + t.Errorf("expected ErrCodeNotFound, got %v", err) |
| 139 | + } |
| 140 | +} |
0 commit comments