From 94b62b2c8893b8d758c885d02aeb4fd4c3f32b8b Mon Sep 17 00:00:00 2001 From: Miguel Duarte Barroso Date: Wed, 6 Jul 2022 16:56:27 +0200 Subject: [PATCH] build, linter, errcheck: lint tests/storage Signed-off-by: Miguel Duarte Barroso --- nogo_config.json | 1 - tests/storage/BUILD.bazel | 1 + tests/storage/datavolume.go | 4 ++- tests/storage/export.go | 47 ++++++++++++++++++++++++------------ tests/storage/imageupload.go | 7 +++--- 5 files changed, 40 insertions(+), 20 deletions(-) diff --git a/nogo_config.json b/nogo_config.json index d553e2768237..eeea8288ef49 100644 --- a/nogo_config.json +++ b/nogo_config.json @@ -152,7 +152,6 @@ "tests/console": "tests/console does not pass errcheck yet", "tests/monitoring/": "tests/monitoring does not pass errcheck yet", "tests/reporter/": "tests/reporter pkg does not pass errcheck yet", - "tests/storage/": "tests/storage/ pkg does not pass errcheck yet", "tests/canary_upgrade_test.go": "canary_upgrade_test does not pass errcheck yet", "tests/infra_test.go": "infra_test.go does not pass errcheck yet", "tests/kubectl_test.go": "kubectl_test does not pass errcheck yet", diff --git a/tests/storage/BUILD.bazel b/tests/storage/BUILD.bazel index 2718872e30e1..34c9831aae9e 100644 --- a/tests/storage/BUILD.bazel +++ b/tests/storage/BUILD.bazel @@ -38,6 +38,7 @@ go_library( "//tests/clientcmd:go_default_library", "//tests/console:go_default_library", "//tests/containerdisk:go_default_library", + "//tests/errorhandling:go_default_library", "//tests/flags:go_default_library", "//tests/framework/checks:go_default_library", "//tests/framework/matcher:go_default_library", diff --git a/tests/storage/datavolume.go b/tests/storage/datavolume.go index 718537422dd2..510a009e9a24 100644 --- a/tests/storage/datavolume.go +++ b/tests/storage/datavolume.go @@ -899,7 +899,9 @@ var _ = SIGDescribe("DataVolume Integration", func() { []string{"ls", "-s", "/var/run/kubevirt-private/vmi-disks/disk0/disk.img"}, ) Expect(err).ToNot(HaveOccurred()) - fmt.Sscanf(lsOutput, "%d %s", &imageSize, &unused) + if _, err := fmt.Sscanf(lsOutput, "%d %s", &imageSize, &unused); err != nil { + return 0 + } return imageSize } diff --git a/tests/storage/export.go b/tests/storage/export.go index 394ab78c03f1..bb504eff7c50 100644 --- a/tests/storage/export.go +++ b/tests/storage/export.go @@ -252,9 +252,9 @@ var _ = SIGDescribe("Export", func() { }, 30*time.Second, 1*time.Second).Should(Equal(k8sv1.ClaimBound)) By("Deleting the trigger pod") immediate := int64(0) - virtClient.CoreV1().Pods(triggerPod.Namespace).Delete(context.Background(), triggerPod.Name, metav1.DeleteOptions{ + Expect(virtClient.CoreV1().Pods(triggerPod.Namespace).Delete(context.Background(), triggerPod.Name, metav1.DeleteOptions{ GracePeriodSeconds: &immediate, - }) + })).To(Succeed()) } createExportTokenSecret := func(name, namespace string) *k8sv1.Secret { @@ -635,12 +635,12 @@ var _ = SIGDescribe("Export", func() { } matchesCNOrAlt := func(cert *x509.Certificate, hostName string) bool { - fmt.Fprintf(GinkgoWriter, "CN: %s, hostname: %s\n", cert.Subject.CommonName, hostName) + logToGinkgoWritter("CN: %s, hostname: %s\n", cert.Subject.CommonName, hostName) if strings.Contains(cert.Subject.CommonName, hostName) { return true } for _, extension := range cert.Extensions { - fmt.Fprintf(GinkgoWriter, "ExtensionID: %s, subjectAltNameId: %s, value: %s, hostname: %s\n", extension.Id.String(), subjectAltNameId, string(extension.Value), hostName) + logToGinkgoWritter("ExtensionID: %s, subjectAltNameId: %s, value: %s, hostname: %s\n", extension.Id.String(), subjectAltNameId, string(extension.Value), hostName) if extension.Id.String() == subjectAltNameId && strings.Contains(string(extension.Value), hostName) { return true } @@ -835,23 +835,33 @@ var _ = SIGDescribe("Export", func() { } }) - generateTestCert := func(hostName string) string { + generateTestCert := func(hostName string) (string, error) { key, err := certutil.NewPrivateKey() - Expect(err).ToNot(HaveOccurred()) + if err != nil { + return "", err + } config := certutil.Config{ CommonName: "blah blah", } cert, err := certutil.NewSelfSignedCACertWithAltNames(config, key, time.Hour, "hahaha.wwoo", hostName, "fgdgd.dfsgdf") - Expect(err).ToNot(HaveOccurred()) + if err != nil { + return "", err + } + pemOut := strings.Builder{} - pem.Encode(&pemOut, &pem.Block{Type: "CERTIFICATE", Bytes: cert.Raw}) - return strings.TrimSpace(pemOut.String()) + if err := pem.Encode(&pemOut, &pem.Block{Type: "CERTIFICATE", Bytes: cert.Raw}); err != nil { + return "", err + } + return strings.TrimSpace(pemOut.String()), nil } - createIngressTLSSecret := func(name string) string { - testCert := generateTestCert(testHostName) + createIngressTLSSecret := func(name string) (string, error) { + testCert, err := generateTestCert(testHostName) + if err != nil { + return "", err + } secret := &k8sv1.Secret{ ObjectMeta: metav1.ObjectMeta{ Name: name, @@ -862,9 +872,11 @@ var _ = SIGDescribe("Export", func() { tlsCert: testCert, }, } - _, err := virtClient.CoreV1().Secrets(flags.KubeVirtInstallNamespace).Create(context.Background(), secret, metav1.CreateOptions{}) - Expect(err).ToNot(HaveOccurred()) - return testCert + _, err = virtClient.CoreV1().Secrets(flags.KubeVirtInstallNamespace).Create(context.Background(), secret, metav1.CreateOptions{}) + if err != nil { + return "", err + } + return testCert, nil } createIngress := func(tlsSecretName string) *networkingv1.Ingress { @@ -927,7 +939,8 @@ var _ = SIGDescribe("Export", func() { if !exists { Skip("Skip test when Filesystem storage is not present") } - testCert := createIngressTLSSecret(tlsSecretName) + testCert, err := createIngressTLSSecret(tlsSecretName) + Expect(err).NotTo(HaveOccurred()) ingress := createIngress(tlsSecretName) vmExport := createRunningExport(sc, k8sv1.PersistentVolumeFilesystem) Expect(vmExport.Status.Links.External.Cert).To(Equal(testCert)) @@ -974,3 +987,7 @@ var _ = SIGDescribe("Export", func() { }) }) }) + +func logToGinkgoWritter(format string, parameters ...interface{}) { + _, _ = fmt.Fprintf(GinkgoWriter, format, parameters...) +} diff --git a/tests/storage/imageupload.go b/tests/storage/imageupload.go index ff5915d4128e..049dd8a223f0 100644 --- a/tests/storage/imageupload.go +++ b/tests/storage/imageupload.go @@ -24,6 +24,7 @@ import ( "kubevirt.io/kubevirt/tests" "kubevirt.io/kubevirt/tests/clientcmd" + "kubevirt.io/kubevirt/tests/errorhandling" "kubevirt.io/kubevirt/tests/flags" "kubevirt.io/kubevirt/tests/libstorage" ) @@ -291,8 +292,8 @@ var _ = SIGDescribe("[Serial]ImageUpload", func() { AfterEach(func() { if kubectlCmd != nil { - kubectlCmd.Process.Kill() - kubectlCmd.Wait() + Expect(kubectlCmd.Process.Kill()).To(Succeed()) + Expect(kubectlCmd.Wait()).To(Succeed()) } err := os.Remove(imagePath) @@ -304,7 +305,7 @@ func createArchive(targetFile, tgtDir string, sourceFilesNames ...string) string tgtPath := filepath.Join(tgtDir, filepath.Base(targetFile)+".tar") tgtFile, err := os.Create(tgtPath) Expect(err).ToNot(HaveOccurred()) - defer tgtFile.Close() + defer errorhandling.SafelyCloseFile(tgtFile) tests.ArchiveToFile(tgtFile, sourceFilesNames...)