-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CLOUDP-178752: Deletion protection for Atlas Teams CR (#1108)
- Loading branch information
Showing
6 changed files
with
264 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
package atlasproject | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"testing" | ||
|
||
"github.com/mongodb/mongodb-atlas-kubernetes/pkg/api/v1/status" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"go.mongodb.org/atlas/mongodbatlas" | ||
|
||
"github.com/mongodb/mongodb-atlas-kubernetes/pkg/controller/atlas" | ||
|
||
v1 "github.com/mongodb/mongodb-atlas-kubernetes/pkg/api/v1" | ||
) | ||
|
||
type teamsClient struct { | ||
GetFunc func() (*mongodbatlas.Team, *mongodbatlas.Response, error) | ||
} | ||
|
||
func (c *teamsClient) List(_ context.Context, _ string, _ *mongodbatlas.ListOptions) ([]mongodbatlas.Team, *mongodbatlas.Response, error) { | ||
return nil, nil, nil | ||
} | ||
|
||
func (c *teamsClient) Get(_ context.Context, _ string, _ string) (*mongodbatlas.Team, *mongodbatlas.Response, error) { | ||
return c.GetFunc() | ||
} | ||
|
||
func (c *teamsClient) GetOneTeamByName(_ context.Context, _ string, _ string) (*mongodbatlas.Team, *mongodbatlas.Response, error) { | ||
return nil, nil, nil | ||
} | ||
|
||
func (c *teamsClient) GetTeamUsersAssigned(_ context.Context, _ string, _ string) ([]mongodbatlas.AtlasUser, *mongodbatlas.Response, error) { | ||
return nil, nil, nil | ||
} | ||
|
||
func (c *teamsClient) Create(_ context.Context, _ string, _ *mongodbatlas.Team) (*mongodbatlas.Team, *mongodbatlas.Response, error) { | ||
return nil, nil, nil | ||
} | ||
|
||
func (c *teamsClient) Rename(_ context.Context, _ string, _ string, _ string) (*mongodbatlas.Team, *mongodbatlas.Response, error) { | ||
return nil, nil, nil | ||
} | ||
|
||
func (c *teamsClient) UpdateTeamRoles(_ context.Context, _ string, _ string, _ *mongodbatlas.TeamUpdateRoles) ([]mongodbatlas.TeamRoles, *mongodbatlas.Response, error) { | ||
return nil, nil, nil | ||
} | ||
|
||
func (c *teamsClient) AddUsersToTeam(_ context.Context, _ string, _ string, _ []string) ([]mongodbatlas.AtlasUser, *mongodbatlas.Response, error) { | ||
return nil, nil, nil | ||
} | ||
|
||
func (c *teamsClient) RemoveUserToTeam(_ context.Context, _ string, _ string, _ string) (*mongodbatlas.Response, error) { | ||
return nil, nil | ||
} | ||
|
||
func (c *teamsClient) RemoveTeamFromOrganization(_ context.Context, _ string, _ string) (*mongodbatlas.Response, error) { | ||
return nil, nil | ||
} | ||
|
||
func (c *teamsClient) RemoveTeamFromProject(_ context.Context, _ string, _ string) (*mongodbatlas.Response, error) { | ||
return nil, nil | ||
} | ||
|
||
func TestTeamManagedByAtlas(t *testing.T) { | ||
t.Run("should return error when passing wrong resource", func(t *testing.T) { | ||
checker := teamsManagedByAtlas(context.TODO(), mongodbatlas.Client{}, "orgID") | ||
result, err := checker(&v1.AtlasProject{}) | ||
assert.EqualError(t, err, "failed to match resource type as AtlasTeams") | ||
assert.False(t, result) | ||
}) | ||
|
||
t.Run("should return false when resource has no Atlas Team ID", func(t *testing.T) { | ||
checker := teamsManagedByAtlas(context.TODO(), mongodbatlas.Client{}, "orgID") | ||
result, err := checker(&v1.AtlasTeam{}) | ||
assert.NoError(t, err) | ||
assert.False(t, result) | ||
}) | ||
|
||
t.Run("should return false when resource was not found in Atlas", func(t *testing.T) { | ||
atlasClient := mongodbatlas.Client{ | ||
Teams: &teamsClient{ | ||
GetFunc: func() (*mongodbatlas.Team, *mongodbatlas.Response, error) { | ||
return nil, &mongodbatlas.Response{}, &mongodbatlas.ErrorResponse{ErrorCode: atlas.ResourceNotFound} | ||
}, | ||
}, | ||
} | ||
team := &v1.AtlasTeam{ | ||
Status: status.TeamStatus{ | ||
ID: "team-id-1", | ||
}, | ||
} | ||
checker := teamsManagedByAtlas(context.TODO(), atlasClient, "orgID") | ||
result, err := checker(team) | ||
assert.NoError(t, err) | ||
assert.False(t, result) | ||
}) | ||
|
||
t.Run("should return error when failed to fetch the team from Atlas", func(t *testing.T) { | ||
atlasClient := mongodbatlas.Client{ | ||
Teams: &teamsClient{ | ||
GetFunc: func() (*mongodbatlas.Team, *mongodbatlas.Response, error) { | ||
return nil, &mongodbatlas.Response{}, errors.New("unavailable") | ||
}, | ||
}, | ||
} | ||
team := &v1.AtlasTeam{ | ||
Status: status.TeamStatus{ | ||
ID: "team-id-1", | ||
}, | ||
} | ||
checker := teamsManagedByAtlas(context.TODO(), atlasClient, "orgID") | ||
result, err := checker(team) | ||
assert.EqualError(t, err, "unavailable") | ||
assert.False(t, result) | ||
}) | ||
|
||
t.Run("should return false when resource are equal", func(t *testing.T) { | ||
atlasClient := mongodbatlas.Client{ | ||
Teams: &teamsClient{ | ||
GetFunc: func() (*mongodbatlas.Team, *mongodbatlas.Response, error) { | ||
return &mongodbatlas.Team{ | ||
ID: "team-id-1", | ||
Name: "My Team", | ||
Usernames: []string{"[email protected]", "[email protected]"}, | ||
}, &mongodbatlas.Response{}, nil | ||
}, | ||
}, | ||
} | ||
team := &v1.AtlasTeam{ | ||
Spec: v1.TeamSpec{ | ||
Name: "My Team", | ||
Usernames: []v1.TeamUser{"[email protected]", "[email protected]"}, | ||
}, | ||
Status: status.TeamStatus{ | ||
ID: "team-id-1", | ||
}, | ||
} | ||
checker := teamsManagedByAtlas(context.TODO(), atlasClient, "orgID-1") | ||
result, err := checker(team) | ||
assert.NoError(t, err) | ||
assert.False(t, result) | ||
}) | ||
|
||
t.Run("should return true when resource are different", func(t *testing.T) { | ||
atlasClient := mongodbatlas.Client{ | ||
Teams: &teamsClient{ | ||
GetFunc: func() (*mongodbatlas.Team, *mongodbatlas.Response, error) { | ||
return &mongodbatlas.Team{ | ||
ID: "team-id-1", | ||
Name: "My Team", | ||
Usernames: []string{"[email protected]", "[email protected]"}, | ||
}, &mongodbatlas.Response{}, nil | ||
}, | ||
}, | ||
} | ||
team := &v1.AtlasTeam{ | ||
Spec: v1.TeamSpec{ | ||
Name: "My Team", | ||
Usernames: []v1.TeamUser{"[email protected]"}, | ||
}, | ||
Status: status.TeamStatus{ | ||
ID: "team-id-1", | ||
}, | ||
} | ||
checker := teamsManagedByAtlas(context.TODO(), atlasClient, "orgID-1") | ||
result, err := checker(team) | ||
assert.NoError(t, err) | ||
assert.True(t, result) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -302,20 +302,17 @@ var _ = Describe("Project Deletion Protection", Label("project", "deletion-prote | |
Expect(err).ToNot(HaveOccurred()) | ||
}) | ||
|
||
By("Creating a project to be managed by the operator", func() { | ||
By("Creating a project and team to be managed by the operator", func() { | ||
akoTeam := &mdbv1.AtlasTeam{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: fmt.Sprintf("%s-team", projectName), | ||
Namespace: testData.Resources.Namespace, | ||
}, | ||
Spec: mdbv1.TeamSpec{ | ||
Name: fmt.Sprintf("%s-team", projectName), | ||
Usernames: make([]mdbv1.TeamUser, 0, len(usernames)), | ||
Usernames: []mdbv1.TeamUser{"[email protected]"}, | ||
}, | ||
} | ||
for _, username := range usernames { | ||
akoTeam.Spec.Usernames = append(akoTeam.Spec.Usernames, mdbv1.TeamUser(username)) | ||
} | ||
testData.Teams = []*mdbv1.AtlasTeam{akoTeam} | ||
Expect(testData.K8SClient.Create(ctx, testData.Teams[0])) | ||
|
||
|
@@ -825,6 +822,20 @@ var _ = Describe("Project Deletion Protection", Label("project", "deletion-prote | |
}).WithTimeout(time.Minute * 5).WithPolling(time.Second * 20).Should(Succeed()) | ||
}) | ||
|
||
By("Team is ready after configured properly", func() { | ||
Expect(testData.K8SClient.Get(context.TODO(), client.ObjectKeyFromObject(testData.Teams[0]), testData.Teams[0])).To(Succeed()) | ||
testData.Teams[0].Spec.Usernames = make([]mdbv1.TeamUser, 0, len(usernames)) | ||
for _, username := range usernames { | ||
testData.Teams[0].Spec.Usernames = append(testData.Teams[0].Spec.Usernames, mdbv1.TeamUser(username)) | ||
} | ||
Expect(testData.K8SClient.Update(context.TODO(), testData.Teams[0])).To(Succeed()) | ||
|
||
Eventually(func(g Gomega) { | ||
g.Expect(testData.K8SClient.Get(context.TODO(), client.ObjectKeyFromObject(testData.Teams[0]), testData.Teams[0])).To(Succeed()) | ||
g.Expect(testData.Teams[0].Status.Conditions).To(ContainElements(testutil.MatchCondition(status.TrueCondition(status.ReadyType)))) | ||
}).WithTimeout(time.Minute * 1).WithPolling(time.Second * 20).Should(Succeed()) | ||
}) | ||
|
||
By("Assigned Teams is ready after configured properly", func() { | ||
Expect(testData.K8SClient.Get(context.TODO(), client.ObjectKeyFromObject(testData.Project), testData.Project)).To(Succeed()) | ||
testData.Project.Spec.Teams[0].Roles[0] = "GROUP_OWNER" | ||
|