Skip to content

Commit 8d12c67

Browse files
committed
fix tests
1 parent cba134c commit 8d12c67

File tree

2 files changed

+47
-16
lines changed

2 files changed

+47
-16
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
Copyright 2024 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package clustercache
18+
19+
import (
20+
"k8s.io/apimachinery/pkg/util/sets"
21+
"sigs.k8s.io/controller-runtime/pkg/client"
22+
)
23+
24+
// NewFakeClusterCache creates a new fake ClusterCache that can be used by unit tests.
25+
func NewFakeClusterCache(workloadClient client.Client, clusterKey client.ObjectKey, watchObjects ...string) ClusterCache {
26+
testCacheTracker := &clusterCache{
27+
clusterAccessors: make(map[client.ObjectKey]*clusterAccessor),
28+
}
29+
30+
testCacheTracker.clusterAccessors[clusterKey] = &clusterAccessor{
31+
lockedState: clusterAccessorLockedState{
32+
connection: &clusterAccessorLockedConnectionState{
33+
cachedClient: workloadClient,
34+
watches: sets.Set[string]{}.Insert(watchObjects...),
35+
},
36+
},
37+
}
38+
return testCacheTracker
39+
}

internal/controllers/machine/machine_controller_test.go

+8-16
Original file line numberDiff line numberDiff line change
@@ -22,27 +22,24 @@ import (
2222
"testing"
2323
"time"
2424

25-
"github.com/go-logr/logr"
2625
. "github.com/onsi/gomega"
2726
appsv1 "k8s.io/api/apps/v1"
2827
corev1 "k8s.io/api/core/v1"
2928
apierrors "k8s.io/apimachinery/pkg/api/errors"
3029
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3130
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
32-
"k8s.io/client-go/kubernetes/scheme"
3331
"k8s.io/client-go/tools/record"
3432
"k8s.io/utils/ptr"
3533
ctrl "sigs.k8s.io/controller-runtime"
3634
"sigs.k8s.io/controller-runtime/pkg/client"
3735
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
3836
"sigs.k8s.io/controller-runtime/pkg/client/fake"
3937
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
40-
"sigs.k8s.io/controller-runtime/pkg/log"
4138
"sigs.k8s.io/controller-runtime/pkg/reconcile"
4239

4340
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
4441
"sigs.k8s.io/cluster-api/api/v1beta1/index"
45-
"sigs.k8s.io/cluster-api/controllers/remote"
42+
"sigs.k8s.io/cluster-api/controllers/clustercache"
4643
"sigs.k8s.io/cluster-api/internal/controllers/machine/drain"
4744
"sigs.k8s.io/cluster-api/internal/test/builder"
4845
"sigs.k8s.io/cluster-api/internal/util/ssa"
@@ -914,7 +911,7 @@ func TestReconcileRequest(t *testing.T) {
914911

915912
r := &Reconciler{
916913
Client: clientFake,
917-
ClusterCache: remote.NewTestClusterCacheTracker(logr.New(log.NullLogSink{}), clientFake, clientFake, scheme.Scheme, client.ObjectKey{Name: testCluster.Name, Namespace: testCluster.Namespace}),
914+
ClusterCache: clustercache.NewFakeClusterCache(clientFake, client.ObjectKey{Name: testCluster.Name, Namespace: testCluster.Namespace}),
918915
ssaCache: ssa.NewCache(),
919916
}
920917

@@ -1194,7 +1191,7 @@ func TestMachineConditions(t *testing.T) {
11941191
r := &Reconciler{
11951192
Client: clientFake,
11961193
recorder: record.NewFakeRecorder(10),
1197-
ClusterCache: remote.NewTestClusterCacheTracker(logr.New(log.NullLogSink{}), clientFake, clientFake, scheme.Scheme, client.ObjectKey{Name: testCluster.Name, Namespace: testCluster.Namespace}),
1194+
ClusterCache: clustercache.NewFakeClusterCache(clientFake, client.ObjectKey{Name: testCluster.Name, Namespace: testCluster.Namespace}),
11981195
ssaCache: ssa.NewCache(),
11991196
}
12001197

@@ -1650,10 +1647,9 @@ func TestDrainNode(t *testing.T) {
16501647
WithObjects(remoteObjs...).
16511648
Build()
16521649

1653-
tracker := remote.NewTestClusterCacheTracker(ctrl.Log, c, remoteClient, fakeScheme, client.ObjectKeyFromObject(testCluster))
16541650
r := &Reconciler{
16551651
Client: c,
1656-
ClusterCache: tracker,
1652+
ClusterCache: clustercache.NewFakeClusterCache(remoteClient, client.ObjectKeyFromObject(testCluster)),
16571653
drainCache: drain.NewCache(),
16581654
}
16591655

@@ -1749,11 +1745,10 @@ func TestDrainNode_withCaching(t *testing.T) {
17491745
WithObjects(remoteObjs...).
17501746
Build()
17511747

1752-
tracker := remote.NewTestClusterCacheTracker(ctrl.Log, c, remoteClient, fakeScheme, client.ObjectKeyFromObject(testCluster))
17531748
drainCache := drain.NewCache()
17541749
r := &Reconciler{
17551750
Client: c,
1756-
ClusterCache: tracker,
1751+
ClusterCache: clustercache.NewFakeClusterCache(remoteClient, client.ObjectKeyFromObject(testCluster)),
17571752
drainCache: drainCache,
17581753
}
17591754

@@ -2050,10 +2045,9 @@ func TestShouldWaitForNodeVolumes(t *testing.T) {
20502045
objs = append(objs, testCluster, tt.node)
20512046

20522047
c := fake.NewClientBuilder().WithObjects(objs...).Build()
2053-
tracker := remote.NewTestClusterCacheTracker(ctrl.Log, c, c, fakeScheme, client.ObjectKeyFromObject(testCluster))
20542048
r := &Reconciler{
20552049
Client: c,
2056-
ClusterCache: tracker,
2050+
ClusterCache: clustercache.NewFakeClusterCache(c, client.ObjectKeyFromObject(testCluster)),
20572051
}
20582052

20592053
got, err := r.shouldWaitForNodeVolumes(ctx, testCluster, tt.node.Name)
@@ -2855,11 +2849,10 @@ func TestNodeDeletion(t *testing.T) {
28552849
m.Spec.NodeDeletionTimeout = tc.deletionTimeout
28562850

28572851
fakeClient := tc.createFakeClient(node, m, cpmachine1)
2858-
tracker := remote.NewTestClusterCacheTracker(ctrl.Log, fakeClient, fakeClient, fakeScheme, client.ObjectKeyFromObject(&testCluster))
28592852

28602853
r := &Reconciler{
28612854
Client: fakeClient,
2862-
ClusterCache: tracker,
2855+
ClusterCache: clustercache.NewFakeClusterCache(fakeClient, client.ObjectKeyFromObject(&testCluster)),
28632856
recorder: record.NewFakeRecorder(10),
28642857
nodeDeletionRetryTimeout: 10 * time.Millisecond,
28652858
}
@@ -2979,11 +2972,10 @@ func TestNodeDeletionWithoutNodeRefFallback(t *testing.T) {
29792972
m.Spec.NodeDeletionTimeout = tc.deletionTimeout
29802973

29812974
fakeClient := tc.createFakeClient(node, m, cpmachine1)
2982-
tracker := remote.NewTestClusterCacheTracker(ctrl.Log, fakeClient, fakeClient, fakeScheme, client.ObjectKeyFromObject(&testCluster))
29832975

29842976
r := &Reconciler{
29852977
Client: fakeClient,
2986-
ClusterCache: tracker,
2978+
ClusterCache: clustercache.NewFakeClusterCache(fakeClient, client.ObjectKeyFromObject(&testCluster)),
29872979
recorder: record.NewFakeRecorder(10),
29882980
nodeDeletionRetryTimeout: 10 * time.Millisecond,
29892981
}

0 commit comments

Comments
 (0)