Skip to content

Commit

Permalink
Add linters
Browse files Browse the repository at this point in the history
dabradley committed Oct 16, 2024
1 parent a16c837 commit d583bcd
Showing 10 changed files with 86 additions and 31 deletions.
58 changes: 49 additions & 9 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -1,17 +1,57 @@
linters:
presets:
- bugs
enable:
- revive
- containedctx
- copyloopvar
- decorder
- dupl
- forbidigo
- forcetypeassert
- gci
- gofmt
- gofumpt
- testifylint
- stylecheck
- goimports
- gosimple
- grouper
- inamedparam
- ineffassign
- misspell
- nolintlint
- nonamedreturns
- nosprintfhostport
- predeclared
- makezero
- revive
- stylecheck
- tagalign
- tenv
- copyloopvar
- protogetter
- nonamedreturns
- gci
- forbidigo
- unconvert
- unused
- usestdlibvars
- wastedassign
- whitespace
linters-settings:
copyloopvar:
check-alias: true
errcheck:
check-blank: true
check-type-assertions: true
disable-default-exclusions: true
gofumpt:
extra-rules: true
grouper:
const-require-grouping: true
const-require-single-const: true
import-require-grouping: false
import-require-single-import: true
type-require-grouping: false
type-require-single-type: false
var-require-grouping: false
var-require-single-var: true
nolintlint:
require-explanation: true
require-specific: true
unparam:
check-exported: true
run:
timeout: 30m0s
4 changes: 3 additions & 1 deletion pkg/azurelustre/fake_mount_test.go
Original file line number Diff line number Diff line change
@@ -22,6 +22,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
mount "k8s.io/mount-utils"
)

@@ -145,7 +146,8 @@ func TestMountSensitiveWithoutSystemdWithMountFlags(t *testing.T) {
t.Errorf("actualErr: (%v), expectedErr: (%v)", err, test.expectedErr)
}

mountPoints, _ := d.mounter.List()
mountPoints, err := d.mounter.List()
require.NoError(t, err)
assert.Equal(t, test.expectedMountpoints, mountPoints)
}
}
27 changes: 17 additions & 10 deletions pkg/azurelustre/nodeserver_test.go
Original file line number Diff line number Diff line change
@@ -117,7 +117,8 @@ func TestEnsureMountPoint(t *testing.T) {
}

for _, test := range tests {
_ = makeDir(alreadyExistTarget)
err := makeDir(alreadyExistTarget)
require.NoError(t, err)

t.Run(test.desc, func(t *testing.T) {
_, err := d.ensureMountPoint(test.target)
@@ -126,7 +127,7 @@ func TestEnsureMountPoint(t *testing.T) {
}
})

err := os.RemoveAll(alreadyExistTarget)
err = os.RemoveAll(alreadyExistTarget)
require.NoError(t, err)
err = os.RemoveAll(targetTest)
require.NoError(t, err)
@@ -542,8 +543,10 @@ func TestNodePublishVolume(t *testing.T) {
Exec: fakeExec,
}
d.workingMountDir = workingMountDir
_ = makeDir(targetTest)
_ = makeDir(alreadyExistTarget)
err := makeDir(targetTest)
require.NoError(t, err)
err = makeDir(alreadyExistTarget)
require.NoError(t, err)

if test.setup != nil {
test.setup(d)
@@ -552,12 +555,13 @@ func TestNodePublishVolume(t *testing.T) {
fakeMounter.ResetLog()

t.Run(test.desc, func(t *testing.T) {
_, err := d.NodePublishVolume(context.Background(), &test.req)
_, err = d.NodePublishVolume(context.Background(), &test.req)
if !reflect.DeepEqual(err, test.expectedErr) {
t.Errorf("Desc: %v, Expected error: %v, Actual error: %v", test.desc, test.expectedErr, err)
}

mountPoints, _ := d.mounter.List()
mountPoints, err := d.mounter.List()
require.NoError(t, err)
assert.Equal(t, test.expectedMountpoints, mountPoints, "Desc: %s - Incorrect mount points: %v - Expected: %v", test.desc, mountPoints, test.expectedMountpoints)
mountActions := fakeMounter.GetLog()
assert.Equal(t, test.expectedMountActions, mountActions, "Desc: %s - Incorrect mount actions: %v - Expected: %v", test.desc, mountActions, test.expectedMountActions)
@@ -732,7 +736,8 @@ func TestNodeUnpublishVolume(t *testing.T) {
Interface: fakeMounter,
Exec: fakeExec,
}
_ = makeDir(targetTest)
err := makeDir(targetTest)
require.NoError(t, err)

if test.setup != nil {
test.setup(d)
@@ -745,7 +750,8 @@ func TestNodeUnpublishVolume(t *testing.T) {
if !reflect.DeepEqual(err, test.expectedErr) {
t.Errorf("Desc: %v, Expected error: %v, Actual error: %v", test.desc, test.expectedErr, err)
}
mountPoints, _ := d.mounter.List()
mountPoints, err := d.mounter.List()
require.NoError(t, err)
assert.Equal(t, test.expectedMountpoints, mountPoints, "Desc: %s - Incorrect mount points: %v - Expected: %v", test.desc, mountPoints, test.expectedMountpoints)
mountActions := fakeMounter.GetLog()
assert.Equal(t, test.expectedMountActions, mountActions, "Desc: %s - Incorrect mount actions: %v - Expected: %v", test.desc, mountActions, test.expectedMountActions)
@@ -853,15 +859,16 @@ func TestNodeGetVolumeStats(t *testing.T) {
d := NewFakeDriver()

for _, test := range tests {
_ = makeDir(fakePath)
err := makeDir(fakePath)
require.NoError(t, err)
t.Run(test.desc, func(t *testing.T) {
_, err := d.NodeGetVolumeStats(context.Background(), &test.req)
if !reflect.DeepEqual(err, test.expectedErr) {
t.Errorf("Desc: %v, Expected error: %v, Actual error: %v", test.desc, test.expectedErr, err)
}
})

err := os.RemoveAll(fakePath)
err = os.RemoveAll(fakePath)
require.NoError(t, err)
}
}
4 changes: 3 additions & 1 deletion pkg/azurelustre/version_test.go
Original file line number Diff line number Diff line change
@@ -23,6 +23,7 @@ import (
"strings"
"testing"

"github.com/stretchr/testify/require"
"sigs.k8s.io/yaml"
)

@@ -51,7 +52,8 @@ func TestGetVersionYAML(t *testing.T) {
}

versionInfo := GetVersion("")
marshalled, _ := yaml.Marshal(&versionInfo)
marshalled, err := yaml.Marshal(&versionInfo)
require.NoError(t, err)

expected := strings.TrimSpace(string(marshalled))

5 changes: 4 additions & 1 deletion pkg/azurelustreplugin/main.go
Original file line number Diff line number Diff line change
@@ -35,7 +35,10 @@ var (

func main() {
klog.InitFlags(nil)
_ = flag.Set("logtostderr", "true")
err := flag.Set("logtostderr", "true")
if err != nil {
klog.Fatalln(err)
}
flag.Parse()
if *version {
info, err := azurelustre.GetVersionYAML(*driverName)
3 changes: 2 additions & 1 deletion pkg/csi-common/utils_test.go
Original file line number Diff line number Diff line change
@@ -132,7 +132,8 @@ func TestLogGRPC(t *testing.T) {
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
// EXECUTE
_, _ = logGRPC(context.Background(), test.req, &info, handler)
_, err := logGRPC(context.Background(), test.req, &info, handler)
require.NoError(t, err)
klog.Flush()

// ASSERT
2 changes: 1 addition & 1 deletion test/integration/integration_test.go
Original file line number Diff line number Diff line change
@@ -72,7 +72,7 @@ func TestIntegration(t *testing.T) {
assert.True(t, strings.HasSuffix(cwd, "azurelustre-csi-driver"))

// Pass in resource group name, storage account name and cloud type
cmd := exec.Command("./test/integration/run-tests-all-clouds.sh", creds.ResourceGroup, creds.Cloud)
cmd := exec.Command("./test/integration/run-tests-all-clouds.sh", creds.ResourceGroup, creds.Cloud) // #nosec G204
cmd.Dir = cwd
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
2 changes: 1 addition & 1 deletion test/sanity/sanity_test.go
Original file line number Diff line number Diff line change
@@ -71,7 +71,7 @@ func TestSanity(t *testing.T) {
require.NoError(t, err)
assert.True(t, strings.HasSuffix(projectRoot, "azurelustre-csi-driver"))

cmd := exec.Command("./test/sanity/run-tests-all-clouds.sh", creds.Cloud)
cmd := exec.Command("./test/sanity/run-tests-all-clouds.sh", creds.Cloud) // #nosec G204
cmd.Dir = projectRoot
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
6 changes: 3 additions & 3 deletions test/utils/credentials/credentials.go
Original file line number Diff line number Diff line change
@@ -30,7 +30,7 @@ import (
const (
AzurePublicCloud = "AzurePublicCloud"
ResourceGroupPrefix = "azurelustre-csi-driver-test-"
TempAzureCredentialFilePath = "/tmp/azure.json"
TempAzureCredentialFilePath = "/tmp/azure.json" // #nosec G101

azureCredentialFileTemplate = `{
"cloud": "{{.Cloud}}",
@@ -40,15 +40,15 @@ const (
"aadClientSecret": "{{.AADClientSecret}}",
"resourceGroup": "{{.ResourceGroup}}",
"location": "{{.Location}}"
}`
}` // #nosec G101
defaultAzurePublicCloudLocation = "eastus2"

// Env vars
cloudNameEnvVar = "AZURE_CLOUD_NAME"
tenantIDEnvVar = "AZURE_TENANT_ID"
subscriptionIDEnvVar = "AZURE_SUBSCRIPTION_ID"
aadClientIDEnvVar = "AZURE_CLIENT_ID"
aadClientSecretEnvVar = "AZURE_CLIENT_SECRET"
aadClientSecretEnvVar = "AZURE_CLIENT_SECRET" // #nosec G101
resourceGroupEnvVar = "AZURE_RESOURCE_GROUP"
locationEnvVar = "AZURE_LOCATION"
)
6 changes: 3 additions & 3 deletions test/utils/credentials/credentials_test.go
Original file line number Diff line number Diff line change
@@ -35,7 +35,7 @@ const (
SubscriptionID = "b9d2281e-xxxx-xxxx-xxxx-0d50377cdf76"
StorageAccountName = "TestStorageAccountName"
StorageAccountKey = "TestStorageAccountKey"
`
` // #nosec G101
testTenantID = "test-tenant-id"
testSubscriptionID = "test-subscription-id"
testAadClientID = "test-aad-client-id"
@@ -122,7 +122,7 @@ func withAzureCredentials(t *testing.T) {
"resourceGroup": "test-resource-group",
"location": "test-location"
}
`
` // #nosec G101
tmpl := template.New("expectedAzureCredentialFileContent")
tmpl, err = tmpl.Parse(expectedAzureCredentialFileContent)
require.NoError(t, err)
@@ -172,7 +172,7 @@ func withEnvironmentVariables(t *testing.T) {
"resourceGroup": "test-resource-group",
"location": "test-location"
}
`
` // #nosec G101
tmpl := template.New("expectedAzureCredentialFileContent")
tmpl, err = tmpl.Parse(expectedAzureCredentialFileContent)
require.NoError(t, err)

0 comments on commit d583bcd

Please sign in to comment.