interpolation_test.go 1.2 KB

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