Skip to content

Commit

Permalink
feat(cicd): add linters and address them
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitry Kropachev committed Jun 6, 2023
1 parent 4921a68 commit 40cabe6
Show file tree
Hide file tree
Showing 19 changed files with 80 additions and 69 deletions.
8 changes: 8 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ linters:
- staticcheck
- lll
- errorlint
- gocritic
- gosimple
- predeclared
- revive
- thelper
- tparallel
- typecheck
- unused
run:
deadline: 10m
modules-download-mode: readonly
Expand Down
2 changes: 1 addition & 1 deletion cmd/gemini/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func (cb createBuilder) ToCql() (stmt string, names []string) {
return cb.stmt, nil
}

func run(cmd *cobra.Command, args []string) error {
func run(_ *cobra.Command, _ []string) error {
logger := createLogger(level)
globalStatus := status.NewGlobalStatus(1000)
defer utils.IgnoreError(logger.Sync)
Expand Down
7 changes: 4 additions & 3 deletions pkg/auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,12 @@ func TestSetAuthenticator(t *testing.T) {
test.input.username,
test.input.password,
)
if test.err == "" && err != nil {
switch {
case test.err == "" && err != nil:
t.Fatalf("Returned unexpected error '%s'", err.Error())
} else if test.err != "" && err == nil {
case test.err != "" && err == nil:
t.Fatalf("Expected error '%s' but none was returned", test.err)
} else if test.err != "" && err != nil && err.Error() != test.err {
case test.err != "" && err != nil && err.Error() != test.err:
t.Fatalf("Returned error '%s' doesn't match expected error '%s'", err.Error(), test.err)
}
if diff := cmp.Diff(test.want, authenticator); diff != "" {
Expand Down
23 changes: 11 additions & 12 deletions pkg/coltypes/bag.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,18 @@ func (ct *BagType) CQLPretty(query string, value []interface{}) (string, int) {
if len(value) == 0 {
return query, 0
}
switch reflect.TypeOf(value[0]).Kind() {
case reflect.Slice:
s := reflect.ValueOf(value[0])
vv := "{"
vv += strings.Repeat("?,", s.Len())
vv = strings.TrimRight(vv, ",")
vv += "}"
for i := 0; i < s.Len(); i++ {
vv, _ = ct.Type.CQLPretty(vv, []interface{}{s.Index(i).Interface()})
}
return strings.Replace(query, "?", vv, 1), 1
if reflect.TypeOf(value[0]).Kind() != reflect.Slice {
panic(fmt.Sprintf("set cql pretty, unknown type %v", ct))
}
panic(fmt.Sprintf("set cql pretty, unknown type %v", ct))
s := reflect.ValueOf(value[0])
vv := "{"
vv += strings.Repeat("?,", s.Len())
vv = strings.TrimRight(vv, ",")
vv += "}"
for i := 0; i < s.Len(); i++ {
vv, _ = ct.Type.CQLPretty(vv, []interface{}{s.Index(i).Interface()})
}
return strings.Replace(query, "?", vv, 1), 1
}

func (ct *BagType) GenValue(r *rand.Rand, p *typedef.PartitionRangeConfig) []interface{} {
Expand Down
2 changes: 1 addition & 1 deletion pkg/coltypes/simple_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func (st SimpleType) GenValue(r *rand.Rand, p *typedef.PartitionRangeConfig) []i
case TYPE_FLOAT:
val = r.Float32()
case TYPE_INET:
val = net.ParseIP(utils.RandIpV4Address(r, r.Intn(255), 2)).String()
val = net.ParseIP(utils.RandIPV4Address(r, r.Intn(255), 2)).String()
case TYPE_INT:
val = r.Int31()
case TYPE_SMALLINT:
Expand Down
26 changes: 13 additions & 13 deletions pkg/coltypes/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/scylladb/gemini/pkg/utils"
)

// nolint:revive
const (
TYPE_ASCII = SimpleType("ascii")
TYPE_BIGINT = SimpleType("bigint")
Expand Down Expand Up @@ -121,19 +122,18 @@ func (mt *MapType) CQLHolder() string {
}

func (mt *MapType) CQLPretty(query string, value []interface{}) (string, int) {
switch reflect.TypeOf(value[0]).Kind() {
case reflect.Map:
s := reflect.ValueOf(value[0]).MapRange()
vv := "{"
for s.Next() {
vv += fmt.Sprintf("%v:?,", s.Key().Interface())
vv, _ = mt.ValueType.CQLPretty(vv, []interface{}{s.Value().Interface()})
}
vv = strings.TrimSuffix(vv, ",")
vv += "}"
return strings.Replace(query, "?", vv, 1), 1
if reflect.TypeOf(value[0]).Kind() != reflect.Map {
panic(fmt.Sprintf("map cql pretty, unknown type %v", mt))
}
panic(fmt.Sprintf("map cql pretty, unknown type %v", mt))
s := reflect.ValueOf(value[0]).MapRange()
vv := "{"
for s.Next() {
vv += fmt.Sprintf("%v:?,", s.Key().Interface())
vv, _ = mt.ValueType.CQLPretty(vv, []interface{}{s.Value().Interface()})
}
vv = strings.TrimSuffix(vv, ",")
vv += "}"
return strings.Replace(query, "?", vv, 1), 1
}

func (mt *MapType) GenValue(r *rand.Rand, p *typedef.PartitionRangeConfig) []interface{} {
Expand Down Expand Up @@ -180,7 +180,7 @@ func (ct *CounterType) CQLPretty(query string, value []interface{}) (string, int
return strings.Replace(query, "?", fmt.Sprintf("%d", value[0]), 1), 1
}

func (ct *CounterType) GenValue(r *rand.Rand, p *typedef.PartitionRangeConfig) []interface{} {
func (ct *CounterType) GenValue(r *rand.Rand, _ *typedef.PartitionRangeConfig) []interface{} {
if utils.UnderTest {
return []interface{}{r.Int63()}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/generators/column_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func GenColumnType(numColumns int, sc *typedef.SchemaConfig) typedef.Type {
}
}

func GenSimpleType(sc *typedef.SchemaConfig) coltypes.SimpleType {
func GenSimpleType(_ *typedef.SchemaConfig) coltypes.SimpleType {
return coltypes.AllTypes[rand.Intn(len(coltypes.AllTypes))]
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/generators/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func (g *Generator) start() {
}
grp.Go(func() error {
g.logger.Info("starting partition key generation loop")
routingKeyCreator := &routingkey.RoutingKeyCreator{}
routingKeyCreator := &routingkey.Creator{}
r := rand.New(rand.NewSource(g.seed))
var (
cntCreated uint64
Expand Down
12 changes: 6 additions & 6 deletions pkg/generators/statement_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ func genMultiplePartitionClusteringRangeQuery(
func genSingleIndexQuery(
s *testschema.Schema,
t *testschema.Table,
g GeneratorInterface,
_ GeneratorInterface,
r *rand.Rand,
p *typedef.PartitionRangeConfig,
idxCount int,
Expand Down Expand Up @@ -398,7 +398,7 @@ func genInsertOrUpdateStmt(
return genInsertStmt(s, t, valuesWithToken, r, p, useLWT)
}

func genUpdateStmt(s *testschema.Schema, t *testschema.Table, valuesWithToken *typedef.ValueWithToken, r *rand.Rand, p *typedef.PartitionRangeConfig) (*typedef.Stmt, error) {
func genUpdateStmt(_ *testschema.Schema, t *testschema.Table, valuesWithToken *typedef.ValueWithToken, r *rand.Rand, p *typedef.PartitionRangeConfig) (*typedef.Stmt, error) {
stmtCache := t.GetQueryCache(typedef.CacheUpdate)
nonCounters := t.Columns.NonCounters()
values := make(typedef.Values, 0, t.PartitionKeys.LenValues()+t.ClusteringKeys.LenValues()+nonCounters.LenValues())
Expand All @@ -417,7 +417,7 @@ func genUpdateStmt(s *testschema.Schema, t *testschema.Table, valuesWithToken *t
}

func genInsertStmt(
s *testschema.Schema,
_ *testschema.Schema,
t *testschema.Table,
valuesWithToken *typedef.ValueWithToken,
r *rand.Rand,
Expand Down Expand Up @@ -507,7 +507,7 @@ func genInsertJSONStmt(
}, nil
}

func genDeleteRows(s *testschema.Schema, t *testschema.Table, valuesWithToken *typedef.ValueWithToken, r *rand.Rand, p *typedef.PartitionRangeConfig) (*typedef.Stmt, error) {
func genDeleteRows(_ *testschema.Schema, t *testschema.Table, valuesWithToken *typedef.ValueWithToken, r *rand.Rand, p *typedef.PartitionRangeConfig) (*typedef.Stmt, error) {
stmtCache := t.GetQueryCache(typedef.CacheDelete)
values := valuesWithToken.Value.Copy()
if len(t.ClusteringKeys) > 0 {
Expand All @@ -522,7 +522,7 @@ func genDeleteRows(s *testschema.Schema, t *testschema.Table, valuesWithToken *t
}, nil
}

func GenDDLStmt(s *testschema.Schema, t *testschema.Table, r *rand.Rand, p *typedef.PartitionRangeConfig, sc *typedef.SchemaConfig) (*typedef.Stmts, error) {
func GenDDLStmt(s *testschema.Schema, t *testschema.Table, r *rand.Rand, _ *typedef.PartitionRangeConfig, sc *typedef.SchemaConfig) (*typedef.Stmts, error) {
maxVariant := 1
if len(t.Columns) > 0 {
maxVariant = 2
Expand Down Expand Up @@ -730,7 +730,7 @@ func GetCreateSchema(s *testschema.Schema) []string {
} else {
createMaterializedView = "CREATE MATERIALIZED VIEW IF NOT EXISTS %s.%s AS SELECT * FROM %s.%s WHERE %s PRIMARY KEY ((%s)"
}
createMaterializedView = createMaterializedView + ",%s)"
createMaterializedView += ",%s)"
stmts = append(stmts, fmt.Sprintf(createMaterializedView,
s.Keyspace.Name, mv.Name, s.Keyspace.Name, t.Name,
strings.Join(mvPrimaryKeysNotNull, " AND "),
Expand Down
17 changes: 9 additions & 8 deletions pkg/generators/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,16 @@ func GetCreateTypes(t *testschema.Table, keyspace typedef.Keyspace) []string {

var stmts []string
for _, column := range t.Columns {
switch c := column.Type.(type) {
case *coltypes.UDTType:
createType := "CREATE TYPE IF NOT EXISTS %s.%s (%s)"
var typs []string
for name, typ := range c.Types {
typs = append(typs, name+" "+typ.CQLDef())
}
stmts = append(stmts, fmt.Sprintf(createType, keyspace.Name, c.TypeName, strings.Join(typs, ",")))
c, ok := column.Type.(*coltypes.UDTType)
if !ok {
continue
}
createType := "CREATE TYPE IF NOT EXISTS %s.%s (%s)"
var typs []string
for name, typ := range c.Types {
typs = append(typs, name+" "+typ.CQLDef())
}
stmts = append(stmts, fmt.Sprintf(createType, keyspace.Name, c.TypeName, strings.Join(typs, ",")))
}
return stmts
}
4 changes: 2 additions & 2 deletions pkg/generators/test_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ type MockGenerator struct {
table *testschema.Table
rand *rand.Rand
partitionsConfig *typedef.PartitionRangeConfig
routingKeyCreator *routingkey.RoutingKeyCreator
routingKeyCreator *routingkey.Creator
}

func NewTestGenerator(
table *testschema.Table,
rnd *rand.Rand,
partitionsConfig *typedef.PartitionRangeConfig,
routingKeyCreator *routingkey.RoutingKeyCreator,
routingKeyCreator *routingkey.Creator,
) *MockGenerator {
return &MockGenerator{table: table, rand: rnd, partitionsConfig: partitionsConfig, routingKeyCreator: routingKeyCreator}
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/jobs/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ var (
errorJobTerminated = errors.New("job terminated")
)

type list struct {
type List struct {
name string
jobs []job
duration time.Duration
Expand All @@ -83,7 +83,7 @@ type job struct {
name string
}

func ListFromMode(mode string, duration time.Duration, workers uint64) list {
func ListFromMode(mode string, duration time.Duration, workers uint64) List {
jobs := make([]job, 0, 2)
name := "work cycle"
switch mode {
Expand All @@ -97,15 +97,15 @@ func ListFromMode(mode string, duration time.Duration, workers uint64) list {
default:
jobs = append(jobs, mutate, validate)
}
return list{
return List{
name: name,
jobs: jobs,
duration: duration,
workers: workers,
}
}

func (l list) Run(
func (l List) Run(
ctx context.Context,
schema *testschema.Schema,
schemaConfig typedef.SchemaConfig,
Expand Down
3 changes: 2 additions & 1 deletion pkg/murmur/murmur_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func TestMurmur3H1(t *testing.T) {
for i, expected := range seriesExpected {
assertMurmur3H1(t, []byte(sample), expected)

sample = sample + strconv.Itoa(i%10)
sample += strconv.Itoa(i % 10)
}

// Here are some test examples from other driver implementations
Expand All @@ -122,6 +122,7 @@ func TestMurmur3H1(t *testing.T) {

// helper function for testing the murmur3 implementation
func assertMurmur3H1(t *testing.T, data []byte, expected uint64) {
t.Helper()
actual := Murmur3H1(data)
if actual != int64(expected) {
t.Errorf("Expected h1 = %x for data = %x, but was %x", int64(expected), data, actual)
Expand Down
6 changes: 3 additions & 3 deletions pkg/routingkey/routing_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ import (
"github.com/gocql/gocql"
)

type RoutingKeyCreator struct {
type Creator struct {
routingKeyBuffer []byte
}

func (rc *RoutingKeyCreator) CreateRoutingKey(table *testschema.Table, values []interface{}) ([]byte, error) {
func (rc *Creator) CreateRoutingKey(table *testschema.Table, values []interface{}) ([]byte, error) {
partitionKeys := table.PartitionKeys
if len(partitionKeys) == 1 {
// single column routing key
Expand Down Expand Up @@ -68,7 +68,7 @@ func (rc *RoutingKeyCreator) CreateRoutingKey(table *testschema.Table, values []
return routingKey, nil
}

func (rc *RoutingKeyCreator) GetHash(t *testschema.Table, values typedef.Values) (uint64, error) {
func (rc *Creator) GetHash(t *testschema.Table, values typedef.Values) (uint64, error) {
b, err := rc.CreateRoutingKey(t, values)
if err != nil {
return 0, err
Expand Down
2 changes: 1 addition & 1 deletion pkg/routingkey/routing_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestRoutingKey(t *testing.T) {
values typedef.Values
want []byte
}
rkc := &routingkey.RoutingKeyCreator{}
rkc := &routingkey.Creator{}
tests := map[string]struct {
table *testschema.Table
data []data
Expand Down
16 changes: 9 additions & 7 deletions pkg/stop/flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestHardStop(t *testing.T) {
testFlag, ctx, workersDone := initVars()
workers := 30

testSignals(workersDone, workers, testFlag.IsHard, testFlag.SetHard, t)
testSignals(t, workersDone, workers, testFlag.IsHard, testFlag.SetHard)
if ctx.Err() == nil {
t.Error("Error:SetHard function does not apply hardStopHandler")
}
Expand All @@ -40,7 +40,7 @@ func TestSoftStop(t *testing.T) {
testFlag, ctx, workersDone := initVars()
workers := 30

testSignals(workersDone, workers, testFlag.IsSoft, testFlag.SetSoft, t)
testSignals(t, workersDone, workers, testFlag.IsSoft, testFlag.SetSoft)
if ctx.Err() != nil {
t.Error("Error:SetSoft function apply hardStopHandler")
}
Expand All @@ -50,21 +50,21 @@ func TestSoftOrHardStop(t *testing.T) {
testFlag, ctx, workersDone := initVars()
workers := 30

testSignals(workersDone, workers, testFlag.IsHardOrSoft, testFlag.SetSoft, t)
testSignals(t, workersDone, workers, testFlag.IsHardOrSoft, testFlag.SetSoft)
if ctx.Err() != nil {
t.Error("Error:SetSoft function apply hardStopHandler")
}

workersDone.Store(uint32(0))
testSignals(workersDone, workers, testFlag.IsHardOrSoft, testFlag.SetHard, t)
testSignals(t, workersDone, workers, testFlag.IsHardOrSoft, testFlag.SetHard)
if ctx.Err() != nil {
t.Error("Error:SetHard function apply hardStopHandler after SetSoft")
}

testFlag, ctx, workersDone = initVars()
workersDone.Store(uint32(0))

testSignals(workersDone, workers, testFlag.IsHardOrSoft, testFlag.SetHard, t)
testSignals(t, workersDone, workers, testFlag.IsHardOrSoft, testFlag.SetHard)
if ctx.Err() == nil {
t.Error("Error:SetHard function does not apply hardStopHandler")
}
Expand All @@ -78,12 +78,14 @@ func initVars() (testFlag *stop.Flag, ctx context.Context, workersDone *atomic.U
return &testFlagOut, ctx, workersDone
}

func testSignals(workersDone *atomic.Uint32,
func testSignals(
t *testing.T,
workersDone *atomic.Uint32,
workers int,
checkFunc func() bool,
setFunc func() bool,
t *testing.T,
) {
t.Helper()
for i := 0; i != workers; i++ {
go func() {
for {
Expand Down
Loading

0 comments on commit 40cabe6

Please sign in to comment.