Skip to content

Commit

Permalink
Converging admin_task_list_commands_test.go
Browse files Browse the repository at this point in the history
To follow a new, shorter approach with newCLITestData/clitest.
  • Loading branch information
dkrotx committed Oct 25, 2024
1 parent 0101d14 commit 12729cb
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 145 deletions.
10 changes: 6 additions & 4 deletions tools/cli/admin_commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@ import (
)

const (
testShardID = 1234
testCluster = "test-cluster"
testDomain = "test-domain"
testQueueType = 2 // transfer queue
testShardID = 1234
testCluster = "test-cluster"
testDomain = "test-domain"
testTaskList = "test-tasklist"
testTaskListType = "decision"
testQueueType = 2 // transfer queue
)

type cliTestData struct {
Expand Down
224 changes: 83 additions & 141 deletions tools/cli/admin_task_list_commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,21 @@
package cli

import (
"flag"
"fmt"
"testing"

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"github.com/urfave/cli/v2"

"github.com/uber/cadence/client/admin"
"github.com/uber/cadence/client/frontend"
"github.com/uber/cadence/common/types"
"github.com/uber/cadence/tools/cli/clitest"
)

func TestAdminDescribeTaskList(t *testing.T) {
mockCtrl := gomock.NewController(t)
serverFrontendClient := frontend.NewMockClient(mockCtrl)
serverAdminClient := admin.NewMockClient(mockCtrl)
app := NewCliApp(&clientFactoryMock{
serverFrontendClient: serverFrontendClient,
serverAdminClient: serverAdminClient,
})
td := newCLITestData(t)

expectedResponse := &types.DescribeTaskListResponse{
Pollers: []*types.PollerInfo{
{
Expand All @@ -54,145 +48,95 @@ func TestAdminDescribeTaskList(t *testing.T) {
BacklogCountHint: 10,
},
}
serverFrontendClient.EXPECT().DescribeTaskList(gomock.Any(), gomock.Any()).Return(expectedResponse, nil).Times(1)
td.mockFrontendClient.EXPECT().DescribeTaskList(gomock.Any(), gomock.Any()).Return(expectedResponse, nil).Times(1)

c := setTasklistMock(app)
err := AdminDescribeTaskList(c)
cliCtx := newTaskListCLIContext(t, td.app)
err := AdminDescribeTaskList(cliCtx)
assert.NoError(t, err)
}

func TestAdminDescribeTaskList_DescribeTaskListFails(t *testing.T) {
mockCtrl := gomock.NewController(t)
serverFrontendClient := frontend.NewMockClient(mockCtrl)
serverAdminClient := admin.NewMockClient(mockCtrl)

app := NewCliApp(&clientFactoryMock{
serverFrontendClient: serverFrontendClient,
serverAdminClient: serverAdminClient,
})
td := newCLITestData(t)

serverFrontendClient.EXPECT().
td.mockFrontendClient.EXPECT().
DescribeTaskList(gomock.Any(), gomock.Any()).
Return(nil, fmt.Errorf("DescribeTaskList failed")).
Times(1)

c := setTasklistMock(app)
err := AdminDescribeTaskList(c)
assert.Error(t, err)
assert.Contains(t, err.Error(), "Operation DescribeTaskList failed.")
cliCtx := newTaskListCLIContext(t, td.app)
err := AdminDescribeTaskList(cliCtx)
assert.ErrorContains(t, err, "Operation DescribeTaskList failed.")
}

func TestAdminDescribeTaskList_NoTaskListStatus(t *testing.T) {
mockCtrl := gomock.NewController(t)
serverFrontendClient := frontend.NewMockClient(mockCtrl)
serverAdminClient := admin.NewMockClient(mockCtrl)

app := NewCliApp(&clientFactoryMock{
serverFrontendClient: serverFrontendClient,
serverAdminClient: serverAdminClient,
})
td := newCLITestData(t)

expectedResponse := &types.DescribeTaskListResponse{
Pollers: []*types.PollerInfo{},
TaskListStatus: nil,
}

serverFrontendClient.EXPECT().
td.mockFrontendClient.EXPECT().
DescribeTaskList(gomock.Any(), gomock.Any()).
Return(expectedResponse, nil).
Times(1)

c := setTasklistMock(app)
err := AdminDescribeTaskList(c)
assert.Error(t, err)
assert.Contains(t, err.Error(), "No tasklist status information.")
cliCtx := newTaskListCLIContext(t, td.app)
err := AdminDescribeTaskList(cliCtx)
assert.ErrorContains(t, err, "No tasklist status information.")
}

func TestAdminDescribeTaskList_NoPollers(t *testing.T) {
mockCtrl := gomock.NewController(t)
serverFrontendClient := frontend.NewMockClient(mockCtrl)
serverAdminClient := admin.NewMockClient(mockCtrl)

app := NewCliApp(&clientFactoryMock{
serverFrontendClient: serverFrontendClient,
serverAdminClient: serverAdminClient,
})

td := newCLITestData(t)
expectedResponse := &types.DescribeTaskListResponse{
Pollers: []*types.PollerInfo{},
TaskListStatus: &types.TaskListStatus{
BacklogCountHint: 0,
},
}

serverFrontendClient.EXPECT().
td.mockFrontendClient.EXPECT().
DescribeTaskList(gomock.Any(), gomock.Any()).
Return(expectedResponse, nil).
Times(1)

c := setTasklistMock(app)
err := AdminDescribeTaskList(c)
assert.Error(t, err)
assert.Contains(t, err.Error(), "No poller for tasklist: test-tasklist")
cliCtx := newTaskListCLIContext(t, td.app)
err := AdminDescribeTaskList(cliCtx)
assert.ErrorContains(t, err, "No poller for tasklist: test-tasklist")
}

func TestAdminDescribeTaskList_GetRequiredOptionDomainError(t *testing.T) {
mockCtrl := gomock.NewController(t)
serverFrontendClient := frontend.NewMockClient(mockCtrl)
serverAdminClient := admin.NewMockClient(mockCtrl)

app := NewCliApp(&clientFactoryMock{
serverFrontendClient: serverFrontendClient,
serverAdminClient: serverAdminClient,
})

// Omit the Domain flag
set := flag.NewFlagSet("test", 0)
set.String(FlagTaskList, "test-tasklist", "TaskList flag")
set.String(FlagTaskListType, "decision", "TaskListType flag")
c := cli.NewContext(app, set, nil)

err := AdminDescribeTaskList(c)
assert.Error(t, err)
assert.Contains(t, err.Error(), "Required flag not found: ")
td := newCLITestData(t)

cliCtx := clitest.NewCLIContext(
t,
td.app,
/* omit the domain flag */
clitest.StringArgument(FlagTaskList, testTaskList),
clitest.StringArgument(FlagTaskListType, testTaskListType),
)

err := AdminDescribeTaskList(cliCtx)
assert.ErrorContains(t, err, "Required flag not found: ")
}

func TestAdminDescribeTaskList_GetRequiredOptionTaskListError(t *testing.T) {
mockCtrl := gomock.NewController(t)
serverFrontendClient := frontend.NewMockClient(mockCtrl)
serverAdminClient := admin.NewMockClient(mockCtrl)

app := NewCliApp(&clientFactoryMock{
serverFrontendClient: serverFrontendClient,
serverAdminClient: serverAdminClient,
})

// Omit the TaskList flag
set := flag.NewFlagSet("test", 0)
set.String(FlagDomain, "test-domain", "Domain flag")
set.String(FlagTaskListType, "decision", "TaskListType flag")
c := cli.NewContext(app, set, nil)

err := AdminDescribeTaskList(c)
assert.Error(t, err)
assert.Contains(t, err.Error(), "Required flag not found: ")
td := newCLITestData(t)

cliCtx := clitest.NewCLIContext(
t,
td.app,
clitest.StringArgument(FlagDomain, testDomain),
/* omit the task-list flag */
clitest.StringArgument(FlagTaskListType, testTaskListType),
)

err := AdminDescribeTaskList(cliCtx)
assert.ErrorContains(t, err, "Required flag not found: ")
}

func TestAdminDescribeTaskList_InvalidTaskListType(t *testing.T) {
mockCtrl := gomock.NewController(t)
serverFrontendClient := frontend.NewMockClient(mockCtrl)
serverAdminClient := admin.NewMockClient(mockCtrl)
app := NewCliApp(&clientFactoryMock{
serverFrontendClient: serverFrontendClient,
serverAdminClient: serverAdminClient,
})

// Set an invalid TaskListType
set := flag.NewFlagSet("test", 0)
set.String(FlagDomain, "test-domain", "Domain flag")
set.String(FlagTaskList, "test-tasklist", "TaskList flag")
set.String(FlagTaskListType, "activity", "TaskListType flag")
c := cli.NewContext(app, set, nil)
td := newCLITestData(t)

expectedResponse := &types.DescribeTaskListResponse{
Pollers: []*types.PollerInfo{
Expand All @@ -204,16 +148,20 @@ func TestAdminDescribeTaskList_InvalidTaskListType(t *testing.T) {
BacklogCountHint: 10,
},
}
serverFrontendClient.EXPECT().DescribeTaskList(gomock.Any(), gomock.Any()).Return(expectedResponse, nil).Times(1)

err := AdminDescribeTaskList(c)
td.mockFrontendClient.EXPECT().DescribeTaskList(gomock.Any(), gomock.Any()).Return(expectedResponse, nil).Times(1)

cliCtx := clitest.NewCLIContext(
t,
td.app,
clitest.StringArgument(FlagDomain, testDomain),
clitest.StringArgument(FlagTaskList, testTaskList),
clitest.StringArgument(FlagTaskListType, "activity"),
)
err := AdminDescribeTaskList(cliCtx)
assert.NoError(t, err)
}

func TestAdminListTaskList(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

// Define table of test cases
tests := []struct {
name string
Expand Down Expand Up @@ -284,48 +232,42 @@ func TestAdminListTaskList(t *testing.T) {
// Loop through test cases
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Mock clients
serverFrontendClient := frontend.NewMockClient(mockCtrl)
serverAdminClient := admin.NewMockClient(mockCtrl)

// Create CLI app with mock clients
app := NewCliApp(&clientFactoryMock{
serverFrontendClient: serverFrontendClient,
serverAdminClient: serverAdminClient,
})
td := newCLITestData(t)

// Set up mocks for the current test case
tt.setupMocks(serverFrontendClient)

// Set up the CLI context
set := flag.NewFlagSet("test", 0)
if tt.domainFlag != "" {
set.String(FlagDomain, tt.domainFlag, "Domain flag")
tt.setupMocks(td.mockFrontendClient)

var cliCtx *cli.Context
if tt.domainFlag == "" {
cliCtx = clitest.NewCLIContext(
t,
td.app,
clitest.StringArgument(FlagTaskList, testTaskList),
/* omit the domain flag */
clitest.StringArgument(FlagTaskListType, testTaskListType),
)
} else {
// construct cli context with all the required arguments
cliCtx = newTaskListCLIContext(t, td.app)
}
set.String(FlagTaskList, tt.taskListFlag, "TaskList flag")
set.String(FlagTaskListType, tt.taskListType, "TaskListType flag")
c := cli.NewContext(app, set, nil)

// Call the function under test
err := AdminListTaskList(c)

// Check expected outcomes
if tt.expectedError != "" {
assert.Error(t, err)
assert.Contains(t, err.Error(), tt.expectedError)
} else {
err := AdminListTaskList(cliCtx)
if tt.expectedError == "" {
assert.NoError(t, err)
} else {
assert.ErrorContains(t, err, tt.expectedError)
}
})
}
}

// Helper function to set up the CLI context
func setTasklistMock(app *cli.App) *cli.Context {
set := flag.NewFlagSet("test", 0)
set.String(FlagDomain, "test-domain", "Domain flag")
set.String(FlagTaskList, "test-tasklist", "TaskList flag")
set.String(FlagTaskListType, "decision", "TaskListType flag")
c := cli.NewContext(app, set, nil)
return c
func newTaskListCLIContext(t *testing.T, app *cli.App) *cli.Context {
return clitest.NewCLIContext(
t,
app,
clitest.StringArgument(FlagDomain, testDomain),
clitest.StringArgument(FlagTaskList, testTaskList),
clitest.StringArgument(FlagTaskListType, testTaskListType),
)
}

0 comments on commit 12729cb

Please sign in to comment.