Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DeleteNamespace API unit tests #2670

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion service/frontend/operator_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ func (h *OperatorHandlerImpl) DeleteNamespace(ctx context.Context, request *oper
ctx,
sdkclient.StartWorkflowOptions{
TaskQueue: worker.DefaultWorkerTaskQueue,
ID: deleteexecutions.WorkflowName,
ID: deletenamespace.WorkflowName,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤦

},
deletenamespace.WorkflowName,
wfParams,
Expand Down
81 changes: 81 additions & 0 deletions service/frontend/operator_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,12 @@ import (
"go.temporal.io/api/serviceerror"
sdkmocks "go.temporal.io/sdk/mocks"

"go.temporal.io/server/common/dynamicconfig"
"go.temporal.io/server/common/metrics"
"go.temporal.io/server/common/persistence/visibility/store/elasticsearch/client"
"go.temporal.io/server/common/resource"
"go.temporal.io/server/common/searchattribute"
"go.temporal.io/server/service/worker/deletenamespace"
)

type (
Expand Down Expand Up @@ -374,3 +376,82 @@ func (s *operatorHandlerSuite) Test_RemoveSearchAttributes() {
s.NoError(err)
s.NotNil(resp)
}

func (s *operatorHandlerSuite) Test_DeleteNamespace() {
handler := s.handler
ctx := context.Background()

type test struct {
Name string
Request *operatorservice.DeleteNamespaceRequest
Expected error
}
// request validation tests
testCases1 := []test{
{
Name: "nil request",
Request: nil,
Expected: &serviceerror.InvalidArgument{Message: "Request is nil."},
},
{
Name: "empty request",
Request: &operatorservice.DeleteNamespaceRequest{},
Expected: &serviceerror.InvalidArgument{Message: "Namespace is not set on request."},
},
}
for _, testCase := range testCases1 {
s.T().Run(testCase.Name, func(t *testing.T) {
resp, err := handler.DeleteNamespace(ctx, testCase.Request)
s.Equal(testCase.Expected, err)
s.Nil(resp)
})
}

mockSdkClient := &sdkmocks.Client{}
s.mockResource.SDKClientFactory.EXPECT().GetSystemClient(gomock.Any()).Return(mockSdkClient).AnyTimes()

handler.config = &Config{
DeleteNamespaceDeleteActivityRPS: dynamicconfig.GetIntPropertyFn(22),
DeleteNamespaceConcurrentDeleteExecutionsActivities: dynamicconfig.GetIntPropertyFn(8),
}

// Start workflow failed.
mockSdkClient.On("ExecuteWorkflow", mock.Anything, mock.Anything, "temporal-sys-delete-namespace-workflow", mock.Anything).Return(nil, errors.New("start failed")).Once()
resp, err := handler.DeleteNamespace(ctx, &operatorservice.DeleteNamespaceRequest{
Namespace: "test-namespace",
})
s.Error(err)
s.Equal("Unable to start temporal-sys-delete-namespace-workflow workflow: start failed.", err.Error())
s.Nil(resp)

// Workflow failed.
mockRun := &sdkmocks.WorkflowRun{}
mockRun.On("Get", mock.Anything, mock.Anything).Return(errors.New("workflow failed")).Once()
const RunId = "9a9f668a-58b1-427e-bed6-bf1401049f7d"
mockRun.On("GetRunID").Return(RunId).Once()
mockSdkClient.On("ExecuteWorkflow", mock.Anything, mock.Anything, "temporal-sys-delete-namespace-workflow", mock.Anything).Return(mockRun, nil)
resp, err = handler.DeleteNamespace(ctx, &operatorservice.DeleteNamespaceRequest{
Namespace: "test-namespace",
})
s.Error(err)
s.Equal(RunId, err.(*serviceerror.SystemWorkflow).WorkflowExecution.RunId)
s.Equal(fmt.Sprintf("System Workflow with WorkflowId temporal-sys-delete-namespace-workflow and RunId %s returned an error: workflow failed", RunId), err.Error())
s.Nil(resp)

// Success case.
mockRun.On("Get", mock.Anything, mock.Anything).Return(func(ctx context.Context, valuePtr interface{}) error {
wfResult := valuePtr.(*deletenamespace.DeleteNamespaceWorkflowResult)
wfResult.DeletedNamespace = "test-namespace-deleted-ka2te"
wfResult.DeletedNamespaceID = "c13c01a7-3887-4eda-ba4b-9a07a6359e7e"
return nil
})

resp, err = handler.DeleteNamespace(ctx, &operatorservice.DeleteNamespaceRequest{
Namespace: "test-namespace",
})
s.NoError(err)
s.NotNil(resp)
s.Equal("test-namespace-deleted-ka2te", resp.DeletedNamespace)
mockRun.AssertExpectations(s.T())
mockSdkClient.AssertExpectations(s.T())
}