Skip to content

Commit 44f49bf

Browse files
authored
test: fix TestMaxRepoSyncNameLength test flake (#1640)
The namespace was deleted before the RepoSync, causing a cleanup race condition where the Namespace garbage collector may run before the RepoSync has a chance to finalize. This can cause the RepoSync finalizer to fail because its RBAC is deleted before it can prune objects. This fixes the issue by managing the Namespace and RepoSync in the same RootSync.
1 parent fad8432 commit 44f49bf

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

e2e/testcases/basic_test.go

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
rbacv1 "k8s.io/api/rbac/v1"
2525
"k8s.io/apimachinery/pkg/types"
2626
"kpt.dev/configsync/e2e/nomostest"
27+
"kpt.dev/configsync/e2e/nomostest/gitproviders"
2728
"kpt.dev/configsync/e2e/nomostest/ntopts"
2829
"kpt.dev/configsync/e2e/nomostest/policy"
2930
"kpt.dev/configsync/e2e/nomostest/syncsource"
@@ -285,15 +286,13 @@ func TestMaxRepoSyncNameLength(t *testing.T) {
285286

286287
repo := nt.SyncSourceGitReadWriteRepository(nomostest.DefaultRootSyncID)
287288

289+
rootSyncDir := gitproviders.DefaultSyncDir
288290
// The ns-reconciler deployment label is constructed as "ns-reconciler-<ns>-<name>-<len(name)>"
289291
// Thus, the max length name + namespace for a RepoSync is 45 characters.
290292
repoSyncNS := strings.Repeat("x", 10)
291-
nt.Must(nt.KubeClient.Create(k8sobjects.NamespaceObject(repoSyncNS)))
292-
nt.T.Cleanup(func() {
293-
if err := nt.KubeClient.Delete(k8sobjects.NamespaceObject(repoSyncNS)); err != nil {
294-
nt.T.Error(err)
295-
}
296-
})
293+
nt.Must(repo.Add(fmt.Sprintf("%s/ns.yaml", rootSyncDir), k8sobjects.NamespaceObject(repoSyncNS)))
294+
nt.Must(repo.CommitAndPush("Create RepoSync namespace"))
295+
nt.Must(nt.WatchForAllSyncs())
297296
nt.Must(nomostest.CreateNamespaceSecrets(nt, repoSyncNS))
298297

299298
// Test scenario for RepoSync that exceeds the max name length by one
@@ -304,15 +303,14 @@ func TestMaxRepoSyncNameLength(t *testing.T) {
304303
repoSyncTooLong := nomostest.RepoSyncObjectV1Beta1FromOtherRootRepo(
305304
nt, repoSyncTooLongNN, nomostest.DefaultRootSyncID.Name)
306305
// Share the repository but set a separate sync dir for the new RepoSync
307-
defaultSyncDir := repoSyncTooLong.Spec.Dir
308306
syncDir := repoSyncTooLongNN.Name
309307
repoSyncTooLong.Spec.Git.Dir = syncDir
310-
nt.Must(repo.Add(fmt.Sprintf("%s/rs1.yaml", defaultSyncDir), repoSyncTooLong))
311-
nt.Must(repo.Add(fmt.Sprintf("%s/rb-1.yaml", defaultSyncDir),
308+
nt.Must(repo.Add(fmt.Sprintf("%s/rs1.yaml", rootSyncDir), repoSyncTooLong))
309+
nt.Must(repo.Add(fmt.Sprintf("%s/rb-1.yaml", rootSyncDir),
312310
nomostest.RepoSyncRoleBinding(repoSyncTooLongNN)))
313-
nt.Must(repo.Add(fmt.Sprintf("%s/cr.yaml", defaultSyncDir), nt.RepoSyncClusterRole()))
311+
nt.Must(repo.Add(fmt.Sprintf("%s/cr.yaml", rootSyncDir), nt.RepoSyncClusterRole()))
314312
nt.Must(repo.Add(
315-
fmt.Sprintf("%s/ns.yaml", syncDir),
313+
fmt.Sprintf("%s/cm.yaml", syncDir),
316314
k8sobjects.ConfigMapObject(
317315
core.Name(repoSyncTooLongNN.Name), core.Namespace(repoSyncTooLongNN.Namespace))))
318316
nt.Must(repo.CommitAndPush("create RepoSync with too long NN"))
@@ -328,11 +326,11 @@ func TestMaxRepoSyncNameLength(t *testing.T) {
328326
nt, repoSyncMaxLengthNN, nomostest.DefaultRootSyncID.Name)
329327
syncDir = repoSyncMaxLengthNN.Name
330328
repoSyncMaxLength.Spec.Git.Dir = syncDir
331-
nt.Must(repo.Add(fmt.Sprintf("%s/rs2.yaml", defaultSyncDir), repoSyncMaxLength))
332-
nt.Must(repo.Add(fmt.Sprintf("%s/rb-2.yaml", defaultSyncDir),
329+
nt.Must(repo.Add(fmt.Sprintf("%s/rs2.yaml", rootSyncDir), repoSyncMaxLength))
330+
nt.Must(repo.Add(fmt.Sprintf("%s/rb-2.yaml", rootSyncDir),
333331
nomostest.RepoSyncRoleBinding(repoSyncMaxLengthNN)))
334332
nt.Must(repo.Add(
335-
fmt.Sprintf("%s/ns.yaml", syncDir),
333+
fmt.Sprintf("%s/cm.yaml", syncDir),
336334
k8sobjects.ConfigMapObject(
337335
core.Name(repoSyncMaxLengthNN.Name), core.Namespace(repoSyncMaxLengthNN.Namespace))))
338336
nt.Must(repo.CommitAndPush("create RepoSync with max length NN"))

0 commit comments

Comments
 (0)