Skip to content

Commit

Permalink
build, linter, errcheck: lint tests/libstorage
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 cdbc0a9 commit e62fc06
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 2 deletions.
1 change: 0 additions & 1 deletion nogo_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,6 @@
"cmd/": "KubeVirt binaries do not pass errcheck yet",
"tools/": "KubeVirt tools do not pass errcheck yet",
"tests/console": "tests/console does not pass errcheck yet",
"tests/libstorage/": "tests/libstorage does not pass errcheck yet",
"tests/monitoring/": "tests/monitoring does not pass errcheck yet",
"tests/network/": "tests/network/ pkg does not pass errcheck yet",
"tests/reporter/": "tests/reporter pkg does not pass errcheck yet",
Expand Down
9 changes: 9 additions & 0 deletions tests/errorhandling/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "go_default_library",
srcs = ["closing_files.go"],
importpath = "kubevirt.io/kubevirt/tests/errorhandling",
visibility = ["//visibility:public"],
deps = ["//vendor/golang.org/x/sys/unix:go_default_library"],
)
23 changes: 23 additions & 0 deletions tests/errorhandling/closing_files.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package errorhandling

import (
"os"

"golang.org/x/sys/unix"
)

func SafelyCloseFile(f *os.File) {
if err := f.Close(); err != nil {
panicIfNotReadOnlyFile(f)
}
}

func panicIfNotReadOnlyFile(f *os.File) {
fileFlags, err := unix.FcntlInt(f.Fd(), unix.F_GETFD, 0)
if err != nil {
panic("could not access file's FD flags")
}
if fileFlags&(unix.O_WRONLY|unix.O_APPEND|unix.O_RDWR) != 0 {
panic("this is not a read-only fd")
}
}
1 change: 1 addition & 0 deletions tests/libstorage/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ go_library(
"//pkg/util/net/ip:go_default_library",
"//staging/src/kubevirt.io/api/core/v1:go_default_library",
"//staging/src/kubevirt.io/client-go/kubecli:go_default_library",
"//tests/errorhandling:go_default_library",
"//tests/flags:go_default_library",
"//tests/framework/cleanup:go_default_library",
"//tests/libnode:go_default_library",
Expand Down
3 changes: 2 additions & 1 deletion tests/libstorage/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"io/ioutil"
"os"

"kubevirt.io/kubevirt/tests/errorhandling"
"kubevirt.io/kubevirt/tests/flags"
)

Expand Down Expand Up @@ -56,7 +57,7 @@ func LoadConfig() (*KubeVirtTestsConfiguration, error) {
return nil, err
}

defer jsonFile.Close()
defer errorhandling.SafelyCloseFile(jsonFile)

// read the configuration file as a byte array
byteValue, _ := ioutil.ReadAll(jsonFile)
Expand Down

0 comments on commit e62fc06

Please sign in to comment.