From af1f53cffae8a396dd4a897d291bd541e81c9481 Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Fri, 9 Oct 2020 10:31:13 +0200 Subject: pkg/host: refactor machine info tests Refactor tests so that they can be used with canned info. The test fails for lots of different archs and we don't have any tests, so these will keep breaking. This change prepared for tests with canned info. --- pkg/host/machine_info_linux_test.go | 55 ++++++++++++++----------------------- 1 file changed, 21 insertions(+), 34 deletions(-) (limited to 'pkg/host/machine_info_linux_test.go') diff --git a/pkg/host/machine_info_linux_test.go b/pkg/host/machine_info_linux_test.go index d318c08b3..682d8b397 100644 --- a/pkg/host/machine_info_linux_test.go +++ b/pkg/host/machine_info_linux_test.go @@ -11,42 +11,25 @@ import ( "testing" ) -func TestMachineInfoLinux(t *testing.T) { - result, err := CollectMachineInfo() - if err != nil { +func TestReadCPUInfoLinux(t *testing.T) { + buf := new(bytes.Buffer) + if err := readCPUInfo(buf); err != nil { t.Fatal(err) } - - scanner := bufio.NewScanner(bytes.NewReader(result)) - - for scanner.Scan() { - line := scanner.Text() - - if line == "[CPU Info]" { - checkCPUInfo(t, scanner) - } - if line == "[KVM]" { - checkKVMInfo(t, scanner) - } - } + checkCPUInfo(t, buf.Bytes(), runtime.GOARCH) } -func checkCPUInfo(t *testing.T, scanner *bufio.Scanner) { +func checkCPUInfo(t *testing.T, data []byte, arch string) { + t.Logf("input data:\n%s", data) keys := make(map[string]bool) - for scanner.Scan() { - line := scanner.Text() - // End of CPU Info section. - if strings.HasPrefix(line, "-----") { - break - } - splitted := strings.Split(line, ":") + for s := bufio.NewScanner(bytes.NewReader(data)); s.Scan(); { + splitted := strings.Split(s.Text(), ":") if len(splitted) != 2 { - t.Fatalf("the format of line \"%s\" is not correct", line) + t.Fatalf("the format of line %q is not correct", s.Text()) } key := strings.TrimSpace(splitted[0]) keys[key] = true } - importantKeys := map[string][]string{ "ppc64le": {"cpu", "revision", "platform", "model", "machine"}, "amd64": {"vendor_id", "model", "flags"}, @@ -57,23 +40,27 @@ func checkCPUInfo(t *testing.T, scanner *bufio.Scanner) { "mips64le": {"system type", "cpu model", "ASEs implemented"}, "riscv64": {"processor", "isa", "mmu"}, } - archKeys := importantKeys[runtime.GOARCH] + archKeys := importantKeys[arch] + if len(archKeys) == 0 { + t.Fatalf("unknown arch %v", arch) + } for _, name := range archKeys { if !keys[name] { - t.Fatalf("key '%s' not found", name) + t.Fatalf("key %q not found", name) } } } -func checkKVMInfo(t *testing.T, scanner *bufio.Scanner) { - for scanner.Scan() { - line := scanner.Text() +func TestReadKVMInfoLinux(t *testing.T) { + buf := new(bytes.Buffer) + if err := readKVMInfo(buf); err != nil { + t.Fatal(err) + } + for s := bufio.NewScanner(buf); s.Scan(); { + line := s.Text() if line == "" { continue } - if strings.HasPrefix(line, "-----") { - break - } splitted := strings.Split(line, ":") if len(splitted) != 2 { t.Fatalf("the format of line \"%s\" is not correct", line) -- cgit mrf-deployment