Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for spawning a daemon process #24

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions readAndExecute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,30 @@
}
}

// Testing with golang 1.20.8
func TestExecuteCommandWithTimeoutII(t *testing.T) {
config := configurationStruct{}
config.setDefaultValues()
config.encryption = false
result := &answer{}

Check failure on line 179 in readAndExecute_test.go

View workflow job for this annotation

GitHub Actions / test (1.19.x, ubuntu-latest)

ineffectual assignment to result (ineffassign)

// check for timeout:
t1 := time.Now()
config.timeoutReturn = 3
result = &answer{}

// Spawning a daemon process
executeCommandLine(result, &receivedStruct{commandLine: `python3 test/python-daemon.py`, timeout: 3}, &config)

if !strings.HasPrefix(result.output, "(Check Timed Out On Worker:") || result.returnCode != 3 {
t.Errorf("got %s, with code: %d but expected: %s and code: %d", result.output, result.returnCode, "timeout", 3)
}
duration := time.Since(t1)
if duration > 2*time.Second {
t.Errorf("command took %s, which is beyond the expected timeout", duration)
}
}

func TestExecuteCommandArgListTooLongError(t *testing.T) {
config := configurationStruct{}
config.setDefaultValues()
Expand Down
35 changes: 35 additions & 0 deletions test/python-daemon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import sys, os, time

def main():
time.sleep(200)

if __name__ == "__main__":
# Do the Unix double-fork magic; see Stevens's book "Advanced
# Programming in the UNIX Environment" (Addison-Wesley) for details
try:
pid = os.fork()
if pid > 0:
# Exit first parent
sys.exit(0)
except OSError as e:
print("fork #1 failed: %d (%s)" % (
e.errno, e.strerror), file=sys.stderr)
sys.exit(1)

# Decouple from parent environment
os.setsid()

# Do second fork
try:
pid = os.fork()
if pid > 0:
# Exit from second parent; print eventual PID before exiting
print("Daemon PID %d" % pid)
sys.exit(0)
except OSError as e:
print("fork #2 failed: %d (%s)" % (
e.errno, e.strerror), file=sys.stderr)
sys.exit(1)

# Start the daemon main loop
main()
Loading