@@ -20,8 +20,10 @@ import (
2020 "reflect"
2121 "strings"
2222 "testing"
23+ "time"
2324
2425 apps "github.com/googlecloudrobotics/core/src/go/pkg/apis/apps/v1alpha1"
26+ crcfake "github.com/googlecloudrobotics/core/src/go/pkg/client/versioned/fake"
2527 "github.com/pkg/errors"
2628 corev1 "k8s.io/api/core/v1"
2729 k8serrors "k8s.io/apimachinery/pkg/api/errors"
@@ -59,6 +61,22 @@ func (d *fakeCachedDiscoveryClient) ServerGroupsAndResources() ([]*metav1.APIGro
5961 }, nil
6062}
6163
64+ // Helpers to override time when testing.
65+ var (
66+ fakeEndTime = metav1 .Date (2025 , 01 , 01 , 0 , 0 , 0 , 0 , time .UTC )
67+ )
68+
69+ func fakeTime (t * testing.T , fakeTime metav1.Time ) {
70+ t .Helper ()
71+ oldFunc := metav1Now
72+ t .Cleanup (func () {
73+ metav1Now = oldFunc
74+ })
75+ metav1Now = func () metav1.Time {
76+ return fakeTime
77+ }
78+ }
79+
6280type fixture struct {
6381 * testing.T
6482 fake * k8stest.Fake
@@ -78,8 +96,9 @@ func (f *fixture) newSynk() *Synk {
7896 scheme .AddToScheme (sc )
7997 apps .AddToScheme (sc ) // For tests with CRDs.
8098 var (
81- client = dynamicfake .NewSimpleDynamicClient (sc , f .objects ... )
82- s = New (client , & fakeCachedDiscoveryClient {})
99+ client = dynamicfake .NewSimpleDynamicClient (sc , f .objects ... )
100+ rsClient = crcfake .NewSimpleClientset ()
101+ s = New (client , rsClient , & fakeCachedDiscoveryClient {})
83102 )
84103 s .mapper = testrestmapper .TestOnlyStaticRESTMapper (sc )
85104 s .resetMapper = func () {}
@@ -167,11 +186,11 @@ func TestSynk_initialize(t *testing.T) {
167186 if err != nil {
168187 t .Fatal (err )
169188 }
170- got , err := s .client . Resource ( resourceSetGVR ).Get (ctx , "test.v1" , metav1.GetOptions {})
189+ got , err := s .rsClient . AppsV1alpha1 (). ResourceSets ( ).Get (ctx , "test.v1" , metav1.GetOptions {})
171190 if err != nil {
172191 t .Fatal (err )
173192 }
174- var want unstructured. Unstructured
193+ var want apps. ResourceSet
175194 unmarshalYAML (t , & want , `
176195apiVersion: apps.cloudrobotics.com/v1alpha1
177196kind: ResourceSet
@@ -200,13 +219,11 @@ status:
200219 if want .GetName () != got .GetName () {
201220 t .Errorf ("expected name %q but got %q" , want .GetName (), got .GetName ())
202221 }
203- wantPhase , _ , _ := unstructured .NestedString (want .Object , "status" , "phase" )
204- gotPhase , _ , _ := unstructured .NestedString (got .Object , "status" , "phase" )
205- if wantPhase != gotPhase {
206- t .Errorf ("expected status phase %q but got %q" , wantPhase , gotPhase )
222+ if want .Status .Phase != got .Status .Phase {
223+ t .Errorf ("expected status phase %q but got %q" , want .Status .Phase , got .Status .Phase )
207224 }
208- if ! reflect .DeepEqual (want .Object [ "spec" ] , got .Object [ "spec" ] ) {
209- t .Errorf ("expected spec\n %v\n but got\n %v" , want .Object [ "spec" ] , got .Object [ "spec" ] )
225+ if ! reflect .DeepEqual (want .Spec , got .Spec ) {
226+ t .Errorf ("expected spec\n %v\n but got\n %v" , want .Spec , got .Spec )
210227 }
211228}
212229
@@ -240,15 +257,16 @@ func TestSynk_updateResourceSetStatus(t *testing.T) {
240257 action : apps .ResourceActionCreate ,
241258 },
242259 }
260+ fakeTime (t , fakeEndTime )
243261 err := s .updateResourceSetStatus (ctx , rs , results )
244262 if err != nil {
245263 t .Fatal (err )
246264 }
247- got , err := s .client . Resource ( resourceSetGVR ).Get (ctx , "set1" , metav1.GetOptions {})
265+ got , err := s .rsClient . AppsV1alpha1 (). ResourceSets ( ).Get (ctx , "set1" , metav1.GetOptions {})
248266 if err != nil {
249267 t .Fatal (err )
250268 }
251- var want unstructured. Unstructured
269+ var want apps. ResourceSet
252270 unmarshalYAML (t , & want , `
253271apiVersion: apps.cloudrobotics.com/v1alpha1
254272kind: ResourceSet
@@ -283,15 +301,10 @@ status:
283301 name: deploy1
284302 action: Create
285303` )
286- if v , _ , _ := unstructured .NestedString (got .Object , "status" , "finishedAt" ); v == "" {
287- t .Errorf ("finishedAt timestamp was not set" )
288- }
289- // Remove unknown timestamps before running DeepEqual.
290- unstructured .RemoveNestedField (got .Object , "status" , "startedAt" )
291- unstructured .RemoveNestedField (got .Object , "status" , "finishedAt" )
304+ want .Status .FinishedAt = fakeEndTime
292305
293- if ! reflect .DeepEqual (got .Object [ "status" ] , want .Object [ "status" ] ) {
294- t .Errorf ("expected status:\n %q\n but got:\n %q" , want .Object [ "status" ] , got .Object [ "status" ] )
306+ if ! reflect .DeepEqual (got .Status , want .Status ) {
307+ t .Errorf ("expected status:\n %q\n but got:\n %q" , want .Status , got .Status )
295308 }
296309}
297310
@@ -502,11 +515,11 @@ func TestSynk_skipsTestResources(t *testing.T) {
502515 if err != nil {
503516 t .Fatal (err )
504517 }
505- got , err := s .client . Resource ( resourceSetGVR ).Get (ctx , "test.v1" , metav1.GetOptions {})
518+ got , err := s .rsClient . AppsV1alpha1 (). ResourceSets ( ).Get (ctx , "test.v1" , metav1.GetOptions {})
506519 if err != nil {
507520 t .Fatal (err )
508521 }
509- var want unstructured. Unstructured
522+ var want apps. ResourceSet
510523 unmarshalYAML (t , & want , `
511524apiVersion: apps.cloudrobotics.com/v1alpha1
512525kind: ResourceSet
@@ -524,33 +537,41 @@ spec:
524537status:
525538 phase: Pending
526539` )
527- if ! reflect .DeepEqual (want .Object [ "spec" ] , got .Object [ "spec" ] ) {
528- t .Errorf ("expected spec\n %v\n but got\n %v" , want .Object [ "spec" ] , got .Object [ "spec" ] )
540+ if ! reflect .DeepEqual (want .Spec , got .Spec ) {
541+ t .Errorf ("expected spec\n %v\n but got\n %v" , want .Spec , got .Spec )
529542 }
530543}
531544
532545func TestSynk_deleteResourceSets (t * testing.T ) {
546+ initialNames := []string {"test.v2" , "bad_name" , "other.v3" , "test.v4" , "test.v7" , "test.v8" }
547+ wantNames := []string {"bad_name" , "other.v3" , "test.v7" , "test.v8" }
548+ wantDeletions := []string {"test.v2" , "test.v4" }
549+
533550 ctx := context .Background ()
534551 f := newFixture (t )
535- f .addObjects (
536- newUnstructured ("apps.cloudrobotics.com/v1alpha1" , "ResourceSet" , "" , "test.v2" ),
537- newUnstructured ("apps.cloudrobotics.com/v1alpha1" , "ResourceSet" , "" , "bad_name" ),
538- newUnstructured ("apps.cloudrobotics.com/v1alpha1" , "ResourceSet" , "" , "other.v3" ),
539- newUnstructured ("apps.cloudrobotics.com/v1alpha1" , "ResourceSet" , "" , "test.v4" ),
540- newUnstructured ("apps.cloudrobotics.com/v1alpha1" , "ResourceSet" , "" , "test.v7" ),
541- newUnstructured ("apps.cloudrobotics.com/v1alpha1" , "ResourceSet" , "" , "test.v8" ),
542- )
543552 synk := f .newSynk ()
553+ rsClient := synk .rsClient .AppsV1alpha1 ().ResourceSets ()
554+
555+ for _ , name := range initialNames {
556+ rsClient .Create (ctx , & apps.ResourceSet {
557+ ObjectMeta : metav1.ObjectMeta {Name : name },
558+ }, metav1.CreateOptions {})
559+ }
544560
545561 err := synk .deleteResourceSets (ctx , "test" , 7 )
546562 if err != nil {
547563 t .Fatal (err )
548564 }
549- f .expectActions (
550- k8stest .NewRootDeleteAction (resourceSetGVR , "test.v2" ),
551- k8stest .NewRootDeleteAction (resourceSetGVR , "test.v4" ),
552- )
553- f .verifyWriteActions ()
565+ for _ , name := range wantNames {
566+ if _ , err := rsClient .Get (ctx , name , metav1.GetOptions {}); err != nil {
567+ t .Errorf ("unexpected error getting ResourceSet %q: err=%v; want nil error" , err , err )
568+ }
569+ }
570+ for _ , name := range wantDeletions {
571+ if _ , err := rsClient .Get (ctx , name , metav1.GetOptions {}); ! k8serrors .IsNotFound (err ) {
572+ t .Errorf ("unexpected error getting ResourceSet %q: err=%v; want not found" , name , err )
573+ }
574+ }
554575}
555576
556577func TestSynk_populateNamespaces (t * testing.T ) {
0 commit comments