Skip to content

Commit

Permalink
Merge pull request #10 from sbueringer/bump-controller-runtime-v0.14.…
Browse files Browse the repository at this point in the history
…1-fix-tests

fix tests
  • Loading branch information
aniruddha2000 committed Jan 18, 2023
2 parents 843a7a9 + 85c7742 commit 71e207e
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 13 deletions.
5 changes: 3 additions & 2 deletions api/v1beta1/index/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,15 @@ const (
func ByClusterClassName(ctx context.Context, mgr ctrl.Manager) error {
if err := mgr.GetCache().IndexField(ctx, &clusterv1.Cluster{},
ClusterClassNameField,
clusterByClassName,
ClusterByClusterClassClassName,
); err != nil {
return errors.Wrap(err, "error setting index field")
}
return nil
}

func clusterByClassName(o client.Object) []string {
// ClusterByClusterClassClassName contains the logic to index Clusters by ClusterClass name.
func ClusterByClusterClassClassName(o client.Object) []string {
cluster, ok := o.(*clusterv1.Cluster)
if !ok {
panic(fmt.Sprintf("Expected Cluster but got a %T", o))
Expand Down
2 changes: 1 addition & 1 deletion api/v1beta1/index/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestClusterByClassName(t *testing.T) {
for _, test := range testCases {
t.Run(test.name, func(t *testing.T) {
g := NewWithT(t)
got := clusterByClassName(test.object)
got := ClusterByClusterClassClassName(test.object)
g.Expect(got).To(Equal(test.expected))
})
}
Expand Down
5 changes: 3 additions & 2 deletions api/v1beta1/index/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,16 @@ const (
func ByMachineNode(ctx context.Context, mgr ctrl.Manager) error {
if err := mgr.GetCache().IndexField(ctx, &clusterv1.Machine{},
MachineNodeNameField,
machineByNodeName,
MachineByNodeName,
); err != nil {
return errors.Wrap(err, "error setting index field")
}

return nil
}

func machineByNodeName(o client.Object) []string {
// MachineByNodeName contains the logic to index Machines by Node name.
func MachineByNodeName(o client.Object) []string {
machine, ok := o.(*clusterv1.Machine)
if !ok {
panic(fmt.Sprintf("Expected a Machine but got a %T", o))
Expand Down
2 changes: 1 addition & 1 deletion api/v1beta1/index/machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func TestIndexMachineByNodeName(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
g := NewWithT(t)
got := machineByNodeName(tc.object)
got := MachineByNodeName(tc.object)
g.Expect(got).To(ConsistOf(tc.expected))
})
}
Expand Down
17 changes: 13 additions & 4 deletions internal/controllers/machine/machine_controller_phases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/log"

clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/api/v1beta1/index"
"sigs.k8s.io/cluster-api/controllers/remote"
"sigs.k8s.io/cluster-api/internal/test/builder"
"sigs.k8s.io/cluster-api/util/conditions"
Expand Down Expand Up @@ -283,7 +284,9 @@ func TestReconcileMachinePhases(t *testing.T) {
bootstrapConfig,
infraConfig,
defaultKubeconfigSecret,
).Build()
).
WithIndex(&corev1.Node{}, index.NodeProviderIDField, index.NodeByProviderID).
Build()
r := &Reconciler{
Client: cl,
Tracker: remote.NewTestClusterCacheTracker(logr.New(log.NullLogSink{}), cl, scheme.Scheme, client.ObjectKey{Name: defaultCluster.Name, Namespace: defaultCluster.Namespace}),
Expand Down Expand Up @@ -349,7 +352,9 @@ func TestReconcileMachinePhases(t *testing.T) {
bootstrapConfig,
infraConfig,
defaultKubeconfigSecret,
).Build()
).
WithIndex(&corev1.Node{}, index.NodeProviderIDField, index.NodeByProviderID).
Build()
r := &Reconciler{
Client: cl,
Tracker: remote.NewTestClusterCacheTracker(logr.New(log.NullLogSink{}), cl, scheme.Scheme, client.ObjectKey{Name: defaultCluster.Name, Namespace: defaultCluster.Namespace}),
Expand Down Expand Up @@ -425,7 +430,9 @@ func TestReconcileMachinePhases(t *testing.T) {
bootstrapConfig,
infraConfig,
defaultKubeconfigSecret,
).Build()
).
WithIndex(&corev1.Node{}, index.NodeProviderIDField, index.NodeByProviderID).
Build()
r := &Reconciler{
Client: cl,
Tracker: remote.NewTestClusterCacheTracker(logr.New(log.NullLogSink{}), cl, scheme.Scheme, client.ObjectKey{Name: defaultCluster.Name, Namespace: defaultCluster.Namespace}),
Expand Down Expand Up @@ -484,7 +491,9 @@ func TestReconcileMachinePhases(t *testing.T) {
builder.GenericInfrastructureMachineCRD.DeepCopy(),
bootstrapConfig,
infraConfig,
).Build()
).
WithIndex(&corev1.Node{}, index.NodeProviderIDField, index.NodeByProviderID).
Build()

r := &Reconciler{
Client: cl,
Expand Down
9 changes: 7 additions & 2 deletions internal/controllers/machine/machine_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/reconcile"

clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/api/v1beta1/index"
"sigs.k8s.io/cluster-api/controllers/remote"
"sigs.k8s.io/cluster-api/internal/test/builder"
"sigs.k8s.io/cluster-api/util"
Expand Down Expand Up @@ -723,7 +724,9 @@ func TestReconcileRequest(t *testing.T) {
&tc.machine,
builder.GenericInfrastructureMachineCRD.DeepCopy(),
&infraConfig,
).Build()
).
WithIndex(&corev1.Node{}, index.NodeProviderIDField, index.NodeByProviderID).
Build()

r := &Reconciler{
Client: clientFake,
Expand Down Expand Up @@ -968,7 +971,9 @@ func TestMachineConditions(t *testing.T) {
builder.GenericBootstrapConfigCRD.DeepCopy(),
bootstrap,
node,
).Build()
).
WithIndex(&corev1.Node{}, index.NodeProviderIDField, index.NodeByProviderID).
Build()

r := &Reconciler{
Client: clientFake,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/reconcile"

clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/api/v1beta1/index"
"sigs.k8s.io/cluster-api/controllers/remote"
capierrors "sigs.k8s.io/cluster-api/errors"
"sigs.k8s.io/cluster-api/internal/test/builder"
Expand Down Expand Up @@ -1957,7 +1958,9 @@ func TestMachineToMachineHealthCheck(t *testing.T) {
}

func TestNodeToMachineHealthCheck(t *testing.T) {
fakeClient := fake.NewClientBuilder().Build()
fakeClient := fake.NewClientBuilder().
WithIndex(&clusterv1.Machine{}, index.MachineNodeNameField, index.MachineByNodeName).
Build()

r := &Reconciler{
Client: fakeClient,
Expand Down
5 changes: 5 additions & 0 deletions internal/webhooks/clusterclass_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client/fake"

clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/api/v1beta1/index"
"sigs.k8s.io/cluster-api/feature"
"sigs.k8s.io/cluster-api/internal/test/builder"
"sigs.k8s.io/cluster-api/internal/webhooks/util"
Expand Down Expand Up @@ -73,6 +74,7 @@ func TestClusterClassDefaultNamespaces(t *testing.T) {

fakeClient := fake.NewClientBuilder().
WithScheme(fakeScheme).
WithIndex(&clusterv1.Cluster{}, index.ClusterClassNameField, index.ClusterByClusterClassClassName).
Build()

// Create the webhook and add the fakeClient as its client.
Expand Down Expand Up @@ -1148,6 +1150,7 @@ func TestClusterClassValidation(t *testing.T) {
// Sets up the fakeClient for the test case.
fakeClient := fake.NewClientBuilder().
WithScheme(fakeScheme).
WithIndex(&clusterv1.Cluster{}, index.ClusterClassNameField, index.ClusterByClusterClassClassName).
Build()

// Create the webhook and add the fakeClient as its client.
Expand Down Expand Up @@ -1625,6 +1628,7 @@ func TestClusterClassValidationWithClusterAwareChecks(t *testing.T) {
fakeClient := fake.NewClientBuilder().
WithScheme(fakeScheme).
WithObjects(tt.clusters...).
WithIndex(&clusterv1.Cluster{}, index.ClusterClassNameField, index.ClusterByClusterClassClassName).
Build()

// Create the webhook and add the fakeClient as its client.
Expand Down Expand Up @@ -2753,6 +2757,7 @@ func TestClusterClassValidationWithVariableChecks(t *testing.T) {
fakeClient := fake.NewClientBuilder().
WithScheme(fakeScheme).
WithObjects(tt.clusters...).
WithIndex(&clusterv1.Cluster{}, index.ClusterClassNameField, index.ClusterByClusterClassClassName).
Build()

// Create the webhook and add the fakeClient as its client.
Expand Down

0 comments on commit 71e207e

Please sign in to comment.