~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/coreos/go-systemd/activation/listeners_test.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
        "io"
 
19
        "net"
 
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 correctStringWrittenNet(t *testing.T, r net.Conn, 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
// 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")
 
42
 
 
43
        l1, err := net.Listen("tcp", ":9999")
 
44
        if err != nil {
 
45
                t.Fatalf(err.Error())
 
46
        }
 
47
        l2, err := net.Listen("tcp", ":1234")
 
48
        if err != nil {
 
49
                t.Fatalf(err.Error())
 
50
        }
 
51
 
 
52
        t1 := l1.(*net.TCPListener)
 
53
        t2 := l2.(*net.TCPListener)
 
54
 
 
55
        f1, _ := t1.File()
 
56
        f2, _ := t2.File()
 
57
 
 
58
        cmd.ExtraFiles = []*os.File{
 
59
                f1,
 
60
                f2,
 
61
        }
 
62
 
 
63
        r1, err := net.Dial("tcp", "127.0.0.1:9999")
 
64
        if err != nil {
 
65
                t.Fatalf(err.Error())
 
66
        }
 
67
        r1.Write([]byte("Hi"))
 
68
 
 
69
        r2, err := net.Dial("tcp", "127.0.0.1:1234")
 
70
        if err != nil {
 
71
                t.Fatalf(err.Error())
 
72
        }
 
73
        r2.Write([]byte("Hi"))
 
74
 
 
75
        cmd.Env = os.Environ()
 
76
        cmd.Env = append(cmd.Env, "LISTEN_FDS=2", "FIX_LISTEN_PID=1")
 
77
 
 
78
        out, err := cmd.Output()
 
79
        if err != nil {
 
80
                println(string(out))
 
81
                t.Fatalf(err.Error())
 
82
        }
 
83
 
 
84
        correctStringWrittenNet(t, r1, "Hello world")
 
85
        correctStringWrittenNet(t, r2, "Goodbye world")
 
86
}