Fix flaky TestPolicyWebhooks (#3777)

* Fix flaky TestPolicyWebhooks

* Run test redis cleanup before running tests
This commit is contained in:
Lucas Manuel Rodriguez 2022-01-19 16:17:00 -03:00 committed by GitHub
parent 47df5e83fe
commit 77c3a8a61e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 14 deletions

View file

@ -75,20 +75,28 @@ func SetupRedis(tb testing.TB, cleanupKeyPrefix string, cluster, redir, readRepl
_, err = conn.Do("PING")
require.Nil(tb, err)
// We run a cleanup before running the tests in case a previous
// run failed to run the cleanup (e.g. Ctrl+C while running tests).
cleanup(tb, pool, cleanupKeyPrefix)
tb.Cleanup(func() {
keys, err := redis.ScanKeys(pool, cleanupKeyPrefix+"*", 1000)
require.NoError(tb, err)
for _, k := range keys {
func() {
conn := pool.Get()
defer conn.Close()
if _, err := conn.Do("DEL", k); err != nil {
require.NoError(tb, err)
}
}()
}
cleanup(tb, pool, cleanupKeyPrefix)
pool.Close()
})
return pool
}
func cleanup(tb testing.TB, pool fleet.RedisPool, cleanupKeyPrefix string) {
keys, err := redis.ScanKeys(pool, cleanupKeyPrefix+"*", 1000)
require.NoError(tb, err)
for _, k := range keys {
func() {
conn := pool.Get()
defer conn.Close()
if _, err := conn.Do("DEL", k); err != nil {
require.NoError(tb, err)
}
}()
}
}

View file

@ -2303,7 +2303,7 @@ func TestPolicyWebhooks(t *testing.T) {
}},
})
return err == nil
}, 5*time.Second, 250*time.Millisecond)
}, 1*time.Minute, 250*time.Millisecond)
require.NoError(t, err)
noPolicyResults := func(queries map[string]string) {
@ -2368,7 +2368,7 @@ func TestPolicyWebhooks(t *testing.T) {
3: {},
})
return err == nil
}, 5*time.Second, 250*time.Millisecond)
}, 1*time.Minute, 250*time.Millisecond)
require.NoError(t, err)
// Simulate webhook trigger by removing the hosts.
@ -2408,7 +2408,7 @@ func TestPolicyWebhooks(t *testing.T) {
3: {},
})
return err == nil
}, 5*time.Second, 250*time.Millisecond)
}, 1*time.Minute, 250*time.Millisecond)
require.NoError(t, err)
}