2016-12-20 16:26:49 -05:00
|
|
|
package schema
|
|
|
|
|
|
|
|
import (
|
2019-02-02 11:55:39 -05:00
|
|
|
"fmt"
|
2016-12-20 16:26:49 -05:00
|
|
|
"testing"
|
|
|
|
|
2018-06-08 12:24:26 -04:00
|
|
|
"gotest.tools/assert"
|
2016-12-20 16:26:49 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type dict map[string]interface{}
|
|
|
|
|
2017-01-10 17:10:53 -05:00
|
|
|
func TestValidate(t *testing.T) {
|
2016-12-20 16:26:49 -05:00
|
|
|
config := dict{
|
2017-01-10 17:10:53 -05:00
|
|
|
"version": "3.0",
|
2016-12-20 16:26:49 -05:00
|
|
|
"services": dict{
|
|
|
|
"foo": dict{
|
|
|
|
"image": "busybox",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, Validate(config, "3.0"))
|
2016-12-20 16:26:49 -05:00
|
|
|
}
|
|
|
|
|
2017-01-10 17:10:53 -05:00
|
|
|
func TestValidateUndefinedTopLevelOption(t *testing.T) {
|
2016-12-20 16:26:49 -05:00
|
|
|
config := dict{
|
2017-01-10 17:10:53 -05:00
|
|
|
"version": "3.0",
|
2016-12-20 16:26:49 -05:00
|
|
|
"helicopters": dict{
|
|
|
|
"foo": dict{
|
|
|
|
"image": "busybox",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-01-10 17:10:53 -05:00
|
|
|
err := Validate(config, "3.0")
|
2017-12-21 16:27:57 -05:00
|
|
|
assert.ErrorContains(t, err, "Additional property helicopters is not allowed")
|
2017-01-10 17:10:53 -05:00
|
|
|
}
|
|
|
|
|
2017-08-17 12:56:33 -04:00
|
|
|
func TestValidateAllowsXTopLevelFields(t *testing.T) {
|
|
|
|
config := dict{
|
|
|
|
"version": "3.4",
|
|
|
|
"x-extra-stuff": dict{},
|
|
|
|
}
|
|
|
|
|
|
|
|
err := Validate(config, "3.4")
|
2018-03-06 14:44:13 -05:00
|
|
|
assert.NilError(t, err)
|
2017-08-17 12:56:33 -04:00
|
|
|
}
|
|
|
|
|
2018-05-31 07:38:58 -04:00
|
|
|
func TestValidateAllowsXFields(t *testing.T) {
|
|
|
|
config := dict{
|
|
|
|
"version": "3.7",
|
|
|
|
"services": dict{
|
|
|
|
"bar": dict{
|
|
|
|
"x-extra-stuff": dict{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"volumes": dict{
|
|
|
|
"bar": dict{
|
|
|
|
"x-extra-stuff": dict{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"networks": dict{
|
|
|
|
"bar": dict{
|
|
|
|
"x-extra-stuff": dict{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"configs": dict{
|
|
|
|
"bar": dict{
|
|
|
|
"x-extra-stuff": dict{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"secrets": dict{
|
|
|
|
"bar": dict{
|
|
|
|
"x-extra-stuff": dict{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := Validate(config, "3.7")
|
|
|
|
assert.NilError(t, err)
|
|
|
|
}
|
|
|
|
|
2019-02-02 11:55:39 -05:00
|
|
|
func TestValidateCredentialSpecs(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
version string
|
|
|
|
expectedErr string
|
|
|
|
}{
|
|
|
|
{version: "3.0", expectedErr: "credential_spec"},
|
|
|
|
{version: "3.1", expectedErr: "credential_spec"},
|
|
|
|
{version: "3.2", expectedErr: "credential_spec"},
|
|
|
|
{version: "3.3", expectedErr: "config"},
|
|
|
|
{version: "3.4", expectedErr: "config"},
|
|
|
|
{version: "3.5", expectedErr: "config"},
|
|
|
|
{version: "3.6", expectedErr: "config"},
|
|
|
|
{version: "3.7", expectedErr: "config"},
|
2019-02-02 10:35:26 -05:00
|
|
|
{version: "3.8"},
|
2019-02-02 11:55:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
tc := tc
|
|
|
|
t.Run(tc.version, func(t *testing.T) {
|
2019-03-18 07:53:38 -04:00
|
|
|
config := dict{
|
|
|
|
"version": "99.99",
|
|
|
|
"services": dict{
|
|
|
|
"foo": dict{
|
|
|
|
"image": "busybox",
|
|
|
|
"credential_spec": dict{
|
2019-02-02 10:35:26 -05:00
|
|
|
"config": "foobar",
|
2019-03-18 07:53:38 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2019-02-02 11:55:39 -05:00
|
|
|
err := Validate(config, tc.version)
|
|
|
|
if tc.expectedErr != "" {
|
|
|
|
assert.ErrorContains(t, err, fmt.Sprintf("Additional property %s is not allowed", tc.expectedErr))
|
|
|
|
} else {
|
|
|
|
assert.NilError(t, err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-10 08:35:29 -05:00
|
|
|
func TestValidateSecretConfigNames(t *testing.T) {
|
|
|
|
config := dict{
|
2017-11-11 00:11:29 -05:00
|
|
|
"version": "3.5",
|
2017-11-10 08:35:29 -05:00
|
|
|
"configs": dict{
|
2017-11-11 00:11:29 -05:00
|
|
|
"bar": dict{
|
|
|
|
"name": "foobar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"secrets": dict{
|
|
|
|
"baz": dict{
|
|
|
|
"name": "foobaz",
|
|
|
|
},
|
|
|
|
},
|
2017-11-10 08:35:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
err := Validate(config, "3.5")
|
2018-03-06 14:44:13 -05:00
|
|
|
assert.NilError(t, err)
|
2017-11-10 08:35:29 -05:00
|
|
|
}
|
|
|
|
|
2017-01-10 17:10:53 -05:00
|
|
|
func TestValidateInvalidVersion(t *testing.T) {
|
|
|
|
config := dict{
|
|
|
|
"version": "2.1",
|
|
|
|
"services": dict{
|
|
|
|
"foo": dict{
|
|
|
|
"image": "busybox",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err := Validate(config, "2.1")
|
2017-12-21 16:27:57 -05:00
|
|
|
assert.ErrorContains(t, err, "unsupported Compose file version: 2.1")
|
2016-12-20 16:26:49 -05:00
|
|
|
}
|
2017-05-06 09:49:42 -04:00
|
|
|
|
|
|
|
type array []interface{}
|
|
|
|
|
|
|
|
func TestValidatePlacement(t *testing.T) {
|
|
|
|
config := dict{
|
|
|
|
"version": "3.3",
|
|
|
|
"services": dict{
|
|
|
|
"foo": dict{
|
|
|
|
"image": "busybox",
|
|
|
|
"deploy": dict{
|
|
|
|
"placement": dict{
|
|
|
|
"preferences": array{
|
|
|
|
dict{
|
|
|
|
"spread": "node.labels.az",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, Validate(config, "3.3"))
|
2017-05-06 09:49:42 -04:00
|
|
|
}
|
2017-11-17 09:31:13 -05:00
|
|
|
|
|
|
|
func TestValidateIsolation(t *testing.T) {
|
|
|
|
config := dict{
|
|
|
|
"version": "3.5",
|
|
|
|
"services": dict{
|
|
|
|
"foo": dict{
|
|
|
|
"image": "busybox",
|
|
|
|
"isolation": "some-isolation-value",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, Validate(config, "3.5"))
|
2017-11-17 09:31:13 -05:00
|
|
|
}
|
2018-05-29 05:37:51 -04:00
|
|
|
|
|
|
|
func TestValidateRollbackConfig(t *testing.T) {
|
|
|
|
config := dict{
|
|
|
|
"version": "3.4",
|
|
|
|
"services": dict{
|
|
|
|
"foo": dict{
|
|
|
|
"image": "busybox",
|
|
|
|
"deploy": dict{
|
|
|
|
"rollback_config": dict{
|
|
|
|
"parallelism": 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.NilError(t, Validate(config, "3.7"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidateRollbackConfigWithOrder(t *testing.T) {
|
|
|
|
config := dict{
|
|
|
|
"version": "3.4",
|
|
|
|
"services": dict{
|
|
|
|
"foo": dict{
|
|
|
|
"image": "busybox",
|
|
|
|
"deploy": dict{
|
|
|
|
"rollback_config": dict{
|
|
|
|
"parallelism": 1,
|
|
|
|
"order": "start-first",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.NilError(t, Validate(config, "3.7"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidateRollbackConfigWithUpdateConfig(t *testing.T) {
|
|
|
|
config := dict{
|
|
|
|
"version": "3.4",
|
|
|
|
"services": dict{
|
|
|
|
"foo": dict{
|
|
|
|
"image": "busybox",
|
|
|
|
"deploy": dict{
|
|
|
|
"update_config": dict{
|
|
|
|
"parallelism": 1,
|
|
|
|
"order": "start-first",
|
|
|
|
},
|
|
|
|
"rollback_config": dict{
|
|
|
|
"parallelism": 1,
|
|
|
|
"order": "start-first",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.NilError(t, Validate(config, "3.7"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidateRollbackConfigWithUpdateConfigFull(t *testing.T) {
|
|
|
|
config := dict{
|
|
|
|
"version": "3.4",
|
|
|
|
"services": dict{
|
|
|
|
"foo": dict{
|
|
|
|
"image": "busybox",
|
|
|
|
"deploy": dict{
|
|
|
|
"update_config": dict{
|
|
|
|
"parallelism": 1,
|
|
|
|
"order": "start-first",
|
|
|
|
"delay": "10s",
|
|
|
|
"failure_action": "pause",
|
|
|
|
"monitor": "10s",
|
|
|
|
},
|
|
|
|
"rollback_config": dict{
|
|
|
|
"parallelism": 1,
|
|
|
|
"order": "start-first",
|
|
|
|
"delay": "10s",
|
|
|
|
"failure_action": "pause",
|
|
|
|
"monitor": "10s",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.NilError(t, Validate(config, "3.7"))
|
|
|
|
}
|