@@ -2,6 +2,7 @@ package updaterun
22
33import (
44 "context"
5+ "errors"
56 "fmt"
67 "strings"
78 "testing"
@@ -10,44 +11,44 @@ import (
1011 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1112 "k8s.io/apimachinery/pkg/runtime"
1213 "k8s.io/apimachinery/pkg/types"
13- "k8s.io/client-go/kubernetes/scheme"
1414 "k8s.io/klog/v2"
1515 "k8s.io/utils/ptr"
1616 "sigs.k8s.io/controller-runtime/pkg/client"
1717 "sigs.k8s.io/controller-runtime/pkg/client/fake"
18+ "sigs.k8s.io/controller-runtime/pkg/client/interceptor"
1819
19- v1beta1 "github.com/kubefleet-dev/kubefleet/apis/placement/v1beta1"
20+ placementv1beta1 "github.com/kubefleet-dev/kubefleet/apis/placement/v1beta1"
2021)
2122
2223func TestValidateAfterStageTask (t * testing.T ) {
2324 tests := []struct {
2425 name string
25- task []v1beta1 .StageTask
26+ task []placementv1beta1 .StageTask
2627 wantErr bool
2728 errMsg string
2829 }{
2930 {
3031 name : "valid AfterTasks" ,
31- task : []v1beta1 .StageTask {
32+ task : []placementv1beta1 .StageTask {
3233 {
33- Type : v1beta1 .StageTaskTypeApproval ,
34+ Type : placementv1beta1 .StageTaskTypeApproval ,
3435 },
3536 {
36- Type : v1beta1 .StageTaskTypeTimedWait ,
37+ Type : placementv1beta1 .StageTaskTypeTimedWait ,
3738 WaitTime : ptr .To (metav1.Duration {Duration : 5 * time .Minute }),
3839 },
3940 },
4041 wantErr : false ,
4142 },
4243 {
4344 name : "invalid AfterTasks, same type of tasks" ,
44- task : []v1beta1 .StageTask {
45+ task : []placementv1beta1 .StageTask {
4546 {
46- Type : v1beta1 .StageTaskTypeTimedWait ,
47+ Type : placementv1beta1 .StageTaskTypeTimedWait ,
4748 WaitTime : ptr .To (metav1.Duration {Duration : 1 * time .Minute }),
4849 },
4950 {
50- Type : v1beta1 .StageTaskTypeTimedWait ,
51+ Type : placementv1beta1 .StageTaskTypeTimedWait ,
5152 WaitTime : ptr .To (metav1.Duration {Duration : 5 * time .Minute }),
5253 },
5354 },
@@ -56,19 +57,19 @@ func TestValidateAfterStageTask(t *testing.T) {
5657 },
5758 {
5859 name : "invalid AfterTasks, with nil duration for TimedWait" ,
59- task : []v1beta1 .StageTask {
60+ task : []placementv1beta1 .StageTask {
6061 {
61- Type : v1beta1 .StageTaskTypeTimedWait ,
62+ Type : placementv1beta1 .StageTaskTypeTimedWait ,
6263 },
6364 },
6465 wantErr : true ,
6566 errMsg : "task 0 of type TimedWait has wait duration set to nil" ,
6667 },
6768 {
6869 name : "invalid AfterTasks, with zero duration for TimedWait" ,
69- task : []v1beta1 .StageTask {
70+ task : []placementv1beta1 .StageTask {
7071 {
71- Type : v1beta1 .StageTaskTypeTimedWait ,
72+ Type : placementv1beta1 .StageTaskTypeTimedWait ,
7273 WaitTime : ptr .To (metav1.Duration {Duration : 0 * time .Minute }),
7374 },
7475 },
@@ -105,25 +106,25 @@ func TestGetResourceSnapshotObjs(t *testing.T) {
105106 }
106107
107108 // Create test resource snapshots
108- masterResourceSnapshot := & v1beta1 .ClusterResourceSnapshot {
109+ masterResourceSnapshot := & placementv1beta1 .ClusterResourceSnapshot {
109110 ObjectMeta : metav1.ObjectMeta {
110111 Name : placementName + "-1-snapshot" ,
111112 Namespace : placementKey .Namespace ,
112113 Labels : map [string ]string {
113- v1beta1 .PlacementTrackingLabel : placementName ,
114- v1beta1 .ResourceIndexLabel : "1" ,
115- v1beta1 .IsLatestSnapshotLabel : "false" ,
116- v1beta1 .ResourceGroupHashAnnotation : "hash123" ,
114+ placementv1beta1 .PlacementTrackingLabel : placementName ,
115+ placementv1beta1 .ResourceIndexLabel : "1" ,
116+ placementv1beta1 .IsLatestSnapshotLabel : "false" ,
117+ placementv1beta1 .ResourceGroupHashAnnotation : "hash123" ,
117118 },
118119 Annotations : map [string ]string {
119- v1beta1 .ResourceGroupHashAnnotation : "hash123" ,
120+ placementv1beta1 .ResourceGroupHashAnnotation : "hash123" ,
120121 },
121122 },
122123 }
123124
124125 tests := []struct {
125126 name string
126- updateRunSpec * v1beta1 .UpdateRunSpec
127+ updateRunSpec * placementv1beta1 .UpdateRunSpec
127128 resourceSnapshots []runtime.Object
128129 wantSnapshotCount int
129130 wantErr bool
@@ -132,7 +133,7 @@ func TestGetResourceSnapshotObjs(t *testing.T) {
132133 // negative cases only
133134 {
134135 name : "invalid resource snapshot index - non-numeric" ,
135- updateRunSpec : & v1beta1 .UpdateRunSpec {
136+ updateRunSpec : & placementv1beta1 .UpdateRunSpec {
136137 ResourceSnapshotIndex : "invalid" ,
137138 },
138139 resourceSnapshots : []runtime.Object {},
@@ -142,7 +143,7 @@ func TestGetResourceSnapshotObjs(t *testing.T) {
142143 },
143144 {
144145 name : "invalid resource snapshot index - negative" ,
145- updateRunSpec : & v1beta1 .UpdateRunSpec {
146+ updateRunSpec : & placementv1beta1 .UpdateRunSpec {
146147 ResourceSnapshotIndex : "-1" ,
147148 },
148149 resourceSnapshots : []runtime.Object {},
@@ -152,7 +153,7 @@ func TestGetResourceSnapshotObjs(t *testing.T) {
152153 },
153154 {
154155 name : "no resource snapshots found for specific index" ,
155- updateRunSpec : & v1beta1 .UpdateRunSpec {
156+ updateRunSpec : & placementv1beta1 .UpdateRunSpec {
156157 ResourceSnapshotIndex : "999" ,
157158 },
158159 resourceSnapshots : []runtime.Object {
@@ -164,7 +165,7 @@ func TestGetResourceSnapshotObjs(t *testing.T) {
164165 },
165166 {
166167 name : "no latest resource snapshots found" ,
167- updateRunSpec : & v1beta1 .UpdateRunSpec {
168+ updateRunSpec : & placementv1beta1 .UpdateRunSpec {
168169 ResourceSnapshotIndex : "" ,
169170 },
170171 resourceSnapshots : []runtime.Object {}, // no snapshots
@@ -177,12 +178,8 @@ func TestGetResourceSnapshotObjs(t *testing.T) {
177178 for _ , tt := range tests {
178179 t .Run (tt .name , func (t * testing.T ) {
179180 // Create a fake client with the test objects
180- s := runtime .NewScheme ()
181- _ = v1beta1 .AddToScheme (s )
182- _ = scheme .AddToScheme (s )
183-
184181 fakeClient := fake .NewClientBuilder ().
185- WithScheme (s ).
182+ WithScheme (serviceScheme ( t ) ).
186183 WithRuntimeObjects (tt .resourceSnapshots ... ).
187184 Build ()
188185
@@ -222,31 +219,22 @@ func TestGetResourceSnapshotObjs(t *testing.T) {
222219 }
223220}
224221
225- // fakeListErrorClient wraps a client and always returns an error on List.
226- type fakeListErrorClient struct {
227- client.Client
228- }
229-
230- func (f * fakeListErrorClient ) List (ctx context.Context , list client.ObjectList , opts ... client.ListOption ) error {
231- return fmt .Errorf ("simulated list error" )
232- }
233-
234222func TestGetResourceSnapshotObjs_ListError (t * testing.T ) {
235223 tests := []struct {
236224 name string
237- spec * v1beta1 .UpdateRunSpec
225+ spec * placementv1beta1 .UpdateRunSpec
238226 wantErrMsg string
239227 }{
240228 {
241229 name : "list error simulation with resource index" ,
242- spec : & v1beta1 .UpdateRunSpec {
230+ spec : & placementv1beta1 .UpdateRunSpec {
243231 ResourceSnapshotIndex : "1" ,
244232 },
245233 wantErrMsg : "Failed to list the resourceSnapshots associated with the placement for the given index" ,
246234 },
247235 {
248236 name : "list error simulation without resource index" ,
249- spec : & v1beta1 .UpdateRunSpec {
237+ spec : & placementv1beta1 .UpdateRunSpec {
250238 ResourceSnapshotIndex : "" ,
251239 },
252240 wantErrMsg : "Failed to list the resourceSnapshots associated with the placement" ,
@@ -263,17 +251,29 @@ func TestGetResourceSnapshotObjs_ListError(t *testing.T) {
263251 Namespace : "test-namespace" ,
264252 }
265253
266- s := runtime .NewScheme ()
267- _ = v1beta1 .AddToScheme (s )
268- _ = scheme .AddToScheme (s )
269-
270- fakeClient := & fakeListErrorClient {Client : fake .NewClientBuilder ().WithScheme (s ).Build ()}
254+ // Use interceptor to make Get calls fail.
255+ fakeClient := interceptor .NewClient (
256+ fake .NewClientBuilder ().WithScheme (serviceScheme (t )).Build (),
257+ interceptor.Funcs {
258+ List : func (ctx context.Context , client client.WithWatch , list client.ObjectList , opts ... client.ListOption ) error {
259+ return errors .New (tt .wantErrMsg )
260+ },
261+ },
262+ )
271263 r := & Reconciler {Client : fakeClient }
272264
273265 _ , err := r .getResourceSnapshotObjs (ctx , tt .spec , placementName , placementKey , updateRunRef )
274- if err == nil || ! strings .Contains (err .Error (), "simulated list error" ) {
266+ if err == nil || ! strings .Contains (err .Error (), tt . wantErrMsg ) {
275267 t .Errorf ("expected simulated list error, got: %v" , err )
276268 }
277269 })
278270 }
279271}
272+
273+ func serviceScheme (t * testing.T ) * runtime.Scheme {
274+ scheme := runtime .NewScheme ()
275+ if err := placementv1beta1 .AddToScheme (scheme ); err != nil {
276+ t .Fatalf ("Failed to add placement v1beta1 scheme: %v" , err )
277+ }
278+ return scheme
279+ }
0 commit comments