Skip to content

Commit

Permalink
build, linter, errcheck: lint tests/storage
Browse files Browse the repository at this point in the history
Signed-off-by: Miguel Duarte Barroso <[email protected]>
  • Loading branch information
maiqueb committed Aug 4, 2022
1 parent fba31a8 commit 94b62b2
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 20 deletions.
1 change: 0 additions & 1 deletion nogo_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions tests/storage/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 3 additions & 1 deletion tests/storage/datavolume.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
47 changes: 32 additions & 15 deletions tests/storage/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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,
Expand All @@ -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 {
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -974,3 +987,7 @@ var _ = SIGDescribe("Export", func() {
})
})
})

func logToGinkgoWritter(format string, parameters ...interface{}) {
_, _ = fmt.Fprintf(GinkgoWriter, format, parameters...)
}
7 changes: 4 additions & 3 deletions tests/storage/imageupload.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)
Expand All @@ -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...)

Expand Down

0 comments on commit 94b62b2

Please sign in to comment.