|
| 1 | +//go:build e2e |
| 2 | + |
| 3 | +// Copyright 2024 Nutanix. All rights reserved. |
| 4 | +// SPDX-License-Identifier: Apache-2.0 |
| 5 | + |
| 6 | +package e2e |
| 7 | + |
| 8 | +import ( |
| 9 | + "context" |
| 10 | + "fmt" |
| 11 | + "strings" |
| 12 | + "time" |
| 13 | + |
| 14 | + . "github.com/onsi/gomega" |
| 15 | + corev1 "k8s.io/api/core/v1" |
| 16 | + "k8s.io/klog/v2" |
| 17 | + capie2e "sigs.k8s.io/cluster-api/test/e2e" |
| 18 | + "sigs.k8s.io/cluster-api/test/framework" |
| 19 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 20 | +) |
| 21 | + |
| 22 | +type WaitForConfigMapDataInput struct { |
| 23 | + Getter framework.Getter |
| 24 | + ConfigMap *corev1.ConfigMap |
| 25 | + DataValidator func(data map[string]string) bool |
| 26 | +} |
| 27 | + |
| 28 | +func WaitForConfigMapData( |
| 29 | + ctx context.Context, |
| 30 | + input WaitForConfigMapDataInput, |
| 31 | + intervals ...interface{}, |
| 32 | +) { |
| 33 | + start := time.Now() |
| 34 | + key := client.ObjectKeyFromObject(input.ConfigMap) |
| 35 | + capie2e.Byf("waiting for ConfigMap %s to have expected data", key) |
| 36 | + Log("starting to wait for ConfigMap to have expected data") |
| 37 | + Eventually(func() bool { |
| 38 | + if err := input.Getter.Get(ctx, key, input.ConfigMap); err == nil { |
| 39 | + return input.DataValidator(input.ConfigMap.Data) |
| 40 | + } |
| 41 | + return false |
| 42 | + }, intervals...).Should(BeTrue(), func() string { |
| 43 | + return DescribeIncorrectConfigMapData(input, input.ConfigMap) |
| 44 | + }) |
| 45 | + Logf("ConfigMap %s has expected data, took %v", key, time.Since(start)) |
| 46 | +} |
| 47 | + |
| 48 | +// DescribeIncorrectConfigMapData returns detailed output to help debug a ConfigMap data validation failure in e2e. |
| 49 | +func DescribeIncorrectConfigMapData( |
| 50 | + input WaitForConfigMapDataInput, |
| 51 | + configMap *corev1.ConfigMap, |
| 52 | +) string { |
| 53 | + b := strings.Builder{} |
| 54 | + b.WriteString(fmt.Sprintf("ConfigMap %s failed to get expected data:\n", |
| 55 | + klog.KObj(input.ConfigMap))) |
| 56 | + if configMap == nil { |
| 57 | + b.WriteString("\nConfigMap: nil\n") |
| 58 | + } else { |
| 59 | + b.WriteString(fmt.Sprintf("\nConfigMap:\n%s\n", framework.PrettyPrint(configMap))) |
| 60 | + } |
| 61 | + return b.String() |
| 62 | +} |
0 commit comments