Skip to content

Commit

Permalink
refactor: Modifies mocking of search deployment unit test directly to…
Browse files Browse the repository at this point in the history
… SDK removing intermediate service (#2028)

* refactor: Modifies mocking of search deployment unit test directly to SDK removing intermediate service

* adjust mockery file during update sdk script

* remove redundant assertions

* pass AtlasSearchApi instead of APIClient
  • Loading branch information
AgustinBettati authored Mar 15, 2024
1 parent 09357b9 commit e923fd5
Show file tree
Hide file tree
Showing 11 changed files with 1,969 additions and 120 deletions.
6 changes: 3 additions & 3 deletions .mockery.yaml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
with-expecter: false
with-expecter: true
disable-version-string: true
dir: internal/testutil/mocksvc
outpkg: mocksvc
filename: "{{ .InterfaceName | snakecase }}.go"
mockname: "{{.InterfaceName}}"

packages:
github.com/mongodb/terraform-provider-mongodbatlas/internal/service/searchdeployment:
go.mongodb.org/atlas-sdk/v20231115007/admin:
interfaces:
DeploymentService:
AtlasSearchApi:

github.com/mongodb/terraform-provider-mongodbatlas/internal/service/encryptionatrest:
interfaces:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (r *searchDeploymentRS) Create(ctx context.Context, req resource.CreateRequ
if resp.Diagnostics.HasError() {
return
}
deploymentResp, err := WaitSearchNodeStateTransition(ctx, projectID, clusterName, ServiceFromClient(connV2),
deploymentResp, err := WaitSearchNodeStateTransition(ctx, projectID, clusterName, connV2.AtlasSearchApi,
retryTimeConfig(createTimeout, minTimeoutCreateUpdate))
if err != nil {
resp.Diagnostics.AddError("error during search deployment creation", err.Error())
Expand Down Expand Up @@ -125,7 +125,7 @@ func (r *searchDeploymentRS) Update(ctx context.Context, req resource.UpdateRequ
if resp.Diagnostics.HasError() {
return
}
deploymentResp, err := WaitSearchNodeStateTransition(ctx, projectID, clusterName, ServiceFromClient(connV2),
deploymentResp, err := WaitSearchNodeStateTransition(ctx, projectID, clusterName, connV2.AtlasSearchApi,
retryTimeConfig(updateTimeout, minTimeoutCreateUpdate))
if err != nil {
resp.Diagnostics.AddError("error during search deployment update", err.Error())
Expand Down Expand Up @@ -159,7 +159,7 @@ func (r *searchDeploymentRS) Delete(ctx context.Context, req resource.DeleteRequ
if resp.Diagnostics.HasError() {
return
}
if err := WaitSearchNodeDelete(ctx, projectID, clusterName, ServiceFromClient(connV2), retryTimeConfig(deleteTimeout, minTimeoutDelete)); err != nil {
if err := WaitSearchNodeDelete(ctx, projectID, clusterName, connV2.AtlasSearchApi, retryTimeConfig(deleteTimeout, minTimeoutDelete)); err != nil {
resp.Diagnostics.AddError("error during search deployment delete", err.Error())
return
}
Expand Down
26 changes: 0 additions & 26 deletions internal/service/searchdeployment/service_search_deployment.go

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

const SearchDeploymentDoesNotExistsError = "ATLAS_FTS_DEPLOYMENT_DOES_NOT_EXIST"

func WaitSearchNodeStateTransition(ctx context.Context, projectID, clusterName string, client DeploymentService,
func WaitSearchNodeStateTransition(ctx context.Context, projectID, clusterName string, client admin.AtlasSearchApi,
timeConfig retrystrategy.TimeConfig) (*admin.ApiSearchDeploymentResponse, error) {
stateConf := &retry.StateChangeConf{
Pending: []string{retrystrategy.RetryStrategyUpdatingState, retrystrategy.RetryStrategyPausedState},
Expand All @@ -36,7 +36,7 @@ func WaitSearchNodeStateTransition(ctx context.Context, projectID, clusterName s
return nil, errors.New("did not obtain valid result when waiting for search deployment state transition")
}

func WaitSearchNodeDelete(ctx context.Context, projectID, clusterName string, client DeploymentService, timeConfig retrystrategy.TimeConfig) error {
func WaitSearchNodeDelete(ctx context.Context, projectID, clusterName string, client admin.AtlasSearchApi, timeConfig retrystrategy.TimeConfig) error {
stateConf := &retry.StateChangeConf{
Pending: []string{retrystrategy.RetryStrategyIdleState, retrystrategy.RetryStrategyUpdatingState, retrystrategy.RetryStrategyPausedState},
Target: []string{retrystrategy.RetryStrategyDeletedState},
Expand All @@ -49,9 +49,9 @@ func WaitSearchNodeDelete(ctx context.Context, projectID, clusterName string, cl
return err
}

func searchDeploymentRefreshFunc(ctx context.Context, projectID, clusterName string, client DeploymentService) retry.StateRefreshFunc {
func searchDeploymentRefreshFunc(ctx context.Context, projectID, clusterName string, client admin.AtlasSearchApi) retry.StateRefreshFunc {
return func() (any, string, error) {
deploymentResp, resp, err := client.GetAtlasSearchDeployment(ctx, projectID, clusterName)
deploymentResp, resp, err := client.GetAtlasSearchDeployment(ctx, projectID, clusterName).Execute()
if err != nil && deploymentResp == nil && resp == nil {
return nil, "", err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/mongodb/terraform-provider-mongodbatlas/internal/service/searchdeployment"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/testutil/mocksvc"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"go.mongodb.org/atlas-sdk/v20231115007/admin"
)

Expand Down Expand Up @@ -73,15 +74,16 @@ func TestSearchDeploymentStateTransition(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
svc := mocksvc.NewDeploymentService(t)
ctx := context.Background()
m := mocksvc.NewAtlasSearchApi(t)
m.EXPECT().GetAtlasSearchDeployment(mock.Anything, mock.Anything, mock.Anything).Return(admin.GetAtlasSearchDeploymentApiRequest{ApiService: m})

for _, resp := range tc.mockResponses {
svc.On("GetAtlasSearchDeployment", ctx, dummyProjectID, clusterName).Return(resp.get()...).Once()
modelResp, httpResp, err := resp.get()
m.EXPECT().GetAtlasSearchDeploymentExecute(mock.Anything).Return(modelResp, httpResp, err).Once()
}
resp, err := searchdeployment.WaitSearchNodeStateTransition(ctx, dummyProjectID, "Cluster0", svc, testTimeoutConfig)
resp, err := searchdeployment.WaitSearchNodeStateTransition(context.Background(), dummyProjectID, "Cluster0", m, testTimeoutConfig)
assert.Equal(t, tc.expectedError, err != nil)
assert.Equal(t, responseWithState(tc.expectedState), resp)
svc.AssertExpectations(t)
})
}
}
Expand Down Expand Up @@ -115,14 +117,15 @@ func TestSearchDeploymentStateTransitionForDelete(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
svc := mocksvc.NewDeploymentService(t)
ctx := context.Background()
m := mocksvc.NewAtlasSearchApi(t)
m.EXPECT().GetAtlasSearchDeployment(mock.Anything, mock.Anything, mock.Anything).Return(admin.GetAtlasSearchDeploymentApiRequest{ApiService: m})

for _, resp := range tc.mockResponses {
svc.On("GetAtlasSearchDeployment", ctx, dummyProjectID, clusterName).Return(resp.get()...).Once()
modelResp, httpResp, err := resp.get()
m.EXPECT().GetAtlasSearchDeploymentExecute(mock.Anything).Return(modelResp, httpResp, err).Once()
}
err := searchdeployment.WaitSearchNodeDelete(ctx, dummyProjectID, clusterName, svc, testTimeoutConfig)
err := searchdeployment.WaitSearchNodeDelete(context.Background(), dummyProjectID, clusterName, m, testTimeoutConfig)
assert.Equal(t, tc.expectedError, err != nil)
svc.AssertExpectations(t)
})
}
}
Expand Down Expand Up @@ -156,10 +159,10 @@ type response struct {
err error
}

func (r *response) get() []interface{} {
func (r *response) get() (*admin.ApiSearchDeploymentResponse, *http.Response, error) {
var httpResp *http.Response
if r.statusCode != nil {
httpResp = &http.Response{StatusCode: *r.statusCode}
}
return []interface{}{responseWithState(r.state), httpResp, r.err}
return responseWithState(r.state), httpResp, r.err
}
Loading

0 comments on commit e923fd5

Please sign in to comment.