2016-09-08 13:11:39 -04:00
|
|
|
package bundlefile
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2016-09-08 13:11:39 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestLoadFileV01Success(t *testing.T) {
|
|
|
|
reader := strings.NewReader(`{
|
|
|
|
"Version": "0.1",
|
|
|
|
"Services": {
|
|
|
|
"redis": {
|
|
|
|
"Image": "redis@sha256:4b24131101fa0117bcaa18ac37055fffd9176aa1a240392bb8ea85e0be50f2ce",
|
|
|
|
"Networks": ["default"]
|
|
|
|
},
|
|
|
|
"web": {
|
|
|
|
"Image": "dockercloud/hello-world@sha256:fe79a2cfbd17eefc344fb8419420808df95a1e22d93b7f621a7399fd1e9dca1d",
|
|
|
|
"Networks": ["default"],
|
|
|
|
"User": "web"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`)
|
|
|
|
|
|
|
|
bundle, err := LoadFile(reader)
|
2018-03-06 14:44:13 -05:00
|
|
|
assert.NilError(t, err)
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal("0.1", bundle.Version))
|
|
|
|
assert.Check(t, is.Len(bundle.Services, 2))
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadFileSyntaxError(t *testing.T) {
|
|
|
|
reader := strings.NewReader(`{
|
|
|
|
"Version": "0.1",
|
|
|
|
"Services": unquoted string
|
|
|
|
}`)
|
|
|
|
|
|
|
|
_, err := LoadFile(reader)
|
2018-03-06 15:54:24 -05:00
|
|
|
assert.Error(t, err, "JSON syntax error at byte 37: invalid character 'u' looking for beginning of value")
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadFileTypeError(t *testing.T) {
|
|
|
|
reader := strings.NewReader(`{
|
|
|
|
"Version": "0.1",
|
|
|
|
"Services": {
|
|
|
|
"web": {
|
|
|
|
"Image": "redis",
|
|
|
|
"Networks": "none"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`)
|
|
|
|
|
|
|
|
_, err := LoadFile(reader)
|
2018-03-06 15:54:24 -05:00
|
|
|
assert.Error(t, err, "Unexpected type at byte 94. Expected []string but received string.")
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrint(t *testing.T) {
|
|
|
|
var buffer bytes.Buffer
|
|
|
|
bundle := &Bundlefile{
|
|
|
|
Version: "0.1",
|
|
|
|
Services: map[string]Service{
|
|
|
|
"web": {
|
|
|
|
Image: "image",
|
|
|
|
Command: []string{"echo", "something"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, Print(&buffer, bundle))
|
2016-09-08 13:11:39 -04:00
|
|
|
output := buffer.String()
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Contains(output, "\"Image\": \"image\""))
|
|
|
|
assert.Check(t, is.Contains(output,
|
2016-09-08 13:11:39 -04:00
|
|
|
`"Command": [
|
|
|
|
"echo",
|
|
|
|
"something"
|
2018-03-05 18:53:52 -05:00
|
|
|
]`))
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|