Skip to content

Commit f316d1e

Browse files
committed
add module crashing test
1 parent 3d7bcf6 commit f316d1e

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

module/modmanager/manager_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,49 @@ func setupModManager(
9696
return mgr
9797
}
9898

99+
// Test that if a module crashes shortly after startup, in UNIX mode we get "module exited too quickly" and
100+
// in TCP mode we get "context cancelled", without waiting for the full ModuleStartupTimeout
101+
func TestCrashedModuleCheckReadyShortCircuit(t *testing.T) {
102+
modPath := filepath.Join(t.TempDir(), "run.sh")
103+
err := os.WriteFile(modPath, []byte("#!/bin/sh\n\nsleep 2\nexit 1"), 0o755)
104+
test.That(t, err, test.ShouldBeNil)
105+
106+
for _, mode := range []string{"unix", "tcp"} {
107+
t.Run(mode, func(t *testing.T) {
108+
ctx := context.Background()
109+
logger := logging.NewTestLogger(t)
110+
111+
if mode == "tcp" {
112+
os.Setenv("VIAM_TCP_SOCKETS", "yes")
113+
t.Cleanup(func() { os.Unsetenv("VIAM_TCP_SOCKETS") })
114+
}
115+
116+
parentAddr := setupSocketWithRobot(t)
117+
118+
viamHomeTemp := t.TempDir()
119+
mgr := setupModManager(t, ctx, parentAddr, logger, modmanageroptions.Options{UntrustedEnv: false, ViamHomeDir: viamHomeTemp})
120+
121+
startTime := time.Now()
122+
err = mgr.Add(context.Background(), config.Module{
123+
Name: "test",
124+
ExePath: modPath,
125+
Type: config.ModuleTypeLocal,
126+
ModuleID: "will:crash",
127+
})
128+
test.That(t, err, test.ShouldNotBeNil)
129+
if mode == "unix" {
130+
// exits with err from startModuleProcess
131+
test.That(t, err.Error(), test.ShouldContainSubstring, "module test exited too quickly after attempted startup")
132+
} else {
133+
// exits with err from checkReady (short circuit after detecting module crashed)
134+
test.That(t, err.Error(), test.ShouldContainSubstring, "error while waiting for module to be ready test: context canceled")
135+
}
136+
// should not wait for the full DefaultModuleStartupTimeout / ModuleStartupTimeoutEnvVar
137+
test.That(t, time.Since(startTime), test.ShouldBeLessThan, time.Second*10)
138+
})
139+
}
140+
}
141+
99142
func TestModManagerFunctions(t *testing.T) {
100143
// Precompile module copies to avoid timeout issues when building takes too long.
101144
modPath := rtestutils.BuildTempModule(t, "examples/customresources/demos/simplemodule")

0 commit comments

Comments
 (0)