test(controller): seed the DataVolume informer for controller-runtime 0.25 (#4729)

## What this PR does

`main` does not build `internal/controller` tests, so `Unit & controller
tests` is red on every open PR. #4380 added DataVolume watch tests that
set `FakeInformer.Synced` and lock `SyncedLock`. #4473 then moved to
controller-runtime v0.25.1, where `FakeInformer` keeps its sync state in
a private channel and exposes only `Synced()` and `HasSynced()`. Each PR
was green on its own base.

The tests now seed the DataVolume informer into
`FakeInformers.InformersByGVK` and call `Synced()` when they want it
synced. Seeding is needed because `FakeInformers` builds every informer
it creates itself already synced, so there is no way to get an unsynced
one from it otherwise. The unsynced cases still assert that no reader is
installed, so they would fail if the informer came up synced.

`go vet ./...` is clean and `go test ./internal/controller/` passes.

### Screenshots

Not a UI change.

### Downstream repositories

- [x] No downstream repository is affected by this change
- [ ] [cozystack/website](https://github.com/cozystack/website) -
follow-up:
- [ ]
[cozystack/terraform-provider-cozystack](https://github.com/cozystack/terraform-provider-cozystack)
- follow-up:
- [ ]
[cozystack/ansible-cozystack](https://github.com/cozystack/ansible-cozystack)
- follow-up:
- [ ] [cozystack/ccp](https://github.com/cozystack/ccp) - follow-up:
- [ ] [cozystack/talm](https://github.com/cozystack/talm) - follow-up:
- [ ] [cozystack/cozyhr](https://github.com/cozystack/cozyhr) -
follow-up:
- [ ] [cozystack/cozy-proxy](https://github.com/cozystack/cozy-proxy) -
follow-up:
- [ ]
[cozystack/cozystack-telemetry-server](https://github.com/cozystack/cozystack-telemetry-server)
- follow-up:
- [ ]
[cozystack/external-apps-example](https://github.com/cozystack/external-apps-example)
- follow-up:
- [ ] [cozystack/examples](https://github.com/cozystack/examples) -
follow-up:
- [ ] [cozystack/community](https://github.com/cozystack/community) -
follow-up:

### Release note

```release-note
NONE
```


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

* **Tests**
* Improved automated checks for workload monitoring across different
data synchronization states. These changes strengthen validation of
watch behavior without changing functionality visible to users.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
IvanHunters authored and GitHub committed 2026-10-03 01:36:33 +03:00
commit 309875ab62
1 file changed
+17 -10
@@ -21,6 +21,7 @@ import (
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
toolscache "k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/record"
"k8s.io/client-go/util/workqueue"
"k8s.io/utils/ptr"
@@ -1965,6 +1966,18 @@ func fakeDataVolumeInformer(t *testing.T, informers *informertest.FakeInformers)
return fi
}
// dataVolumeInformers seeds the DataVolume informer, because FakeInformers
// creates every informer it builds itself already synced.
func dataVolumeInformers(synced bool) (*informertest.FakeInformers, *controllertest.FakeInformer) {
fi := controllertest.NewFakeInformer()
if synced {
fi.Synced()
}
return &informertest.FakeInformers{
InformersByGVK: map[schema.GroupVersionKind]toolscache.SharedIndexInformer{dataVolumeGVK: fi},
}, fi
}
func TestTryStartDataVolumeWatch_WaitsUntilTheKindIsServed(t *testing.T) {
r := watchReconciler()
w := &startingWatcher{}
@@ -2025,9 +2038,7 @@ func TestWatchDataVolumes_StartsOnTheFirstReconcile(t *testing.T) {
func TestTryStartDataVolumeWatch_UnsyncedSourceLeavesNoReaderAndRetries(t *testing.T) {
r := watchReconciler()
w := &startingWatcher{}
informers := &informertest.FakeInformers{}
fi := fakeDataVolumeInformer(t, informers)
fi.Synced = false
informers, fi := dataVolumeInformers(false)
for attempt := 0; attempt < 3; attempt++ {
done, err := r.tryStartDataVolumeWatch(context.Background(), servedMapper(), w, informers, 50*time.Millisecond)
@@ -2036,9 +2047,7 @@ func TestTryStartDataVolumeWatch_UnsyncedSourceLeavesNoReaderAndRetries(t *testi
}
}
fi.SyncedLock.Lock()
fi.Synced = true
fi.SyncedLock.Unlock()
fi.Synced()
done, err := r.tryStartDataVolumeWatch(context.Background(), servedMapper(), w, informers, time.Second)
if err != nil || !done || readerOf(r) == nil {
@@ -2054,8 +2063,7 @@ func TestTryStartDataVolumeWatch_UnsyncedSourceLeavesNoReaderAndRetries(t *testi
// A stopping manager ends the sync wait, which says nothing about the informer.
func TestTryStartDataVolumeWatch_StoppingManagerLeavesNoReader(t *testing.T) {
r := watchReconciler()
informers := &informertest.FakeInformers{}
fakeDataVolumeInformer(t, informers).Synced = false
informers, _ := dataVolumeInformers(false)
ctx, cancel := context.WithCancel(context.Background())
cancel()
done, err := r.tryStartDataVolumeWatch(ctx, servedMapper(), &startingWatcher{}, informers, time.Second)
@@ -2074,8 +2082,7 @@ func TestTryStartDataVolumeWatch_ReconcileBeforeTheReaderIsRetried(t *testing.T)
for _, synced := range []bool{true, false} {
t.Run(fmt.Sprintf("synced=%v", synced), func(t *testing.T) {
r := watchReconciler()
informers := &informertest.FakeInformers{}
fakeDataVolumeInformer(t, informers).Synced = synced
informers, _ := dataVolumeInformers(synced)
monitor := &cozyv1alpha1.WorkloadMonitor{ObjectMeta: metav1.ObjectMeta{Name: "m", Namespace: "default"}}
var errDuringWatch error
w := &startingWatcher{}