Skip to content

Commit

Permalink
Merge pull request #2880 from alexandear/test/refactor-to-use-assert
Browse files Browse the repository at this point in the history
test: refactor to use assert pkg instead of t.Fatal
  • Loading branch information
jandubois authored Nov 8, 2024
2 parents 97ef064 + e4073ad commit 784625e
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 60 deletions.
9 changes: 9 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 2 additions & 3 deletions pkg/downloader/fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"testing"

"github.com/opencontainers/go-digest"
"gotest.tools/v3/assert"
)

var algorithm = digest.Algorithm("sha256")
Expand All @@ -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)
Expand Down
10 changes: 4 additions & 6 deletions pkg/guestagent/iptables/iptables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions pkg/iso9660util/fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
4 changes: 1 addition & 3 deletions pkg/limayaml/limayaml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
10 changes: 4 additions & 6 deletions pkg/lockutil/lockutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"path/filepath"
"strings"
"testing"

"gotest.tools/v3/assert"
)

const parallel = 20
Expand Down Expand Up @@ -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")
}
6 changes: 3 additions & 3 deletions pkg/nativeimgutil/fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ import (
"os"
"path/filepath"
"testing"

"gotest.tools/v3/assert"
)

func FuzzConvertToRaw(f *testing.F) {
f.Fuzz(func(t *testing.T, imgData []byte, withBacking bool, size int64) {
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)
})
}
17 changes: 5 additions & 12 deletions pkg/store/fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
Expand All @@ -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")
})
}
34 changes: 10 additions & 24 deletions pkg/vz/network_darwin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"net"
"path/filepath"
"testing"

"gotest.tools/v3/assert"
)

const (
Expand All @@ -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())

Expand All @@ -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()
Expand Down Expand Up @@ -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)
}
}

Expand Down

0 comments on commit 784625e

Please sign in to comment.