fix require-error rule from testifylint

Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
This commit is contained in:
Matthieu MOREL
2025-06-01 21:44:09 +02:00
parent 3024e6223e
commit 07ea14962c
99 changed files with 483 additions and 440 deletions

View File

@@ -59,10 +59,10 @@ func TestCreateOptions_BuildBackup(t *testing.T) {
},
}
o.OrSelector.OrLabelSelectors = orLabelSelectors
assert.NoError(t, err)
require.NoError(t, err)
backup, err := o.BuildBackup(cmdtest.VeleroNameSpace)
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, velerov1api.BackupSpec{
TTL: metav1.Duration{Duration: o.TTL},
@@ -159,7 +159,7 @@ func TestCreateOptions_BuildBackupFromSchedule(t *testing.T) {
o.Labels.Set("velero.io/test=yes,custom-label=true")
o.Annotations.Set("velero.io/test=yes,custom-annotation=true")
backup, err := o.BuildBackup(cmdtest.VeleroNameSpace)
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, expectedBackupSpec, backup.Spec)
assert.Equal(t, map[string]string{
@@ -176,10 +176,10 @@ func TestCreateOptions_BuildBackupFromSchedule(t *testing.T) {
func TestCreateOptions_OrderedResources(t *testing.T) {
_, err := ParseOrderedResources("pods= ns1/p1; ns1/p2; persistentvolumeclaims=ns2/pvc1, ns2/pvc2")
assert.Error(t, err)
require.Error(t, err)
orderedResources, err := ParseOrderedResources("pods= ns1/p1,ns1/p2 ; persistentvolumeclaims=ns2/pvc1,ns2/pvc2")
assert.NoError(t, err)
require.NoError(t, err)
expectedResources := map[string]string{
"pods": "ns1/p1,ns1/p2",
@@ -188,7 +188,7 @@ func TestCreateOptions_OrderedResources(t *testing.T) {
assert.Equal(t, expectedResources, orderedResources)
orderedResources, err = ParseOrderedResources("pods= ns1/p1,ns1/p2 ; persistentvolumes=pv1,pv2")
assert.NoError(t, err)
require.NoError(t, err)
expectedMixedResources := map[string]string{
"pods": "ns1/p1,ns1/p2",
@@ -338,7 +338,7 @@ func TestCreateCommand(t *testing.T) {
// Complete
e := o.Complete(args, f)
assert.NoError(t, e)
require.NoError(t, e)
// Validate
e = o.Validate(cmd, args, f)
@@ -369,7 +369,7 @@ func TestCreateCommand(t *testing.T) {
// Complete
e := o.Complete(args, f)
assert.NoError(t, e)
require.NoError(t, e)
// Validate
e = o.Validate(cmd, args, f)
@@ -397,10 +397,10 @@ func TestCreateCommand(t *testing.T) {
f.On("KubebuilderWatchClient").Return(kbclient, nil)
e := o.Complete(args, f)
assert.NoError(t, e)
require.NoError(t, e)
e = o.Run(c, f)
assert.NoError(t, e)
require.NoError(t, e)
c.SetArgs([]string{"bk-1"})
e = c.Execute()

View File

@@ -24,6 +24,7 @@ import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"k8s.io/client-go/rest"
controllerclient "sigs.k8s.io/controller-runtime/pkg/client"
@@ -58,7 +59,7 @@ func TestNewDescribeCommand(t *testing.T) {
c.SetArgs([]string{backupName})
e := c.Execute()
assert.NoError(t, e)
require.NoError(t, e)
if os.Getenv(cmdtest.CaptureFlag) == "1" {
return

View File

@@ -74,10 +74,10 @@ func TestNewDownloadCommand(t *testing.T) {
args := []string{backupName, "arg2"}
e := o.Complete(args)
assert.NoError(t, e)
require.NoError(t, e)
e = o.Validate(c, args, f)
assert.NoError(t, e)
require.NoError(t, e)
// verify all options are set as expected
assert.Equal(t, output, o.Output)

View File

@@ -58,7 +58,7 @@ func TestNewGetCommand(t *testing.T) {
c.SetArgs(args)
e := c.Execute()
assert.NoError(t, e)
require.NoError(t, e)
if os.Getenv(cmdtest.CaptureFlag) == "1" {
return
@@ -83,7 +83,7 @@ func TestNewGetCommand(t *testing.T) {
d := NewGetCommand(f, "velero backup get")
c.SetArgs([]string{"-l", "abc=abc"})
e = d.Execute()
assert.NoError(t, e)
require.NoError(t, e)
cmd = exec.Command(os.Args[0], []string{"-test.run=TestNewGetCommand"}...)
cmd.Env = append(os.Environ(), fmt.Sprintf("%s=1", cmdtest.CaptureFlag))

View File

@@ -26,6 +26,7 @@ import (
flag "github.com/spf13/pflag"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
corev1api "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -38,7 +39,7 @@ func TestBuildBackupStorageLocationSetsNamespace(t *testing.T) {
o := NewCreateOptions()
bsl, err := o.BuildBackupStorageLocation("velero-test-ns", false, false)
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, "velero-test-ns", bsl.Namespace)
}
@@ -47,11 +48,11 @@ func TestBuildBackupStorageLocationSetsSyncPeriod(t *testing.T) {
o.BackupSyncPeriod = 2 * time.Minute
bsl, err := o.BuildBackupStorageLocation("velero-test-ns", false, false)
assert.NoError(t, err)
require.NoError(t, err)
assert.Nil(t, bsl.Spec.BackupSyncPeriod)
bsl, err = o.BuildBackupStorageLocation("velero-test-ns", true, false)
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, &metav1.Duration{Duration: 2 * time.Minute}, bsl.Spec.BackupSyncPeriod)
}
@@ -60,11 +61,11 @@ func TestBuildBackupStorageLocationSetsValidationFrequency(t *testing.T) {
o.ValidationFrequency = 2 * time.Minute
bsl, err := o.BuildBackupStorageLocation("velero-test-ns", false, false)
assert.NoError(t, err)
require.NoError(t, err)
assert.Nil(t, bsl.Spec.ValidationFrequency)
bsl, err = o.BuildBackupStorageLocation("velero-test-ns", false, true)
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, &metav1.Duration{Duration: 2 * time.Minute}, bsl.Spec.ValidationFrequency)
}
@@ -72,14 +73,14 @@ func TestBuildBackupStorageLocationSetsCredential(t *testing.T) {
o := NewCreateOptions()
bsl, err := o.BuildBackupStorageLocation("velero-test-ns", false, false)
assert.NoError(t, err)
require.NoError(t, err)
assert.Nil(t, bsl.Spec.Credential)
setErr := o.Credential.Set("my-secret=key-from-secret")
assert.NoError(t, setErr)
require.NoError(t, setErr)
bsl, err = o.BuildBackupStorageLocation("velero-test-ns", false, true)
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, &corev1api.SecretKeySelector{
LocalObjectReference: corev1api.LocalObjectReference{Name: "my-secret"},
Key: "key-from-secret",
@@ -90,10 +91,10 @@ func TestBuildBackupStorageLocationSetsLabels(t *testing.T) {
o := NewCreateOptions()
err := o.Labels.Set("key=value")
assert.NoError(t, err)
require.NoError(t, err)
bsl, err := o.BuildBackupStorageLocation("velero-test-ns", false, false)
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, map[string]string{"key": "value"}, bsl.Labels)
}
@@ -148,10 +149,10 @@ func TestCreateCommand_Run(t *testing.T) {
o.Complete(args, f)
e := o.Validate(c, args, f)
assert.NoError(t, e)
require.NoError(t, e)
e = o.Run(c, f)
assert.ErrorContains(t, e, fmt.Sprintf("%s: no such file or directory", caCertFile))
require.ErrorContains(t, e, fmt.Sprintf("%s: no such file or directory", caCertFile))
// verify all options are set as expected
assert.Equal(t, name, o.Name)
@@ -178,7 +179,7 @@ func TestCreateCommand_Run(t *testing.T) {
o.Complete(args, f)
e = o.Run(c, f)
assert.NoError(t, e)
require.NoError(t, e)
c.SetArgs([]string{"bsl-1", "--provider=aws", "--bucket=bk1", "--default"})
e = c.Execute()
assert.NoError(t, e)

View File

@@ -25,6 +25,7 @@ import (
flag "github.com/spf13/pflag"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
factorymocks "github.com/vmware-tanzu/velero/pkg/client/mocks"
@@ -52,15 +53,15 @@ func TestNewDeleteCommand(t *testing.T) {
args := []string{"bk-loc-1", "bk-loc-2"}
e := o.Complete(f, args)
assert.NoError(t, e)
require.NoError(t, e)
e = o.Validate(c, f, args)
assert.NoError(t, e)
require.NoError(t, e)
c.SetArgs([]string{"bk-1", "--confirm"})
e = c.Execute()
assert.NoError(t, e)
require.NoError(t, e)
e = Run(f, o)
assert.NoError(t, e)
require.NoError(t, e)
if os.Getenv(cmdtest.CaptureFlag) == "1" {
return
}

View File

@@ -26,6 +26,7 @@ import (
flag "github.com/spf13/pflag"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
factorymocks "github.com/vmware-tanzu/velero/pkg/client/mocks"
cmdtest "github.com/vmware-tanzu/velero/pkg/cmd/test"
@@ -66,10 +67,10 @@ func TestNewSetCommand(t *testing.T) {
args := []string{backupName}
o.Complete(args, f)
e := o.Validate(c, args, f)
assert.NoError(t, e)
require.NoError(t, e)
e = o.Run(c, f)
assert.ErrorContains(t, e, fmt.Sprintf("%s: no such file or directory", cacert))
require.ErrorContains(t, e, fmt.Sprintf("%s: no such file or directory", cacert))
// verify all options are set as expected
assert.Equal(t, backupName, o.Name)

View File

@@ -24,6 +24,7 @@ import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"k8s.io/client-go/rest"
controllerclient "sigs.k8s.io/controller-runtime/pkg/client"
@@ -58,7 +59,7 @@ func TestNewDescribeCommand(t *testing.T) {
c.SetArgs([]string{restoreName})
e := c.Execute()
assert.NoError(t, e)
require.NoError(t, e)
if os.Getenv(cmdtest.CaptureFlag) == "1" {
return

View File

@@ -38,7 +38,7 @@ func TestValidateOfSelectOption(t *testing.T) {
Selector: flag.LabelSelector{},
All: false,
}
assert.Error(t, option.Validate())
require.Error(t, option.Validate())
option.All = true
assert.NoError(t, option.Validate())

View File

@@ -63,7 +63,7 @@ func TestVeleroResourcesExist(t *testing.T) {
},
},
}
assert.Error(t, server.veleroResourcesExist())
require.Error(t, server.veleroResourcesExist())
// Velero v1 API group doesn't contain any custom resources: should error
veleroAPIResourceListVelerov1 := &metav1.APIResourceList{
@@ -71,7 +71,7 @@ func TestVeleroResourcesExist(t *testing.T) {
}
fakeDiscoveryHelper.ResourceList = append(fakeDiscoveryHelper.ResourceList, veleroAPIResourceListVelerov1)
assert.Error(t, server.veleroResourcesExist())
require.Error(t, server.veleroResourcesExist())
// Velero v2alpha1 API group doesn't contain any custom resources: should error
veleroAPIResourceListVeleroV2alpha1 := &metav1.APIResourceList{
@@ -79,7 +79,7 @@ func TestVeleroResourcesExist(t *testing.T) {
}
fakeDiscoveryHelper.ResourceList = append(fakeDiscoveryHelper.ResourceList, veleroAPIResourceListVeleroV2alpha1)
assert.Error(t, server.veleroResourcesExist())
require.Error(t, server.veleroResourcesExist())
// Velero v1 API group contains all custom resources, but v2alpha1 doesn't contain any custom resources: should error
for kind := range velerov1api.CustomResources() {
@@ -87,7 +87,7 @@ func TestVeleroResourcesExist(t *testing.T) {
Kind: kind,
})
}
assert.Error(t, server.veleroResourcesExist())
require.Error(t, server.veleroResourcesExist())
// Velero v1 and v2alpha1 API group contain all custom resources: should not error
for kind := range velerov2alpha1api.CustomResources() {
@@ -95,7 +95,7 @@ func TestVeleroResourcesExist(t *testing.T) {
Kind: kind,
})
}
assert.NoError(t, server.veleroResourcesExist())
require.NoError(t, server.veleroResourcesExist())
// Velero API group contains some but not all custom resources: should error
veleroAPIResourceListVelerov1.APIResources = veleroAPIResourceListVelerov1.APIResources[:3]
@@ -169,7 +169,7 @@ func TestRemoveControllers(t *testing.T) {
if tt.errorExpected {
assert.Error(t, removeControllers(tt.disabledControllers, enabledRuntimeControllers, logger))
} else {
assert.NoError(t, removeControllers(tt.disabledControllers, enabledRuntimeControllers, logger))
require.NoError(t, removeControllers(tt.disabledControllers, enabledRuntimeControllers, logger))
totalNumEnabledControllers := len(enabledRuntimeControllers)
assert.Equal(t, totalNumEnabledControllers, totalNumOriginalControllers-len(tt.disabledControllers))
@@ -195,21 +195,21 @@ func Test_newServer(t *testing.T) {
_, err := newServer(factory, &config.Config{
UploaderType: "invalid",
}, logger)
assert.Error(t, err)
require.Error(t, err)
// invalid clientQPS
_, err = newServer(factory, &config.Config{
UploaderType: uploader.KopiaType,
ClientQPS: -1,
}, logger)
assert.Error(t, err)
require.Error(t, err)
// invalid clientQPS Restic uploader
_, err = newServer(factory, &config.Config{
UploaderType: uploader.ResticType,
ClientQPS: -1,
}, logger)
assert.Error(t, err)
require.Error(t, err)
// invalid clientBurst
factory.On("SetClientQPS", mock.Anything).Return()
@@ -218,7 +218,7 @@ func Test_newServer(t *testing.T) {
ClientQPS: 1,
ClientBurst: -1,
}, logger)
assert.Error(t, err)
require.Error(t, err)
// invalid clientBclientPageSizeurst
factory.On("SetClientQPS", mock.Anything).Return().
@@ -229,7 +229,7 @@ func Test_newServer(t *testing.T) {
ClientBurst: 1,
ClientPageSize: -1,
}, logger)
assert.Error(t, err)
require.Error(t, err)
// got error when creating client
factory.On("SetClientQPS", mock.Anything).Return().
@@ -258,7 +258,7 @@ func Test_namespaceExists(t *testing.T) {
}
// namespace doesn't exist
assert.Error(t, server.namespaceExists("not-exist"))
require.Error(t, server.namespaceExists("not-exist"))
// namespace exists
assert.NoError(t, server.namespaceExists("velero"))
@@ -273,7 +273,7 @@ func Test_veleroResourcesExist(t *testing.T) {
// velero resources don't exist
helper.On("Resources").Return(nil)
assert.Error(t, server.veleroResourcesExist())
require.Error(t, server.veleroResourcesExist())
// velero resources exist
helper.On("Resources").Unset()
@@ -421,7 +421,7 @@ func Test_setDefaultBackupLocation(t *testing.T) {
// no default location specified
c = fake.NewClientBuilder().WithScheme(scheme).Build()
err := setDefaultBackupLocation(context.Background(), c, "velero", "", logrus.New())
assert.NoError(t, err)
require.NoError(t, err)
// no default location created
err = setDefaultBackupLocation(context.Background(), c, "velero", "default", logrus.New())

View File

@@ -14,7 +14,7 @@ func TestStringOfEnum(t *testing.T) {
func TestSetOfEnum(t *testing.T) {
enum := NewEnum("a", "a", "b", "c")
assert.Error(t, enum.Set("d"))
require.Error(t, enum.Set("d"))
require.NoError(t, enum.Set("b"))
assert.Equal(t, "b", enum.String())

View File

@@ -4,6 +4,7 @@ import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestStringOfOptionalBool(t *testing.T) {
@@ -25,16 +26,16 @@ func TestStringOfOptionalBool(t *testing.T) {
func TestSetOfOptionalBool(t *testing.T) {
// error
ob := NewOptionalBool(nil)
assert.Error(t, ob.Set("invalid"))
require.Error(t, ob.Set("invalid"))
// nil
ob = NewOptionalBool(nil)
assert.NoError(t, ob.Set(""))
require.NoError(t, ob.Set(""))
assert.Nil(t, ob.Value)
// true
ob = NewOptionalBool(nil)
assert.NoError(t, ob.Set("true"))
require.NoError(t, ob.Set("true"))
assert.True(t, *ob.Value)
}

View File

@@ -3,6 +3,7 @@ package output
import (
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"k8s.io/apimachinery/pkg/runtime"
velerov1 "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
@@ -372,9 +373,9 @@ func TestPrintWithFormat(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
p, err := PrintWithFormat(tc.input.cmd, tc.input.obj)
if tc.hasErr {
assert.Error(t, err)
require.Error(t, err)
} else {
assert.NoError(t, err)
require.NoError(t, err)
}
assert.Equal(t, tc.printed, p)
})