Skip to content

Commit

Permalink
fix unit tests expecting wrong context
Browse files Browse the repository at this point in the history
  • Loading branch information
s-urbaniak committed Jan 8, 2025
1 parent fbd1adf commit b4bf714
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ func TestRegularClusterReconciliation(t *testing.T) {
},
SdkClientFunc: func(secretRef *client.ObjectKey, log *zap.SugaredLogger) (*admin.APIClient, string, error) {
clusterAPI := mockadmin.NewClustersApi(t)
clusterAPI.EXPECT().GetCluster(context.Background(), project.ID(), d.GetDeploymentName()).
clusterAPI.EXPECT().GetCluster(mock.Anything, project.ID(), d.GetDeploymentName()).
Return(admin.GetClusterApiRequest{ApiService: clusterAPI})
clusterAPI.EXPECT().GetClusterExecute(mock.AnythingOfType("admin.GetClusterApiRequest")).
Return(
Expand Down Expand Up @@ -423,7 +423,7 @@ func TestRegularClusterReconciliation(t *testing.T) {
&http.Response{},
nil,
)
clusterAPI.EXPECT().GetClusterAdvancedConfiguration(context.Background(), project.ID(), d.GetDeploymentName()).
clusterAPI.EXPECT().GetClusterAdvancedConfiguration(mock.Anything, project.ID(), d.GetDeploymentName()).
Return(admin.GetClusterAdvancedConfigurationApiRequest{ApiService: clusterAPI})
clusterAPI.EXPECT().GetClusterAdvancedConfigurationExecute(mock.AnythingOfType("admin.GetClusterAdvancedConfigurationApiRequest")).
Return(
Expand All @@ -433,7 +433,7 @@ func TestRegularClusterReconciliation(t *testing.T) {
)

searchAPI := mockadmin.NewAtlasSearchApi(t)
searchAPI.EXPECT().GetAtlasSearchDeployment(context.Background(), project.ID(), d.Spec.DeploymentSpec.Name).
searchAPI.EXPECT().GetAtlasSearchDeployment(mock.Anything, project.ID(), d.Spec.DeploymentSpec.Name).
Return(admin.GetAtlasSearchDeploymentApiRequest{ApiService: searchAPI})
searchAPI.EXPECT().GetAtlasSearchDeploymentExecute(mock.Anything).
Return(
Expand All @@ -452,7 +452,7 @@ func TestRegularClusterReconciliation(t *testing.T) {
)

globalAPI := mockadmin.NewGlobalClustersApi(t)
globalAPI.EXPECT().GetManagedNamespace(context.Background(), project.ID(), d.Spec.DeploymentSpec.Name).
globalAPI.EXPECT().GetManagedNamespace(mock.Anything, project.ID(), d.Spec.DeploymentSpec.Name).
Return(admin.GetManagedNamespaceApiRequest{ApiService: globalAPI})
globalAPI.EXPECT().GetManagedNamespaceExecute(mock.Anything).
Return(&admin.GeoSharding{}, nil, nil)
Expand Down Expand Up @@ -615,13 +615,13 @@ func TestServerlessInstanceReconciliation(t *testing.T) {
err := &admin.GenericOpenAPIError{}
err.SetModel(admin.ApiError{ErrorCode: pointer.MakePtr(atlas.ServerlessInstanceFromClusterAPI)})
clusterAPI := mockadmin.NewClustersApi(t)
clusterAPI.EXPECT().GetCluster(context.Background(), project.ID(), d.GetDeploymentName()).
clusterAPI.EXPECT().GetCluster(mock.Anything, project.ID(), d.GetDeploymentName()).
Return(admin.GetClusterApiRequest{ApiService: clusterAPI})
clusterAPI.EXPECT().GetClusterExecute(mock.AnythingOfType("admin.GetClusterApiRequest")).
Return(nil, nil, err)

serverlessAPI := mockadmin.NewServerlessInstancesApi(t)
serverlessAPI.EXPECT().GetServerlessInstance(context.Background(), project.ID(), d.GetDeploymentName()).
serverlessAPI.EXPECT().GetServerlessInstance(mock.Anything, project.ID(), d.GetDeploymentName()).
Return(admin.GetServerlessInstanceApiRequest{ApiService: serverlessAPI})
serverlessAPI.EXPECT().GetServerlessInstanceExecute(mock.AnythingOfType("admin.GetServerlessInstanceApiRequest")).
Return(
Expand All @@ -644,7 +644,7 @@ func TestServerlessInstanceReconciliation(t *testing.T) {
)

speClient := mockadmin.NewServerlessPrivateEndpointsApi(t)
speClient.EXPECT().ListServerlessPrivateEndpoints(context.Background(), project.ID(), d.GetDeploymentName()).
speClient.EXPECT().ListServerlessPrivateEndpoints(mock.Anything, project.ID(), d.GetDeploymentName()).
Return(admin.ListServerlessPrivateEndpointsApiRequest{ApiService: speClient})
speClient.EXPECT().ListServerlessPrivateEndpointsExecute(mock.AnythingOfType("admin.ListServerlessPrivateEndpointsApiRequest")).
Return(nil, &http.Response{}, nil)
Expand Down Expand Up @@ -785,7 +785,7 @@ func TestDeletionReconciliation(t *testing.T) {
},
SdkClientFunc: func(secretRef *client.ObjectKey, log *zap.SugaredLogger) (*admin.APIClient, string, error) {
clusterAPI := mockadmin.NewClustersApi(t)
clusterAPI.EXPECT().GetCluster(context.Background(), project.ID(), d.GetDeploymentName()).
clusterAPI.EXPECT().GetCluster(mock.Anything, project.ID(), d.GetDeploymentName()).
Return(admin.GetClusterApiRequest{ApiService: clusterAPI})
clusterAPI.EXPECT().GetClusterExecute(mock.AnythingOfType("admin.GetClusterApiRequest")).
Return(
Expand Down Expand Up @@ -816,7 +816,7 @@ func TestDeletionReconciliation(t *testing.T) {
&http.Response{},
nil,
)
clusterAPI.EXPECT().DeleteCluster(context.Background(), project.ID(), d.GetDeploymentName()).
clusterAPI.EXPECT().DeleteCluster(mock.Anything, project.ID(), d.GetDeploymentName()).
Return(admin.DeleteClusterApiRequest{ApiService: clusterAPI})
clusterAPI.EXPECT().DeleteClusterExecute(mock.AnythingOfType("admin.DeleteClusterApiRequest")).
Return(&http.Response{}, nil)
Expand Down Expand Up @@ -1163,7 +1163,7 @@ func TestGetProjectFromAtlas(t *testing.T) {
},
SdkClientFunc: func(secretRef *client.ObjectKey, log *zap.SugaredLogger) (*admin.APIClient, string, error) {
projectAPI := mockadmin.NewProjectsApi(t)
projectAPI.EXPECT().GetProject(context.Background(), "project-id").
projectAPI.EXPECT().GetProject(mock.Anything, "project-id").
Return(admin.GetProjectApiRequest{ApiService: projectAPI})
projectAPI.EXPECT().GetProjectExecute(mock.AnythingOfType("admin.GetProjectApiRequest")).
Return(nil, nil, errors.New("failed to get project"))
Expand Down Expand Up @@ -1211,7 +1211,7 @@ func TestGetProjectFromAtlas(t *testing.T) {
},
SdkClientFunc: func(secretRef *client.ObjectKey, log *zap.SugaredLogger) (*admin.APIClient, string, error) {
projectAPI := mockadmin.NewProjectsApi(t)
projectAPI.EXPECT().GetProject(context.Background(), "project-id").
projectAPI.EXPECT().GetProject(mock.Anything, "project-id").
Return(admin.GetProjectApiRequest{ApiService: projectAPI})
projectAPI.EXPECT().GetProjectExecute(mock.AnythingOfType("admin.GetProjectApiRequest")).
Return(&admin.Group{Id: pointer.MakePtr("project-id")}, nil, nil)
Expand Down Expand Up @@ -1557,7 +1557,7 @@ func TestChangeDeploymentType(t *testing.T) {
},
SdkClientFunc: func(secretRef *client.ObjectKey, log *zap.SugaredLogger) (*admin.APIClient, string, error) {
clusterAPI := mockadmin.NewClustersApi(t)
clusterAPI.EXPECT().GetCluster(ctx, "abc123", "cluster0").
clusterAPI.EXPECT().GetCluster(mock.Anything, "abc123", "cluster0").
Return(admin.GetClusterApiRequest{ApiService: clusterAPI})
clusterAPI.EXPECT().GetClusterExecute(mock.AnythingOfType("admin.GetClusterApiRequest")).
RunAndReturn(
Expand All @@ -1573,7 +1573,7 @@ func TestChangeDeploymentType(t *testing.T) {

serverlessAPI := mockadmin.NewServerlessInstancesApi(t)
if !tt.deployment.IsServerless() {
serverlessAPI.EXPECT().GetServerlessInstance(ctx, "abc123", "cluster0").
serverlessAPI.EXPECT().GetServerlessInstance(mock.Anything, "abc123", "cluster0").
Return(admin.GetServerlessInstanceApiRequest{ApiService: serverlessAPI})
serverlessAPI.EXPECT().GetServerlessInstanceExecute(mock.AnythingOfType("admin.GetServerlessInstanceApiRequest")).
Return(&admin.ServerlessInstanceDescription{Name: pointer.MakePtr("cluster0")}, nil, nil)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func TestReconcile(t *testing.T) {

logger := zaptest.NewLogger(t).Sugar()
fedAuthAPI := mockadminv20241113001.NewFederatedAuthenticationApi(t)
fedAuthAPI.EXPECT().GetFederationSettings(context.Background(), orgID).
fedAuthAPI.EXPECT().GetFederationSettings(mock.Anything, orgID).
Return(adminv20241113001.GetFederationSettingsApiRequest{ApiService: fedAuthAPI})
fedAuthAPI.EXPECT().GetFederationSettingsExecute(mock.Anything).
Return(
Expand All @@ -108,7 +108,7 @@ func TestReconcile(t *testing.T) {
&http.Response{},
nil,
)
fedAuthAPI.EXPECT().ListIdentityProviders(context.Background(), fedSettingsID).
fedAuthAPI.EXPECT().ListIdentityProviders(mock.Anything, fedSettingsID).
Return(adminv20241113001.ListIdentityProvidersApiRequest{ApiService: fedAuthAPI})
fedAuthAPI.EXPECT().ListIdentityProvidersExecute(mock.Anything).
Return(
Expand All @@ -124,7 +124,7 @@ func TestReconcile(t *testing.T) {
&http.Response{},
nil,
)
fedAuthAPI.EXPECT().GetConnectedOrgConfig(context.Background(), fedSettingsID, orgID).
fedAuthAPI.EXPECT().GetConnectedOrgConfig(mock.Anything, fedSettingsID, orgID).
Return(adminv20241113001.GetConnectedOrgConfigApiRequest{ApiService: fedAuthAPI})
fedAuthAPI.EXPECT().GetConnectedOrgConfigExecute(mock.Anything).
Return(
Expand All @@ -139,7 +139,7 @@ func TestReconcile(t *testing.T) {
nil,
)
groupAPI := mockadmin.NewProjectsApi(t)
groupAPI.EXPECT().ListProjects(context.Background()).
groupAPI.EXPECT().ListProjects(mock.Anything).
Return(admin.ListProjectsApiRequest{ApiService: groupAPI})
groupAPI.EXPECT().ListProjectsExecute(mock.Anything).
Return(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,12 +318,12 @@ func TestEnsureCustomResource(t *testing.T) {
},
SdkClientFunc: func(secretRef *client.ObjectKey, log *zap.SugaredLogger) (*admin.APIClient, string, error) {
projectAPI := mockadmin.NewProjectsApi(t)
projectAPI.EXPECT().GetProject(ctx, projectID).Return(admin.GetProjectApiRequest{ApiService: projectAPI})
projectAPI.EXPECT().GetProject(mock.Anything, projectID).Return(admin.GetProjectApiRequest{ApiService: projectAPI})
projectAPI.EXPECT().GetProjectExecute(mock.AnythingOfType("admin.GetProjectApiRequest")).
Return(&admin.Group{Id: &projectID}, nil, nil)

peAPI := mockadmin.NewPrivateEndpointServicesApi(t)
peAPI.EXPECT().GetPrivateEndpointService(ctx, projectID, "AWS", "pe-service-id").
peAPI.EXPECT().GetPrivateEndpointService(mock.Anything, projectID, "AWS", "pe-service-id").
Return(admin.GetPrivateEndpointServiceApiRequest{ApiService: peAPI})
peAPI.EXPECT().GetPrivateEndpointServiceExecute(mock.AnythingOfType("admin.GetPrivateEndpointServiceApiRequest")).
Return(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ func TestRenconcile(t *testing.T) {
notFoundErr := &admin.GenericOpenAPIError{}
notFoundErr.SetModel(admin.ApiError{ErrorCode: pointer.MakePtr("NOT_IN_GROUP")})
projectsAPI := mockadmin.NewProjectsApi(t)
projectsAPI.EXPECT().GetProjectByName(context.Background(), "my-project").
projectsAPI.EXPECT().GetProjectByName(mock.Anything, "my-project").
Return(admin.GetProjectByNameApiRequest{ApiService: projectsAPI})
projectsAPI.EXPECT().GetProjectByNameExecute(mock.AnythingOfType("admin.GetProjectByNameApiRequest")).
Return(
nil,
&http.Response{},
notFoundErr,
)
projectsAPI.EXPECT().CreateProject(context.Background(), mock.AnythingOfType("*admin.Group")).
projectsAPI.EXPECT().CreateProject(mock.Anything, mock.AnythingOfType("*admin.Group")).
Return(admin.CreateProjectApiRequest{ApiService: projectsAPI})
projectsAPI.EXPECT().CreateProjectExecute(mock.AnythingOfType("admin.CreateProjectApiRequest")).
Return(
Expand Down
10 changes: 6 additions & 4 deletions internal/controller/atlasproject/teams_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"go.mongodb.org/atlas-sdk/v20231115008/admin"
"go.uber.org/zap/zaptest"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -154,10 +155,11 @@ func TestSyncAssignedTeams(t *testing.T) {
ctx := &workflow.Context{
Log: logger,
SdkClient: atlasClient,
Context: context.Background(),
}
teamService := func() teams.TeamsService {
service := translation.NewTeamsServiceMock(t)
service.EXPECT().ListProjectTeams(nil, "projectID").Return([]teams.AssignedTeam{
service.EXPECT().ListProjectTeams(mock.Anything, "projectID").Return([]teams.AssignedTeam{
{
Roles: []string{"GROUP_OWNER"},
TeamID: team1.Status.ID,
Expand All @@ -174,9 +176,9 @@ func TestSyncAssignedTeams(t *testing.T) {
TeamName: "teamName_3",
},
}, nil)
service.EXPECT().Unassign(nil, "projectID", "teamID_2").Return(nil)
service.EXPECT().Unassign(nil, "projectID", "teamID_3").Return(nil)
service.EXPECT().Assign(nil,
service.EXPECT().Unassign(mock.Anything, "projectID", "teamID_2").Return(nil)
service.EXPECT().Unassign(mock.Anything, "projectID", "teamID_3").Return(nil)
service.EXPECT().Assign(mock.Anything,
&[]teams.AssignedTeam{
{
Roles: []string{"GROUP_READ_ONLY"},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ func TestEnsureAtlasStreamsInstance(t *testing.T) {
Build()

streamsAPI := mockadmin.NewStreamsApi(t)
streamsAPI.EXPECT().GetStreamInstance(context.Background(), "my-project-id", "instance-0").
streamsAPI.EXPECT().GetStreamInstance(mock.Anything, "my-project-id", "instance-0").
Return(admin.GetStreamInstanceApiRequest{ApiService: streamsAPI})
streamsAPI.EXPECT().GetStreamInstanceExecute(mock.AnythingOfType("admin.GetStreamInstanceApiRequest")).
Return(nil, &http.Response{}, errors.New("failed to get instance"))
Expand Down Expand Up @@ -554,7 +554,7 @@ func TestEnsureAtlasStreamsInstance(t *testing.T) {
Build()

streamsAPI := mockadmin.NewStreamsApi(t)
streamsAPI.EXPECT().GetStreamInstance(context.Background(), "my-project-id", "instance-0").
streamsAPI.EXPECT().GetStreamInstance(mock.Anything, "my-project-id", "instance-0").
Return(admin.GetStreamInstanceApiRequest{ApiService: streamsAPI})
streamsAPI.EXPECT().GetStreamInstanceExecute(mock.AnythingOfType("admin.GetStreamInstanceApiRequest")).
Return(
Expand All @@ -574,7 +574,7 @@ func TestEnsureAtlasStreamsInstance(t *testing.T) {
&http.Response{},
nil,
)
streamsAPI.EXPECT().ListStreamConnections(context.Background(), "my-project-id", "instance-0").
streamsAPI.EXPECT().ListStreamConnections(mock.Anything, "my-project-id", "instance-0").
Return(admin.ListStreamConnectionsApiRequest{ApiService: streamsAPI})
streamsAPI.EXPECT().ListStreamConnectionsExecute(mock.AnythingOfType("admin.ListStreamConnectionsApiRequest")).
Return(
Expand Down Expand Up @@ -665,7 +665,7 @@ func TestEnsureAtlasStreamsInstance(t *testing.T) {
Build()

streamsAPI := mockadmin.NewStreamsApi(t)
streamsAPI.EXPECT().GetStreamInstance(context.Background(), "my-project-id", "instance-0").
streamsAPI.EXPECT().GetStreamInstance(mock.Anything, "my-project-id", "instance-0").
Return(admin.GetStreamInstanceApiRequest{ApiService: streamsAPI})
notFound := admin.ApiError{}
notFound.SetError(404)
Expand All @@ -678,7 +678,7 @@ func TestEnsureAtlasStreamsInstance(t *testing.T) {
&http.Response{},
&apiError,
)
streamsAPI.EXPECT().CreateStreamInstance(context.Background(), "my-project-id", mock.AnythingOfType("*admin.StreamsTenant")).
streamsAPI.EXPECT().CreateStreamInstance(mock.Anything, "my-project-id", mock.AnythingOfType("*admin.StreamsTenant")).
Return(admin.CreateStreamInstanceApiRequest{ApiService: streamsAPI})
streamsAPI.EXPECT().CreateStreamInstanceExecute(mock.AnythingOfType("admin.CreateStreamInstanceApiRequest")).
Return(
Expand Down Expand Up @@ -786,7 +786,7 @@ func TestEnsureAtlasStreamsInstance(t *testing.T) {
Build()

streamsAPI := mockadmin.NewStreamsApi(t)
streamsAPI.EXPECT().GetStreamInstance(context.Background(), "my-project-id", "instance-0").
streamsAPI.EXPECT().GetStreamInstance(mock.Anything, "my-project-id", "instance-0").
Return(admin.GetStreamInstanceApiRequest{ApiService: streamsAPI})
streamsAPI.EXPECT().GetStreamInstanceExecute(mock.AnythingOfType("admin.GetStreamInstanceApiRequest")).
Return(
Expand All @@ -806,7 +806,7 @@ func TestEnsureAtlasStreamsInstance(t *testing.T) {
&http.Response{},
nil,
)
streamsAPI.EXPECT().DeleteStreamInstance(context.Background(), "my-project-id", "instance-0").
streamsAPI.EXPECT().DeleteStreamInstance(mock.Anything, "my-project-id", "instance-0").
Return(admin.DeleteStreamInstanceApiRequest{ApiService: streamsAPI})
streamsAPI.EXPECT().DeleteStreamInstanceExecute(mock.AnythingOfType("admin.DeleteStreamInstanceApiRequest")).
Return(
Expand Down Expand Up @@ -888,7 +888,7 @@ func TestEnsureAtlasStreamsInstance(t *testing.T) {
Build()

streamsAPI := mockadmin.NewStreamsApi(t)
streamsAPI.EXPECT().GetStreamInstance(context.Background(), "my-project-id", "instance-0").
streamsAPI.EXPECT().GetStreamInstance(mock.Anything, "my-project-id", "instance-0").
Return(admin.GetStreamInstanceApiRequest{ApiService: streamsAPI})
streamsAPI.EXPECT().GetStreamInstanceExecute(mock.AnythingOfType("admin.GetStreamInstanceApiRequest")).
Return(
Expand All @@ -908,7 +908,7 @@ func TestEnsureAtlasStreamsInstance(t *testing.T) {
&http.Response{},
nil,
)
streamsAPI.EXPECT().UpdateStreamInstance(context.Background(), "my-project-id", "instance-0", mock.AnythingOfType("*admin.StreamsDataProcessRegion")).
streamsAPI.EXPECT().UpdateStreamInstance(mock.Anything, "my-project-id", "instance-0", mock.AnythingOfType("*admin.StreamsDataProcessRegion")).
Return(admin.UpdateStreamInstanceApiRequest{ApiService: streamsAPI})
streamsAPI.EXPECT().UpdateStreamInstanceExecute(mock.AnythingOfType("admin.UpdateStreamInstanceApiRequest")).
Return(
Expand Down

0 comments on commit b4bf714

Please sign in to comment.