...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package activation
16
17 import (
18 "bytes"
19 "io"
20 "os"
21 "os/exec"
22 "testing"
23 )
24
25
26
27 func correctStringWritten(t *testing.T, r *os.File, expected string) bool {
28 bytes := make([]byte, len(expected))
29 io.ReadAtLeast(r, bytes, len(expected))
30
31 if string(bytes) != expected {
32 t.Fatalf("Unexpected string %s", string(bytes))
33 }
34
35 return true
36 }
37
38
39
40 func TestActivation(t *testing.T) {
41 arg0, cmdline := exampleCmd("activation")
42 cmd := exec.Command(arg0, cmdline...)
43
44 r1, w1, _ := os.Pipe()
45 r2, w2, _ := os.Pipe()
46 cmd.ExtraFiles = []*os.File{
47 w1,
48 w2,
49 }
50
51 cmd.Env = os.Environ()
52 cmd.Env = append(cmd.Env, "LISTEN_FDS=2", "LISTEN_FDNAMES=fd1", "FIX_LISTEN_PID=1")
53
54 err := cmd.Run()
55 if err != nil {
56 t.Fatalf(err.Error())
57 }
58
59 correctStringWritten(t, r1, "Hello world: fd1")
60 correctStringWritten(t, r2, "Goodbye world: LISTEN_FD_4")
61 }
62
63 func TestActivationNoFix(t *testing.T) {
64 arg0, cmdline := exampleCmd("activation")
65 cmd := exec.Command(arg0, cmdline...)
66 cmd.Env = os.Environ()
67 cmd.Env = append(cmd.Env, "LISTEN_FDS=2")
68
69 out, _ := cmd.CombinedOutput()
70 if !bytes.Contains(out, []byte("No files")) {
71 t.Fatalf("Child didn't error out as expected")
72 }
73 }
74
75 func TestActivationNoFiles(t *testing.T) {
76 arg0, cmdline := exampleCmd("activation")
77 cmd := exec.Command(arg0, cmdline...)
78 cmd.Env = os.Environ()
79 cmd.Env = append(cmd.Env, "LISTEN_FDS=0", "FIX_LISTEN_PID=1")
80
81 out, _ := cmd.CombinedOutput()
82 if !bytes.Contains(out, []byte("No files")) {
83 t.Fatalf("Child didn't error out as expected")
84 }
85 }
86
View as plain text