2018-02-05 16:05:59 -05:00
|
|
|
package container // import "github.com/docker/docker/container"
|
2013-01-18 19:13:39 -05:00
|
|
|
|
2020-04-22 13:51:03 -04:00
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
)
|
2013-01-18 19:13:39 -05:00
|
|
|
|
2014-03-01 02:29:00 -05:00
|
|
|
func TestReplaceAndAppendEnvVars(t *testing.T) {
|
|
|
|
var (
|
2017-03-07 23:26:02 -05:00
|
|
|
d = []string{"HOME=/", "FOO=foo_default"}
|
|
|
|
// remove FOO from env
|
|
|
|
// remove BAR from env (nop)
|
|
|
|
o = []string{"HOME=/root", "TERM=xterm", "FOO", "BAR"}
|
2014-03-01 02:29:00 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
env := ReplaceOrAppendEnvValues(d, o)
|
2017-03-07 23:26:02 -05:00
|
|
|
t.Logf("default=%v, override=%v, result=%v", d, o, env)
|
2014-03-01 02:29:00 -05:00
|
|
|
if len(env) != 2 {
|
|
|
|
t.Fatalf("expected len of 2 got %d", len(env))
|
|
|
|
}
|
|
|
|
if env[0] != "HOME=/root" {
|
|
|
|
t.Fatalf("expected HOME=/root got '%s'", env[0])
|
|
|
|
}
|
|
|
|
if env[1] != "TERM=xterm" {
|
|
|
|
t.Fatalf("expected TERM=xterm got '%s'", env[1])
|
2015-02-26 21:23:50 -05:00
|
|
|
}
|
|
|
|
}
|
2020-04-22 13:51:03 -04:00
|
|
|
|
|
|
|
func BenchmarkReplaceOrAppendEnvValues(b *testing.B) {
|
|
|
|
b.Run("0", func(b *testing.B) {
|
|
|
|
benchmarkReplaceOrAppendEnvValues(b, 0)
|
|
|
|
})
|
|
|
|
b.Run("100", func(b *testing.B) {
|
|
|
|
benchmarkReplaceOrAppendEnvValues(b, 100)
|
|
|
|
})
|
|
|
|
b.Run("1000", func(b *testing.B) {
|
|
|
|
benchmarkReplaceOrAppendEnvValues(b, 1000)
|
|
|
|
})
|
|
|
|
b.Run("10000", func(b *testing.B) {
|
|
|
|
benchmarkReplaceOrAppendEnvValues(b, 10000)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func benchmarkReplaceOrAppendEnvValues(b *testing.B, extraEnv int) {
|
|
|
|
b.StopTimer()
|
|
|
|
// remove FOO from env
|
|
|
|
// remove BAR from env (nop)
|
|
|
|
o := []string{"HOME=/root", "TERM=xterm", "FOO", "BAR"}
|
|
|
|
|
|
|
|
if extraEnv > 0 {
|
|
|
|
buf := make([]byte, 5)
|
|
|
|
for i := 0; i < extraEnv; i++ {
|
|
|
|
n, err := rand.Read(buf)
|
|
|
|
assert.NilError(b, err)
|
|
|
|
key := string(buf[:n])
|
|
|
|
|
|
|
|
n, err = rand.Read(buf)
|
|
|
|
assert.NilError(b, err)
|
|
|
|
val := string(buf[:n])
|
|
|
|
|
|
|
|
o = append(o, key+"="+val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
d := make([]string, 0, len(o)+2)
|
|
|
|
d = append(d, []string{"HOME=/", "FOO=foo_default"}...)
|
|
|
|
|
|
|
|
b.StartTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
_ = ReplaceOrAppendEnvValues(d, o)
|
|
|
|
}
|
|
|
|
}
|