test: replace hand-written mocks with gomock (#63)
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
package internal_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
@ -8,29 +9,28 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/golang/mock/gomock"
|
||||
"github.com/influxdata/influx-cli/v2/internal"
|
||||
"github.com/influxdata/influx-cli/v2/internal/api"
|
||||
"github.com/influxdata/influx-cli/v2/internal/config"
|
||||
"github.com/influxdata/influx-cli/v2/internal/duration"
|
||||
"github.com/influxdata/influx-cli/v2/internal/mock"
|
||||
"github.com/stretchr/testify/assert"
|
||||
tmock "github.com/stretchr/testify/mock"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func Test_SetupConfigNameCollision(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
client := &mock.SetupApi{
|
||||
GetSetupExecuteFn: func(api.ApiGetSetupRequest) (api.InlineResponse200, error) {
|
||||
return api.InlineResponse200{Allowed: api.PtrBool(true)}, nil
|
||||
},
|
||||
}
|
||||
ctrl := gomock.NewController(t)
|
||||
client := mock.NewMockSetupApi(ctrl)
|
||||
client.EXPECT().GetSetup(gomock.Any()).Return(api.ApiGetSetupRequest{ApiService: client})
|
||||
client.EXPECT().GetSetupExecute(gomock.Any()).Return(api.InlineResponse200{Allowed: api.PtrBool(true)}, nil)
|
||||
|
||||
cfg := "foo"
|
||||
configSvc := &mock.ConfigService{
|
||||
ListConfigsFn: func() (config.Configs, error) {
|
||||
return map[string]config.Config{cfg: {}}, nil
|
||||
},
|
||||
}
|
||||
configSvc := mock.NewMockConfigService(ctrl)
|
||||
configSvc.EXPECT().ListConfigs().Return(map[string]config.Config{cfg: {}}, nil)
|
||||
cli := &internal.CLI{ConfigService: configSvc}
|
||||
|
||||
err := cli.Setup(context.Background(), client, &internal.SetupParams{ConfigName: cfg})
|
||||
@ -42,17 +42,13 @@ func Test_SetupConfigNameCollision(t *testing.T) {
|
||||
func Test_SetupConfigNameRequired(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
client := &mock.SetupApi{
|
||||
GetSetupExecuteFn: func(api.ApiGetSetupRequest) (api.InlineResponse200, error) {
|
||||
return api.InlineResponse200{Allowed: api.PtrBool(true)}, nil
|
||||
},
|
||||
}
|
||||
ctrl := gomock.NewController(t)
|
||||
client := mock.NewMockSetupApi(ctrl)
|
||||
client.EXPECT().GetSetup(gomock.Any()).Return(api.ApiGetSetupRequest{ApiService: client})
|
||||
client.EXPECT().GetSetupExecute(gomock.Any()).Return(api.InlineResponse200{Allowed: api.PtrBool(true)}, nil)
|
||||
|
||||
configSvc := &mock.ConfigService{
|
||||
ListConfigsFn: func() (config.Configs, error) {
|
||||
return map[string]config.Config{"foo": {}}, nil
|
||||
},
|
||||
}
|
||||
configSvc := mock.NewMockConfigService(ctrl)
|
||||
configSvc.EXPECT().ListConfigs().Return(map[string]config.Config{"foo": {}}, nil)
|
||||
cli := &internal.CLI{ConfigService: configSvc}
|
||||
|
||||
err := cli.Setup(context.Background(), client, &internal.SetupParams{})
|
||||
@ -62,18 +58,12 @@ func Test_SetupConfigNameRequired(t *testing.T) {
|
||||
|
||||
func Test_SetupAlreadySetup(t *testing.T) {
|
||||
t.Parallel()
|
||||
ctrl := gomock.NewController(t)
|
||||
client := mock.NewMockSetupApi(ctrl)
|
||||
client.EXPECT().GetSetup(gomock.Any()).Return(api.ApiGetSetupRequest{ApiService: client})
|
||||
client.EXPECT().GetSetupExecute(gomock.Any()).Return(api.InlineResponse200{Allowed: api.PtrBool(false)}, nil)
|
||||
|
||||
client := &mock.SetupApi{
|
||||
GetSetupExecuteFn: func(api.ApiGetSetupRequest) (api.InlineResponse200, error) {
|
||||
return api.InlineResponse200{Allowed: api.PtrBool(false)}, nil
|
||||
},
|
||||
}
|
||||
|
||||
configSvc := &mock.ConfigService{
|
||||
ListConfigsFn: func() (config.Configs, error) {
|
||||
return map[string]config.Config{"foo": {}}, nil
|
||||
},
|
||||
}
|
||||
configSvc := mock.NewMockConfigService(ctrl)
|
||||
cli := &internal.CLI{ConfigService: configSvc}
|
||||
|
||||
err := cli.Setup(context.Background(), client, &internal.SetupParams{})
|
||||
@ -85,17 +75,12 @@ func Test_SetupCheckFailed(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
e := "oh no"
|
||||
client := &mock.SetupApi{
|
||||
GetSetupExecuteFn: func(api.ApiGetSetupRequest) (api.InlineResponse200, error) {
|
||||
return api.InlineResponse200{}, errors.New(e)
|
||||
},
|
||||
}
|
||||
ctrl := gomock.NewController(t)
|
||||
client := mock.NewMockSetupApi(ctrl)
|
||||
client.EXPECT().GetSetup(gomock.Any()).Return(api.ApiGetSetupRequest{ApiService: client})
|
||||
client.EXPECT().GetSetupExecute(gomock.Any()).Return(api.InlineResponse200{}, errors.New(e))
|
||||
|
||||
configSvc := &mock.ConfigService{
|
||||
ListConfigsFn: func() (config.Configs, error) {
|
||||
return nil, nil
|
||||
},
|
||||
}
|
||||
configSvc := mock.NewMockConfigService(ctrl)
|
||||
cli := &internal.CLI{ConfigService: configSvc}
|
||||
|
||||
err := cli.Setup(context.Background(), client, &internal.SetupParams{})
|
||||
@ -123,40 +108,42 @@ func Test_SetupSuccessNoninteractive(t *testing.T) {
|
||||
User: &api.UserResponse{Name: params.Username},
|
||||
Bucket: &api.Bucket{Name: params.Bucket},
|
||||
}
|
||||
client := &mock.SetupApi{
|
||||
GetSetupExecuteFn: func(api.ApiGetSetupRequest) (api.InlineResponse200, error) {
|
||||
return api.InlineResponse200{Allowed: api.PtrBool(true)}, nil
|
||||
},
|
||||
PostSetupExecuteFn: func(req api.ApiPostSetupRequest) (api.OnboardingResponse, error) {
|
||||
body := req.GetOnboardingRequest()
|
||||
require.Equal(t, params.Username, body.Username)
|
||||
require.Equal(t, params.Password, *body.Password)
|
||||
require.Equal(t, params.AuthToken, *body.Token)
|
||||
require.Equal(t, params.Org, body.Org)
|
||||
require.Equal(t, params.Bucket, body.Bucket)
|
||||
require.Equal(t, retentionSecs, *body.RetentionPeriodSeconds)
|
||||
return resp, nil
|
||||
},
|
||||
}
|
||||
|
||||
ctrl := gomock.NewController(t)
|
||||
client := mock.NewMockSetupApi(ctrl)
|
||||
client.EXPECT().GetSetup(gomock.Any()).Return(api.ApiGetSetupRequest{ApiService: client})
|
||||
client.EXPECT().GetSetupExecute(gomock.Any()).Return(api.InlineResponse200{Allowed: api.PtrBool(true)}, nil)
|
||||
client.EXPECT().PostSetup(gomock.Any()).Return(api.ApiPostSetupRequest{ApiService: client})
|
||||
client.EXPECT().PostSetupExecute(tmock.MatchedBy(func(in api.ApiPostSetupRequest) bool {
|
||||
body := in.GetOnboardingRequest()
|
||||
return assert.NotNil(t, body) &&
|
||||
assert.Equal(t, params.Username, body.Username) &&
|
||||
assert.Equal(t, params.Password, *body.Password) &&
|
||||
assert.Equal(t, params.AuthToken, *body.Token) &&
|
||||
assert.Equal(t, params.Org, body.Org) &&
|
||||
assert.Equal(t, params.Bucket, body.Bucket) &&
|
||||
assert.Equal(t, retentionSecs, *body.RetentionPeriodSeconds)
|
||||
})).Return(resp, nil)
|
||||
|
||||
host := "fake-host"
|
||||
configSvc := &mock.ConfigService{
|
||||
ListConfigsFn: func() (config.Configs, error) {
|
||||
return nil, nil
|
||||
},
|
||||
CreateConfigFn: func(cfg config.Config) (config.Config, error) {
|
||||
require.Equal(t, params.ConfigName, cfg.Name)
|
||||
require.Equal(t, params.AuthToken, cfg.Token)
|
||||
require.Equal(t, host, cfg.Host)
|
||||
require.Equal(t, params.Org, cfg.Org)
|
||||
return cfg, nil
|
||||
},
|
||||
}
|
||||
stdio := mock.NewMockStdio(nil, true)
|
||||
configSvc := mock.NewMockConfigService(ctrl)
|
||||
configSvc.EXPECT().ListConfigs().Return(nil, nil)
|
||||
configSvc.EXPECT().CreateConfig(tmock.MatchedBy(func(in config.Config) bool {
|
||||
return assert.Equal(t, params.ConfigName, in.Name) &&
|
||||
assert.Equal(t, params.AuthToken, in.Token) &&
|
||||
assert.Equal(t, host, in.Host) &&
|
||||
assert.Equal(t, params.Org, in.Org)
|
||||
})).DoAndReturn(func(in config.Config) (config.Config, error) {
|
||||
return in, nil
|
||||
})
|
||||
|
||||
stdio := mock.NewMockStdIO(ctrl)
|
||||
bytesWritten := bytes.Buffer{}
|
||||
stdio.EXPECT().Write(gomock.Any()).DoAndReturn(bytesWritten.Write).AnyTimes()
|
||||
cli := &internal.CLI{ConfigService: configSvc, ActiveConfig: config.Config{Host: host}, StdIO: stdio}
|
||||
require.NoError(t, cli.Setup(context.Background(), client, ¶ms))
|
||||
|
||||
outLines := strings.Split(strings.TrimSpace(stdio.Stdout()), "\n")
|
||||
outLines := strings.Split(strings.TrimSpace(bytesWritten.String()), "\n")
|
||||
require.Len(t, outLines, 2)
|
||||
header, data := outLines[0], outLines[1]
|
||||
require.Regexp(t, "User\\s+Organization\\s+Bucket", header)
|
||||
@ -180,47 +167,49 @@ func Test_SetupSuccessInteractive(t *testing.T) {
|
||||
User: &api.UserResponse{Name: username},
|
||||
Bucket: &api.Bucket{Name: bucket},
|
||||
}
|
||||
client := &mock.SetupApi{
|
||||
GetSetupExecuteFn: func(api.ApiGetSetupRequest) (api.InlineResponse200, error) {
|
||||
return api.InlineResponse200{Allowed: api.PtrBool(true)}, nil
|
||||
},
|
||||
PostSetupExecuteFn: func(req api.ApiPostSetupRequest) (api.OnboardingResponse, error) {
|
||||
body := req.GetOnboardingRequest()
|
||||
require.Equal(t, username, body.Username)
|
||||
require.Equal(t, password, *body.Password)
|
||||
require.Nil(t, body.Token)
|
||||
require.Equal(t, org, body.Org)
|
||||
require.Equal(t, bucket, body.Bucket)
|
||||
require.Equal(t, retentionSecs, *body.RetentionPeriodSeconds)
|
||||
return resp, nil
|
||||
},
|
||||
}
|
||||
ctrl := gomock.NewController(t)
|
||||
client := mock.NewMockSetupApi(ctrl)
|
||||
client.EXPECT().GetSetup(gomock.Any()).Return(api.ApiGetSetupRequest{ApiService: client})
|
||||
client.EXPECT().GetSetupExecute(gomock.Any()).Return(api.InlineResponse200{Allowed: api.PtrBool(true)}, nil)
|
||||
client.EXPECT().PostSetup(gomock.Any()).Return(api.ApiPostSetupRequest{ApiService: client})
|
||||
client.EXPECT().PostSetupExecute(tmock.MatchedBy(func(in api.ApiPostSetupRequest) bool {
|
||||
body := in.GetOnboardingRequest()
|
||||
return assert.NotNil(t, body) &&
|
||||
assert.Equal(t, username, body.Username) &&
|
||||
assert.Equal(t, password, *body.Password) &&
|
||||
assert.Nil(t, body.Token) &&
|
||||
assert.Equal(t, org, body.Org) &&
|
||||
assert.Equal(t, bucket, body.Bucket) &&
|
||||
assert.Equal(t, retentionSecs, *body.RetentionPeriodSeconds)
|
||||
})).Return(resp, nil)
|
||||
|
||||
host := "fake-host"
|
||||
configSvc := &mock.ConfigService{
|
||||
ListConfigsFn: func() (config.Configs, error) {
|
||||
return nil, nil
|
||||
},
|
||||
CreateConfigFn: func(cfg config.Config) (config.Config, error) {
|
||||
require.Equal(t, config.DefaultConfig.Name, cfg.Name)
|
||||
require.Equal(t, token, cfg.Token)
|
||||
require.Equal(t, host, cfg.Host)
|
||||
require.Equal(t, org, cfg.Org)
|
||||
return cfg, nil
|
||||
},
|
||||
}
|
||||
stdio := mock.NewMockStdio(map[string]string{
|
||||
"Please type your primary username": username,
|
||||
"Please type your password": password,
|
||||
"Please type your password again": password,
|
||||
"Please type your primary organization name": org,
|
||||
"Please type your primary bucket name": bucket,
|
||||
"Please type your retention period in hours, or 0 for infinite": strconv.Itoa(retentionHrs),
|
||||
}, true)
|
||||
configSvc := mock.NewMockConfigService(ctrl)
|
||||
configSvc.EXPECT().ListConfigs().Return(nil, nil)
|
||||
configSvc.EXPECT().CreateConfig(tmock.MatchedBy(func(in config.Config) bool {
|
||||
return assert.Equal(t, config.DefaultConfig.Name, in.Name) &&
|
||||
assert.Equal(t, token, in.Token) &&
|
||||
assert.Equal(t, host, in.Host) &&
|
||||
assert.Equal(t, org, in.Org)
|
||||
})).DoAndReturn(func(in config.Config) (config.Config, error) {
|
||||
return in, nil
|
||||
})
|
||||
|
||||
stdio := mock.NewMockStdIO(ctrl)
|
||||
bytesWritten := bytes.Buffer{}
|
||||
stdio.EXPECT().Write(gomock.Any()).DoAndReturn(bytesWritten.Write).AnyTimes()
|
||||
stdio.EXPECT().Banner(gomock.Any())
|
||||
stdio.EXPECT().GetStringInput(gomock.Eq("Please type your primary username"), gomock.Any()).Return(username, nil)
|
||||
stdio.EXPECT().GetPassword(gomock.Eq("Please type your password"), gomock.Any()).Return(password, nil)
|
||||
stdio.EXPECT().GetPassword(gomock.Eq("Please type your password again"), gomock.Any()).Return(password, nil)
|
||||
stdio.EXPECT().GetStringInput(gomock.Eq("Please type your primary organization name"), gomock.Any()).Return(org, nil)
|
||||
stdio.EXPECT().GetStringInput("Please type your primary bucket name", gomock.Any()).Return(bucket, nil)
|
||||
stdio.EXPECT().GetStringInput("Please type your retention period in hours, or 0 for infinite", gomock.Any()).Return(strconv.Itoa(retentionHrs), nil)
|
||||
stdio.EXPECT().GetConfirm(gomock.Any()).Return(true)
|
||||
cli := &internal.CLI{ConfigService: configSvc, ActiveConfig: config.Config{Host: host}, StdIO: stdio}
|
||||
require.NoError(t, cli.Setup(context.Background(), client, &internal.SetupParams{}))
|
||||
|
||||
outLines := strings.Split(strings.TrimSpace(stdio.Stdout()), "\n")
|
||||
outLines := strings.Split(strings.TrimSpace(bytesWritten.String()), "\n")
|
||||
require.Len(t, outLines, 2)
|
||||
header, data := outLines[0], outLines[1]
|
||||
require.Regexp(t, "User\\s+Organization\\s+Bucket", header)
|
||||
@ -240,19 +229,17 @@ func Test_SetupPasswordParamToShort(t *testing.T) {
|
||||
Retention: fmt.Sprintf("%ds", retentionSecs),
|
||||
Force: false,
|
||||
}
|
||||
client := &mock.SetupApi{
|
||||
GetSetupExecuteFn: func(api.ApiGetSetupRequest) (api.InlineResponse200, error) {
|
||||
return api.InlineResponse200{Allowed: api.PtrBool(true)}, nil
|
||||
},
|
||||
}
|
||||
|
||||
ctrl := gomock.NewController(t)
|
||||
client := mock.NewMockSetupApi(ctrl)
|
||||
client.EXPECT().GetSetup(gomock.Any()).Return(api.ApiGetSetupRequest{ApiService: client})
|
||||
client.EXPECT().GetSetupExecute(gomock.Any()).Return(api.InlineResponse200{Allowed: api.PtrBool(true)}, nil)
|
||||
|
||||
host := "fake-host"
|
||||
configSvc := &mock.ConfigService{
|
||||
ListConfigsFn: func() (config.Configs, error) {
|
||||
return nil, nil
|
||||
},
|
||||
}
|
||||
stdio := mock.NewMockStdio(nil, false)
|
||||
configSvc := mock.NewMockConfigService(ctrl)
|
||||
configSvc.EXPECT().ListConfigs().Return(nil, nil)
|
||||
|
||||
stdio := mock.NewMockStdIO(ctrl)
|
||||
cli := &internal.CLI{ConfigService: configSvc, ActiveConfig: config.Config{Host: host}, StdIO: stdio}
|
||||
err := cli.Setup(context.Background(), client, ¶ms)
|
||||
require.Equal(t, internal.ErrPasswordIsTooShort, err)
|
||||
@ -271,19 +258,20 @@ func Test_SetupCancelAtConfirmation(t *testing.T) {
|
||||
Retention: fmt.Sprintf("%ds", retentionSecs),
|
||||
Force: false,
|
||||
}
|
||||
client := &mock.SetupApi{
|
||||
GetSetupExecuteFn: func(api.ApiGetSetupRequest) (api.InlineResponse200, error) {
|
||||
return api.InlineResponse200{Allowed: api.PtrBool(true)}, nil
|
||||
},
|
||||
}
|
||||
|
||||
ctrl := gomock.NewController(t)
|
||||
client := mock.NewMockSetupApi(ctrl)
|
||||
client.EXPECT().GetSetup(gomock.Any()).Return(api.ApiGetSetupRequest{ApiService: client})
|
||||
client.EXPECT().GetSetupExecute(gomock.Any()).Return(api.InlineResponse200{Allowed: api.PtrBool(true)}, nil)
|
||||
|
||||
host := "fake-host"
|
||||
configSvc := &mock.ConfigService{
|
||||
ListConfigsFn: func() (config.Configs, error) {
|
||||
return nil, nil
|
||||
},
|
||||
}
|
||||
stdio := mock.NewMockStdio(nil, false)
|
||||
configSvc := mock.NewMockConfigService(ctrl)
|
||||
configSvc.EXPECT().ListConfigs().Return(nil, nil)
|
||||
|
||||
stdio := mock.NewMockStdIO(ctrl)
|
||||
stdio.EXPECT().Banner(gomock.Any())
|
||||
stdio.EXPECT().GetConfirm(gomock.Any()).Return(false)
|
||||
|
||||
cli := &internal.CLI{ConfigService: configSvc, ActiveConfig: config.Config{Host: host}, StdIO: stdio}
|
||||
err := cli.Setup(context.Background(), client, ¶ms)
|
||||
require.Equal(t, internal.ErrSetupCanceled, err)
|
||||
|
Reference in New Issue
Block a user