Skip to content

Commit 8545eb1

Browse files
author
utam0k
committed
Add some tests for mergeEnv.
1 parent 6177765 commit 8545eb1

File tree

1 file changed

+138
-31
lines changed

1 file changed

+138
-31
lines changed

pkg/dazzle/combiner_test.go

Lines changed: 138 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -27,54 +27,161 @@ import (
2727
)
2828

2929
func TestMergeEnv(t *testing.T) {
30-
base := ociv1.Image{
31-
Config: ociv1.ImageConfig{
32-
Env: []string{
33-
"PATH=first:second:third:$PATH",
34-
},
35-
},
30+
type args struct {
3631
}
37-
others := []*ociv1.Image{
32+
tests := []struct {
33+
name string
34+
base *ociv1.Image
35+
others []*ociv1.Image
36+
vars []EnvVarCombination
37+
expect []string
38+
}{
3839
{
39-
Config: ociv1.ImageConfig{
40-
Env: []string{
41-
"PATH=fourth:fifth:$PATH",
40+
name: "EnvVarCombineMergeUnique",
41+
base: &ociv1.Image{
42+
Config: ociv1.ImageConfig{
43+
Env: []string{
44+
"PATH=first:second:third:$PATH",
45+
},
46+
},
47+
},
48+
others: []*ociv1.Image{
49+
{
50+
Config: ociv1.ImageConfig{
51+
Env: []string{
52+
"PATH=fourth:fifth:$PATH",
53+
},
54+
},
55+
},
56+
{
57+
Config: ociv1.ImageConfig{
58+
Env: []string{
59+
"PATH=sixth:sixth:$PATH",
60+
},
61+
},
62+
},
63+
{
64+
Config: ociv1.ImageConfig{
65+
Env: []string{
66+
"PATH=eighth:seventh:eighth:$PATH",
67+
},
68+
},
69+
},
70+
},
71+
vars: []EnvVarCombination{
72+
{
73+
Name: "PATH",
74+
Action: EnvVarCombineMergeUnique,
4275
},
4376
},
77+
expect: []string{"PATH=first:second:third:fourth:fifth:sixth:seventh:eighth:$PATH"},
4478
},
4579
{
46-
Config: ociv1.ImageConfig{
47-
Env: []string{
48-
"PATH=sixth:sixth:$PATH",
80+
name: "EnvVarCombineMerge",
81+
base: &ociv1.Image{
82+
Config: ociv1.ImageConfig{
83+
Env: []string{
84+
"PATH=first:second:third:$PATH",
85+
},
4986
},
5087
},
88+
others: []*ociv1.Image{
89+
{
90+
Config: ociv1.ImageConfig{
91+
Env: []string{
92+
"PATH=fourth:fifth:$PATH",
93+
},
94+
},
95+
},
96+
{
97+
Config: ociv1.ImageConfig{
98+
Env: []string{
99+
"PATH=sixth:sixth:$PATH",
100+
},
101+
},
102+
},
103+
{
104+
Config: ociv1.ImageConfig{
105+
Env: []string{
106+
"PATH=eighth:seventh:eighth:$PATH",
107+
},
108+
},
109+
},
110+
},
111+
vars: []EnvVarCombination{
112+
{
113+
Name: "PATH",
114+
Action: EnvVarCombineMerge,
115+
},
116+
},
117+
expect: []string{"PATH=first:second:third:$PATH:fourth:fifth:$PATH:sixth:sixth:$PATH:eighth:seventh:eighth:$PATH"},
51118
},
52119
{
53-
Config: ociv1.ImageConfig{
54-
Env: []string{
55-
"PATH=eighth:seventh:eighth:$PATH",
120+
name: "EnvVarCombineUseLast",
121+
base: &ociv1.Image{
122+
Config: ociv1.ImageConfig{
123+
Env: []string{
124+
"PATH=first:second:third:$PATH",
125+
},
56126
},
57127
},
128+
others: []*ociv1.Image{
129+
{
130+
Config: ociv1.ImageConfig{
131+
Env: []string{
132+
"PATH=fourth:fifth:$PATH",
133+
},
134+
},
135+
},
136+
},
137+
vars: []EnvVarCombination{
138+
{
139+
Name: "PATH",
140+
Action: EnvVarCombineUseLast,
141+
},
142+
},
143+
expect: []string{"PATH=fourth:fifth:$PATH"},
58144
},
59-
}
60-
vars := []EnvVarCombination{
61145
{
62-
Name: "PATH",
63-
Action: EnvVarCombineMergeUnique,
146+
name: "EnvVarCombineUseFirst",
147+
base: &ociv1.Image{
148+
Config: ociv1.ImageConfig{
149+
Env: []string{
150+
"PATH=first:second:third:$PATH",
151+
},
152+
},
153+
},
154+
others: []*ociv1.Image{
155+
{
156+
Config: ociv1.ImageConfig{
157+
Env: []string{
158+
"PATH=fourth:fifth:$PATH",
159+
},
160+
},
161+
},
162+
},
163+
vars: []EnvVarCombination{
164+
{
165+
Name: "PATH",
166+
Action: EnvVarCombineUseFirst,
167+
},
168+
},
169+
expect: []string{"PATH=first:second:third:$PATH"},
64170
},
65171
}
66-
envs, err := mergeEnv(&base, others, vars)
67-
if err != nil {
68-
t.Fatal(err)
69-
}
70-
expect := []string{"PATH=first:second:third:fourth:fifth:sixth:seventh:eighth:$PATH"}
71-
if len(envs) != len(expect) {
72-
t.Fatal("unexpected length", len(envs))
73-
}
74-
for i, env := range envs {
75-
if env != expect[i] {
76-
t.Fatal("unexpected env", envs)
172+
for _, test := range tests {
173+
envs, err := mergeEnv(test.base, test.others, test.vars)
174+
if err != nil {
175+
t.Fatal(err)
176+
}
177+
if len(envs) != len(test.expect) {
178+
t.Fatal("unexpected length", len(envs))
77179
}
180+
for i, env := range envs {
181+
if env != test.expect[i] {
182+
t.Fatal("unexpected env", envs)
183+
}
78184

185+
}
79186
}
80187
}

0 commit comments

Comments
 (0)