1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Setup a predictable, repeatable environment for containers

This commit is contained in:
Solomon Hykes 2013-03-07 09:25:41 -08:00
parent 2df0bc6bc0
commit fb350e0c77
2 changed files with 58 additions and 0 deletions

View file

@ -4,6 +4,7 @@ import (
"fmt"
"io"
"io/ioutil"
"sort"
"strings"
"testing"
"time"
@ -511,6 +512,55 @@ func TestTty(t *testing.T) {
}
}
func TestEnv(t *testing.T) {
docker, err := newTestDocker()
if err != nil {
t.Fatal(err)
}
container, err := docker.Create(
"env_test",
"/usr/bin/env",
[]string{},
[]string{testLayerPath},
&Config{},
)
if err != nil {
t.Fatal(err)
}
defer docker.Destroy(container)
stdout, err := container.StdoutPipe()
if err != nil {
t.Fatal(err)
}
defer stdout.Close()
if err := container.Start(); err != nil {
t.Fatal(err)
}
container.Wait()
output, err := ioutil.ReadAll(stdout)
if err != nil {
t.Fatal(err)
}
actualEnv := strings.Split(string(output), "\n")
if actualEnv[len(actualEnv)-1] == "" {
actualEnv = actualEnv[:len(actualEnv)-1]
}
sort.Strings(actualEnv)
goodEnv := []string{
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"HOME=/",
}
sort.Strings(goodEnv)
if len(goodEnv) != len(actualEnv) {
t.Fatalf("Wrong environment: should be %d variables, not: '%s'\n", len(goodEnv), strings.Join(actualEnv, ", "))
}
for i := range goodEnv {
if actualEnv[i] != goodEnv[i] {
t.Fatalf("Wrong environment variable: should be %s, not %s", goodEnv[i], actualEnv[i])
}
}
}
func BenchmarkRunSequencial(b *testing.B) {
docker, err := newTestDocker()
if err != nil {

View file

@ -52,6 +52,13 @@ func changeUser(u string) {
}
}
// Set the environment to a known, repeatable state
func setupEnv() {
os.Clearenv()
os.Setenv("HOME", "/")
os.Setenv("PATH", "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin")
}
func executeProgram(name string, args []string) {
path, err := exec.LookPath(name)
if err != nil {
@ -79,5 +86,6 @@ func SysInit() {
setupNetworking(*gw)
changeUser(*u)
setupEnv()
executeProgram(flag.Arg(0), flag.Args())
}