Skip to content

Commit f203f8a

Browse files
committed
test: validate CA status ConfigMap
1 parent a37209a commit f203f8a

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

test/e2e/clusterautoscaler_helpers.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@ import (
1414
. "github.com/onsi/ginkgo/v2"
1515
. "github.com/onsi/gomega"
1616
appsv1 "k8s.io/api/apps/v1"
17+
corev1 "k8s.io/api/core/v1"
1718
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1819
"k8s.io/apimachinery/pkg/types"
1920
"k8s.io/utils/ptr"
2021
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
2122
addonsv1 "sigs.k8s.io/cluster-api/exp/addons/api/v1beta1"
2223
"sigs.k8s.io/cluster-api/test/framework"
24+
"sigs.k8s.io/yaml"
2325

2426
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/v1alpha1"
2527
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/utils"
@@ -130,4 +132,24 @@ func WaitForClusterAutoscalerToBeReadyForWorkloadCluster(
130132
),
131133
),
132134
)
135+
136+
statusConfigMap := &corev1.ConfigMap{
137+
ObjectMeta: metav1.ObjectMeta{
138+
Namespace: "kube-system",
139+
Name: "cluster-autoscaler-status",
140+
},
141+
}
142+
143+
WaitForConfigMapData(ctx, WaitForConfigMapDataInput{
144+
Getter: workloadClusterClient,
145+
ConfigMap: statusConfigMap,
146+
DataValidator: func(data map[string]string) bool {
147+
type clusterAutoscalerStatus struct {
148+
AutoScalerStatus string `json:"autoscalerStatus,omitempty" yaml:"autoscalerStatus,omitempty"`
149+
}
150+
status := &clusterAutoscalerStatus{}
151+
err = yaml.Unmarshal([]byte(data["status"]), status)
152+
return err == nil && status.AutoScalerStatus == "Running"
153+
},
154+
}, input.DeploymentIntervals...)
133155
}

test/e2e/configmap_helpers.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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

Comments
 (0)