Skip to content

Commit a345693

Browse files
authored
Autorecover from duplicate project in tests (#1053)
Signed-off-by: Jose Vazquez <[email protected]>
1 parent 007a9e7 commit a345693

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

test/e2e/actions/deploy/deploy_operator.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ import (
1414

1515
"github.com/mongodb/mongodb-atlas-kubernetes/pkg/api/v1/status"
1616
"github.com/mongodb/mongodb-atlas-kubernetes/test/e2e/actions/kube"
17+
"github.com/mongodb/mongodb-atlas-kubernetes/test/e2e/api/atlas"
1718
"github.com/mongodb/mongodb-atlas-kubernetes/test/e2e/config"
19+
"github.com/mongodb/mongodb-atlas-kubernetes/test/e2e/fixtest"
1820
"github.com/mongodb/mongodb-atlas-kubernetes/test/e2e/k8s"
1921
"github.com/mongodb/mongodb-atlas-kubernetes/test/e2e/model"
2022
"github.com/mongodb/mongodb-atlas-kubernetes/test/e2e/utils"
@@ -50,9 +52,16 @@ func CreateProject(testData *model.TestDataProvider) {
5052
testData.Project.Namespace = testData.Resources.Namespace
5153
}
5254
By(fmt.Sprintf("Deploy Project %s", testData.Project.GetName()), func() {
55+
aClient := atlas.GetClientOrFail()
5356
err := testData.K8SClient.Create(testData.Context, testData.Project)
5457
Expect(err).ShouldNot(HaveOccurred(), "Project %s was not created", testData.Project.GetName())
5558
Eventually(func(g Gomega) {
59+
// We reported Atlas creating duplicates of a project with the same name
60+
// See https://jira.mongodb.org/browse/CLOUDP-187749
61+
// this fix in our tests allows them to automatically fix this issue
62+
// and thus avoid a flaky failure when this duplicates happens
63+
g.Expect(fixtest.EnsureNoDuplicates(aClient.Client, fixtest.ZapLoggerFrom(GinkgoWriter), testData.Project.Spec.Name)).ToNot(HaveOccurred())
64+
5665
condition, _ := k8s.GetProjectStatusCondition(
5766
testData.Context,
5867
testData.K8SClient,
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package fixtest
2+
3+
import (
4+
"context"
5+
"io"
6+
"sort"
7+
8+
"go.mongodb.org/atlas/mongodbatlas"
9+
"go.uber.org/zap"
10+
"go.uber.org/zap/zapcore"
11+
)
12+
13+
// ZapLoggerFrom builds a zap.SugaredLogger from an IO Writer
14+
func ZapLoggerFrom(w io.Writer) *zap.SugaredLogger {
15+
zcore := zapcore.NewCore(
16+
zapcore.NewJSONEncoder(zap.NewDevelopmentEncoderConfig()),
17+
zapcore.Lock(zapcore.AddSync(w)),
18+
zap.NewAtomicLevel(),
19+
)
20+
return zap.New(zcore).Sugar()
21+
}
22+
23+
// EnsureNoDuplicates removes projects with same name but different ID.
24+
// Atlas sometimes creates duplicate projects, we need our tests to defend
25+
// against that to avoid flaky tests
26+
func EnsureNoDuplicates(client *mongodbatlas.Client, logger *zap.SugaredLogger, projectName string) error {
27+
found, err := listProjectsByName(client, projectName)
28+
if err != nil || len(found) <= 1 {
29+
return err
30+
}
31+
logger.Warnf("Found more than one project with name %q", projectName)
32+
keep, rest := selectProject(found)
33+
logger.Warnf("Will keep project ID %s as %s and remove the rest %v", keep.ID, projectName, ids(rest))
34+
return removeProjects(client, rest)
35+
}
36+
37+
func listProjectsByName(client *mongodbatlas.Client, projectName string) ([]*mongodbatlas.Project, error) {
38+
projects, _, err := client.Projects.GetAllProjects(
39+
context.Background(),
40+
&mongodbatlas.ListOptions{},
41+
)
42+
found := []*mongodbatlas.Project{}
43+
if err != nil {
44+
return found, err
45+
}
46+
for _, project := range projects.Results {
47+
if project.Name == projectName {
48+
found = append(found, project)
49+
}
50+
}
51+
return found, nil
52+
}
53+
54+
func selectProject(projects []*mongodbatlas.Project) (*mongodbatlas.Project, []*mongodbatlas.Project) {
55+
sort.Slice(projects, func(i, j int) bool {
56+
return projects[i].ID < projects[j].ID
57+
})
58+
return projects[0], projects[1:]
59+
}
60+
61+
func removeProjects(client *mongodbatlas.Client, projects []*mongodbatlas.Project) error {
62+
for _, project := range projects {
63+
_, err := client.Projects.Delete(context.Background(), project.ID)
64+
if err != nil {
65+
return err
66+
}
67+
}
68+
return nil
69+
}
70+
71+
func ids(projects []*mongodbatlas.Project) []string {
72+
ids := []string{}
73+
for _, prj := range projects {
74+
ids = append(ids, prj.ID)
75+
}
76+
return ids
77+
}

0 commit comments

Comments
 (0)