Skip to content

Commit

Permalink
simulation: test new log behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
jellevandenhooff committed Dec 6, 2024
1 parent fb82171 commit ad2f335
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 14 deletions.
7 changes: 7 additions & 0 deletions internal/prettylog/prettylog.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ package prettylog

import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"io"
Expand Down Expand Up @@ -225,6 +226,12 @@ func (w Writer) writeFields(evt map[string]interface{}, buf *bytes.Buffer) {

switch value := evt[field].(type) {
case string:
if strings.HasPrefix(value, "base64:") {
bytes, err := base64.StdEncoding.AppendDecode(nil, []byte(value[len("base64:"):]))
if err == nil {
value = string(bytes)
}
}
if needsQuote(value) {
buf.WriteString(w.formatter.fieldValue(field, strconv.Quote(value)))
} else {
Expand Down
1 change: 0 additions & 1 deletion internal/simulation/os_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,6 @@ func RawSyscall6(trap, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err sysc
panic("syscall should have been rewritten somewhere")
}

// TODO: test this
logf("unsupported syscall %s (%d) %d %d %d %d %d %d", SyscallName(trap), trap, a1, a2, a3, a4, a5, a6)

return 0, 0, syscall.ENOSYS
Expand Down
37 changes: 24 additions & 13 deletions internal/tests/behavior/log_meta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package behavior_test
import (
"bytes"
"encoding/json"
"fmt"
"strings"
"testing"

Expand Down Expand Up @@ -124,6 +125,19 @@ func TestLogDuringInit(t *testing.T) {
}
}

func formatLogsWithExtra(logs []*gosimlog.Log) []string {
// TODO: share this code with prettylog somehow?
var lines []string
for _, log := range logs {
line := fmt.Sprintf("%d %s/%d %s %s", log.Step, log.Machine, log.Goroutine, log.Level, log.Msg)
for _, kv := range log.Unknown {
line += fmt.Sprintf(" %s=%s", kv.Key, kv.Value)
}
lines = append(lines, line)
}
return lines
}

func TestLogTraceSyscall(t *testing.T) {
if race.Enabled {
// TODO: repair, which will need a reasonable plan for printing data
Expand Down Expand Up @@ -155,20 +169,17 @@ func TestLogTraceSyscall(t *testing.T) {
t.Fatal(err)
}

// TODO: make sure we check traceKind?
// TODO: support snapshotting these logs?
// TODO: include machine etc.?
if diff := cmp.Diff(metatesting.SimplifyParsedLog(metatesting.ParseLog(run.LogOutput)), []string{
"INFO unsupported syscall unknown (9999) 0 0 0 0 0 0",
// TODO: check flags
"INFO call SysOpenat",
"INFO ret SysOpenat",
"INFO call SysFcntl",
"INFO ret SysFcntl",
"INFO call SysWrite",
"INFO ret SysWrite",
"INFO call SysClose",
"INFO ret SysClose",
if diff := cmp.Diff(formatLogsWithExtra(gosimlog.ParseLog(run.LogOutput)), []string{
"1 main/4 INFO unsupported syscall unknown (9999) 0 0 0 0 0 0",
`2 main/4 INFO call SysOpenat dirfd="AT_FDCWD" path="hello" flags="O_WRONLY|O_TRUNC|O_CREAT|O_CLOEXEC" mode="0o644"`,
"3 main/4 INFO ret SysOpenat fd=5 err=null",
"4 main/4 INFO call SysFcntl",
"5 main/4 INFO ret SysFcntl",
`6 main/4 INFO call SysWrite fd=5 p="world"`,
"7 main/4 INFO ret SysWrite n=5 err=null",
"8 main/4 INFO call SysClose fd=5",
"9 main/4 INFO ret SysClose err=null",
}); diff != "" {
t.Error("diff", diff)
}
Expand Down
1 change: 1 addition & 0 deletions internal/tests/behavior/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ func TestLogDuringInit(t *testing.T) {

func TestLogTraceSyscall(t *testing.T) {
// should print ENOSYS
// TODO: test this with a known number and make it work both on arm64 and amd64
syscall.Syscall(9999, 0, 0, 0)

// should print open, write, close
Expand Down

0 comments on commit ad2f335

Please sign in to comment.