Deflake TestKubeServerWatcher and TestNodeWatcher tests (#30671)

* Deflake `TestKubeServerWatcher` test

This PR fixes `TestKubeServerWatcher`.

The flakiness arrised from the fact that the test didn't wait till the watcher
is initialized. Since the watch loop runs in a separate goroutine it's
not imediatly scheduled. If the watcher is stale - happens on stale
conditions or if it wasn't properly initialized because `watchLoop`
didn't run yet - `GetKubernetesServers` goes directly to the backend
trying to fetch the most recent data and replaces the watcher state.
If the `watchLoop` starts before the delete operation but only replaces
the watcher state after the delete operation finishes, it is possible to
have the same exact sate before the deletion.
This PR forces the test to wait for init so the out-of order update
won't happen.

Fixes #30566

Signed-off-by: Tiago Silva <tiago.silva@goteleport.com>

* fix TestNodeWatcher

---------

Signed-off-by: Tiago Silva <tiago.silva@goteleport.com>
This commit is contained in:
Tiago Silva 2023-08-18 15:17:30 +01:00 committed by GitHub
parent 430d67ac80
commit 80480bfff3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -958,7 +958,7 @@ func TestNodeWatcher(t *testing.T) {
})
require.NoError(t, err)
t.Cleanup(w.Close)
require.NoError(t, w.WaitInitialization())
// Add some node servers.
nodes := make([]types.Server, 0, 5)
for i := 0; i < 5; i++ {
@ -1028,7 +1028,7 @@ func TestKubeServerWatcher(t *testing.T) {
})
require.NoError(t, err)
t.Cleanup(w.Close)
require.NoError(t, w.WaitInitialization())
newKubeServer := func(t *testing.T, name, addr, hostID string) types.KubeServer {
kube, err := types.NewKubernetesClusterV3(
types.Metadata{