Skip to content

Commit

Permalink
simulation: fix and test syscalls and logs during init
Browse files Browse the repository at this point in the history
Make syscalls work during init, and make sure logs work
as well for when things during init go wrong.
  • Loading branch information
jellevandenhooff committed Nov 25, 2024
1 parent f1bc358 commit 2cc5fb3
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 9 deletions.
14 changes: 8 additions & 6 deletions internal/simulation/userspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ func setupSlog(machineLabel string) {
panic(err)
}

// TODO: capture stdout? stderr?
// stdout and stderr are currently captured with some special logic in
// LinuxOS for writes to their file descriptors, see TestStdoutStderr.

ho := slog.HandlerOptions{
Level: level,
Expand All @@ -91,14 +92,15 @@ func setupSlog(machineLabel string) {
}

func setupUserspace(gosimOS_ *GosimOS, linuxOS_ *LinuxOS, machineID int, label string) {
// XXX: does logging work during global init?
gosimruntime.InitGlobals(false, false)

// yikes... how do we order this? should machine exist first? should these happen in an init() in here SOMEHOW? short-circuit this package?
// XXX: provide directoyr
// initialize gosimOS etc. before invoking initializers so that init() calls
// can make syscalls, see TestSyscallsDuringInit.
gosimOS = gosimOS_
linuxOS = linuxOS_
currentMachineID = machineID

gosimruntime.InitGlobals(false, false)

// setupSlog only works once globals are initialized. logs during init are
// printed to stdout/stderr, see TestLogDuringInit.
setupSlog(label)
}
29 changes: 26 additions & 3 deletions internal/tests/behavior/log_meta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func parseLog(t *testing.T, log []byte) []map[string]any {
return logs
}

func TestMetaLogMachineGoroutineTime(t *testing.T) {
func TestLogMachineGoroutineTime(t *testing.T) {
mt := metatesting.ForCurrentPackage(t)
run, err := mt.Run(t, &metatesting.RunConfig{
Test: "TestLogMachineGoroutineTime",
Expand All @@ -55,7 +55,7 @@ func TestMetaLogMachineGoroutineTime(t *testing.T) {
}
}

func TestMetaLogSLog(t *testing.T) {
func TestLogSLog(t *testing.T) {
mt := metatesting.ForCurrentPackage(t)
run, err := mt.Run(t, &metatesting.RunConfig{
Test: "TestLogSLog",
Expand All @@ -74,7 +74,7 @@ func TestMetaLogSLog(t *testing.T) {
}
}

func TestMetaStdoutStderr(t *testing.T) {
func TestStdoutStderr(t *testing.T) {
mt := metatesting.ForCurrentPackage(t)
run, err := mt.Run(t, &metatesting.RunConfig{
Test: "TestStdoutStderr",
Expand All @@ -93,3 +93,26 @@ func TestMetaStdoutStderr(t *testing.T) {
t.Error("diff", diff)
}
}

func TestLogDuringInit(t *testing.T) {
mt := metatesting.ForCurrentPackage(t)
run, err := mt.Run(t, &metatesting.RunConfig{
Test: "TestLogDuringInit",
Seed: 1,
ExtraEnv: []string{
"LOGDURINGINIT=1",
},
})
if err != nil {
t.Fatal(err)
}

actual := parseLog(t, run.LogOutput)
expected := parseLog(t, []byte(`{"time":"2020-01-15T14:10:03.000001234Z","level":"INFO","msg":"hello\n","machine":"os","method":"stdout","from":"main","goroutine":1,"step":1}
{"time":"2020-01-15T14:10:03.000001234Z","level":"INFO","msg":"2020/01/15 14:10:03 INFO help\n","machine":"os","method":"stderr","from":"main","goroutine":1,"step":2}
`))

if diff := cmp.Diff(expected, actual); diff != "" {
t.Error("diff", diff)
}
}
13 changes: 13 additions & 0 deletions internal/tests/behavior/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package behavior_test

import (
"fmt"
"log"
"log/slog"
"os"
Expand Down Expand Up @@ -62,3 +63,15 @@ func TestLogForPrettyTest(t *testing.T) {
m.Wait()
log.Println("never")
}

func init() {
// logs during init will print to stdout/stderr bypassing the gosim handler
if os.Getenv("LOGDURINGINIT") == "1" {
fmt.Println("hello")
slog.Info("help")
}
}

func TestLogDuringInit(t *testing.T) {
// logs in above init() should print
}
27 changes: 27 additions & 0 deletions internal/tests/behavior/os_sim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,30 @@ func TestGetpagesize(t *testing.T) {
t.Fatalf("bad page size %d", pgsize)
}
}

var globalHostname string

func init() {
globalHostname, _ = os.Hostname()
}

func TestSyscallsDuringInit(t *testing.T) {
// Test that os.Hostname() called in an init func can make syscalls.

expected := "main"
if globalHostname != expected {
t.Errorf("bad name %q, expected %q", globalHostname, expected)
}

m := gosim.NewMachine(gosim.MachineConfig{
Label: "hello123",
MainFunc: func() {
expected := "hello123"
if globalHostname != expected {
t.Errorf("bad name %q, expected %q", globalHostname, expected)
}
},
})

m.Wait()
}

0 comments on commit 2cc5fb3

Please sign in to comment.