Skip to content

Commit ccce7a8

Browse files
committed
fix: fix resource assertion logic and its logging
Fix two things in the resource asserters: 1. We were doing a busy `state.Get` loop in the resource assertions. This was unnecessary to wait for a resource to be created, as we also create a watch, and the moment the resource gets created, we get notified. Changed this logic to follow the same logic with the rest of the assertions on the resource (block on `watch` events). 2. This busy wait also produced a test log every single time the resource was not found, producing an excessive number of logs. Remove this log and unify the logic with the rest of the assertions (see fix 1), so that the NotFound logs will also be aggregated/de-duplicated. Signed-off-by: Utku Ozdemir <[email protected]>
1 parent bcd981d commit ccce7a8

File tree

1 file changed

+17
-21
lines changed

1 file changed

+17
-21
lines changed

pkg/resource/rtestutils/assertions.go

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -57,20 +57,16 @@ func AssertResource[R ResourceWithRD](
5757

5858
res, err := safe.StateGet[R](ctx, st, resource.NewMetadata(namespace, rds.Type, id, resource.VersionUndefined))
5959
if err != nil {
60-
if state.IsNotFoundError(err) {
61-
asserter.NoError(err)
62-
63-
t.Logf("assertions:\n%s", &aggregator)
64-
65-
continue
60+
if !state.IsNotFoundError(err) {
61+
require.NoError(err)
6662
}
6763

68-
require.NoError(err)
64+
asserter.NoError(err)
6965
}
7066

71-
aggregator.hadErrors = false
72-
73-
assertionFunc(res, asserter)
67+
if !aggregator.hadErrors { // the resource was found, run the assertions
68+
assertionFunc(res, asserter)
69+
}
7470

7571
if !aggregator.hadErrors {
7672
return
@@ -132,7 +128,7 @@ func AssertResources[R ResourceWithRD](
132128
var (
133129
doReport bool
134130
lastReportedAggregator assertionAggregator
135-
lastReporedOk int
131+
lastReportedOk int
136132
)
137133

138134
for {
@@ -144,18 +140,18 @@ func AssertResources[R ResourceWithRD](
144140
for _, id := range ids {
145141
res, err := safe.StateGet[R](ctx, st, resource.NewMetadata(namespace, rds.Type, id, resource.VersionUndefined))
146142
if err != nil {
147-
if state.IsNotFoundError(err) {
148-
asserter.NoError(err)
149-
150-
continue
143+
if !state.IsNotFoundError(err) {
144+
require.NoError(err)
151145
}
152146

153-
require.NoError(err)
147+
asserter.NoError(err)
148+
} else {
149+
aggregator.hadErrors = false
154150
}
155151

156-
aggregator.hadErrors = false
157-
158-
assertionFunc(res, asserter)
152+
if !aggregator.hadErrors { // the resource was found, run the assertions
153+
assertionFunc(res, asserter)
154+
}
159155

160156
if !aggregator.hadErrors {
161157
ok++
@@ -168,11 +164,11 @@ func AssertResources[R ResourceWithRD](
168164

169165
if doReport {
170166
// suppress duplicate reports
171-
if !lastReportedAggregator.Equal(&aggregator) || lastReporedOk != ok {
167+
if !lastReportedAggregator.Equal(&aggregator) || lastReportedOk != ok {
172168
t.Logf("ok: %d/%d, assertions:\n%s", ok, len(ids), &aggregator)
173169
}
174170

175-
lastReporedOk = ok
171+
lastReportedOk = ok
176172
lastReportedAggregator = aggregator
177173
}
178174

0 commit comments

Comments
 (0)