Skip to content

Commit

Permalink
test for stderr/stdout
Browse files Browse the repository at this point in the history
  • Loading branch information
nbari committed Sep 22, 2018
1 parent 62ec11a commit bbdba03
Show file tree
Hide file tree
Showing 2 changed files with 190 additions and 3 deletions.
20 changes: 18 additions & 2 deletions a_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,20 @@ import (
func TestMain(m *testing.M) {
switch os.Getenv("GO_WANT_HELPER_PROCESS") {
case "sleep10":
GWHP_sleep1()
GWHP_sleep10()
case "nosleep":
GWHP_nosleep()
case "signalsUDOT":
GWHP_signalsUDOT()
case "logStdoutStderr":
GWHP_logstdoutstderr()
default:
os.Exit(m.Run())
}
}

func GWHP_sleep1() {
// GWHP_sleep1 - test function, exit 1 after sleeping 10 seconds
func GWHP_sleep10() {
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, os.Kill)
select {
Expand All @@ -35,10 +38,12 @@ func GWHP_sleep1() {
}
}

// GWHP_nosleep - test function, exit immediately
func GWHP_nosleep() {
os.Exit(0)
}

// GWHP_signalsUDOT - test function for signals up, down, once, terminate
func GWHP_signalsUDOT() {
fmt.Println("5D675098-45D7-4089-A72C-3628713EA5BA")
c := make(chan os.Signal)
Expand All @@ -51,6 +56,17 @@ func GWHP_signalsUDOT() {
}
}

// GWHP_logstdoutstderr - test function for login stdout & stderr
func GWHP_logstdoutstderr() {
for i := 1; i < 5; i++ {
if i%3 == 0 {
fmt.Fprintf(os.Stderr, "STDERR i: %d\n", i)
} else {
fmt.Printf("STDOUT i: %d\n", i)
}
}
}

/* Test Helpers */
func expect(t *testing.T, a interface{}, b interface{}) {
_, fn, line, _ := runtime.Caller(1)
Expand Down
173 changes: 172 additions & 1 deletion process_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package immortal

import "testing"
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
)

func TestProcessStart(t *testing.T) {
cfg := &Config{
Expand All @@ -23,3 +29,168 @@ func TestProcessStart(t *testing.T) {
t.Error("Expecting exec: --: executable file not found in $PATH")
}
}

func TestProcessLogStderrStdout(t *testing.T) {
sdir, err := ioutil.TempDir("", "TestProcessLogStderrStdout")
if err != nil {
t.Error(err)
}
defer os.RemoveAll(sdir)
tmpfileStdout, err := ioutil.TempFile(sdir, "log.stdout")
if err != nil {
t.Error(err)
}
tmpfileStderr, err := ioutil.TempFile(sdir, "log.stderr")
if err != nil {
t.Error(err)
}
cfg := &Config{
Env: map[string]string{"GO_WANT_HELPER_PROCESS": "logStdoutStderr"},
command: []string{os.Args[0]},
Cwd: sdir,
ctl: sdir,
Pid: Pid{
Parent: filepath.Join(sdir, "parent.pid"),
Child: filepath.Join(sdir, "child.pid"),
},
Log: Log{
File: tmpfileStdout.Name(),
},
Stderr: Log{
File: tmpfileStderr.Name(),
},
}

// create new daemon
d, err := New(cfg)
if err != nil {
t.Fatal(err)
}

np := NewProcess(cfg)
expect(t, 0, np.Pid())
p, err := d.Run(np)
if err != nil {
t.Error(err)
}

// create socket
if err := d.Listen(); err != nil {
t.Fatal(err)
}

// check pids
if pid, err := d.ReadPidFile(filepath.Join(sdir, "parent.pid")); err != nil {
t.Error(err)
} else {
expect(t, os.Getpid(), pid)
}
if pid, err := d.ReadPidFile(filepath.Join(sdir, "child.pid")); err != nil {
t.Error(err, pid)
} else {
expect(t, p.Pid(), pid)
}

// check lock
if _, err = os.Stat(filepath.Join(sdir, "immortal.sock")); err != nil {
t.Fatal(err)
}

// wait for process to finish
err = <-p.errch

t.Log("verifying stdout")
content, err := ioutil.ReadFile(tmpfileStdout.Name())
if err != nil {
t.Fatal(err)
}
expectStdout := fmt.Sprintf("STDOUT i: 1\nSTDOUT i: 2\nSTDOUT i: 4\n")
expect(t, expectStdout, string(content))

t.Log("verifying stderr")
content, err = ioutil.ReadFile(tmpfileStderr.Name())
if err != nil {
t.Fatal(err)
}
expectStderr := fmt.Sprintf("STDERR i: 3\n")
expect(t, expectStderr, string(content))

// closing socket
close(d.quit)
d.wg.Wait()
}

func TestProcessLogStderr(t *testing.T) {
sdir, err := ioutil.TempDir("", "TestProcessLogStderr")
if err != nil {
t.Error(err)
}
defer os.RemoveAll(sdir)
tmpfileStderr, err := ioutil.TempFile(sdir, "log.stderr")
if err != nil {
t.Error(err)
}
cfg := &Config{
Env: map[string]string{"GO_WANT_HELPER_PROCESS": "logStdoutStderr"},
command: []string{os.Args[0]},
Cwd: sdir,
ctl: sdir,
Pid: Pid{
Parent: filepath.Join(sdir, "parent.pid"),
Child: filepath.Join(sdir, "child.pid"),
},
Stderr: Log{
File: tmpfileStderr.Name(),
},
}

// create new daemon
d, err := New(cfg)
if err != nil {
t.Fatal(err)
}

np := NewProcess(cfg)
expect(t, 0, np.Pid())
p, err := d.Run(np)
if err != nil {
t.Error(err)
}

// create socket
if err := d.Listen(); err != nil {
t.Fatal(err)
}

// check pids
if pid, err := d.ReadPidFile(filepath.Join(sdir, "parent.pid")); err != nil {
t.Error(err)
} else {
expect(t, os.Getpid(), pid)
}
if pid, err := d.ReadPidFile(filepath.Join(sdir, "child.pid")); err != nil {
t.Error(err, pid)
} else {
expect(t, p.Pid(), pid)
}

// check lock
if _, err = os.Stat(filepath.Join(sdir, "immortal.sock")); err != nil {
t.Fatal(err)
}

// wait for process to finish
err = <-p.errch

t.Log("verifying stderr")
content, err := ioutil.ReadFile(tmpfileStderr.Name())
if err != nil {
t.Fatal(err)
}
expectStderr := fmt.Sprintf("STDERR i: 3\n")
expect(t, expectStderr, string(content))

// closing socket
close(d.quit)
d.wg.Wait()
}

0 comments on commit bbdba03

Please sign in to comment.