cleanup: address golangci 'usetesting' linter issues

When a context.Context is needed in a unit test, t.Context() should be
used instead of creating a new one with context.TODO() or
context.Background().

Signed-off-by: Niels de Vos <ndevos@ibm.com>
This commit is contained in:
Niels de Vos 2025-04-29 11:36:03 +02:00 committed by mergify[bot]
parent be5462cf62
commit 457ffe884a
19 changed files with 35 additions and 51 deletions

View File

@ -47,7 +47,7 @@ func TestControllerServer_validateCreateVolumeGroupSnapshotRequest(t *testing.T)
{
"valid CreateVolumeGroupSnapshotRequest",
args{
context.Background(), &csi.CreateVolumeGroupSnapshotRequest{
t.Context(), &csi.CreateVolumeGroupSnapshotRequest{
Name: "vg-snap-1",
SourceVolumeIds: []string{"vg-1"},
Parameters: map[string]string{
@ -62,7 +62,7 @@ func TestControllerServer_validateCreateVolumeGroupSnapshotRequest(t *testing.T)
{
"empty request name in CreateVolumeGroupSnapshotRequest",
args{
context.Background(), &csi.CreateVolumeGroupSnapshotRequest{
t.Context(), &csi.CreateVolumeGroupSnapshotRequest{
SourceVolumeIds: []string{"vg-1"},
},
},
@ -72,7 +72,7 @@ func TestControllerServer_validateCreateVolumeGroupSnapshotRequest(t *testing.T)
{
"empty SourceVolumeIds in CreateVolumeGroupSnapshotRequest",
args{
context.Background(), &csi.CreateVolumeGroupSnapshotRequest{
t.Context(), &csi.CreateVolumeGroupSnapshotRequest{
Name: "vg-snap-1",
SourceVolumeIds: []string{"vg-1"},
},
@ -83,7 +83,7 @@ func TestControllerServer_validateCreateVolumeGroupSnapshotRequest(t *testing.T)
{
"empty clusterID in CreateVolumeGroupSnapshotRequest",
args{
context.Background(), &csi.CreateVolumeGroupSnapshotRequest{
t.Context(), &csi.CreateVolumeGroupSnapshotRequest{
Name: "vg-snap-1",
SourceVolumeIds: []string{"vg-1"},
Parameters: map[string]string{"fsName": "value"},
@ -95,7 +95,7 @@ func TestControllerServer_validateCreateVolumeGroupSnapshotRequest(t *testing.T)
{
"empty fsName in CreateVolumeGroupSnapshotRequest",
args{
context.Background(), &csi.CreateVolumeGroupSnapshotRequest{
t.Context(), &csi.CreateVolumeGroupSnapshotRequest{
Name: "vg-snap-1",
SourceVolumeIds: []string{"vg-1"},
Parameters: map[string]string{"clusterID": "value"},

View File

@ -17,7 +17,6 @@ limitations under the License.
package cephfs
import (
"context"
"testing"
"github.com/csi-addons/spec/lib/go/fence"
@ -38,7 +37,7 @@ func TestFenceClusterNetwork(t *testing.T) {
Cidrs: nil,
}
_, err := controller.FenceClusterNetwork(context.TODO(), req)
_, err := controller.FenceClusterNetwork(t.Context(), req)
require.Error(t, err)
}
@ -54,6 +53,6 @@ func TestUnfenceClusterNetwork(t *testing.T) {
Secrets: nil,
Cidrs: nil,
}
_, err := controller.UnfenceClusterNetwork(context.TODO(), req)
_, err := controller.UnfenceClusterNetwork(t.Context(), req)
require.Error(t, err)
}

View File

@ -17,7 +17,6 @@ limitations under the License.
package networkfence
import (
"context"
"testing"
"github.com/stretchr/testify/require"
@ -252,7 +251,7 @@ listed 1 entries`,
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
result := nf.parseBlocklistForCIDR(context.TODO(), tc.blocklist, tc.cidr)
result := nf.parseBlocklistForCIDR(t.Context(), tc.blocklist, tc.cidr)
require.Equal(t, tc.expected, result)
})
}

View File

@ -14,7 +14,6 @@ limitations under the License.
package rbd
import (
"context"
"testing"
"github.com/csi-addons/spec/lib/go/fence"
@ -35,7 +34,7 @@ func TestFenceClusterNetwork(t *testing.T) {
Cidrs: nil,
}
_, err := controller.FenceClusterNetwork(context.TODO(), req)
_, err := controller.FenceClusterNetwork(t.Context(), req)
require.Error(t, err)
}
@ -51,6 +50,6 @@ func TestUnfenceClusterNetwork(t *testing.T) {
Secrets: nil,
Cidrs: nil,
}
_, err := controller.UnfenceClusterNetwork(context.TODO(), req)
_, err := controller.UnfenceClusterNetwork(t.Context(), req)
require.Error(t, err)
}

View File

@ -17,7 +17,6 @@ limitations under the License.
package rbd
import (
"context"
"testing"
"github.com/ceph/ceph-csi/internal/util"
@ -39,7 +38,7 @@ func TestControllerReclaimSpace(t *testing.T) {
Secrets: nil,
}
_, err := controller.ControllerReclaimSpace(context.TODO(), req)
_, err := controller.ControllerReclaimSpace(t.Context(), req)
require.Error(t, err)
}
@ -58,6 +57,6 @@ func TestNodeReclaimSpace(t *testing.T) {
Secrets: nil,
}
_, err := node.NodeReclaimSpace(context.TODO(), req)
_, err := node.NodeReclaimSpace(t.Context(), req)
require.Error(t, err)
}

View File

@ -83,7 +83,7 @@ func TestValidateSchedulingInterval(t *testing.T) {
func TestValidateSchedulingDetails(t *testing.T) {
t.Parallel()
ctx := context.TODO()
ctx := t.Context()
tests := []struct {
name string
parameters map[string]string
@ -213,7 +213,7 @@ func TestGetSchedulingDetails(t *testing.T) {
}
func TestCheckVolumeResyncStatus(t *testing.T) {
ctx := context.TODO()
ctx := t.Context()
t.Parallel()
tests := []struct {
name string
@ -406,7 +406,7 @@ func TestCheckRemoteSiteStatus(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if ready := checkRemoteSiteStatus(context.TODO(), tt.args.GetAllSitesStatus()); ready != tt.wantReady {
if ready := checkRemoteSiteStatus(t.Context(), tt.args.GetAllSitesStatus()); ready != tt.wantReady {
t.Errorf("checkRemoteSiteStatus() ready = %v, expect ready = %v", ready, tt.wantReady)
}
})
@ -546,7 +546,7 @@ func Test_getFlattenMode(t *testing.T) {
{
name: "flattenMode option not set",
args: args{
ctx: context.TODO(),
ctx: t.Context(),
parameters: map[string]string{},
},
want: types.FlattenModeNever,
@ -554,7 +554,7 @@ func Test_getFlattenMode(t *testing.T) {
{
name: "flattenMode option set to never",
args: args{
ctx: context.TODO(),
ctx: t.Context(),
parameters: map[string]string{
flattenModeKey: string(types.FlattenModeNever),
},
@ -564,7 +564,7 @@ func Test_getFlattenMode(t *testing.T) {
{
name: "flattenMode option set to force",
args: args{
ctx: context.TODO(),
ctx: t.Context(),
parameters: map[string]string{
flattenModeKey: string(types.FlattenModeForce),
},
@ -575,7 +575,7 @@ func Test_getFlattenMode(t *testing.T) {
{
name: "flattenMode option set to invalid value",
args: args{
ctx: context.TODO(),
ctx: t.Context(),
parameters: map[string]string{
flattenModeKey: "invalid123",
},

View File

@ -17,7 +17,6 @@ limitations under the License.
package csicommon
import (
"context"
"os"
"path/filepath"
"strings"
@ -233,7 +232,7 @@ func TestFilesystemNodeGetVolumeStats(t *testing.T) {
// retry until a mountpoint is found
for {
stats, err := FilesystemNodeGetVolumeStats(context.TODO(), mount.New(""), cwd, true)
stats, err := FilesystemNodeGetVolumeStats(t.Context(), mount.New(""), cwd, true)
if err != nil && cwd != "/" && strings.HasSuffix(err.Error(), "is not mounted") {
// try again with the parent directory
cwd = filepath.Dir(cwd)

View File

@ -17,7 +17,6 @@ limitations under the License.
package kms
import (
"context"
"testing"
"github.com/stretchr/testify/require"
@ -93,7 +92,7 @@ func TestWorkflowSecretsMetadataKMS(t *testing.T) {
// plainDEK is the (LUKS) passphrase for the volume
plainDEK := "usually created with generateNewEncryptionPassphrase()"
ctx := context.TODO()
ctx := t.Context()
// with missing encryptionPassphraseKey, encrypting should fail
_, err = kms.EncryptDEK(ctx, volumeID, plainDEK)

View File

@ -17,7 +17,6 @@ limitations under the License.
package rbd
import (
"context"
"testing"
)
@ -160,7 +159,7 @@ func TestToCSIVolume(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if _, err := tt.rv.ToCSI(context.TODO()); (err != nil) != tt.wantErr {
if _, err := tt.rv.ToCSI(t.Context()); (err != nil) != tt.wantErr {
t.Errorf("ToCSI() error = %v, wantErr %v", err, tt.wantErr)
}
})

View File

@ -17,7 +17,6 @@ limitations under the License.
package rbd
import (
"context"
"testing"
)
@ -84,7 +83,7 @@ func TestMakeVolumeGroupID(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
ctx := context.TODO()
ctx := t.Context()
mgr := NewManager("rbd.example.org", tt.parameters, nil)
defer mgr.Destroy(ctx)

View File

@ -17,7 +17,6 @@ limitations under the License.
package rbd
import (
"context"
"strings"
"testing"
@ -27,7 +26,6 @@ import (
func TestValidateLastSyncInfo(t *testing.T) {
t.Parallel()
ctx := context.TODO()
duration := int64(56743)
zero := int64(0)
@ -124,7 +122,7 @@ func TestValidateLastSyncInfo(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
teststruct, err := newSyncInfo(ctx, tt.description)
teststruct, err := newSyncInfo(t.Context(), tt.description)
if err != nil && !strings.Contains(err.Error(), tt.expectedErr) {
// returned error
t.Errorf("newSyncInfo() returned error, expected: %v, got: %v",

View File

@ -17,7 +17,6 @@ limitations under the License.
package rbd
import (
"context"
"encoding/json"
"os"
"testing"
@ -67,7 +66,7 @@ func TestGetStagingPath(t *testing.T) {
func TestParseBoolOption(t *testing.T) {
t.Parallel()
ctx := context.TODO()
ctx := t.Context()
optionName := "myOption"
defaultValue := false

View File

@ -17,7 +17,6 @@ limitations under the License.
package rbd
import (
"context"
"testing"
)
@ -41,7 +40,7 @@ func checkQOS(
func TestSetQOS(t *testing.T) {
t.Parallel()
ctx := context.TODO()
ctx := t.Context()
tests := map[string]string{
baseReadIops: "2000",

View File

@ -17,7 +17,6 @@ limitations under the License.
package rbd
import (
"context"
"errors"
"os"
"strings"
@ -249,7 +248,7 @@ func TestGetCephClientLogFileName(t *testing.T) {
func TestStrategicActionOnLogFile(t *testing.T) {
t.Parallel()
ctx := context.TODO()
ctx := t.Context()
tmpDir := t.TempDir()
var logFile [3]string
@ -320,7 +319,7 @@ func TestStrategicActionOnLogFile(t *testing.T) {
func TestIsKrbdFeatureSupported(t *testing.T) {
t.Parallel()
ctx := context.TODO()
ctx := t.Context()
tests := []struct {
name string

View File

@ -17,7 +17,6 @@ limitations under the License.
package rbd
import (
"context"
"testing"
"time"
)
@ -78,7 +77,7 @@ func TestToCSISnapshot(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if _, err := tt.rs.ToCSI(context.TODO()); (err != nil) != tt.wantErr {
if _, err := tt.rs.ToCSI(t.Context()); (err != nil) != tt.wantErr {
t.Errorf("ToCSI() error = %v, wantErr %v", err, tt.wantErr)
}
})

View File

@ -41,7 +41,7 @@ func TestExecCommandWithTimeout(t *testing.T) {
{
name: "echo hello",
args: args{
ctx: context.TODO(),
ctx: t.Context(),
program: "echo",
timeout: time.Second,
args: []string{"hello"},
@ -53,7 +53,7 @@ func TestExecCommandWithTimeout(t *testing.T) {
{
name: "sleep with timeout",
args: args{
ctx: context.TODO(),
ctx: t.Context(),
program: "sleep",
timeout: time.Second,
args: []string{"3"},

View File

@ -295,7 +295,7 @@ func TestGetMappedID(t *testing.T) {
func TestFetchMappedClusterIDAndMons(t *testing.T) {
t.Parallel()
ctx := context.TODO()
ctx := t.Context()
type args struct {
ctx context.Context
clusterID string

View File

@ -17,7 +17,6 @@ limitations under the License.
package util
import (
"context"
"encoding/base64"
"testing"
@ -56,7 +55,7 @@ func TestKMSWorkflow(t *testing.T) {
require.Equal(t, kms.DefaultKMSType, ve.GetID())
volumeID := "volume-id"
ctx := context.TODO()
ctx := t.Context()
err = ve.StoreNewCryptoPassphrase(ctx, volumeID, defaultEncryptionPassphraseSize)
require.NoError(t, err)

View File

@ -14,7 +14,6 @@ limitations under the License.
package util
import (
"context"
"errors"
"testing"
@ -35,7 +34,7 @@ func TestGetPassphraseFromKMS(t *testing.T) {
volEnc, err := NewVolumeEncryption(provider.UniqueID, kms)
if errors.Is(err, ErrDEKStoreNeeded) {
_, err = volEnc.KMS.GetSecret(context.TODO(), "")
_, err = volEnc.KMS.GetSecret(t.Context(), "")
if errors.Is(err, kmsapi.ErrGetSecretUnsupported) {
continue // currently unsupported by fscrypt integration
}
@ -46,7 +45,7 @@ func TestGetPassphraseFromKMS(t *testing.T) {
continue
}
secret, err := kms.GetSecret(context.TODO(), "")
secret, err := kms.GetSecret(t.Context(), "")
require.NoError(t, err, provider.UniqueID)
require.NotEmpty(t, secret, provider.UniqueID)
}