2018-02-05 16:05:59 -05:00
|
|
|
package reexec // import "github.com/docker/docker/pkg/reexec"
|
2017-06-01 07:58:10 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"testing"
|
|
|
|
|
2018-03-13 15:28:34 -04:00
|
|
|
"github.com/gotestyourself/gotestyourself/assert"
|
2017-06-01 07:58:10 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
Register("reexec", func() {
|
|
|
|
panic("Return Error")
|
|
|
|
})
|
|
|
|
Init()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRegister(t *testing.T) {
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Equal(t, `reexec func already registered under name "reexec"`, r)
|
2017-06-01 07:58:10 -04:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
Register("reexec", func() {})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCommand(t *testing.T) {
|
|
|
|
cmd := Command("reexec")
|
|
|
|
w, err := cmd.StdinPipe()
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err, "Error on pipe creation: %v", err)
|
2017-06-01 07:58:10 -04:00
|
|
|
defer w.Close()
|
|
|
|
|
|
|
|
err = cmd.Start()
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err, "Error on re-exec cmd: %v", err)
|
2017-06-01 07:58:10 -04:00
|
|
|
err = cmd.Wait()
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Error(t, err, "exit status 2")
|
2017-06-01 07:58:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNaiveSelf(t *testing.T) {
|
|
|
|
if os.Getenv("TEST_CHECK") == "1" {
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
|
|
|
cmd := exec.Command(naiveSelf(), "-test.run=TestNaiveSelf")
|
|
|
|
cmd.Env = append(os.Environ(), "TEST_CHECK=1")
|
|
|
|
err := cmd.Start()
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err, "Unable to start command")
|
2017-06-01 07:58:10 -04:00
|
|
|
err = cmd.Wait()
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Error(t, err, "exit status 2")
|
2017-06-01 07:58:10 -04:00
|
|
|
|
|
|
|
os.Args[0] = "mkdir"
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, naiveSelf() != os.Args[0])
|
2017-06-01 07:58:10 -04:00
|
|
|
}
|