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