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 coverage in cli/isolation_groups #6395

Merged
merged 1 commit into from
Oct 23, 2024
Merged
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
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)
}
})
}
}
Loading