Debian Codegraph @

activation/packetconns_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 "net"
19 "os"
20 "os/exec"
21 "testing"
22 )
23
24 // TestActivation forks out a copy of activation.go example and reads back two
25 // strings from the pipes that are passed in.
26 func TestPacketConns(t *testing.T) {
27 arg0, cmdline := exampleCmd("udpconn")
28 cmd := exec.Command(arg0, cmdline...)
29
30 u1, err := net.ListenUDP("udp", &net.UDPAddr{Port: 9999})
31 if err != nil {
32 t.Fatal(err)
33 }
34 u2, err := net.ListenUDP("udp", &net.UDPAddr{Port: 1234})
35 if err != nil {
36 t.Fatal(err)
37 }
38
39 f1, _ := u1.File()
40 f2, _ := u2.File()
41
42 cmd.ExtraFiles = []*os.File{
43 f1,
44 f2,
45 }
46
47 r1, err := net.Dial("udp", "127.0.0.1:9999")
48 if err != nil {
49 t.Fatal(err)
50 }
51 if _, err := r1.Write([]byte("Hi")); err != nil {
52 t.Fatal(err)
53 }
54
55 r2, err := net.Dial("udp", "127.0.0.1:1234")
56 if err != nil {
57 t.Fatal(err)
58 }
59 if _, err := r2.Write([]byte("Hi")); err != nil {
60 t.Fatal(err)
61 }
62
63 cmd.Env = os.Environ()
64 cmd.Env = append(cmd.Env, "LISTEN_FDS=2", "LISTEN_FDNAMES=fd1:fd2", "FIX_LISTEN_PID=1")
65
66 out, err := cmd.CombinedOutput()
67 if err != nil {
68 t.Fatalf("Cmd output '%s', err: '%s'\n", out, err)
69 }
70
71 correctStringWritten(t, r1, "Hello world")
72 correctStringWritten(t, r2, "Goodbye world")
73 }