/
workload_test_utils.go
116 lines (105 loc) · 5.97 KB
/
workload_test_utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
Copyright (c) Microsoft Corporation.
Licensed under the MIT license.
*/
package utils
import (
"context"
"fmt"
"time"
// Lint check prohibits non "_test" ending files to have dot imports for ginkgo / gomega.
"github.com/google/go-cmp/cmp"
"github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
fleetv1alpha1 "go.goms.io/fleet/apis/v1alpha1"
"go.goms.io/fleet/test/e2e/framework"
)
// CmpClusterRole compares actual cluster role with expected cluster role.
func CmpClusterRole(ctx context.Context, cluster framework.Cluster, objectKey *types.NamespacedName, wantClusterRole *rbacv1.ClusterRole, cmpOptions []cmp.Option) {
gotClusterRole := &rbacv1.ClusterRole{}
gomega.Eventually(func() error {
if err := cluster.KubeClient.Get(ctx, types.NamespacedName{Name: objectKey.Name}, gotClusterRole); err != nil {
return err
}
if diff := cmp.Diff(wantClusterRole, gotClusterRole, cmpOptions...); diff != "" {
return fmt.Errorf("cluster role(%s) mismatch (-want +got):\n%s", gotClusterRole.Name, diff)
}
return nil
}, PollTimeout, PollInterval).Should(gomega.Succeed(), "Failed to compare actual and expected cluster roles in %s cluster", cluster.ClusterName)
}
// CmpNamespace compares actual namespace with expected namespace.
func CmpNamespace(ctx context.Context, cluster framework.Cluster, objectKey *types.NamespacedName, wantNamespace *corev1.Namespace, cmpOptions []cmp.Option) {
gotNamespace := &corev1.Namespace{}
gomega.Eventually(func() error {
if err := cluster.KubeClient.Get(ctx, types.NamespacedName{Name: objectKey.Name}, gotNamespace); err != nil {
return err
}
if diff := cmp.Diff(wantNamespace, gotNamespace, cmpOptions...); diff != "" {
return fmt.Errorf(" namespace(%s) mismatch (-want +got):\n%s", gotNamespace.Name, diff)
}
return nil
}, PollTimeout, PollInterval).Should(gomega.Succeed(), "Failed to compare actual and expected namespaces in %s cluster", cluster.ClusterName)
}
// CmpRole compares actual role with expected role.
func CmpRole(ctx context.Context, cluster framework.Cluster, objectKey *types.NamespacedName, wantRole *rbacv1.Role, cmpOptions []cmp.Option) {
gotRole := &rbacv1.Role{}
gomega.Eventually(func() error {
if err := cluster.KubeClient.Get(ctx, types.NamespacedName{Name: objectKey.Name, Namespace: objectKey.Namespace}, gotRole); err != nil {
return err
}
if diff := cmp.Diff(wantRole, gotRole, cmpOptions...); diff != "" {
return fmt.Errorf("role(%s) mismatch (-want +got):\n%s", gotRole.Name, diff)
}
return nil
}, PollTimeout, PollInterval).Should(gomega.Succeed(), "Failed to compare actual and expected roles in %s cluster", cluster.ClusterName)
}
// CmpRoleBinding compares actual role binding with expected role binding.
func CmpRoleBinding(ctx context.Context, cluster framework.Cluster, objectKey *types.NamespacedName, wantRoleBinding *rbacv1.RoleBinding, cmpOptions []cmp.Option) {
gotRoleBinding := &rbacv1.RoleBinding{}
gomega.Eventually(func() error {
if err := cluster.KubeClient.Get(ctx, types.NamespacedName{Name: objectKey.Name, Namespace: objectKey.Namespace}, gotRoleBinding); err != nil {
return err
}
if diff := cmp.Diff(wantRoleBinding, gotRoleBinding, cmpOptions...); diff != "" {
return fmt.Errorf("role binding(%s) mismatch (-want +got):\n%s", gotRoleBinding.Name, diff)
}
return nil
}, PollTimeout, PollInterval).Should(gomega.Succeed(), "Failed to compare actual and expected role bindings in %s cluster", cluster.ClusterName)
}
// CmpSecret compares actual secret with expected secret and returns actual secret.
func CmpSecret(ctx context.Context, cluster framework.Cluster, objectKey *types.NamespacedName, wantSecret *corev1.Secret, cmpOptions []cmp.Option) *corev1.Secret {
gotSecret := &corev1.Secret{}
gomega.Eventually(func() error {
if err := cluster.KubeClient.Get(ctx, types.NamespacedName{Name: objectKey.Name, Namespace: objectKey.Namespace}, gotSecret); err != nil {
return err
}
if diff := cmp.Diff(wantSecret, gotSecret, cmpOptions...); diff != "" {
return fmt.Errorf("secret binding(%s) mismatch (-want +got):\n%s", gotSecret.Name, diff)
}
return nil
}, PollTimeout, PollInterval).Should(gomega.Succeed(), "Failed to compare actual and expected secret in %s cluster", cluster.ClusterName)
return gotSecret
}
// WaitCreateClusterResourcePlacementStatus waits for ClusterResourcePlacement to present on th hub cluster with a specific status.
func WaitCreateClusterResourcePlacementStatus(ctx context.Context, cluster framework.Cluster, objectKey *types.NamespacedName, wantCRPStatus fleetv1alpha1.ClusterResourcePlacementStatus, crpStatusCmpOptions []cmp.Option, customTimeout time.Duration) {
gotCRP := &fleetv1alpha1.ClusterResourcePlacement{}
gomega.Eventually(func() error {
if err := cluster.KubeClient.Get(ctx, types.NamespacedName{Name: objectKey.Name}, gotCRP); err != nil {
return err
}
if statusDiff := cmp.Diff(wantCRPStatus, gotCRP.Status, crpStatusCmpOptions...); statusDiff != "" {
return fmt.Errorf("cluster resource placment(%s) status mismatch (-want +got):\n%s", gotCRP.Name, statusDiff)
}
return nil
}, customTimeout, PollInterval).Should(gomega.Succeed(), "Failed to wait for cluster resource placement %s status to be updated", gotCRP.Name, cluster.ClusterName)
}
// DeleteClusterResourcePlacement is used delete ClusterResourcePlacement on the hub cluster.
func DeleteClusterResourcePlacement(ctx context.Context, cluster framework.Cluster, crp *fleetv1alpha1.ClusterResourcePlacement) {
gomega.Expect(cluster.KubeClient.Delete(ctx, crp)).Should(gomega.Succeed(), "Failed to delete cluster resource placement %s in %s cluster", crp.Name, cluster.ClusterName)
gomega.Eventually(func() bool {
return apierrors.IsNotFound(cluster.KubeClient.Get(ctx, types.NamespacedName{Name: crp.Name}, crp))
}, PollTimeout, PollInterval).Should(gomega.BeTrue(), "Failed to wait for cluster resource placement %s to be deleted in %s cluster", crp.Name, cluster.ClusterName)
}