11package integration
22
33import (
4+ "bytes"
5+ "math/rand"
46 "os"
7+ "strings"
58 "testing"
69
710 "github.com/opencontainers/runc/libcontainer"
@@ -27,9 +30,7 @@ func BenchmarkExecTrue(b *testing.B) {
2730 _ = stdinR .Close ()
2831 defer func () {
2932 _ = stdinW .Close ()
30- if _ , err := process .Wait (); err != nil {
31- b .Log (err )
32- }
33+ waitProcess (process , b )
3334 }()
3435 ok (b , err )
3536
@@ -42,10 +43,81 @@ func BenchmarkExecTrue(b *testing.B) {
4243 LogLevel : "0" , // Minimize forwardChildLogs involvement.
4344 }
4445 err := container .Run (exec )
45- if err != nil {
46- b .Fatal ("exec failed:" , err )
46+ ok (b , err )
47+ waitProcess (exec , b )
48+ }
49+ b .StopTimer ()
50+ }
51+
52+ func genBigEnv (count int ) []string {
53+ randStr := func (length int ) string {
54+ const charset = "abcdefghijklmnopqrstuvwxyz0123456789_"
55+ b := make ([]byte , length )
56+ for i := range b {
57+ b [i ] = charset [rand .Intn (len (charset ))]
4758 }
59+ return string (b )
60+ }
61+
62+ envs := make ([]string , count )
63+ for i := 0 ; i < count ; i ++ {
64+ key := strings .ToUpper (randStr (10 ))
65+ value := randStr (20 )
66+ envs [i ] = key + "=" + value
67+ }
68+
69+ return envs
70+ }
71+
72+ func BenchmarkExecInBigEnv (b * testing.B ) {
73+ config := newTemplateConfig (b , nil )
74+ container , err := newContainer (b , config )
75+ ok (b , err )
76+ defer destroyContainer (container )
77+
78+ // Execute a first process in the container
79+ stdinR , stdinW , err := os .Pipe ()
80+ ok (b , err )
81+ process := & libcontainer.Process {
82+ Cwd : "/" ,
83+ Args : []string {"cat" },
84+ Env : standardEnvironment ,
85+ Stdin : stdinR ,
86+ Init : true ,
87+ }
88+ err = container .Run (process )
89+ _ = stdinR .Close ()
90+ defer func () {
91+ _ = stdinW .Close ()
92+ waitProcess (process , b )
93+ }()
94+ ok (b , err )
95+
96+ const numEnv = 5000
97+ env := append (standardEnvironment , genBigEnv (numEnv )... )
98+ // Construct the expected output.
99+ var wantOut bytes.Buffer
100+ for _ , e := range env {
101+ wantOut .WriteString (e + "\n " )
102+ }
103+
104+ b .ResetTimer ()
105+ for i := 0 ; i < b .N ; i ++ {
106+ buffers := newStdBuffers ()
107+ exec := & libcontainer.Process {
108+ Cwd : "/" ,
109+ Args : []string {"env" },
110+ Env : env ,
111+ Stdin : buffers .Stdin ,
112+ Stdout : buffers .Stdout ,
113+ Stderr : buffers .Stderr ,
114+ }
115+ err = container .Run (exec )
116+ ok (b , err )
48117 waitProcess (exec , b )
118+ if ! bytes .Equal (buffers .Stdout .Bytes (), wantOut .Bytes ()) {
119+ b .Fatalf ("unexpected output: %s (stderr: %s)" , buffers .Stdout , buffers .Stderr )
120+ }
49121 }
50122 b .StopTimer ()
51123}
0 commit comments