1
// Copyright 2015 CoreOS, Inc.
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
7
// http://www.apache.org/licenses/LICENSE-2.0
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.
25
// correctStringWritten fails the text if the correct string wasn't written
26
// to the other side of the pipe.
27
func correctStringWrittenNet(t *testing.T, r net.Conn, expected string) bool {
28
bytes := make([]byte, len(expected))
29
io.ReadAtLeast(r, bytes, len(expected))
31
if string(bytes) != expected {
32
t.Fatalf("Unexpected string %s", string(bytes))
38
// TestActivation forks out a copy of activation.go example and reads back two
39
// strings from the pipes that are passed in.
40
func TestListeners(t *testing.T) {
41
cmd := exec.Command("go", "run", "../examples/activation/listen.go")
43
l1, err := net.Listen("tcp", ":9999")
47
l2, err := net.Listen("tcp", ":1234")
52
t1 := l1.(*net.TCPListener)
53
t2 := l2.(*net.TCPListener)
58
cmd.ExtraFiles = []*os.File{
63
r1, err := net.Dial("tcp", "127.0.0.1:9999")
67
r1.Write([]byte("Hi"))
69
r2, err := net.Dial("tcp", "127.0.0.1:1234")
73
r2.Write([]byte("Hi"))
75
cmd.Env = os.Environ()
76
cmd.Env = append(cmd.Env, "LISTEN_FDS=2", "FIX_LISTEN_PID=1")
78
out, err := cmd.Output()
84
correctStringWrittenNet(t, r1, "Hello world")
85
correctStringWrittenNet(t, r2, "Goodbye world")