github.com/kunnos/engine@v1.13.1/cli/compose/interpolation/interpolation_test.go (about) 1 package interpolation 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 8 "github.com/docker/docker/cli/compose/types" 9 ) 10 11 var defaults = map[string]string{ 12 "USER": "jenny", 13 "FOO": "bar", 14 } 15 16 func defaultMapping(name string) (string, bool) { 17 val, ok := defaults[name] 18 return val, ok 19 } 20 21 func TestInterpolate(t *testing.T) { 22 services := types.Dict{ 23 "servicea": types.Dict{ 24 "image": "example:${USER}", 25 "volumes": []interface{}{"$FOO:/target"}, 26 "logging": types.Dict{ 27 "driver": "${FOO}", 28 "options": types.Dict{ 29 "user": "$USER", 30 }, 31 }, 32 }, 33 } 34 expected := types.Dict{ 35 "servicea": types.Dict{ 36 "image": "example:jenny", 37 "volumes": []interface{}{"bar:/target"}, 38 "logging": types.Dict{ 39 "driver": "bar", 40 "options": types.Dict{ 41 "user": "jenny", 42 }, 43 }, 44 }, 45 } 46 result, err := Interpolate(services, "service", defaultMapping) 47 assert.NoError(t, err) 48 assert.Equal(t, expected, result) 49 } 50 51 func TestInvalidInterpolation(t *testing.T) { 52 services := types.Dict{ 53 "servicea": types.Dict{ 54 "image": "${", 55 }, 56 } 57 _, err := Interpolate(services, "service", defaultMapping) 58 assert.EqualError(t, err, `Invalid interpolation format for "image" option in service "servicea": "${"`) 59 }