|
4 | 4 | "bytes" |
5 | 5 | "fmt" |
6 | 6 | "io" |
| 7 | + "math/rand" |
7 | 8 | "os" |
8 | 9 | "strconv" |
9 | 10 | "strings" |
@@ -377,6 +378,80 @@ func TestExecInEnvironment(t *testing.T) { |
377 | 378 | } |
378 | 379 | } |
379 | 380 |
|
| 381 | +func genBigEnv(count int) []string { |
| 382 | + randStr := func(length int) string { |
| 383 | + const charset = "abcdefghijklmnopqrstuvwxyz0123456789_" |
| 384 | + b := make([]byte, length) |
| 385 | + for i := range b { |
| 386 | + b[i] = charset[rand.Intn(len(charset))] |
| 387 | + } |
| 388 | + return string(b) |
| 389 | + } |
| 390 | + |
| 391 | + envs := make([]string, count) |
| 392 | + for i := 0; i < count; i++ { |
| 393 | + key := strings.ToUpper(randStr(10)) |
| 394 | + value := randStr(20) |
| 395 | + envs[i] = key + "=" + value |
| 396 | + } |
| 397 | + |
| 398 | + return envs |
| 399 | +} |
| 400 | + |
| 401 | +func BenchmarkExecInBigEnv(b *testing.B) { |
| 402 | + if testing.Short() { |
| 403 | + return |
| 404 | + } |
| 405 | + config := newTemplateConfig(b, nil) |
| 406 | + container, err := newContainer(b, config) |
| 407 | + ok(b, err) |
| 408 | + defer destroyContainer(container) |
| 409 | + |
| 410 | + // Execute a first process in the container |
| 411 | + stdinR, stdinW, err := os.Pipe() |
| 412 | + ok(b, err) |
| 413 | + process := &libcontainer.Process{ |
| 414 | + Cwd: "/", |
| 415 | + Args: []string{"cat"}, |
| 416 | + Env: standardEnvironment, |
| 417 | + Stdin: stdinR, |
| 418 | + Init: true, |
| 419 | + } |
| 420 | + err = container.Run(process) |
| 421 | + _ = stdinR.Close() |
| 422 | + defer stdinW.Close() //nolint: errcheck |
| 423 | + ok(b, err) |
| 424 | + |
| 425 | + const numEnv = 5000 |
| 426 | + env := append(standardEnvironment, genBigEnv(numEnv)...) |
| 427 | + // Construct the expected output. |
| 428 | + var wantOut bytes.Buffer |
| 429 | + for _, e := range env { |
| 430 | + wantOut.WriteString(e + "\n") |
| 431 | + } |
| 432 | + |
| 433 | + b.ResetTimer() |
| 434 | + for i := 0; i < b.N; i++ { |
| 435 | + buffers := newStdBuffers() |
| 436 | + process2 := &libcontainer.Process{ |
| 437 | + Cwd: "/", |
| 438 | + Args: []string{"env"}, |
| 439 | + Env: env, |
| 440 | + Stdin: buffers.Stdin, |
| 441 | + Stdout: buffers.Stdout, |
| 442 | + Stderr: buffers.Stderr, |
| 443 | + } |
| 444 | + err = container.Run(process2) |
| 445 | + ok(b, err) |
| 446 | + waitProcess(process2, b) |
| 447 | + if !bytes.Equal(buffers.Stdout.Bytes(), wantOut.Bytes()) { |
| 448 | + b.Fatalf("unexpected output: %s (stderr: %s)", buffers.Stdout, buffers.Stderr) |
| 449 | + } |
| 450 | + } |
| 451 | + _ = stdinW.Close() |
| 452 | + waitProcess(process, b) |
| 453 | +} |
| 454 | + |
380 | 455 | func TestExecinPassExtraFiles(t *testing.T) { |
381 | 456 | if testing.Short() { |
382 | 457 | return |
|
0 commit comments