-
Notifications
You must be signed in to change notification settings - Fork 0
/
env_test.go
73 lines (63 loc) · 1.69 KB
/
env_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package container // import "github.com/docker/docker/container"
import (
"crypto/rand"
"testing"
"gotest.tools/v3/assert"
)
func TestReplaceAndAppendEnvVars(t *testing.T) {
var (
d = []string{"HOME=/", "FOO=foo_default"}
// remove FOO from env
// remove BAR from env (nop)
o = []string{"HOME=/root", "TERM=xterm", "FOO", "BAR"}
)
env := ReplaceOrAppendEnvValues(d, o)
t.Logf("default=%v, override=%v, result=%v", d, o, env)
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])
}
}
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)
}
}