From 990d3cbe39f4c5749bf753f60d6cc24b51b8de6b Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Tue, 15 Jun 2021 14:08:13 +0200 Subject: pkg/host: fix globs test It creates a temp dir in cwd, which is not guaranteed to be writable. Create temp dir in temp instead. Also don't assume Linux path separator, won't work on Windows. Also actually check the result, current test would be happy if glob always returns empty match as well. --- pkg/host/machine_info_linux_test.go | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) (limited to 'pkg') diff --git a/pkg/host/machine_info_linux_test.go b/pkg/host/machine_info_linux_test.go index 47a565d7e..fdb3f3011 100644 --- a/pkg/host/machine_info_linux_test.go +++ b/pkg/host/machine_info_linux_test.go @@ -7,11 +7,14 @@ import ( "bufio" "bytes" "fmt" + "io/ioutil" "os" + "path/filepath" "runtime" "strings" "testing" + "github.com/google/go-cmp/cmp" "github.com/google/syzkaller/pkg/osutil" "github.com/google/syzkaller/sys/targets" ) @@ -382,32 +385,37 @@ power management: } func TestGetGlobsInfo(t *testing.T) { - if err := osutil.MkdirAll("globstest/a/b/c/d"); err != nil { + dir, err := ioutil.TempDir("", "syz-host-globstest") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(dir) + if err := osutil.MkdirAll(filepath.Join(dir, "a", "b", "c", "d")); err != nil { t.Fatal(err) } - if err := osutil.MkdirAll("globstest/a/b/c/e"); err != nil { + if err := osutil.MkdirAll(filepath.Join(dir, "a", "b", "c", "e")); err != nil { t.Fatal(err) } - if err := osutil.MkdirAll("globstest/a/c/d"); err != nil { + if err := osutil.MkdirAll(filepath.Join(dir, "a", "c", "d")); err != nil { t.Fatal(err) } - if err := osutil.MkdirAll("globstest/a/c/e"); err != nil { + if err := osutil.MkdirAll(filepath.Join(dir, "a", "c", "e")); err != nil { t.Fatal(err) } - defer os.RemoveAll("globstest") + glob := filepath.Join(dir, "a/**/*") + ":-" + filepath.Join(dir, "a/c/e") globs := map[string]bool{ - "globstest/a/**/*:-globstest/a/c/e": true, + glob: true, } infos, err := getGlobsInfo(globs) if err != nil { t.Fatal(err) } - for _, files := range infos { - for _, file := range files { - if file == "globstest/a/c/e" { - t.Fatal("failed to exclude globstest/a/c/e") - } - } + want := []string{ + filepath.Join(dir, "a/b/c"), + filepath.Join(dir, "a/c/d"), + } + if diff := cmp.Diff(infos[glob], want); diff != "" { + t.Fatal(diff) } } -- cgit mrf-deployment