@@ -17,18 +17,41 @@ limitations under the License.
1717package controller_manager
1818
1919import (
20+ "context"
21+ "strings"
2022 "testing"
2123 "time"
2224
2325 "github.com/stretchr/testify/assert"
2426 "github.com/stretchr/testify/require"
27+ clientset "github.com/volcano-sh/kthena/client-go/clientset/versioned"
2528 workload "github.com/volcano-sh/kthena/pkg/apis/workload/v1alpha1"
2629 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2730)
2831
32+ // waitForWebhookReady waits until the webhook is ready to accept connections.
33+ func waitForWebhookReady (t * testing.T , ctx context.Context , kthenaClient * clientset.Clientset , timeout time.Duration ) {
34+ t .Helper ()
35+ deadline := time .Now ().Add (timeout )
36+ probe := createValidModelBoosterForWebhookTest ()
37+ for time .Now ().Before (deadline ) {
38+ _ , err := kthenaClient .WorkloadV1alpha1 ().ModelBoosters (testNamespace ).Create (ctx , probe , metav1.CreateOptions {DryRun : []string {"All" }})
39+ if err == nil {
40+ return
41+ }
42+ if ! strings .Contains (err .Error (), "connect: connection refused" ) {
43+ t .Fatalf ("Webhook probe failed: %v" , err )
44+ }
45+ t .Logf ("Webhook not ready, retrying: %v" , err )
46+ time .Sleep (2 * time .Second )
47+ }
48+ t .Fatal ("Webhook did not become ready within timeout" )
49+ }
50+
2951// TestWebhook tests that the webhooks (validation and mutation) work as expected.
3052func TestWebhook (t * testing.T ) {
3153 ctx , kthenaClient , _ := setupControllerManagerE2ETest (t )
54+ waitForWebhookReady (t , ctx , kthenaClient , 2 * time .Minute ) // avoid flaky "connection refused" after controller-manager restart
3255
3356 testCases := []struct {
3457 name string
0 commit comments