Skip to content

Commit

Permalink
Add coverage in cli/isolation_groups
Browse files Browse the repository at this point in the history
  • Loading branch information
timl3136 committed Oct 23, 2024
1 parent 3c14611 commit 81a0480
Showing 1 changed file with 158 additions and 0 deletions.
158 changes: 158 additions & 0 deletions tools/cli/isolation_groups_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,15 @@ package cli

import (
"errors"
"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/common/types"
)

Expand Down Expand Up @@ -189,3 +194,156 @@ zone-4 Unknown state: 5
})
}
}

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

// Table of test cases
tests := []struct {
name string
setupMocks func(*admin.MockClient)
expectedError string
flagFormat string
mockDepsError error
mockContextError error
}{
{
name: "Success with JSON format",
setupMocks: func(client *admin.MockClient) {
expectedResponse := &types.GetGlobalIsolationGroupsResponse{
IsolationGroups: types.IsolationGroupConfiguration{
"zone-1": {
Name: "zone-1",
State: types.IsolationGroupStateHealthy,
},
"zone-2": {
Name: "zone-2",
State: types.IsolationGroupStateDrained,
},
},
}
client.EXPECT().
GetGlobalIsolationGroups(gomock.Any(), gomock.Any()).
Return(expectedResponse, nil).
Times(1)
},
expectedError: "",
flagFormat: "json",
},
{
name: "Failed to get global isolation groups",
setupMocks: func(client *admin.MockClient) {
client.EXPECT().
GetGlobalIsolationGroups(gomock.Any(), gomock.Any()).
Return(nil, fmt.Errorf("failed to get isolation-groups")).
Times(1)
},
expectedError: "failed to get isolation-groups",
flagFormat: "json",
},
}

// Loop through test cases
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Mock the admin client
adminClient := admin.NewMockClient(mockCtrl)

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

// Create mock app with clientFactoryMock, including any deps errors
app := NewCliApp(&clientFactoryMock{
serverAdminClient: adminClient,
})

// Create CLI context with flags
set := flag.NewFlagSet("test", 0)
set.String(FlagFormat, tt.flagFormat, "Format flag")
c := cli.NewContext(app, set, nil)

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

// Check the expected outcome
if tt.expectedError != "" {
assert.Error(t, err)
assert.Contains(t, err.Error(), tt.expectedError)
} else {
assert.NoError(t, err)
}
})
}
}

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

// Define table-driven tests
tests := []struct {
name string
setupMocks func(*admin.MockClient)
expectedError string
flagDomain string
removeAllDrains bool
mockDepsError error
mockContextError error
validationError error
parseConfigError error
}{
{
name: "Success",
setupMocks: func(client *admin.MockClient) {
client.EXPECT().
UpdateGlobalIsolationGroups(gomock.Any(), gomock.Any()).
Return(&types.UpdateGlobalIsolationGroupsResponse{}, nil).
Times(1)
},
expectedError: "",
flagDomain: "test-domain",
removeAllDrains: true,
},
{
name: "parse failure",
setupMocks: func(client *admin.MockClient) {
},
expectedError: "invalid args:",
flagDomain: "test-domain",
removeAllDrains: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Mock the admin client
adminClient := admin.NewMockClient(mockCtrl)

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

// Create mock app with clientFactoryMock, including any deps errors
app := NewCliApp(&clientFactoryMock{
serverAdminClient: adminClient,
})

// Set up CLI context with flags
set := flag.NewFlagSet("test", 0)
set.String(FlagDomain, tt.flagDomain, "Domain flag")
set.Bool(FlagIsolationGroupsRemoveAllDrains, tt.removeAllDrains, "RemoveAllDrains flag")
c := cli.NewContext(app, set, nil)

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

// Check the expected outcome
if tt.expectedError != "" {
assert.Error(t, err)
assert.Contains(t, err.Error(), tt.expectedError)
} else {
assert.NoError(t, err)
}
})
}
}

0 comments on commit 81a0480

Please sign in to comment.