Skip to content

Commit fd371c7

Browse files
committed
test: refactor WatchForSync to take opt struct
This refactor adds flexibility to WatchForSync to accept options in addition to WatchOption. This is built on by a subsequent PR. The usage of ResourceGroup predicates are removed because WatchForAllSyncs watches RSyncs, so these predicates were invalid.
1 parent 575f958 commit fd371c7

File tree

2 files changed

+39
-40
lines changed

2 files changed

+39
-40
lines changed

e2e/nomostest/wait_for_sync.go

+34-24
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,12 @@ import (
3737
)
3838

3939
type watchForAllSyncsOptions struct {
40-
timeout time.Duration
4140
readyCheck bool
4241
syncRootRepos bool
4342
syncNamespaceRepos bool
4443
skipRootRepos map[string]bool
4544
skipNonRootRepos map[types.NamespacedName]bool
46-
predicates []testpredicates.Predicate
45+
watchForSyncOpts []WatchForSyncOption
4746
}
4847

4948
// WatchForAllSyncsOptions is an optional parameter for WaitForRepoSyncs.
@@ -52,14 +51,8 @@ type WatchForAllSyncsOptions func(*watchForAllSyncsOptions)
5251
// WithTimeout provides the timeout to WaitForRepoSyncs.
5352
func WithTimeout(timeout time.Duration) WatchForAllSyncsOptions {
5453
return func(options *watchForAllSyncsOptions) {
55-
options.timeout = timeout
56-
}
57-
}
58-
59-
// WithPredicates adds additional predicates for all reconcilers to WaitForRepoSyncs.
60-
func WithPredicates(predicates ...testpredicates.Predicate) WatchForAllSyncsOptions {
61-
return func(options *watchForAllSyncsOptions) {
62-
options.predicates = append(predicates, predicates...)
54+
options.watchForSyncOpts = append(options.watchForSyncOpts,
55+
WithWatchOptions(testwatcher.WatchTimeout(timeout)))
6356
}
6457
}
6558

@@ -114,13 +107,12 @@ func SkipReadyCheck() WatchForAllSyncsOptions {
114107
func (nt *NT) WatchForAllSyncs(options ...WatchForAllSyncsOptions) error {
115108
nt.T.Helper()
116109
opts := watchForAllSyncsOptions{
117-
timeout: nt.DefaultWaitTimeout,
118110
readyCheck: true,
119111
syncRootRepos: true,
120112
syncNamespaceRepos: true,
121113
skipRootRepos: make(map[string]bool),
122114
skipNonRootRepos: make(map[types.NamespacedName]bool),
123-
predicates: []testpredicates.Predicate{},
115+
watchForSyncOpts: []WatchForSyncOption{},
124116
}
125117
// Override defaults with specified options
126118
for _, option := range options {
@@ -133,13 +125,6 @@ func (nt *NT) WatchForAllSyncs(options ...WatchForAllSyncsOptions) error {
133125
}
134126
}
135127

136-
watchOptions := []testwatcher.WatchOption{
137-
testwatcher.WatchTimeout(opts.timeout),
138-
}
139-
if len(opts.predicates) > 0 {
140-
watchOptions = append(watchOptions, testwatcher.WatchPredicates(opts.predicates...))
141-
}
142-
143128
tg := taskgroup.New()
144129

145130
if opts.syncRootRepos {
@@ -151,7 +136,7 @@ func (nt *NT) WatchForAllSyncs(options ...WatchForAllSyncsOptions) error {
151136
tg.Go(func() error {
152137
return nt.WatchForSync(
153138
kinds.RootSyncV1Beta1(), idPtr.Name, idPtr.Namespace, source,
154-
watchOptions...)
139+
opts.watchForSyncOpts...)
155140
})
156141
}
157142
}
@@ -165,14 +150,29 @@ func (nt *NT) WatchForAllSyncs(options ...WatchForAllSyncsOptions) error {
165150
tg.Go(func() error {
166151
return nt.WatchForSync(
167152
kinds.RepoSyncV1Beta1(), idPtr.Name, idPtr.Namespace, source,
168-
watchOptions...)
153+
opts.watchForSyncOpts...)
169154
})
170155
}
171156
}
172157

173158
return tg.Wait()
174159
}
175160

161+
type watchForSyncOptions struct {
162+
watchOptions []testwatcher.WatchOption
163+
}
164+
165+
// WatchForSyncOption is an optional parameter for WatchForSync.
166+
type WatchForSyncOption func(*watchForSyncOptions)
167+
168+
// WithWatchOptions is an optional parameter to specify WatchOptions used for
169+
// both the RSync watch and ResourceGroup watch.
170+
func WithWatchOptions(watchOpts ...testwatcher.WatchOption) WatchForSyncOption {
171+
return func(options *watchForSyncOptions) {
172+
options.watchOptions = append(options.watchOptions, watchOpts...)
173+
}
174+
}
175+
176176
// WatchForSync watches the specified sync object until it's synced.
177177
//
178178
// - gvk (required) is the sync object GroupVersionKind
@@ -184,9 +184,16 @@ func (nt *NT) WatchForSync(
184184
gvk schema.GroupVersionKind,
185185
name, namespace string,
186186
source syncsource.SyncSource,
187-
opts ...testwatcher.WatchOption,
187+
options ...WatchForSyncOption,
188188
) error {
189189
nt.T.Helper()
190+
opts := watchForSyncOptions{
191+
watchOptions: []testwatcher.WatchOption{},
192+
}
193+
// Override defaults with specified options
194+
for _, option := range options {
195+
option(&opts)
196+
}
190197
if namespace == "" {
191198
// If namespace is empty, use the default namespace
192199
namespace = configsync.ControllerNamespace
@@ -222,9 +229,12 @@ func (nt *NT) WatchForSync(
222229
testpredicates.ErrWrongType, gvk.Kind))
223230
}
224231

225-
opts = append(opts, testwatcher.WatchPredicates(predicates...))
232+
rsyncWatchOptions := []testwatcher.WatchOption{
233+
testwatcher.WatchPredicates(predicates...),
234+
}
235+
rsyncWatchOptions = append(rsyncWatchOptions, opts.watchOptions...)
226236

227-
err = nt.Watcher.WatchObject(gvk, name, namespace, opts...)
237+
err = nt.Watcher.WatchObject(gvk, name, namespace, rsyncWatchOptions...)
228238
if err != nil {
229239
return fmt.Errorf("waiting for sync: %w", err)
230240
}

e2e/testcases/profiling_test.go

+5-16
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"kpt.dev/configsync/e2e/nomostest/gitproviders"
2525
"kpt.dev/configsync/e2e/nomostest/ntopts"
2626
nomostesting "kpt.dev/configsync/e2e/nomostest/testing"
27-
"kpt.dev/configsync/e2e/nomostest/testpredicates"
2827
"kpt.dev/configsync/pkg/api/configsync"
2928
"kpt.dev/configsync/pkg/api/configsync/v1beta1"
3029
"kpt.dev/configsync/pkg/core/k8sobjects"
@@ -68,9 +67,7 @@ func TestProfilingResourcesByObjectCount(t *testing.T) {
6867

6968
// Validate that the resources sync without the reconciler running out of
7069
// memory, getting OOMKilled, and crash looping.
71-
nt.Must(nt.WatchForAllSyncs(nomostest.WithPredicates(
72-
testpredicates.AllResourcesReconciled(nt.Scheme)),
73-
))
70+
nt.Must(nt.WatchForAllSyncs())
7471

7572
nt.T.Logf("Verify the number of Deployment objects")
7673
nt.Must(validateNumberOfObjectsEquals(nt, kinds.Deployment(), deployCount,
@@ -83,9 +80,7 @@ func TestProfilingResourcesByObjectCount(t *testing.T) {
8380

8481
// Validate that the resources sync without the reconciler running out of
8582
// memory, getting OOMKilled, and crash looping.
86-
nt.Must(nt.WatchForAllSyncs(nomostest.WithPredicates(
87-
testpredicates.AllResourcesReconciled(nt.Scheme)),
88-
))
83+
nt.Must(nt.WatchForAllSyncs())
8984
}
9085
}
9186

@@ -158,9 +153,7 @@ func TestProfilingResourcesByObjectCountWithMultiSync(t *testing.T) {
158153

159154
// Validate that the resources sync without the reconciler running out of
160155
// memory, getting OOMKilled, and crash looping.
161-
nt.Must(nt.WatchForAllSyncs(nomostest.WithPredicates(
162-
testpredicates.AllResourcesReconciled(nt.Scheme)),
163-
))
156+
nt.Must(nt.WatchForAllSyncs())
164157

165158
nt.T.Logf("Verify the number of Deployment objects")
166159
nt.Must(validateNumberOfObjectsEquals(nt, kinds.Deployment(), totalDeployCount,
@@ -179,9 +172,7 @@ func TestProfilingResourcesByObjectCountWithMultiSync(t *testing.T) {
179172

180173
// Validate that the resources sync without the reconciler running out of
181174
// memory, getting OOMKilled, and crash looping.
182-
nt.Must(nt.WatchForAllSyncs(nomostest.WithPredicates(
183-
testpredicates.AllResourcesReconciled(nt.Scheme)),
184-
))
175+
nt.Must(nt.WatchForAllSyncs())
185176

186177
nt.T.Logf("Verify all Deployments deleted")
187178
nt.Must(validateNumberOfObjectsEquals(nt, kinds.Deployment(), 0,
@@ -277,9 +268,7 @@ func TestProfilingByObjectCountAndSyncCount(t *testing.T) {
277268

278269
// Validate that the resources sync without the reconciler running out of
279270
// memory, getting OOMKilled, and crash looping.
280-
nt.Must(nt.WatchForAllSyncs(nomostest.WithPredicates(
281-
testpredicates.AllResourcesReconciled(nt.Scheme)),
282-
))
271+
nt.Must(nt.WatchForAllSyncs())
283272

284273
nt.T.Logf("Verify the number of Deployment objects")
285274
nt.Must(validateNumberOfObjectsEquals(nt, kinds.Deployment(), totalDeployCount,

0 commit comments

Comments
 (0)