...

Source file src/github.com/coreos/go-systemd/v22/activation/packetconns_test.go

Documentation: github.com/coreos/go-systemd/v22/activation

     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.Fatalf(err.Error())
    33  	}
    34  	u2, err := net.ListenUDP("udp", &net.UDPAddr{Port: 1234})
    35  	if err != nil {
    36  		t.Fatalf(err.Error())
    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.Fatalf(err.Error())
    50  	}
    51  	r1.Write([]byte("Hi"))
    52  
    53  	r2, err := net.Dial("udp", "127.0.0.1:1234")
    54  	if err != nil {
    55  		t.Fatalf(err.Error())
    56  	}
    57  	r2.Write([]byte("Hi"))
    58  
    59  	cmd.Env = os.Environ()
    60  	cmd.Env = append(cmd.Env, "LISTEN_FDS=2", "LISTEN_FDNAMES=fd1:fd2", "FIX_LISTEN_PID=1")
    61  
    62  	out, err := cmd.CombinedOutput()
    63  	if err != nil {
    64  		t.Fatalf("Cmd output '%s', err: '%s'\n", out, err)
    65  	}
    66  
    67  	correctStringWrittenNet(t, r1, "Hello world")
    68  	correctStringWrittenNet(t, r2, "Goodbye world")
    69  }
    70  

View as plain text