Skip to content

Commit

Permalink
Setup a predictable, repeatable environment for containers
Browse files Browse the repository at this point in the history
  • Loading branch information
Solomon Hykes committed Mar 7, 2013
1 parent 2df0bc6 commit fb350e0
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
50 changes: 50 additions & 0 deletions container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io"
"io/ioutil"
"sort"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -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 {
Expand Down
8 changes: 8 additions & 0 deletions sysinit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -79,5 +86,6 @@ func SysInit() {

setupNetworking(*gw)
changeUser(*u)
setupEnv()
executeProgram(flag.Arg(0), flag.Args())
}

0 comments on commit fb350e0

Please sign in to comment.