Debian Codegraph @

activation/files_test.go

raw
1 // Copyright 2015 CoreOS, Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package activation
16
17 import (
18 "bytes"
19 "io"
20 "os"
21 "os/exec"
22 "testing"
23 )
24
25 // correctStringWritten fails the text if the correct string wasn't written
26 // to the other side of the pipe.
27 func correctStringWritten(t *testing.T, r io.Reader, expected string) {
28 t.Helper()
29
30 bytes := make([]byte, len(expected))
31 _, err := io.ReadAtLeast(r, bytes, len(expected))
32 if err != nil {
33 t.Fatal(err)
34 }
35
36 if string(bytes) != expected {
37 t.Fatalf("Unexpected string %s", string(bytes))
38 }
39 }
40
41 // TestActivation forks out a copy of activation.go example and reads back two
42 // strings from the pipes that are passed in.
43 func TestActivation(t *testing.T) {
44 arg0, cmdline := exampleCmd("activation")
45 cmd := exec.Command(arg0, cmdline...)
46
47 r1, w1, _ := os.Pipe()
48 r2, w2, _ := os.Pipe()
49 cmd.ExtraFiles = []*os.File{
50 w1,
51 w2,
52 }
53
54 cmd.Env = os.Environ()
55 cmd.Env = append(cmd.Env, "LISTEN_FDS=2", "LISTEN_FDNAMES=fd1", "FIX_LISTEN_PID=1")
56
57 err := cmd.Run()
58 if err != nil {
59 t.Fatal(err)
60 }
61
62 correctStringWritten(t, r1, "Hello world: fd1")
63 correctStringWritten(t, r2, "Goodbye world: LISTEN_FD_4")
64 }
65
66 func TestActivationNoFix(t *testing.T) {
67 arg0, cmdline := exampleCmd("activation")
68 cmd := exec.Command(arg0, cmdline...)
69 cmd.Env = os.Environ()
70 cmd.Env = append(cmd.Env, "LISTEN_FDS=2")
71
72 out, _ := cmd.CombinedOutput()
73 if !bytes.Contains(out, []byte("No files")) {
74 t.Fatal("Child didn't error out as expected")
75 }
76 }
77
78 func TestActivationNoFiles(t *testing.T) {
79 arg0, cmdline := exampleCmd("activation")
80 cmd := exec.Command(arg0, cmdline...)
81 cmd.Env = os.Environ()
82 cmd.Env = append(cmd.Env, "LISTEN_FDS=0", "FIX_LISTEN_PID=1")
83
84 out, _ := cmd.CombinedOutput()
85 if !bytes.Contains(out, []byte("No files")) {
86 t.Fatal("Child didn't error out as expected")
87 }
88 }