github.com/ttys3/engine@v17.12.1-ce-rc2+incompatible/pkg/reexec/reexec_test.go (about)

     1  package reexec
     2  
     3  import (
     4  	"os"
     5  	"os/exec"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  func init() {
    13  	Register("reexec", func() {
    14  		panic("Return Error")
    15  	})
    16  	Init()
    17  }
    18  
    19  func TestRegister(t *testing.T) {
    20  	defer func() {
    21  		if r := recover(); r != nil {
    22  			require.Equal(t, `reexec func already registered under name "reexec"`, r)
    23  		}
    24  	}()
    25  	Register("reexec", func() {})
    26  }
    27  
    28  func TestCommand(t *testing.T) {
    29  	cmd := Command("reexec")
    30  	w, err := cmd.StdinPipe()
    31  	require.NoError(t, err, "Error on pipe creation: %v", err)
    32  	defer w.Close()
    33  
    34  	err = cmd.Start()
    35  	require.NoError(t, err, "Error on re-exec cmd: %v", err)
    36  	err = cmd.Wait()
    37  	require.EqualError(t, err, "exit status 2")
    38  }
    39  
    40  func TestNaiveSelf(t *testing.T) {
    41  	if os.Getenv("TEST_CHECK") == "1" {
    42  		os.Exit(2)
    43  	}
    44  	cmd := exec.Command(naiveSelf(), "-test.run=TestNaiveSelf")
    45  	cmd.Env = append(os.Environ(), "TEST_CHECK=1")
    46  	err := cmd.Start()
    47  	require.NoError(t, err, "Unable to start command")
    48  	err = cmd.Wait()
    49  	require.EqualError(t, err, "exit status 2")
    50  
    51  	os.Args[0] = "mkdir"
    52  	assert.NotEqual(t, naiveSelf(), os.Args[0])
    53  }