mirror of
https://github.com/lima-vm/lima.git
synced 2026-10-28 11:44:35 +08:00
test: refactor to use assert pkg instead of t.Fatal
Signed-off-by: Oleksandr Redko <oleksandr.red+github@gmail.com>
This commit is contained in:
9 files changed
+41
-60
No files matched your search
@@ -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:
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
})
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
@@ -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)
|
||||
})
|
||||
}
|
||||
+5
-12
@@ -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")
|
||||
})
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user