| 1 |
package imagebuilder |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"sort" |
| 6 |
"strings" |
| 7 |
"testing" |
| 8 |
) |
| 9 |
|
| 10 |
func TestMergeEnv(t *testing.T) { |
| 11 |
tests := [][3][]string{ |
| 12 |
{ |
| 13 |
[]string{"A=B", "B=C", "C=D"}, |
| 14 |
nil, |
| 15 |
[]string{"A=B", "B=C", "C=D"}, |
| 16 |
}, |
| 17 |
{ |
| 18 |
nil, |
| 19 |
[]string{"A=B", "B=C", "C=D"}, |
| 20 |
[]string{"A=B", "B=C", "C=D"}, |
| 21 |
}, |
| 22 |
{ |
| 23 |
[]string{"A=B", "B=C", "C=D", "E=F"}, |
| 24 |
[]string{"B=O", "F=G"}, |
| 25 |
[]string{"A=B", "B=O", "C=D", "E=F", "F=G"}, |
| 26 |
}, |
| 27 |
} |
| 28 |
for i, test := range tests { |
| 29 |
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) { |
| 30 |
result := mergeEnv(test[0], test[1]) |
| 31 |
if len(result) != len(test[2]) { |
| 32 |
t.Fatalf("expected %v, got %v", test[2], result) |
| 33 |
} |
| 34 |
for i := range result { |
| 35 |
if result[i] != test[2][i] { |
| 36 |
t.Fatalf("expected %v, got %v", test[2], result) |
| 37 |
} |
| 38 |
} |
| 39 |
}) |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
func TestEnvMapAsSlice(t *testing.T) { |
| 44 |
tests := [][2][]string{ |
| 45 |
{ |
| 46 |
[]string{"A=B", "B=C", "C=D"}, |
| 47 |
[]string{"A=B", "B=C", "C=D"}, |
| 48 |
}, |
| 49 |
{ |
| 50 |
[]string{"A=B", "B=C", "C=D", "E=F", "B=O", "F=G"}, |
| 51 |
[]string{"A=B", "B=O", "C=D", "E=F", "F=G"}, |
| 52 |
}, |
| 53 |
{ |
| 54 |
[]string{"A=B", "C=D", "B=C", "E=F", "F=G", "B=O"}, |
| 55 |
[]string{"A=B", "B=O", "C=D", "E=F", "F=G"}, |
| 56 |
}, |
| 57 |
} |
| 58 |
for i, test := range tests { |
| 59 |
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) { |
| 60 |
m := make(map[string]string) |
| 61 |
for _, spec := range test[0] { |
| 62 |
s := strings.SplitN(spec, "=", 2) |
| 63 |
m[s[0]] = s[1] |
| 64 |
} |
| 65 |
result := envMapAsSlice(m) |
| 66 |
sort.Strings(result) |
| 67 |
if len(result) != len(test[1]) { |
| 68 |
t.Fatalf("expected %v, got %v", test[1], result) |
| 69 |
} |
| 70 |
for i := range result { |
| 71 |
if result[i] != test[1][i] { |
| 72 |
t.Fatalf("expected %v, got %v", test[1], result) |
| 73 |
} |
| 74 |
} |
| 75 |
}) |
| 76 |
} |
| 77 |
} |