...

Source file src/github.com/coreos/go-systemd/v22/activation/files_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  	"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 *os.File, 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 TestActivation(t *testing.T) {
    41  	arg0, cmdline := exampleCmd("activation")
    42  	cmd := exec.Command(arg0, cmdline...)
    43  
    44  	r1, w1, _ := os.Pipe()
    45  	r2, w2, _ := os.Pipe()
    46  	cmd.ExtraFiles = []*os.File{
    47  		w1,
    48  		w2,
    49  	}
    50  
    51  	cmd.Env = os.Environ()
    52  	cmd.Env = append(cmd.Env, "LISTEN_FDS=2", "LISTEN_FDNAMES=fd1", "FIX_LISTEN_PID=1")
    53  
    54  	err := cmd.Run()
    55  	if err != nil {
    56  		t.Fatalf(err.Error())
    57  	}
    58  
    59  	correctStringWritten(t, r1, "Hello world: fd1")
    60  	correctStringWritten(t, r2, "Goodbye world: LISTEN_FD_4")
    61  }
    62  
    63  func TestActivationNoFix(t *testing.T) {
    64  	arg0, cmdline := exampleCmd("activation")
    65  	cmd := exec.Command(arg0, cmdline...)
    66  	cmd.Env = os.Environ()
    67  	cmd.Env = append(cmd.Env, "LISTEN_FDS=2")
    68  
    69  	out, _ := cmd.CombinedOutput()
    70  	if !bytes.Contains(out, []byte("No files")) {
    71  		t.Fatalf("Child didn't error out as expected")
    72  	}
    73  }
    74  
    75  func TestActivationNoFiles(t *testing.T) {
    76  	arg0, cmdline := exampleCmd("activation")
    77  	cmd := exec.Command(arg0, cmdline...)
    78  	cmd.Env = os.Environ()
    79  	cmd.Env = append(cmd.Env, "LISTEN_FDS=0", "FIX_LISTEN_PID=1")
    80  
    81  	out, _ := cmd.CombinedOutput()
    82  	if !bytes.Contains(out, []byte("No files")) {
    83  		t.Fatalf("Child didn't error out as expected")
    84  	}
    85  }
    86  

View as plain text