Skip to content

Commit e5a2b87

Browse files
committed
Refactor fuzz tests
Signed-off-by: Oleksandr Redko <[email protected]>
1 parent ac1fc4f commit e5a2b87

File tree

6 files changed

+18
-32
lines changed

6 files changed

+18
-32
lines changed

pkg/cidata/fuzz_test.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,23 @@
11
package cidata
22

33
import (
4-
"fmt"
54
"testing"
65

6+
"github.com/lima-vm/lima/pkg/limayaml"
77
"github.com/lima-vm/lima/pkg/networks"
88
"github.com/lima-vm/lima/pkg/ptr"
9-
10-
"github.com/lima-vm/lima/pkg/limayaml"
119
)
1210

1311
func FuzzSetupEnv(f *testing.F) {
1412
f.Fuzz(func(_ *testing.T, suffix string, localhost bool) {
15-
var prefix string
13+
prefix := "http://127.0.0.1:8080/"
1614
if localhost {
1715
prefix = "http://localhost:8080/"
18-
} else {
19-
prefix = "http://127.0.0.1:8080/"
2016
}
21-
envKey := "http_proxy"
22-
envValue := fmt.Sprintf("%s%s", prefix, suffix)
2317
templateArgs := TemplateArgs{SlirpGateway: networks.SlirpGateway}
24-
envAttr := map[string]string{envKey: envValue}
25-
_, _ = setupEnv(&limayaml.LimaYAML{PropagateProxyEnv: ptr.Of(false), Env: envAttr}, templateArgs)
18+
_, _ = setupEnv(&limayaml.LimaYAML{
19+
PropagateProxyEnv: ptr.Of(false),
20+
Env: map[string]string{"http_proxy": prefix + suffix},
21+
}, templateArgs)
2622
})
2723
}

pkg/downloader/fuzz_test.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"github.com/opencontainers/go-digest"
1010
)
1111

12-
var a = digest.Algorithm("sha256")
12+
var algorithm = digest.Algorithm("sha256")
1313

1414
func FuzzDownload(f *testing.F) {
1515
f.Fuzz(func(t *testing.T, fileContents []byte, checkDigest bool) {
@@ -21,15 +21,10 @@ func FuzzDownload(f *testing.F) {
2121
}
2222
testLocalFileURL := "file://" + remoteFile
2323
if checkDigest {
24-
d := a.FromBytes(fileContents)
25-
_, _ = Download(context.Background(),
26-
localFile,
27-
testLocalFileURL,
28-
WithExpectedDigest(d))
24+
d := algorithm.FromBytes(fileContents)
25+
_, _ = Download(context.Background(), localFile, testLocalFileURL, WithExpectedDigest(d))
2926
} else {
30-
_, _ = Download(context.Background(),
31-
localFile,
32-
testLocalFileURL)
27+
_, _ = Download(context.Background(), localFile, testLocalFileURL)
3328
}
3429
})
3530
}

pkg/guestagent/procnettcp/fuzz_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@ import (
77

88
func FuzzParse(f *testing.F) {
99
f.Fuzz(func(_ *testing.T, data []byte, tcp6 bool) {
10-
var kind Kind
10+
kind := TCP
1111
if tcp6 {
1212
kind = TCP6
13-
} else {
14-
kind = TCP
1513
}
1614
_, _ = Parse(bytes.NewReader(data), kind)
1715
})

pkg/iso9660util/fuzz_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ func FuzzIsISO9660(f *testing.F) {
1313
if err != nil {
1414
t.Fatal(err)
1515
}
16-
//nolint:errcheck // The test doesn't check the return value
17-
IsISO9660(imageFile)
16+
_, _ = IsISO9660(imageFile)
1817
})
1918
}

pkg/nativeimgutil/fuzz_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ func FuzzConvertToRaw(f *testing.F) {
1212
destPath := filepath.Join(t.TempDir(), "dest.img")
1313
err := os.WriteFile(srcPath, imgData, 0o600)
1414
if err != nil {
15-
return
15+
t.Fatal(err)
1616
}
1717
_ = ConvertToRaw(srcPath, destPath, &size, withBacking)
1818
})

pkg/store/fuzz_test.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,27 @@ func FuzzLoadYAMLByFilePath(f *testing.F) {
1515
if err != nil {
1616
t.Fatal(err)
1717
}
18-
//nolint:errcheck // The test doesn't check the return value
19-
LoadYAMLByFilePath(localFile)
18+
_, _ = LoadYAMLByFilePath(localFile)
2019
})
2120
}
2221

2322
func FuzzInspect(f *testing.F) {
2423
f.Fuzz(func(t *testing.T, yml, limaVersion []byte) {
2524
limaDir := t.TempDir()
26-
os.Setenv("LIMA_HOME", limaDir)
25+
t.Setenv("LIMA_HOME", limaDir)
2726
err := os.MkdirAll(filepath.Join(limaDir, "fuzz-instance"), 0o700)
2827
if err != nil {
29-
// panic so that we know of problems here
30-
panic(err)
28+
t.Fatal(err)
3129
}
3230
ymlFile := filepath.Join(limaDir, "fuzz-instance", filenames.LimaYAML)
3331
limaVersionFile := filepath.Join(limaDir, filenames.LimaVersion)
3432
err = os.WriteFile(ymlFile, yml, 0o600)
3533
if err != nil {
36-
return
34+
t.Fatal(err)
3735
}
3836
err = os.WriteFile(limaVersionFile, limaVersion, 0o600)
3937
if err != nil {
40-
return
38+
t.Fatal(err)
4139
}
4240
_, _ = Inspect("fuzz-instance")
4341
})

0 commit comments

Comments
 (0)