@@ -2,9 +2,12 @@ package docker
22
33import (
44 "./fs"
5+ "bufio"
56 "fmt"
67 "io"
78 "io/ioutil"
9+ "math/rand"
10+ "os"
811 "sort"
912 "strings"
1013 "testing"
@@ -23,7 +26,7 @@ func TestCommitRun(t *testing.T) {
2326 []string {"-c" , "echo hello > /world" },
2427 GetTestImage (docker ),
2528 & Config {
26- Ram : 33554432 ,
29+ Memory : 33554432 ,
2730 },
2831 )
2932 if err != nil {
@@ -65,7 +68,7 @@ func TestCommitRun(t *testing.T) {
6568 []string {"/world" },
6669 img ,
6770 & Config {
68- Ram : 33554432 ,
71+ Memory : 33554432 ,
6972 },
7073 )
7174 if err != nil {
@@ -100,7 +103,7 @@ func TestRun(t *testing.T) {
100103 []string {"-al" },
101104 GetTestImage (docker ),
102105 & Config {
103- Ram : 33554432 ,
106+ Memory : 33554432 ,
104107 },
105108 )
106109 if err != nil {
@@ -349,7 +352,7 @@ func TestUser(t *testing.T) {
349352 // Set a username
350353 container , err = docker .Create (
351354 "user_root" ,
352- "/bin/ id" ,
355+ "id" ,
353356 []string {},
354357 GetTestImage (docker ),
355358 & Config {
@@ -393,7 +396,7 @@ func TestUser(t *testing.T) {
393396 // Set a different user by uid
394397 container , err = docker .Create (
395398 "user_uid1" ,
396- "/usr/bin/ id" ,
399+ "id" ,
397400 []string {},
398401 GetTestImage (docker ),
399402 & Config {
@@ -417,7 +420,7 @@ func TestUser(t *testing.T) {
417420 // Set a different user by username
418421 container , err = docker .Create (
419422 "user_daemon" ,
420- "/usr/bin/ id" ,
423+ "id" ,
421424 []string {},
422425 GetTestImage (docker ),
423426 & Config {
@@ -616,6 +619,59 @@ func TestEnv(t *testing.T) {
616619 }
617620}
618621
622+ func grepFile (t * testing.T , path string , pattern string ) {
623+ f , err := os .Open (path )
624+ if err != nil {
625+ t .Fatal (err )
626+ }
627+ defer f .Close ()
628+ r := bufio .NewReader (f )
629+ var (
630+ line string
631+ )
632+ err = nil
633+ for err == nil {
634+ line , err = r .ReadString ('\n' )
635+ if strings .Contains (line , pattern ) == true {
636+ return
637+ }
638+ }
639+ t .Fatalf ("grepFile: pattern \" %s\" not found in \" %s\" " , pattern , path )
640+ }
641+
642+ func TestLXCConfig (t * testing.T ) {
643+ docker , err := newTestDocker ()
644+ if err != nil {
645+ t .Fatal (err )
646+ }
647+ defer nuke (docker )
648+ // Memory is allocated randomly for testing
649+ rand .Seed (time .Now ().UTC ().UnixNano ())
650+ memMin := 33554432
651+ memMax := 536870912
652+ mem := memMin + rand .Intn (memMax - memMin )
653+ container , err := docker .Create (
654+ "config_test" ,
655+ "/bin/true" ,
656+ []string {},
657+ GetTestImage (docker ),
658+ & Config {
659+ Hostname : "foobar" ,
660+ Memory : int64 (mem ),
661+ },
662+ )
663+ if err != nil {
664+ t .Fatal (err )
665+ }
666+ defer docker .Destroy (container )
667+ container .generateLXCConfig ()
668+ grepFile (t , container .lxcConfigPath , "lxc.utsname = foobar" )
669+ grepFile (t , container .lxcConfigPath ,
670+ fmt .Sprintf ("lxc.cgroup.memory.limit_in_bytes = %d" , mem ))
671+ grepFile (t , container .lxcConfigPath ,
672+ fmt .Sprintf ("lxc.cgroup.memory.memsw.limit_in_bytes = %d" , mem * 2 ))
673+ }
674+
619675func BenchmarkRunSequencial (b * testing.B ) {
620676 docker , err := newTestDocker ()
621677 if err != nil {
0 commit comments