github.com/Mirantis/virtlet@v1.5.2-0.20191204181327-1659b8a48e9b/pkg/stream/utils_test.go (about)

     1  /*
     2  Copyright 2017 Mirantis
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package stream
    18  
    19  import (
    20  	"fmt"
    21  	"io/ioutil"
    22  	"net"
    23  	"os"
    24  	"runtime"
    25  	"testing"
    26  	"time"
    27  
    28  	testutils "github.com/Mirantis/virtlet/pkg/utils/testing"
    29  )
    30  
    31  func TestGetPidFromConnection(t *testing.T) {
    32  	if runtime.GOOS != "linux" {
    33  		t.Skip("getPidFromConnection only works on Linux")
    34  	}
    35  	socket, err := ioutil.TempFile(os.TempDir(), "utilTest")
    36  	defer os.Remove(socket.Name())
    37  	socketPath := socket.Name()
    38  
    39  	tc := testutils.RunProcess(t, "nc", []string{"-l", "-U", socketPath}, nil)
    40  	defer tc.Stop()
    41  
    42  	// wait for nc to start
    43  	time.Sleep(2 * time.Second)
    44  
    45  	conn, err := net.DialUnix("unix", nil, &net.UnixAddr{Name: socketPath, Net: "unix"})
    46  	if err != nil {
    47  		t.Fatal("Error when connecting to socket:", err)
    48  	}
    49  
    50  	pid, err := getPidFromConnection(conn)
    51  
    52  	if err != nil {
    53  		t.Errorf("Couldn't get pid from Unix socket: %v", err)
    54  	}
    55  	if pid != int32(tc.Pid()) {
    56  		t.Errorf("Wrong pid from getPidFromConnection. Expected: %d, got %d", tc.Pid(), pid)
    57  	}
    58  }
    59  
    60  func TestGetProcessEnvironment(t *testing.T) {
    61  	if runtime.GOOS != "linux" {
    62  		t.Skip("getProcessEnvironment only works on Linux")
    63  	}
    64  
    65  	tc := testutils.RunProcess(t, "sleep", []string{"10"}, []string{
    66  		"FOO=1",
    67  		"BAR=asd",
    68  	})
    69  	defer tc.Stop()
    70  	waitForChildExec(t, tc.Pid())
    71  	env, err := getProcessEnvironment(int32(tc.Pid()))
    72  	if err != nil {
    73  		t.Error(err)
    74  	}
    75  	for k, v := range map[string]string{"FOO": "1", "BAR": "asd"} {
    76  		envVal, ok := env[k]
    77  		if !ok {
    78  			t.Errorf("%s variable not found in env", k)
    79  		}
    80  		if envVal != v {
    81  			t.Errorf("%s variable value not equal to %s", k, v)
    82  		}
    83  	}
    84  }
    85  
    86  // waitForChildExec waits for a child process to perform exec()
    87  // by means of waiting when its `/proc/PID/cmdline` becomes different from the
    88  // cmdline of the current process
    89  func waitForChildExec(t *testing.T, pid int) {
    90  	ownPid := os.Getpid()
    91  	ownCmdline, err := ioutil.ReadFile(fmt.Sprintf("/proc/%d/cmdline", ownPid))
    92  	if err != nil {
    93  		t.Fatalf("cannot read own cmdline: %v", err)
    94  	}
    95  	processCmdlineLocation := fmt.Sprintf("/proc/%d/cmdline", pid)
    96  	for {
    97  		processCmdline, err := ioutil.ReadFile(processCmdlineLocation)
    98  		if err != nil {
    99  			t.Fatalf("cannot read cmdline for pid %q: %v", pid, err)
   100  		}
   101  		if string(processCmdline) != string(ownCmdline) {
   102  			break
   103  		}
   104  		time.Sleep(100 * time.Millisecond)
   105  	}
   106  }