github.com/soltysh/source-to-image@v1.2.0/pkg/scripts/environment_test.go (about) 1 package scripts 2 3 import ( 4 "testing" 5 6 "github.com/docker/docker/api/types/container" 7 "github.com/docker/docker/builder/dockerfile" 8 "github.com/openshift/source-to-image/pkg/api" 9 ) 10 11 func TestConvertEnvironmentList(t *testing.T) { 12 testEnv := api.EnvironmentList{ 13 {Name: "Key1", Value: "Value1"}, 14 {Name: "Key2", Value: "Value2"}, 15 {Name: "Key3", Value: "Value3"}, 16 {Name: "Key4", Value: "Value=4"}, 17 {Name: "Key5", Value: "Value,5"}, 18 } 19 result := ConvertEnvironmentList(testEnv) 20 expected := []string{"Key1=Value1", "Key2=Value2", "Key3=Value3", "Key4=Value=4", "Key5=Value,5"} 21 if !equalArrayContents(result, expected) { 22 t.Errorf("Unexpected result. Expected: %#v. Actual: %#v", 23 expected, result) 24 } 25 } 26 27 func equalArrayContents(a []string, b []string) bool { 28 if len(a) != len(b) { 29 return false 30 } 31 for _, e := range a { 32 found := false 33 for _, f := range b { 34 if f == e { 35 found = true 36 break 37 } 38 } 39 if !found { 40 return false 41 } 42 } 43 return true 44 } 45 46 func TestConvertEnvironmentToDocker(t *testing.T) { 47 testValues := []string{ 48 "Value1", 49 "$Value1", 50 "${Value1}", 51 "`Value1`", 52 `"`, 53 } 54 var charValues []string 55 for ch := rune(32); ch < 127; ch++ { 56 charValues = append(charValues, "AB"+string(ch)+"CD", "AB\\"+string(ch)+"CD") 57 } 58 for _, testValue := range append(testValues, charValues...) { 59 list := api.EnvironmentList{{Name: "TEST", Value: testValue}} 60 converted := ConvertEnvironmentToDocker(list) 61 config, err := dockerfile.BuildFromConfig(&container.Config{}, []string{converted}) 62 if err != nil { 63 t.Fatalf("Unexpected error building using Dockerfile contents %v: %v", converted, err) 64 } 65 if len(config.Env) != 1 { 66 t.Errorf("Unexpected result. Expected 1 environment variable, got config %#v", config) 67 } 68 if config.Env[0] != "TEST="+testValue { 69 t.Errorf("Unexpected result. Expected: %s=%#v -> %q -> %s=%v. Got: %#v", list[0].Name, testValue, converted, list[0].Name, testValue, config.Env[0]) 70 } 71 } 72 }