Skip to content

Commit

Permalink
Move adminClientMock methods to single file (#2583)
Browse files Browse the repository at this point in the history
  • Loading branch information
reivaj05 authored Jan 23, 2023
1 parent 0595ec1 commit ccaaa21
Show file tree
Hide file tree
Showing 24 changed files with 444 additions and 515 deletions.
2 changes: 1 addition & 1 deletion restapi/admin_arns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import (

func TestArnsList(t *testing.T) {
assert := asrt.New(t)
adminClient := adminClientMock{}
adminClient := AdminClientMock{}
// Test-1 : getArns() returns proper arn list
minioServerInfoMock = func(ctx context.Context) (madmin.InfoMessage, error) {
return madmin.InfoMessage{
Expand Down
387 changes: 387 additions & 0 deletions restapi/admin_client_mock.go

Large diffs are not rendered by default.

38 changes: 5 additions & 33 deletions restapi/admin_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,37 +40,9 @@ const (
PostgresQueueLimit = "queue_limit"
)

// assigning mock at runtime instead of compile time
var minioHelpConfigKVMock func(subSys, key string, envOnly bool) (madmin.Help, error)

var (
minioGetConfigKVMock func(key string) ([]byte, error)
minioSetConfigKVMock func(kv string) (restart bool, err error)
minioDelConfigKVMock func(name string) (err error)
)

// mock function helpConfigKV()
func (ac adminClientMock) helpConfigKV(ctx context.Context, subSys, key string, envOnly bool) (madmin.Help, error) {
return minioHelpConfigKVMock(subSys, key, envOnly)
}

// mock function getConfigKV()
func (ac adminClientMock) getConfigKV(ctx context.Context, name string) ([]byte, error) {
return minioGetConfigKVMock(name)
}

// mock function setConfigKV()
func (ac adminClientMock) setConfigKV(ctx context.Context, kv string) (restart bool, err error) {
return minioSetConfigKVMock(kv)
}

func (ac adminClientMock) delConfigKV(ctx context.Context, name string) (err error) {
return minioDelConfigKVMock(name)
}

func TestListConfig(t *testing.T) {
assert := assert.New(t)
adminClient := adminClientMock{}
adminClient := AdminClientMock{}
function := "listConfig()"
// Test-1 : listConfig() get list of two configurations and ensure is output correctly
configListMock := []madmin.HelpKV{
Expand Down Expand Up @@ -119,7 +91,7 @@ func TestListConfig(t *testing.T) {

func TestSetConfig(t *testing.T) {
assert := assert.New(t)
adminClient := adminClientMock{}
adminClient := AdminClientMock{}
function := "setConfig()"
// mock function response from setConfig()
minioSetConfigKVMock = func(kv string) (restart bool, err error) {
Expand Down Expand Up @@ -169,7 +141,7 @@ func TestSetConfig(t *testing.T) {

func TestDelConfig(t *testing.T) {
assert := assert.New(t)
adminClient := adminClientMock{}
adminClient := AdminClientMock{}
function := "resetConfig()"
// mock function response from setConfig()
minioDelConfigKVMock = func(name string) (err error) {
Expand Down Expand Up @@ -258,7 +230,7 @@ func Test_buildConfig(t *testing.T) {

func Test_setConfigWithARN(t *testing.T) {
assert := assert.New(t)
client := adminClientMock{}
client := AdminClientMock{}

type args struct {
ctx context.Context
Expand Down Expand Up @@ -369,7 +341,7 @@ func Test_setConfigWithARN(t *testing.T) {
}

func Test_getConfig(t *testing.T) {
client := adminClientMock{}
client := AdminClientMock{}
type args struct {
client MinioAdmin
name string
Expand Down
10 changes: 1 addition & 9 deletions restapi/admin_console_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,9 @@ import (
"github.com/stretchr/testify/assert"
)

// assigning mock at runtime instead of compile time
var minioGetLogsMock func(ctx context.Context, node string, lineCnt int, logKind string) <-chan madmin.LogInfo

// mock function of listPolicies()
func (ac adminClientMock) getLogs(ctx context.Context, node string, lineCnt int, logKind string) <-chan madmin.LogInfo {
return minioGetLogsMock(ctx, node, lineCnt, logKind)
}

func TestAdminConsoleLog(t *testing.T) {
assert := assert.New(t)
adminClient := adminClientMock{}
adminClient := AdminClientMock{}
mockWSConn := mockConn{}
function := "startConsoleLog(ctx, )"
ctx, cancel := context.WithCancel(context.Background())
Expand Down
41 changes: 6 additions & 35 deletions restapi/admin_groups_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,38 +28,9 @@ import (
"github.com/stretchr/testify/assert"
)

// assigning mock at runtime instead of compile time
var minioListGroupsMock func() ([]string, error)

var (
minioUpdateGroupMembersMock func(madmin.GroupAddRemove) error
minioGetGroupDescriptionMock func(group string) (*madmin.GroupDesc, error)
minioSetGroupStatusMock func(group string, status madmin.GroupStatus) error
)

// mock function of listGroups()
func (ac adminClientMock) listGroups(ctx context.Context) ([]string, error) {
return minioListGroupsMock()
}

// mock function of updateGroupMembers()
func (ac adminClientMock) updateGroupMembers(ctx context.Context, req madmin.GroupAddRemove) error {
return minioUpdateGroupMembersMock(req)
}

// mock function of getGroupDescription()
func (ac adminClientMock) getGroupDescription(ctx context.Context, group string) (*madmin.GroupDesc, error) {
return minioGetGroupDescriptionMock(group)
}

// mock function setGroupStatus()
func (ac adminClientMock) setGroupStatus(ctx context.Context, group string, status madmin.GroupStatus) error {
return minioSetGroupStatusMock(group, status)
}

func TestListGroups(t *testing.T) {
assert := assert.New(t)
adminClient := adminClientMock{}
adminClient := AdminClientMock{}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Test-1 : listGroups() Get response from minio client with two Groups and return the same number on listGroups()
Expand Down Expand Up @@ -95,7 +66,7 @@ func TestListGroups(t *testing.T) {

func TestAddGroup(t *testing.T) {
assert := assert.New(t)
adminClient := adminClientMock{}
adminClient := AdminClientMock{}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Test-1 : addGroup() add a new group with two members
Expand All @@ -122,7 +93,7 @@ func TestAddGroup(t *testing.T) {

func TestRemoveGroup(t *testing.T) {
assert := assert.New(t)
adminClient := adminClientMock{}
adminClient := AdminClientMock{}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Test-1 : removeGroup() remove group assume it has no members
Expand All @@ -147,7 +118,7 @@ func TestRemoveGroup(t *testing.T) {

func TestGroupInfo(t *testing.T) {
assert := assert.New(t)
adminClient := adminClientMock{}
adminClient := AdminClientMock{}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Test-1 : groupInfo() get group info
Expand Down Expand Up @@ -184,7 +155,7 @@ func TestGroupInfo(t *testing.T) {

func TestUpdateGroup(t *testing.T) {
assert := assert.New(t)
adminClient := adminClientMock{}
adminClient := AdminClientMock{}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Test-1 : addOrDeleteMembers() update group members add user3 and delete user2
Expand Down Expand Up @@ -280,7 +251,7 @@ func TestUpdateGroup(t *testing.T) {

func TestSetGroupStatus(t *testing.T) {
assert := assert.New(t)
adminClient := adminClientMock{}
adminClient := AdminClientMock{}
function := "setGroupStatus()"
groupName := "acmeGroup"
ctx, cancel := context.WithCancel(context.Background())
Expand Down
12 changes: 1 addition & 11 deletions restapi/admin_heal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,10 @@ import (
"github.com/stretchr/testify/assert"
)

// assigning mock at runtime instead of compile time
var minioHealMock func(ctx context.Context, bucket, prefix string, healOpts madmin.HealOpts, clientToken string,
forceStart, forceStop bool) (healStart madmin.HealStartSuccess, healTaskStatus madmin.HealTaskStatus, err error)

func (ac adminClientMock) heal(ctx context.Context, bucket, prefix string, healOpts madmin.HealOpts, clientToken string,
forceStart, forceStop bool,
) (healStart madmin.HealStartSuccess, healTaskStatus madmin.HealTaskStatus, err error) {
return minioHealMock(ctx, bucket, prefix, healOpts, clientToken, forceStart, forceStop)
}

func TestHeal(t *testing.T) {
assert := assert.New(t)

client := adminClientMock{}
client := AdminClientMock{}
mockWSConn := mockConn{}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand Down
10 changes: 1 addition & 9 deletions restapi/admin_health_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,12 @@ import (
madmin "github.com/minio/madmin-go/v2"
)

// assigning mock at runtime instead of compile time
var minioServerHealthInfoMock func(ctx context.Context, healthDataTypes []madmin.HealthDataType, deadline time.Duration) (interface{}, string, error)

// mock function serverHealthInfo
func (ac adminClientMock) serverHealthInfo(ctx context.Context, healthDataTypes []madmin.HealthDataType, deadline time.Duration) (interface{}, string, error) {
return minioServerHealthInfoMock(ctx, healthDataTypes, deadline)
}

func Test_serverHealthInfo(t *testing.T) {
var testReceiver chan madmin.HealthInfo

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
client := adminClientMock{}
client := AdminClientMock{}
mockWSConn := mockConn{}
deadlineDuration, _ := time.ParseDuration("1h")

Expand Down
21 changes: 2 additions & 19 deletions restapi/admin_idp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,39 +26,22 @@ import (
"github.com/minio/console/models"
"github.com/minio/console/restapi/operations"
"github.com/minio/console/restapi/operations/idp"
"github.com/minio/madmin-go/v2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)

func (ac adminClientMock) addOrUpdateIDPConfig(ctx context.Context, idpType, cfgName, cfgData string, update bool) (restart bool, err error) {
return true, nil
}

func (ac adminClientMock) listIDPConfig(ctx context.Context, idpType string) ([]madmin.IDPListItem, error) {
return []madmin.IDPListItem{{Name: "mock"}}, nil
}

func (ac adminClientMock) deleteIDPConfig(ctx context.Context, idpType, cfgName string) (restart bool, err error) {
return true, nil
}

func (ac adminClientMock) getIDPConfig(ctx context.Context, cfgType, cfgName string) (c madmin.IDPConfig, err error) {
return madmin.IDPConfig{Info: []madmin.IDPCfgInfo{{Key: "mock", Value: "mock"}}}, nil
}

type IDPTestSuite struct {
suite.Suite
assert *assert.Assertions
currentServer string
isServerSet bool
server *httptest.Server
adminClient adminClientMock
adminClient AdminClientMock
}

func (suite *IDPTestSuite) SetupSuite() {
suite.assert = assert.New(suite.T())
suite.adminClient = adminClientMock{}
suite.adminClient = AdminClientMock{}
minioServiceRestartMock = func(ctx context.Context) error {
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions restapi/admin_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ type AdminInfoTestSuite struct {
isServerSet bool
isPrometheusRequest bool
server *httptest.Server
adminClient adminClientMock
adminClient AdminClientMock
}

func (suite *AdminInfoTestSuite) SetupSuite() {
suite.assert = assert.New(suite.T())
suite.adminClient = adminClientMock{}
suite.adminClient = AdminClientMock{}
minioServerInfoMock = func(ctx context.Context) (madmin.InfoMessage, error) {
return madmin.InfoMessage{
Servers: []madmin.ServerProperties{{
Expand Down
89 changes: 2 additions & 87 deletions restapi/admin_kms_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,107 +26,22 @@ import (
"github.com/minio/console/models"
"github.com/minio/console/restapi/operations"
kmsAPI "github.com/minio/console/restapi/operations/k_m_s"
"github.com/minio/madmin-go/v2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)

func (ac adminClientMock) kmsStatus(ctx context.Context) (madmin.KMSStatus, error) {
return madmin.KMSStatus{Name: "name", DefaultKeyID: "key", Endpoints: map[string]madmin.ItemState{"localhost": madmin.ItemState("online")}}, nil
}

func (ac adminClientMock) kmsAPIs(ctx context.Context) ([]madmin.KMSAPI, error) {
return []madmin.KMSAPI{{Method: "GET", Path: "/mock"}}, nil
}

func (ac adminClientMock) kmsMetrics(ctx context.Context) (*madmin.KMSMetrics, error) {
return &madmin.KMSMetrics{}, nil
}

func (ac adminClientMock) kmsVersion(ctx context.Context) (*madmin.KMSVersion, error) {
return &madmin.KMSVersion{Version: "test-version"}, nil
}

func (ac adminClientMock) createKey(ctx context.Context, key string) error {
return nil
}

func (ac adminClientMock) importKey(ctx context.Context, key string, content []byte) error {
return nil
}

func (ac adminClientMock) listKeys(ctx context.Context, pattern string) ([]madmin.KMSKeyInfo, error) {
return []madmin.KMSKeyInfo{{
Name: "name",
CreatedBy: "by",
}}, nil
}

func (ac adminClientMock) keyStatus(ctx context.Context, key string) (*madmin.KMSKeyStatus, error) {
return &madmin.KMSKeyStatus{KeyID: "key"}, nil
}

func (ac adminClientMock) deleteKey(ctx context.Context, key string) error {
return nil
}

func (ac adminClientMock) setKMSPolicy(ctx context.Context, policy string, content []byte) error {
return nil
}

func (ac adminClientMock) assignPolicy(ctx context.Context, policy string, content []byte) error {
return nil
}

func (ac adminClientMock) describePolicy(ctx context.Context, policy string) (*madmin.KMSDescribePolicy, error) {
return &madmin.KMSDescribePolicy{Name: "name"}, nil
}

func (ac adminClientMock) getKMSPolicy(ctx context.Context, policy string) (*madmin.KMSPolicy, error) {
return &madmin.KMSPolicy{Allow: []string{""}, Deny: []string{""}}, nil
}

func (ac adminClientMock) listKMSPolicies(ctx context.Context, pattern string) ([]madmin.KMSPolicyInfo, error) {
return []madmin.KMSPolicyInfo{{
Name: "name",
CreatedBy: "by",
}}, nil
}

func (ac adminClientMock) deletePolicy(ctx context.Context, policy string) error {
return nil
}

func (ac adminClientMock) describeIdentity(ctx context.Context, identity string) (*madmin.KMSDescribeIdentity, error) {
return &madmin.KMSDescribeIdentity{}, nil
}

func (ac adminClientMock) describeSelfIdentity(ctx context.Context) (*madmin.KMSDescribeSelfIdentity, error) {
return &madmin.KMSDescribeSelfIdentity{
Policy: &madmin.KMSPolicy{Allow: []string{}, Deny: []string{}},
}, nil
}

func (ac adminClientMock) deleteIdentity(ctx context.Context, identity string) error {
return nil
}

func (ac adminClientMock) listIdentities(ctx context.Context, pattern string) ([]madmin.KMSIdentityInfo, error) {
return []madmin.KMSIdentityInfo{{Identity: "identity"}}, nil
}

type KMSTestSuite struct {
suite.Suite
assert *assert.Assertions
currentServer string
isServerSet bool
server *httptest.Server
adminClient adminClientMock
adminClient AdminClientMock
}

func (suite *KMSTestSuite) SetupSuite() {
suite.assert = assert.New(suite.T())
suite.adminClient = adminClientMock{}
suite.adminClient = AdminClientMock{}
}

func (suite *KMSTestSuite) SetupTest() {
Expand Down
Loading

0 comments on commit ccaaa21

Please sign in to comment.