Skip to content

Commit

Permalink
enable golangci-lint: scopelint, structcheck (kedacore#1158)
Browse files Browse the repository at this point in the history
Signed-off-by: akartasov <[email protected]>
  • Loading branch information
agneum committed Oct 17, 2020
1 parent 4b4e473 commit 8036cd9
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 26 deletions.
4 changes: 2 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ linters:
- ineffassign
- staticcheck
#- interfacer
#- scopelint
#- structcheck
- scopelint
- structcheck
- deadcode
#- depguard
#- dogsled
Expand Down
1 change: 1 addition & 0 deletions controllers/scaledjob_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ func (r *ScaledJobReconciler) deletePreviousVersionScaleJobs(logger logr.Logger,
logger.Info("Deleting jobs owned by the previous version of the scaledJob", "Number of jobs to delete", jobs.Size())
}
for _, job := range jobs.Items {
job := job
err = r.Client.Delete(context.TODO(), &job, client.PropagationPolicy(metav1.DeletePropagationBackground))
if err != nil {
return "Not able to delete job: " + job.Name, err
Expand Down
24 changes: 18 additions & 6 deletions pkg/scalers/mysql_scaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,28 @@ type mySQLMetricIdentifier struct {

var testMySQLMetadata = []parseMySQLMetadataTestData{
// No metadata
{map[string]string{}, testMySQLResolvedEnv, true},
{
metadata: map[string]string{},
resolvedEnv: testMySQLResolvedEnv,
raisesError: true,
},
// connectionString
{map[string]string{"query": "query", "queryValue": "12", "connectionStringFromEnv": "MYSQL_CONN_STR"}, testMySQLResolvedEnv, false},
{
metadata: map[string]string{"query": "query", "queryValue": "12", "connectionStringFromEnv": "MYSQL_CONN_STR"},
resolvedEnv: testMySQLResolvedEnv,
raisesError: false,
},
// Params instead of conn str
{map[string]string{"query": "query", "queryValue": "12", "host": "test_host", "port": "test_port", "username": "test_username", "passwordFromEnv": "MYSQL_PASSWORD", "dbName": "test_dbname"}, testMySQLResolvedEnv, false},
{
metadata: map[string]string{"query": "query", "queryValue": "12", "host": "test_host", "port": "test_port", "username": "test_username", "passwordFromEnv": "MYSQL_PASSWORD", "dbName": "test_dbname"},
resolvedEnv: testMySQLResolvedEnv,
raisesError: false,
},
}

var mySQLMetricIdentifiers = []mySQLMetricIdentifier{
{&testMySQLMetadata[1], "mysql-test_conn_str"},
{&testMySQLMetadata[2], "mysql-test_dbname"},
{metadataTestData: &testMySQLMetadata[1], name: "mysql-test_conn_str"},
{metadataTestData: &testMySQLMetadata[2], name: "mysql-test_dbname"},
}

func TestParseMySQLMetadata(t *testing.T) {
Expand Down Expand Up @@ -69,7 +81,7 @@ func TestMetadataToConnectionStrBuildNew(t *testing.T) {

func TestMySQLGetMetricSpecForScaling(t *testing.T) {
for _, testData := range mySQLMetricIdentifiers {
meta, err := parseMySQLMetadata(&ScalerConfig{ResolvedEnv: testMySQLResolvedEnv, TriggerMetadata: testData.metadataTestData.metadata, AuthParams: nil})
meta, err := parseMySQLMetadata(&ScalerConfig{ResolvedEnv: testData.metadataTestData.resolvedEnv, TriggerMetadata: testData.metadataTestData.metadata, AuthParams: nil})
if err != nil {
t.Fatal("Could not parse metadata:", err)
}
Expand Down
9 changes: 4 additions & 5 deletions pkg/scalers/postgresql_scaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import (
)

type parsePostgreSQLMetadataTestData struct {
metdadata map[string]string
isError bool
metadata map[string]string
}

type postgreSQLMetricIdentifier struct {
Expand All @@ -16,9 +15,9 @@ type postgreSQLMetricIdentifier struct {

var testPostgreSQLMetdata = []parsePostgreSQLMetadataTestData{
// connection
{map[string]string{"query": "test_query", "targetQueryValue": "5", "connectionFromEnv": "test_connection_string"}, false},
{metadata: map[string]string{"query": "test_query", "targetQueryValue": "5", "connectionFromEnv": "test_connection_string"}},
// dbName
{map[string]string{"query": "test_query", "targetQueryValue": "5", "host": "test_host", "port": "test_port", "userName": "test_user_name", "dbName": "test_db_name", "sslmode": "test_ssl_mode"}, false},
{metadata: map[string]string{"query": "test_query", "targetQueryValue": "5", "host": "test_host", "port": "test_port", "userName": "test_user_name", "dbName": "test_db_name", "sslmode": "test_ssl_mode"}},
}

var postgreSQLMetricIdentifiers = []postgreSQLMetricIdentifier{
Expand All @@ -28,7 +27,7 @@ var postgreSQLMetricIdentifiers = []postgreSQLMetricIdentifier{

func TestPosgresSQLGetMetricSpecForScaling(t *testing.T) {
for _, testData := range postgreSQLMetricIdentifiers {
meta, err := parsePostgreSQLMetadata(&ScalerConfig{ResolvedEnv: map[string]string{"test_connection_string": "test_connection_string"}, TriggerMetadata: testData.metadataTestData.metdadata, AuthParams: nil})
meta, err := parsePostgreSQLMetadata(&ScalerConfig{ResolvedEnv: map[string]string{"test_connection_string": "test_connection_string"}, TriggerMetadata: testData.metadataTestData.metadata, AuthParams: nil})
if err != nil {
t.Fatal("Could not parse metadata:", err)
}
Expand Down
1 change: 1 addition & 0 deletions pkg/scalers/rabbitmq_scaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ var vhostPathes = []string{"/myhost", "", "/", "//", "/%2F"}

func TestGetQueueInfo(t *testing.T) {
for _, testData := range testQueueInfoTestData {
testData := testData
for _, vhostPath := range vhostPathes {
expectedVhost := "myhost"

Expand Down
38 changes: 25 additions & 13 deletions pkg/scalers/redis_streams_scaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,26 @@ func TestParseRedisStreamsMetadata(t *testing.T) {
authParams := map[string]string{"password": "foobarred"}

testCases := []testCase{
{"with address", map[string]string{"stream": "my-stream", "consumerGroup": "my-stream-consumer-group", "pendingEntriesCount": "5", "addressFromEnv": "REDIS_SERVICE", "passwordFromEnv": "REDIS_PASSWORD", "databaseIndex": "0", "enableTLS": "true"}, map[string]string{
"REDIS_SERVICE": "myredis:6379",
"REDIS_PASSWORD": "foobarred",
}, nil},

{"with host and port", map[string]string{"stream": "my-stream", "consumerGroup": "my-stream-consumer-group", "pendingEntriesCount": "15", "hostFromEnv": "REDIS_HOST", "port": "REDIS_PORT", "passwordFromEnv": "REDIS_PASSWORD", "databaseIndex": "0", "enableTLS": "false"}, map[string]string{
"REDIS_HOST": "myredis",
"REDIS_PORT": "6379",
"REDIS_PASSWORD": "foobarred",
}, authParams},
{
name: "with address",
metadata: map[string]string{"stream": "my-stream", "consumerGroup": "my-stream-consumer-group", "pendingEntriesCount": "5", "addressFromEnv": "REDIS_SERVICE", "passwordFromEnv": "REDIS_PASSWORD", "databaseIndex": "0", "enableTLS": "true"},
resolvedEnv: map[string]string{
"REDIS_SERVICE": "myredis:6379",
"REDIS_PASSWORD": "foobarred",
},
authParams: nil,
},

{
name: "with host and port",
metadata: map[string]string{"stream": "my-stream", "consumerGroup": "my-stream-consumer-group", "pendingEntriesCount": "15", "hostFromEnv": "REDIS_HOST", "port": "REDIS_PORT", "passwordFromEnv": "REDIS_PASSWORD", "databaseIndex": "0", "enableTLS": "false"},
resolvedEnv: map[string]string{
"REDIS_HOST": "myredis",
"REDIS_PORT": "6379",
"REDIS_PASSWORD": "foobarred",
},
authParams: authParams,
},
}

for _, tc := range testCases {
Expand Down Expand Up @@ -99,7 +109,6 @@ func TestParseRedisStreamsMetadataForInvalidCases(t *testing.T) {

type redisStreamsTestMetadata struct {
metadata map[string]string
isError bool
authParams map[string]string
}

Expand All @@ -110,15 +119,18 @@ func TestRedisStreamsGetMetricSpecForScaling(t *testing.T) {
}

var redisStreamsTestData = []redisStreamsTestMetadata{
{map[string]string{"stream": "my-stream", "consumerGroup": "my-stream-consumer-group", "pendingEntriesCount": "5", "address": "REDIS_SERVICE", "password": "REDIS_PASSWORD", "databaseIndex": "0", "enableTLS": "true"}, false, nil},
{
metadata: map[string]string{"stream": "my-stream", "consumerGroup": "my-stream-consumer-group", "pendingEntriesCount": "5", "address": "REDIS_SERVICE", "password": "REDIS_PASSWORD", "databaseIndex": "0", "enableTLS": "true"},
authParams: nil,
},
}

var redisStreamMetricIdentifiers = []redisStreamsMetricIdentifier{
{&redisStreamsTestData[0], "redis-streams-my-stream-my-stream-consumer-group"},
}

for _, testData := range redisStreamMetricIdentifiers {
meta, err := parseRedisStreamsMetadata(&ScalerConfig{TriggerMetadata: testData.metadataTestData.metadata, ResolvedEnv: map[string]string{"REDIS_SERVICE": "my-address"}, AuthParams: nil})
meta, err := parseRedisStreamsMetadata(&ScalerConfig{TriggerMetadata: testData.metadataTestData.metadata, ResolvedEnv: map[string]string{"REDIS_SERVICE": "my-address"}, AuthParams: testData.metadataTestData.authParams})
if err != nil {
t.Fatal("Could not parse metadata:", err)
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/scaling/executor/scale_jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ func (e *scaleExecutor) getRunningJobCount(scaledJob *kedav1alpha1.ScaledJob) in
}

for _, job := range jobs.Items {
job := job
if !e.isJobFinished(&job) {
runningJobs++
}
Expand Down Expand Up @@ -152,6 +153,7 @@ func (e *scaleExecutor) cleanUp(scaledJob *kedav1alpha1.ScaledJob) error {
completedJobs := []batchv1.Job{}
failedJobs := []batchv1.Job{}
for _, job := range jobs.Items {
job := job
finishedJobConditionType := e.getFinishedJobConditionType(&job)
switch finishedJobConditionType {
case batchv1.JobComplete:
Expand Down
2 changes: 2 additions & 0 deletions pkg/scaling/resolver/scale_resolvers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ func TestResolveAuthRef(t *testing.T) {
},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
gotMap, gotPodIdentity := ResolveAuthRef(fake.NewFakeClientWithScheme(scheme.Scheme, test.existing...), logf.Log.WithName("test"), test.soar, test.podSpec, namespace)
if diff := cmp.Diff(gotMap, test.expected); diff != "" {
Expand Down Expand Up @@ -337,6 +338,7 @@ func TestResolveDependentEnv(t *testing.T) {
},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
envMap, _ := resolveEnv(fake.NewFakeClient(), logf.Log.WithName("test"), test.container, namespace)
if diff := cmp.Diff(envMap, test.expected); diff != "" {
Expand Down
2 changes: 2 additions & 0 deletions pkg/util/k8sversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ var testMetadatas = []testMetadata{

func TestResolveK8sVersion(t *testing.T) {
for _, testData := range testMetadatas {
t.Log(testData.comment)

version := NewK8sVersion(testData.version)

if version.MinorVersion != testData.expectedMinor {
Expand Down

0 comments on commit 8036cd9

Please sign in to comment.