diff --git a/internal/hook/wait_exec_hook_handler_test.go b/internal/hook/wait_exec_hook_handler_test.go index 922df19db..933e5d34d 100644 --- a/internal/hook/wait_exec_hook_handler_test.go +++ b/internal/hook/wait_exec_hook_handler_test.go @@ -738,7 +738,9 @@ func TestWaitExecHandleHooks(t *testing.T) { ctx := context.Background() if test.sharedHooksContextTimeout > 0 { - ctx, _ = context.WithTimeout(ctx, test.sharedHooksContextTimeout) + var ctxCancel context.CancelFunc + ctx, ctxCancel = context.WithTimeout(ctx, test.sharedHooksContextTimeout) + defer ctxCancel() } errs := h.HandleHooks(ctx, velerotest.NewLogger(), test.initialPod, test.byContainer) diff --git a/test/e2e/backups/deletion.go b/test/e2e/backups/deletion.go index 3d70948d8..aaef02652 100644 --- a/test/e2e/backups/deletion.go +++ b/test/e2e/backups/deletion.go @@ -91,7 +91,8 @@ func backup_deletion_test(useVolumeSnapshots bool) { // runUpgradeTests runs upgrade test on the provider by kibishii. func runBackupDeletionTests(client TestClient, veleroCfg VeleroConfig, backupName, backupLocation string, useVolumeSnapshots bool, kibishiiDirectory string) error { - oneHourTimeout, _ := context.WithTimeout(context.Background(), time.Minute*60) + oneHourTimeout, ctxCancel := context.WithTimeout(context.Background(), time.Minute*60) + defer ctxCancel() veleroCLI := veleroCfg.VeleroCLI providerName := veleroCfg.CloudProvider veleroNamespace := veleroCfg.VeleroNamespace diff --git a/test/e2e/backups/sync_backups.go b/test/e2e/backups/sync_backups.go index 1de243414..d381e38f8 100644 --- a/test/e2e/backups/sync_backups.go +++ b/test/e2e/backups/sync_backups.go @@ -40,6 +40,7 @@ type SyncBackups struct { testNS string backupName string ctx context.Context + ctxCancel context.CancelFunc } func (b *SyncBackups) Init() { @@ -47,7 +48,7 @@ func (b *SyncBackups) Init() { UUIDgen, _ = uuid.NewRandom() b.testNS = "sync-bsl-test-" + UUIDgen.String() b.backupName = "sync-bsl-test-" + UUIDgen.String() - b.ctx, _ = context.WithTimeout(context.Background(), time.Minute*10) + b.ctx, b.ctxCancel = context.WithTimeout(context.Background(), time.Minute*10) } func BackupsSyncTest() { @@ -79,6 +80,7 @@ func BackupsSyncTest() { It("Backups in object storage should be synced to a new Velero successfully", func() { test.Init() + defer test.ctxCancel() By(fmt.Sprintf("Prepare workload as target to backup by creating namespace %s namespace", test.testNS)) Expect(CreateNamespace(test.ctx, *VeleroCfg.ClientToInstallVelero, test.testNS)).To(Succeed(), fmt.Sprintf("Failed to create %s namespace", test.testNS)) @@ -119,6 +121,7 @@ func BackupsSyncTest() { It("Deleted backups in object storage are synced to be deleted in Velero", func() { test.Init() + defer test.ctxCancel() By(fmt.Sprintf("Prepare workload as target to backup by creating namespace in %s namespace", test.testNS), func() { Expect(CreateNamespace(test.ctx, *VeleroCfg.ClientToInstallVelero, test.testNS)).To(Succeed(), fmt.Sprintf("Failed to create %s namespace", test.testNS)) diff --git a/test/e2e/backups/ttl.go b/test/e2e/backups/ttl.go index a513d22ca..322201a33 100644 --- a/test/e2e/backups/ttl.go +++ b/test/e2e/backups/ttl.go @@ -43,6 +43,7 @@ type TTL struct { backupName string restoreName string ctx context.Context + ctxCancel context.CancelFunc ttl time.Duration } @@ -52,7 +53,7 @@ func (b *TTL) Init() { b.testNS = "backup-ttl-test-" + UUIDgen.String() b.backupName = "backup-ttl-test-" + UUIDgen.String() b.restoreName = "restore-ttl-test-" + UUIDgen.String() - b.ctx, _ = context.WithTimeout(context.Background(), 2*time.Hour) + b.ctx, b.ctxCancel = context.WithTimeout(context.Background(), 2*time.Hour) b.ttl = 20 * time.Minute } @@ -93,6 +94,7 @@ func TTLTest() { It("Backups in object storage should be synced to a new Velero successfully", func() { test.Init() + defer test.ctxCancel() By(fmt.Sprintf("Prepare workload as target to backup by creating namespace %s namespace", test.testNS), func() { Expect(CreateNamespace(test.ctx, client, test.testNS)).To(Succeed(), fmt.Sprintf("Failed to create %s namespace", test.testNS)) diff --git a/test/e2e/upgrade/upgrade.go b/test/e2e/upgrade/upgrade.go index 0bf0ccc96..c9f0c00ab 100644 --- a/test/e2e/upgrade/upgrade.go +++ b/test/e2e/upgrade/upgrade.go @@ -96,7 +96,8 @@ func BackupUpgradeRestoreTest(useVolumeSnapshots bool, veleroCLI2Version VeleroC flag.Parse() UUIDgen, err = uuid.NewRandom() Expect(err).To(Succeed()) - oneHourTimeout, _ := context.WithTimeout(context.Background(), time.Minute*60) + oneHourTimeout, ctxCancel := context.WithTimeout(context.Background(), time.Minute*60) + defer ctxCancel() supportUploaderType, err := IsSupportUploaderType(veleroCLI2Version.VeleroVersion) Expect(err).To(Succeed()) if veleroCLI2Version.VeleroCLI == "" { diff --git a/test/e2e/util/k8s/namespace.go b/test/e2e/util/k8s/namespace.go index 3135549a3..e19582dd7 100644 --- a/test/e2e/util/k8s/namespace.go +++ b/test/e2e/util/k8s/namespace.go @@ -67,7 +67,8 @@ func GetNamespace(ctx context.Context, client TestClient, namespace string) (*co } func DeleteNamespace(ctx context.Context, client TestClient, namespace string, wait bool) error { - tenMinuteTimeout, _ := context.WithTimeout(context.Background(), time.Minute*10) + tenMinuteTimeout, ctxCancel := context.WithTimeout(context.Background(), time.Minute*10) + defer ctxCancel() if err := client.ClientGo.CoreV1().Namespaces().Delete(context.TODO(), namespace, metav1.DeleteOptions{}); err != nil { return errors.Wrap(err, fmt.Sprintf("failed to delete the namespace %q", namespace)) } diff --git a/test/e2e/util/kibishii/kibishii_utils.go b/test/e2e/util/kibishii/kibishii_utils.go index 950599735..2f103508a 100644 --- a/test/e2e/util/kibishii/kibishii_utils.go +++ b/test/e2e/util/kibishii/kibishii_utils.go @@ -55,7 +55,8 @@ var KibishiiPodNameList = []string{"kibishii-deployment-0", "kibishii-deployment func RunKibishiiTests(veleroCfg VeleroConfig, backupName, restoreName, backupLocation, kibishiiNamespace string, useVolumeSnapshots, defaultVolumesToFsBackup bool) error { client := *veleroCfg.ClientToInstallVelero - oneHourTimeout, _ := context.WithTimeout(context.Background(), time.Minute*60) + oneHourTimeout, ctxCancel := context.WithTimeout(context.Background(), time.Minute*60) + defer ctxCancel() veleroCLI := veleroCfg.VeleroCLI providerName := veleroCfg.CloudProvider veleroNamespace := veleroCfg.VeleroNamespace @@ -221,7 +222,8 @@ func generateData(ctx context.Context, namespace string, kibishiiData *KibishiiD timeout := 30 * time.Minute interval := 1 * time.Second err := wait.PollImmediate(interval, timeout, func() (bool, error) { - timeout, _ := context.WithTimeout(context.Background(), time.Minute*20) + timeout, ctxCancel := context.WithTimeout(context.Background(), time.Minute*20) + defer ctxCancel() kibishiiGenerateCmd := exec.CommandContext(timeout, "kubectl", "exec", "-n", namespace, "jump-pad", "--", "/usr/local/bin/generate.sh", strconv.Itoa(kibishiiData.Levels), strconv.Itoa(kibishiiData.DirsPerLevel), strconv.Itoa(kibishiiData.FilesPerLevel), strconv.Itoa(kibishiiData.FileLength), @@ -246,7 +248,8 @@ func verifyData(ctx context.Context, namespace string, kibishiiData *KibishiiDat timeout := 10 * time.Minute interval := 5 * time.Second err := wait.PollImmediate(interval, timeout, func() (bool, error) { - timeout, _ := context.WithTimeout(context.Background(), time.Minute*20) + timeout, ctxCancel := context.WithTimeout(context.Background(), time.Minute*20) + defer ctxCancel() kibishiiVerifyCmd := exec.CommandContext(timeout, "kubectl", "exec", "-n", namespace, "jump-pad", "--", "/usr/local/bin/verify.sh", strconv.Itoa(kibishiiData.Levels), strconv.Itoa(kibishiiData.DirsPerLevel), strconv.Itoa(kibishiiData.FilesPerLevel), strconv.Itoa(kibishiiData.FileLength), diff --git a/test/e2e/util/providers/common.go b/test/e2e/util/providers/common.go index 8c37d79d9..eaeb5c3c1 100644 --- a/test/e2e/util/providers/common.go +++ b/test/e2e/util/providers/common.go @@ -138,7 +138,8 @@ func IsSnapshotExisted(cloudProvider, cloudCredentialsFile, bslBucket, bslConfig } if cloudProvider == "vsphere" { var retSnapshotIDs []string - ctx, _ := context.WithTimeout(context.Background(), time.Minute*2) + ctx, ctxCancel := context.WithTimeout(context.Background(), time.Minute*2) + defer ctxCancel() retSnapshotIDs, err = velero.GetVsphereSnapshotIDs(ctx, time.Hour, snapshotCheck.NamespaceBackedUp, snapshotCheck.PodName) if err != nil { return errors.Wrapf(err, fmt.Sprintf("Fail to get snapshot CRs of backup%s", backupName))