Skip to content

Commit c51a860

Browse files
EspenAlbertlantoli
andauthored
refactor: Supports cleaning resources for static projects with test-acc-tf-p-keep prefix (#3269)
* refactor: support empty prefix for projects that shouldn't be deleted but have resources removed * refactor: Remove resources from projects with `test-acc-tf-p-keep` (only found a few stream-instances for now) * Update internal/testutil/clean/org_clean_test.go Co-authored-by: Leo Antoli <[email protected]> * refactor: Use a bool for skipProjectDelete * refactor: Rename projectsToDelete to projectsToClean for clarity in project resource management --------- Co-authored-by: Leo Antoli <[email protected]>
1 parent b6d1166 commit c51a860

File tree

1 file changed

+25
-14
lines changed

1 file changed

+25
-14
lines changed

internal/testutil/clean/org_clean_test.go

+25-14
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ var (
3434
"cfn-test-bot-",
3535
"test-acc-tf-p-",
3636
}
37+
// keptPrefixes has the prefix of the projects that we want to delete their resources but keep the projects themselves.
38+
// Useful when a feature flag or cloud provider is configured outside of the test
3739
keptPrefixes = []string{
3840
"test-acc-tf-p-keep",
3941
}
@@ -82,30 +84,36 @@ func TestCleanProjectAndClusters(t *testing.T) {
8284
projects := readAllProjects(t.Context(), t, client)
8385
projectsBefore := len(projects)
8486
t.Logf("found %d projects (DRY_RUN=%t)", projectsBefore, dryRun)
85-
projectsToDelete := map[string]string{}
87+
projectsToClean := map[string]string{}
8688
projectInfos := []string{}
8789
for _, p := range projects {
8890
skipReason := projectSkipReason(&p, skipProjectsAfter, onlyZeroClusters)
8991
projectName := p.GetName()
92+
projectID := p.GetId()
9093
if skipReason != "" {
91-
t.Logf("skip project %s, reason: %s", projectName, skipReason)
94+
t.Logf("skip project %s (%s), reason: %s", projectName, projectID, skipReason)
9295
continue
9396
}
9497
projectInfos = append(projectInfos, fmt.Sprintf("Project created at %s name %s (%s)", p.GetCreated().Format(time.RFC3339), projectName, p.GetId()))
95-
projectID := p.GetId()
96-
projectsToDelete[projectName] = projectID
98+
projectsToClean[projectName] = projectID
9799
}
98-
t.Logf("will try to delete %d projects:", len(projectsToDelete))
100+
t.Logf("deleting project resources and optionally delete projects for %d projects", len(projectsToClean))
99101
slices.Sort(projectInfos)
100102
t.Log(strings.Join(projectInfos, "\n"))
101103
var deleteErrors int
102-
for name, projectID := range projectsToDelete {
104+
var emptyProjectCount int
105+
for name, projectID := range projectsToClean {
103106
t.Run(name, func(t *testing.T) {
104107
t.Parallel()
105108
changes := removeProjectResources(t.Context(), t, dryRun, client, projectID)
106109
if changes != "" {
107110
t.Logf("project %s %s", name, changes)
108111
}
112+
if skipProjectDelete(name) {
113+
t.Logf("keep project empty, but no delete %s (%s)", name, projectID)
114+
emptyProjectCount++
115+
return
116+
}
109117
var err error
110118
for i := range runRetries {
111119
attempt := i + 1
@@ -131,9 +139,8 @@ func TestCleanProjectAndClusters(t *testing.T) {
131139
})
132140
}
133141
t.Cleanup(func() {
134-
//nolint:usetesting // reason: using context.Background() here intentionally because t.Context() is canceled at cleanup
135-
projectsAfter := readAllProjects(context.Background(), t, client)
136-
t.Logf("SUMMARY\nProjects changed from %d to %d\ndelete_errors=%d\nDRY_RUN=%t", projectsBefore, len(projectsAfter), deleteErrors, dryRun)
142+
projectsAfter := readAllProjects(context.Background(), t, client) //nolint:usetesting // reason: using context.Background() here intentionally because t.Context() is canceled at cleanup
143+
t.Logf("SUMMARY\nProjects changed from %d to %d\ndelete_errors=%d\nempty_project_count=%d\nDRY_RUN=%t", projectsBefore, len(projectsAfter), deleteErrors, emptyProjectCount, dryRun)
137144
})
138145
}
139146

@@ -204,11 +211,6 @@ func removeProjectResources(ctx context.Context, t *testing.T, dryRun bool, clie
204211
}
205212

206213
func projectSkipReason(p *admin.Group, skipProjectsAfter time.Time, onlyEmpty bool) string {
207-
for _, blessedPrefix := range keptPrefixes {
208-
if strings.HasPrefix(p.GetName(), blessedPrefix) {
209-
return "blessed prefix: " + blessedPrefix
210-
}
211-
}
212214
usesBotPrefix := false
213215
for _, botPrefix := range botProjectPrefixes {
214216
if strings.HasPrefix(p.GetName(), botPrefix) {
@@ -228,6 +230,15 @@ func projectSkipReason(p *admin.Group, skipProjectsAfter time.Time, onlyEmpty bo
228230
return ""
229231
}
230232

233+
func skipProjectDelete(name string) bool {
234+
for _, keepPrefix := range keptPrefixes {
235+
if strings.HasPrefix(name, keepPrefix) {
236+
return true
237+
}
238+
}
239+
return false
240+
}
241+
231242
func removeClusters(ctx context.Context, t *testing.T, dryRun bool, client *admin.APIClient, projectID string) int {
232243
t.Helper()
233244
clusters, _, err := client.ClustersApi.ListClusters(ctx, projectID).ItemsPerPage(itemsPerPage).Execute()

0 commit comments

Comments
 (0)