interpolation_test.go 1.3 KB

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