From e4073addb559e209f7d57a35493781aaacd0bfd3 Mon Sep 17 00:00:00 2001 From: Oleksandr Redko Date: Fri, 8 Nov 2024 13:54:11 +0200 Subject: [PATCH] test: refactor to use assert pkg instead of t.Fatal Signed-off-by: Oleksandr Redko --- .golangci.yml | 9 +++++++ pkg/downloader/fuzz_test.go | 5 ++-- pkg/guestagent/iptables/iptables_test.go | 10 +++---- pkg/iso9660util/fuzz_test.go | 6 ++--- pkg/limayaml/limayaml_test.go | 4 +-- pkg/lockutil/lockutil_test.go | 10 +++---- pkg/nativeimgutil/fuzz_test.go | 6 ++--- pkg/store/fuzz_test.go | 17 ++++-------- pkg/vz/network_darwin_test.go | 34 +++++++----------------- 9 files changed, 41 insertions(+), 60 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 44b4f9d10e3..2376633fe37 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -88,6 +88,15 @@ linters-settings: deny: - pkg: "golang.org/x/net/context" desc: "use the 'context' package from the standard library" + forbidigo: + analyze-types: true + forbid: + - p: ^print(ln)?$ + msg: Do not use builtin print functions. It's for bootstrapping only and may be removed in the future. + - p: ^fmt\.Print.*$ + msg: Do not use fmt.Print statements. + - p: ^testing.T.Fatal.*$ + msg: Use assert functions from the gotest.tools/v3/assert package instead. gocritic: # See "Tags" section in https://github.com/go-critic/go-critic#usage enabled-tags: diff --git a/pkg/downloader/fuzz_test.go b/pkg/downloader/fuzz_test.go index e54b8346444..e52f28ae794 100644 --- a/pkg/downloader/fuzz_test.go +++ b/pkg/downloader/fuzz_test.go @@ -7,6 +7,7 @@ import ( "testing" "github.com/opencontainers/go-digest" + "gotest.tools/v3/assert" ) var algorithm = digest.Algorithm("sha256") @@ -16,9 +17,7 @@ func FuzzDownload(f *testing.F) { localFile := filepath.Join(t.TempDir(), "localFile") remoteFile := filepath.Join(t.TempDir(), "remoteFile") err := os.WriteFile(remoteFile, fileContents, 0o600) - if err != nil { - t.Fatal(err) - } + assert.NilError(t, err) testLocalFileURL := "file://" + remoteFile if checkDigest { d := algorithm.FromBytes(fileContents) diff --git a/pkg/guestagent/iptables/iptables_test.go b/pkg/guestagent/iptables/iptables_test.go index 22f96c354e2..263d2ec3a39 100644 --- a/pkg/guestagent/iptables/iptables_test.go +++ b/pkg/guestagent/iptables/iptables_test.go @@ -3,6 +3,8 @@ package iptables import ( "strings" "testing" + + "gotest.tools/v3/assert" ) // data is from a run of `iptables -t nat -S` with two containers running (started @@ -74,14 +76,10 @@ func TestParsePortsFromRules(t *testing.T) { } res, err := parsePortsFromRules(rules) - if err != nil { - t.Errorf("parsing iptables ports failed with error: %s", err) - } + assert.NilError(t, err, "parsing iptables ports failed") l := len(res) - if l != 2 { - t.Fatalf("expected 2 ports parsed from iptables but parsed %d", l) - } + assert.Equal(t, l, 2, "unexpected number of ports parsed from iptables") if res[0].IP.String() != "0.0.0.0" || res[0].Port != 8082 || res[0].TCP != true { t.Errorf("expected port 8082 on IP 0.0.0.0 with TCP true but got port %d on IP %s with TCP %t", res[0].Port, res[0].IP.String(), res[0].TCP) diff --git a/pkg/iso9660util/fuzz_test.go b/pkg/iso9660util/fuzz_test.go index b357c78443d..b07e24e3857 100644 --- a/pkg/iso9660util/fuzz_test.go +++ b/pkg/iso9660util/fuzz_test.go @@ -4,15 +4,15 @@ import ( "os" "path/filepath" "testing" + + "gotest.tools/v3/assert" ) func FuzzIsISO9660(f *testing.F) { f.Fuzz(func(t *testing.T, fileContents []byte) { imageFile := filepath.Join(t.TempDir(), "fuzz.iso") err := os.WriteFile(imageFile, fileContents, 0o600) - if err != nil { - t.Fatal(err) - } + assert.NilError(t, err) _, _ = IsISO9660(imageFile) }) } diff --git a/pkg/limayaml/limayaml_test.go b/pkg/limayaml/limayaml_test.go index 706cbb1c4ed..d6e01d4c54e 100644 --- a/pkg/limayaml/limayaml_test.go +++ b/pkg/limayaml/limayaml_test.go @@ -10,9 +10,7 @@ import ( func dumpJSON(t *testing.T, d interface{}) string { b, err := json.Marshal(d) - if err != nil { - t.Fatal(err) - } + assert.NilError(t, err) return string(b) } diff --git a/pkg/lockutil/lockutil_test.go b/pkg/lockutil/lockutil_test.go index 210110c4dac..25225152b72 100644 --- a/pkg/lockutil/lockutil_test.go +++ b/pkg/lockutil/lockutil_test.go @@ -7,6 +7,8 @@ import ( "path/filepath" "strings" "testing" + + "gotest.tools/v3/assert" ) const parallel = 20 @@ -46,11 +48,7 @@ func TestWithDirLock(t *testing.T) { } data, err := os.ReadFile(log) - if err != nil { - t.Fatal(err) - } + assert.NilError(t, err) lines := strings.Split(strings.Trim(string(data), "\n"), "\n") - if len(lines) != 1 { - t.Errorf("Expected one writer, got %v", lines) - } + assert.Equal(t, len(lines), 1, "unexpected number of writers") } diff --git a/pkg/nativeimgutil/fuzz_test.go b/pkg/nativeimgutil/fuzz_test.go index 0a840d37f2d..82d021d6d58 100644 --- a/pkg/nativeimgutil/fuzz_test.go +++ b/pkg/nativeimgutil/fuzz_test.go @@ -4,6 +4,8 @@ import ( "os" "path/filepath" "testing" + + "gotest.tools/v3/assert" ) func FuzzConvertToRaw(f *testing.F) { @@ -11,9 +13,7 @@ func FuzzConvertToRaw(f *testing.F) { srcPath := filepath.Join(t.TempDir(), "src.img") destPath := filepath.Join(t.TempDir(), "dest.img") err := os.WriteFile(srcPath, imgData, 0o600) - if err != nil { - t.Fatal(err) - } + assert.NilError(t, err) _ = ConvertToRaw(srcPath, destPath, &size, withBacking) }) } diff --git a/pkg/store/fuzz_test.go b/pkg/store/fuzz_test.go index ecb0f1163c0..f4527891896 100644 --- a/pkg/store/fuzz_test.go +++ b/pkg/store/fuzz_test.go @@ -6,15 +6,14 @@ import ( "testing" "github.com/lima-vm/lima/pkg/store/filenames" + "gotest.tools/v3/assert" ) func FuzzLoadYAMLByFilePath(f *testing.F) { f.Fuzz(func(t *testing.T, fileContents []byte) { localFile := filepath.Join(t.TempDir(), "yaml_file.yml") err := os.WriteFile(localFile, fileContents, 0o600) - if err != nil { - t.Fatal(err) - } + assert.NilError(t, err) _, _ = LoadYAMLByFilePath(localFile) }) } @@ -24,19 +23,13 @@ func FuzzInspect(f *testing.F) { limaDir := t.TempDir() t.Setenv("LIMA_HOME", limaDir) err := os.MkdirAll(filepath.Join(limaDir, "fuzz-instance"), 0o700) - if err != nil { - t.Fatal(err) - } + assert.NilError(t, err) ymlFile := filepath.Join(limaDir, "fuzz-instance", filenames.LimaYAML) limaVersionFile := filepath.Join(limaDir, filenames.LimaVersion) err = os.WriteFile(ymlFile, yml, 0o600) - if err != nil { - t.Fatal(err) - } + assert.NilError(t, err) err = os.WriteFile(limaVersionFile, limaVersion, 0o600) - if err != nil { - t.Fatal(err) - } + assert.NilError(t, err) _, _ = Inspect("fuzz-instance") }) } diff --git a/pkg/vz/network_darwin_test.go b/pkg/vz/network_darwin_test.go index 4c52c4daf08..ead4135e829 100644 --- a/pkg/vz/network_darwin_test.go +++ b/pkg/vz/network_darwin_test.go @@ -8,6 +8,8 @@ import ( "net" "path/filepath" "testing" + + "gotest.tools/v3/assert" ) const ( @@ -17,9 +19,7 @@ const ( func TestDialQemu(t *testing.T) { listener, err := listenUnix(t.TempDir()) - if err != nil { - t.Fatal(err) - } + assert.NilError(t, err) defer listener.Close() t.Logf("Listening at %q", listener.Addr()) @@ -34,15 +34,11 @@ func TestDialQemu(t *testing.T) { // Connect to the fake vmnet server. client, err := DialQemu(listener.Addr().String()) - if err != nil { - t.Fatal(err) - } + assert.NilError(t, err) t.Log("Connected to fake vment server") dgramConn, err := net.FileConn(client) - if err != nil { - t.Fatal(err) - } + assert.NilError(t, err) vzConn := packetConn{Conn: dgramConn} defer vzConn.Close() @@ -85,31 +81,21 @@ func TestDialQemu(t *testing.T) { t.Logf("Receiving and verifying data packets...") for i := 0; i < packetsCount; i++ { n, err := vzConn.Read(buf) - if err != nil { - t.Fatal(err) - } - if n < vmnetMaxPacketSize { - t.Fatalf("Expected %d bytes, got %d", vmnetMaxPacketSize, n) - } + assert.NilError(t, err) + assert.Assert(t, n >= vmnetMaxPacketSize, "unexpected number of bytes") number := binary.BigEndian.Uint32(buf[:4]) - if number != uint32(i) { - t.Fatalf("Expected packet %d, got packet %d", i, number) - } + assert.Equal(t, number, uint32(i), "unexpected packet") for j := 4; j < vmnetMaxPacketSize; j++ { - if buf[j] != 0x55 { - t.Fatalf("Expected byte 0x55 at offset %d, got 0x%02x", j, buf[j]) - } + assert.Equal(t, buf[j], byte(0x55), "unexpected byte at offset %d", j) } } t.Logf("Received and verified %d data packets", packetsCount) for i := 0; i < 2; i++ { err := <-errc - if err != nil { - t.Fatal(err) - } + assert.NilError(t, err) } }