2016-12-20 16:26:49 -05:00
|
|
|
package interpolation
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
var defaults = map[string]string{
|
|
|
|
"USER": "jenny",
|
|
|
|
"FOO": "bar",
|
|
|
|
}
|
|
|
|
|
|
|
|
func defaultMapping(name string) (string, bool) {
|
|
|
|
val, ok := defaults[name]
|
|
|
|
return val, ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInterpolate(t *testing.T) {
|
2017-03-22 10:42:03 -04:00
|
|
|
services := map[string]interface{}{
|
|
|
|
"servicea": map[string]interface{}{
|
2016-12-20 16:26:49 -05:00
|
|
|
"image": "example:${USER}",
|
|
|
|
"volumes": []interface{}{"$FOO:/target"},
|
2017-03-22 10:42:03 -04:00
|
|
|
"logging": map[string]interface{}{
|
2016-12-20 16:26:49 -05:00
|
|
|
"driver": "${FOO}",
|
2017-03-22 10:42:03 -04:00
|
|
|
"options": map[string]interface{}{
|
2016-12-20 16:26:49 -05:00
|
|
|
"user": "$USER",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2017-03-22 10:42:03 -04:00
|
|
|
expected := map[string]interface{}{
|
|
|
|
"servicea": map[string]interface{}{
|
2016-12-20 16:26:49 -05:00
|
|
|
"image": "example:jenny",
|
|
|
|
"volumes": []interface{}{"bar:/target"},
|
2017-03-22 10:42:03 -04:00
|
|
|
"logging": map[string]interface{}{
|
2016-12-20 16:26:49 -05:00
|
|
|
"driver": "bar",
|
2017-03-22 10:42:03 -04:00
|
|
|
"options": map[string]interface{}{
|
2016-12-20 16:26:49 -05:00
|
|
|
"user": "jenny",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
result, err := Interpolate(services, "service", defaultMapping)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, expected, result)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInvalidInterpolation(t *testing.T) {
|
2017-03-22 10:42:03 -04:00
|
|
|
services := map[string]interface{}{
|
|
|
|
"servicea": map[string]interface{}{
|
2016-12-20 16:26:49 -05:00
|
|
|
"image": "${",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
_, err := Interpolate(services, "service", defaultMapping)
|
2017-02-18 00:29:51 -05:00
|
|
|
assert.EqualError(t, err, `Invalid interpolation format for "image" option in service "servicea": "${". You may need to escape any $ with another $.`)
|
2016-12-20 16:26:49 -05:00
|
|
|
}
|