2016-12-20 16:26:49 -05:00
|
|
|
package loader
|
|
|
|
|
|
|
|
import (
|
2017-11-06 17:03:43 -05:00
|
|
|
"bytes"
|
2016-12-20 16:26:49 -05:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"sort"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli/compose/types"
|
2017-11-06 17:03:43 -05:00
|
|
|
"github.com/sirupsen/logrus"
|
2016-12-20 16:26:49 -05:00
|
|
|
"github.com/stretchr/testify/assert"
|
2017-09-19 10:48:52 -04:00
|
|
|
"github.com/stretchr/testify/require"
|
2016-12-20 16:26:49 -05:00
|
|
|
)
|
|
|
|
|
2017-03-22 10:42:03 -04:00
|
|
|
func buildConfigDetails(source map[string]interface{}, env map[string]string) types.ConfigDetails {
|
2016-12-20 16:26:49 -05:00
|
|
|
workingDir, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return types.ConfigDetails{
|
|
|
|
WorkingDir: workingDir,
|
|
|
|
ConfigFiles: []types.ConfigFile{
|
|
|
|
{Filename: "filename.yml", Config: source},
|
|
|
|
},
|
2017-02-07 04:44:47 -05:00
|
|
|
Environment: env,
|
2016-12-20 16:26:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-14 12:39:26 -04:00
|
|
|
func loadYAML(yaml string) (*types.Config, error) {
|
|
|
|
return loadYAMLWithEnv(yaml, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadYAMLWithEnv(yaml string, env map[string]string) (*types.Config, error) {
|
|
|
|
dict, err := ParseYAML([]byte(yaml))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return Load(buildConfigDetails(dict, env))
|
|
|
|
}
|
|
|
|
|
2016-12-20 16:26:49 -05:00
|
|
|
var sampleYAML = `
|
|
|
|
version: "3"
|
|
|
|
services:
|
|
|
|
foo:
|
|
|
|
image: busybox
|
|
|
|
networks:
|
|
|
|
with_me:
|
|
|
|
bar:
|
|
|
|
image: busybox
|
|
|
|
environment:
|
|
|
|
- FOO=1
|
|
|
|
networks:
|
|
|
|
- with_ipam
|
|
|
|
volumes:
|
|
|
|
hello:
|
|
|
|
driver: default
|
|
|
|
driver_opts:
|
|
|
|
beep: boop
|
|
|
|
networks:
|
|
|
|
default:
|
|
|
|
driver: bridge
|
|
|
|
driver_opts:
|
|
|
|
beep: boop
|
|
|
|
with_ipam:
|
|
|
|
ipam:
|
|
|
|
driver: default
|
|
|
|
config:
|
|
|
|
- subnet: 172.28.0.0/16
|
|
|
|
`
|
|
|
|
|
2017-03-22 10:42:03 -04:00
|
|
|
var sampleDict = map[string]interface{}{
|
2016-12-20 16:26:49 -05:00
|
|
|
"version": "3",
|
2017-03-22 10:42:03 -04:00
|
|
|
"services": map[string]interface{}{
|
|
|
|
"foo": map[string]interface{}{
|
2016-12-20 16:26:49 -05:00
|
|
|
"image": "busybox",
|
2017-03-22 10:42:03 -04:00
|
|
|
"networks": map[string]interface{}{"with_me": nil},
|
2016-12-20 16:26:49 -05:00
|
|
|
},
|
2017-03-22 10:42:03 -04:00
|
|
|
"bar": map[string]interface{}{
|
2016-12-20 16:26:49 -05:00
|
|
|
"image": "busybox",
|
|
|
|
"environment": []interface{}{"FOO=1"},
|
|
|
|
"networks": []interface{}{"with_ipam"},
|
|
|
|
},
|
|
|
|
},
|
2017-03-22 10:42:03 -04:00
|
|
|
"volumes": map[string]interface{}{
|
|
|
|
"hello": map[string]interface{}{
|
2016-12-20 16:26:49 -05:00
|
|
|
"driver": "default",
|
2017-03-22 10:42:03 -04:00
|
|
|
"driver_opts": map[string]interface{}{
|
2016-12-20 16:26:49 -05:00
|
|
|
"beep": "boop",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-03-22 10:42:03 -04:00
|
|
|
"networks": map[string]interface{}{
|
|
|
|
"default": map[string]interface{}{
|
2016-12-20 16:26:49 -05:00
|
|
|
"driver": "bridge",
|
2017-03-22 10:42:03 -04:00
|
|
|
"driver_opts": map[string]interface{}{
|
2016-12-20 16:26:49 -05:00
|
|
|
"beep": "boop",
|
|
|
|
},
|
|
|
|
},
|
2017-03-22 10:42:03 -04:00
|
|
|
"with_ipam": map[string]interface{}{
|
|
|
|
"ipam": map[string]interface{}{
|
2016-12-20 16:26:49 -05:00
|
|
|
"driver": "default",
|
|
|
|
"config": []interface{}{
|
2017-03-22 10:42:03 -04:00
|
|
|
map[string]interface{}{
|
2016-12-20 16:26:49 -05:00
|
|
|
"subnet": "172.28.0.0/16",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-03-14 12:39:26 -04:00
|
|
|
func strPtr(val string) *string {
|
|
|
|
return &val
|
|
|
|
}
|
|
|
|
|
2016-12-20 16:26:49 -05:00
|
|
|
var sampleConfig = types.Config{
|
|
|
|
Services: []types.ServiceConfig{
|
|
|
|
{
|
|
|
|
Name: "foo",
|
|
|
|
Image: "busybox",
|
2017-03-14 12:39:26 -04:00
|
|
|
Environment: map[string]*string{},
|
2016-12-20 16:26:49 -05:00
|
|
|
Networks: map[string]*types.ServiceNetworkConfig{
|
|
|
|
"with_me": nil,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "bar",
|
|
|
|
Image: "busybox",
|
2017-03-14 12:39:26 -04:00
|
|
|
Environment: map[string]*string{"FOO": strPtr("1")},
|
2016-12-20 16:26:49 -05:00
|
|
|
Networks: map[string]*types.ServiceNetworkConfig{
|
|
|
|
"with_ipam": nil,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Networks: map[string]types.NetworkConfig{
|
|
|
|
"default": {
|
|
|
|
Driver: "bridge",
|
|
|
|
DriverOpts: map[string]string{
|
|
|
|
"beep": "boop",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"with_ipam": {
|
|
|
|
Ipam: types.IPAMConfig{
|
|
|
|
Driver: "default",
|
|
|
|
Config: []*types.IPAMPool{
|
|
|
|
{
|
|
|
|
Subnet: "172.28.0.0/16",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Volumes: map[string]types.VolumeConfig{
|
|
|
|
"hello": {
|
|
|
|
Driver: "default",
|
|
|
|
DriverOpts: map[string]string{
|
|
|
|
"beep": "boop",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseYAML(t *testing.T) {
|
|
|
|
dict, err := ParseYAML([]byte(sampleYAML))
|
2017-09-19 10:48:52 -04:00
|
|
|
require.NoError(t, err)
|
2016-12-20 16:26:49 -05:00
|
|
|
assert.Equal(t, sampleDict, dict)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoad(t *testing.T) {
|
2017-02-07 04:44:47 -05:00
|
|
|
actual, err := Load(buildConfigDetails(sampleDict, nil))
|
2017-09-19 10:48:52 -04:00
|
|
|
require.NoError(t, err)
|
2016-12-20 16:26:49 -05:00
|
|
|
assert.Equal(t, serviceSort(sampleConfig.Services), serviceSort(actual.Services))
|
|
|
|
assert.Equal(t, sampleConfig.Networks, actual.Networks)
|
|
|
|
assert.Equal(t, sampleConfig.Volumes, actual.Volumes)
|
|
|
|
}
|
|
|
|
|
2017-01-10 17:40:53 -05:00
|
|
|
func TestLoadV31(t *testing.T) {
|
|
|
|
actual, err := loadYAML(`
|
|
|
|
version: "3.1"
|
|
|
|
services:
|
|
|
|
foo:
|
|
|
|
image: busybox
|
|
|
|
secrets: [super]
|
|
|
|
secrets:
|
|
|
|
super:
|
|
|
|
external: true
|
2017-03-14 12:39:26 -04:00
|
|
|
`)
|
2017-09-19 10:48:52 -04:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Len(t, actual.Services, 1)
|
|
|
|
assert.Len(t, actual.Secrets, 1)
|
2017-01-10 17:40:53 -05:00
|
|
|
}
|
|
|
|
|
2017-05-11 08:30:04 -04:00
|
|
|
func TestLoadV33(t *testing.T) {
|
|
|
|
actual, err := loadYAML(`
|
|
|
|
version: "3.3"
|
|
|
|
services:
|
|
|
|
foo:
|
|
|
|
image: busybox
|
|
|
|
credential_spec:
|
|
|
|
File: "/foo"
|
2017-05-15 11:19:32 -04:00
|
|
|
configs: [super]
|
|
|
|
configs:
|
|
|
|
super:
|
|
|
|
external: true
|
2017-05-11 08:30:04 -04:00
|
|
|
`)
|
2017-09-19 10:48:52 -04:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, actual.Services, 1)
|
2017-05-11 08:30:04 -04:00
|
|
|
assert.Equal(t, actual.Services[0].CredentialSpec.File, "/foo")
|
2017-09-19 10:48:52 -04:00
|
|
|
require.Len(t, actual.Configs, 1)
|
2017-05-11 08:30:04 -04:00
|
|
|
}
|
|
|
|
|
2016-12-20 16:26:49 -05:00
|
|
|
func TestParseAndLoad(t *testing.T) {
|
2017-03-14 12:39:26 -04:00
|
|
|
actual, err := loadYAML(sampleYAML)
|
2017-09-19 10:48:52 -04:00
|
|
|
require.NoError(t, err)
|
2016-12-20 16:26:49 -05:00
|
|
|
assert.Equal(t, serviceSort(sampleConfig.Services), serviceSort(actual.Services))
|
|
|
|
assert.Equal(t, sampleConfig.Networks, actual.Networks)
|
|
|
|
assert.Equal(t, sampleConfig.Volumes, actual.Volumes)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInvalidTopLevelObjectType(t *testing.T) {
|
2017-03-14 12:39:26 -04:00
|
|
|
_, err := loadYAML("1")
|
2017-09-19 10:48:52 -04:00
|
|
|
require.Error(t, err)
|
2016-12-20 16:26:49 -05:00
|
|
|
assert.Contains(t, err.Error(), "Top-level object must be a mapping")
|
|
|
|
|
2017-03-14 12:39:26 -04:00
|
|
|
_, err = loadYAML("\"hello\"")
|
2017-09-19 10:48:52 -04:00
|
|
|
require.Error(t, err)
|
2016-12-20 16:26:49 -05:00
|
|
|
assert.Contains(t, err.Error(), "Top-level object must be a mapping")
|
|
|
|
|
2017-03-14 12:39:26 -04:00
|
|
|
_, err = loadYAML("[\"hello\"]")
|
2017-09-19 10:48:52 -04:00
|
|
|
require.Error(t, err)
|
2016-12-20 16:26:49 -05:00
|
|
|
assert.Contains(t, err.Error(), "Top-level object must be a mapping")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNonStringKeys(t *testing.T) {
|
|
|
|
_, err := loadYAML(`
|
|
|
|
version: "3"
|
|
|
|
123:
|
|
|
|
foo:
|
|
|
|
image: busybox
|
2017-03-14 12:39:26 -04:00
|
|
|
`)
|
2017-09-19 10:48:52 -04:00
|
|
|
require.Error(t, err)
|
2016-12-20 16:26:49 -05:00
|
|
|
assert.Contains(t, err.Error(), "Non-string key at top level: 123")
|
|
|
|
|
|
|
|
_, err = loadYAML(`
|
|
|
|
version: "3"
|
|
|
|
services:
|
|
|
|
foo:
|
|
|
|
image: busybox
|
|
|
|
123:
|
|
|
|
image: busybox
|
2017-03-14 12:39:26 -04:00
|
|
|
`)
|
2017-09-19 10:48:52 -04:00
|
|
|
require.Error(t, err)
|
2016-12-20 16:26:49 -05:00
|
|
|
assert.Contains(t, err.Error(), "Non-string key in services: 123")
|
|
|
|
|
|
|
|
_, err = loadYAML(`
|
|
|
|
version: "3"
|
|
|
|
services:
|
|
|
|
foo:
|
|
|
|
image: busybox
|
|
|
|
networks:
|
|
|
|
default:
|
|
|
|
ipam:
|
|
|
|
config:
|
|
|
|
- 123: oh dear
|
2017-03-14 12:39:26 -04:00
|
|
|
`)
|
2017-09-19 10:48:52 -04:00
|
|
|
require.Error(t, err)
|
2016-12-20 16:26:49 -05:00
|
|
|
assert.Contains(t, err.Error(), "Non-string key in networks.default.ipam.config[0]: 123")
|
|
|
|
|
|
|
|
_, err = loadYAML(`
|
|
|
|
version: "3"
|
|
|
|
services:
|
|
|
|
dict-env:
|
|
|
|
image: busybox
|
|
|
|
environment:
|
|
|
|
1: FOO
|
2017-03-14 12:39:26 -04:00
|
|
|
`)
|
2017-09-19 10:48:52 -04:00
|
|
|
require.Error(t, err)
|
2016-12-20 16:26:49 -05:00
|
|
|
assert.Contains(t, err.Error(), "Non-string key in services.dict-env.environment: 1")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSupportedVersion(t *testing.T) {
|
|
|
|
_, err := loadYAML(`
|
|
|
|
version: "3"
|
|
|
|
services:
|
|
|
|
foo:
|
|
|
|
image: busybox
|
2017-03-14 12:39:26 -04:00
|
|
|
`)
|
2017-09-19 10:48:52 -04:00
|
|
|
require.NoError(t, err)
|
2016-12-20 16:26:49 -05:00
|
|
|
|
|
|
|
_, err = loadYAML(`
|
|
|
|
version: "3.0"
|
|
|
|
services:
|
|
|
|
foo:
|
|
|
|
image: busybox
|
2017-03-14 12:39:26 -04:00
|
|
|
`)
|
2017-09-19 10:48:52 -04:00
|
|
|
require.NoError(t, err)
|
2016-12-20 16:26:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnsupportedVersion(t *testing.T) {
|
|
|
|
_, err := loadYAML(`
|
|
|
|
version: "2"
|
|
|
|
services:
|
|
|
|
foo:
|
|
|
|
image: busybox
|
2017-03-14 12:39:26 -04:00
|
|
|
`)
|
2017-09-19 10:48:52 -04:00
|
|
|
require.Error(t, err)
|
2016-12-20 16:26:49 -05:00
|
|
|
assert.Contains(t, err.Error(), "version")
|
|
|
|
|
|
|
|
_, err = loadYAML(`
|
|
|
|
version: "2.0"
|
|
|
|
services:
|
|
|
|
foo:
|
|
|
|
image: busybox
|
2017-03-14 12:39:26 -04:00
|
|
|
`)
|
2017-09-19 10:48:52 -04:00
|
|
|
require.Error(t, err)
|
2016-12-20 16:26:49 -05:00
|
|
|
assert.Contains(t, err.Error(), "version")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInvalidVersion(t *testing.T) {
|
|
|
|
_, err := loadYAML(`
|
|
|
|
version: 3
|
|
|
|
services:
|
|
|
|
foo:
|
|
|
|
image: busybox
|
2017-03-14 12:39:26 -04:00
|
|
|
`)
|
2017-09-19 10:48:52 -04:00
|
|
|
require.Error(t, err)
|
2016-12-20 16:26:49 -05:00
|
|
|
assert.Contains(t, err.Error(), "version must be a string")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestV1Unsupported(t *testing.T) {
|
|
|
|
_, err := loadYAML(`
|
|
|
|
foo:
|
|
|
|
image: busybox
|
2017-03-14 12:39:26 -04:00
|
|
|
`)
|
2016-12-20 16:26:49 -05:00
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNonMappingObject(t *testing.T) {
|
|
|
|
_, err := loadYAML(`
|
|
|
|
version: "3"
|
|
|
|
services:
|
|
|
|
- foo:
|
|
|
|
image: busybox
|
2017-03-14 12:39:26 -04:00
|
|
|
`)
|
2017-09-19 10:48:52 -04:00
|
|
|
require.Error(t, err)
|
2016-12-20 16:26:49 -05:00
|
|
|
assert.Contains(t, err.Error(), "services must be a mapping")
|
|
|
|
|
|
|
|
_, err = loadYAML(`
|
|
|
|
version: "3"
|
|
|
|
services:
|
|
|
|
foo: busybox
|
2017-03-14 12:39:26 -04:00
|
|
|
`)
|
2017-09-19 10:48:52 -04:00
|
|
|
require.Error(t, err)
|
2016-12-20 16:26:49 -05:00
|
|
|
assert.Contains(t, err.Error(), "services.foo must be a mapping")
|
|
|
|
|
|
|
|
_, err = loadYAML(`
|
|
|
|
version: "3"
|
|
|
|
networks:
|
|
|
|
- default:
|
|
|
|
driver: bridge
|
2017-03-14 12:39:26 -04:00
|
|
|
`)
|
2017-09-19 10:48:52 -04:00
|
|
|
require.Error(t, err)
|
2016-12-20 16:26:49 -05:00
|
|
|
assert.Contains(t, err.Error(), "networks must be a mapping")
|
|
|
|
|
|
|
|
_, err = loadYAML(`
|
|
|
|
version: "3"
|
|
|
|
networks:
|
|
|
|
default: bridge
|
2017-03-14 12:39:26 -04:00
|
|
|
`)
|
2017-09-19 10:48:52 -04:00
|
|
|
require.Error(t, err)
|
2016-12-20 16:26:49 -05:00
|
|
|
assert.Contains(t, err.Error(), "networks.default must be a mapping")
|
|
|
|
|
|
|
|
_, err = loadYAML(`
|
|
|
|
version: "3"
|
|
|
|
volumes:
|
|
|
|
- data:
|
|
|
|
driver: local
|
2017-03-14 12:39:26 -04:00
|
|
|
`)
|
2017-09-19 10:48:52 -04:00
|
|
|
require.Error(t, err)
|
2016-12-20 16:26:49 -05:00
|
|
|
assert.Contains(t, err.Error(), "volumes must be a mapping")
|
|
|
|
|
|
|
|
_, err = loadYAML(`
|
|
|
|
version: "3"
|
|
|
|
volumes:
|
|
|
|
data: local
|
2017-03-14 12:39:26 -04:00
|
|
|
`)
|
2017-09-19 10:48:52 -04:00
|
|
|
require.Error(t, err)
|
2016-12-20 16:26:49 -05:00
|
|
|
assert.Contains(t, err.Error(), "volumes.data must be a mapping")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNonStringImage(t *testing.T) {
|
|
|
|
_, err := loadYAML(`
|
|
|
|
version: "3"
|
|
|
|
services:
|
|
|
|
foo:
|
|
|
|
image: ["busybox", "latest"]
|
2017-03-14 12:39:26 -04:00
|
|
|
`)
|
2017-09-19 10:48:52 -04:00
|
|
|
require.Error(t, err)
|
2016-12-20 16:26:49 -05:00
|
|
|
assert.Contains(t, err.Error(), "services.foo.image must be a string")
|
|
|
|
}
|
|
|
|
|
2017-03-14 12:39:26 -04:00
|
|
|
func TestLoadWithEnvironment(t *testing.T) {
|
|
|
|
config, err := loadYAMLWithEnv(`
|
2016-12-20 16:26:49 -05:00
|
|
|
version: "3"
|
|
|
|
services:
|
|
|
|
dict-env:
|
|
|
|
image: busybox
|
|
|
|
environment:
|
|
|
|
FOO: "1"
|
|
|
|
BAR: 2
|
|
|
|
BAZ: 2.5
|
2017-02-07 04:44:47 -05:00
|
|
|
QUX:
|
2016-12-20 16:26:49 -05:00
|
|
|
QUUX:
|
|
|
|
list-env:
|
|
|
|
image: busybox
|
|
|
|
environment:
|
|
|
|
- FOO=1
|
|
|
|
- BAR=2
|
|
|
|
- BAZ=2.5
|
2017-03-14 12:39:26 -04:00
|
|
|
- QUX=
|
|
|
|
- QUUX
|
2017-02-07 04:44:47 -05:00
|
|
|
`, map[string]string{"QUX": "qux"})
|
2016-12-20 16:26:49 -05:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2017-01-18 15:27:02 -05:00
|
|
|
expected := types.MappingWithEquals{
|
2017-03-14 12:39:26 -04:00
|
|
|
"FOO": strPtr("1"),
|
|
|
|
"BAR": strPtr("2"),
|
|
|
|
"BAZ": strPtr("2.5"),
|
|
|
|
"QUX": strPtr("qux"),
|
|
|
|
"QUUX": nil,
|
2016-12-20 16:26:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, 2, len(config.Services))
|
|
|
|
|
|
|
|
for _, service := range config.Services {
|
|
|
|
assert.Equal(t, expected, service.Environment)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInvalidEnvironmentValue(t *testing.T) {
|
|
|
|
_, err := loadYAML(`
|
|
|
|
version: "3"
|
|
|
|
services:
|
|
|
|
dict-env:
|
|
|
|
image: busybox
|
|
|
|
environment:
|
|
|
|
FOO: ["1"]
|
2017-03-14 12:39:26 -04:00
|
|
|
`)
|
2017-09-19 10:48:52 -04:00
|
|
|
require.Error(t, err)
|
2016-12-20 16:26:49 -05:00
|
|
|
assert.Contains(t, err.Error(), "services.dict-env.environment.FOO must be a string, number or null")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInvalidEnvironmentObject(t *testing.T) {
|
|
|
|
_, err := loadYAML(`
|
|
|
|
version: "3"
|
|
|
|
services:
|
|
|
|
dict-env:
|
|
|
|
image: busybox
|
|
|
|
environment: "FOO=1"
|
2017-03-14 12:39:26 -04:00
|
|
|
`)
|
2017-09-19 10:48:52 -04:00
|
|
|
require.Error(t, err)
|
2016-12-20 16:26:49 -05:00
|
|
|
assert.Contains(t, err.Error(), "services.dict-env.environment must be a mapping")
|
|
|
|
}
|
|
|
|
|
2017-10-03 18:03:20 -04:00
|
|
|
func TestLoadWithEnvironmentInterpolation(t *testing.T) {
|
2017-02-07 04:44:47 -05:00
|
|
|
home := "/home/foo"
|
2017-03-14 12:39:26 -04:00
|
|
|
config, err := loadYAMLWithEnv(`
|
2016-12-20 16:26:49 -05:00
|
|
|
version: "3"
|
|
|
|
services:
|
|
|
|
test:
|
|
|
|
image: busybox
|
|
|
|
labels:
|
|
|
|
- home1=$HOME
|
|
|
|
- home2=${HOME}
|
|
|
|
- nonexistent=$NONEXISTENT
|
|
|
|
- default=${NONEXISTENT-default}
|
|
|
|
networks:
|
|
|
|
test:
|
|
|
|
driver: $HOME
|
|
|
|
volumes:
|
|
|
|
test:
|
|
|
|
driver: $HOME
|
2017-02-07 04:44:47 -05:00
|
|
|
`, map[string]string{
|
|
|
|
"HOME": home,
|
|
|
|
"FOO": "foo",
|
|
|
|
})
|
2016-12-20 16:26:49 -05:00
|
|
|
|
2017-09-19 10:48:52 -04:00
|
|
|
require.NoError(t, err)
|
2016-12-20 16:26:49 -05:00
|
|
|
|
2017-03-14 12:39:26 -04:00
|
|
|
expectedLabels := types.Labels{
|
2016-12-20 16:26:49 -05:00
|
|
|
"home1": home,
|
|
|
|
"home2": home,
|
|
|
|
"nonexistent": "",
|
|
|
|
"default": "default",
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, expectedLabels, config.Services[0].Labels)
|
|
|
|
assert.Equal(t, home, config.Networks["test"].Driver)
|
|
|
|
assert.Equal(t, home, config.Volumes["test"].Driver)
|
|
|
|
}
|
|
|
|
|
2017-10-03 18:03:20 -04:00
|
|
|
func TestLoadWithInterpolationCastFull(t *testing.T) {
|
|
|
|
dict, err := ParseYAML([]byte(`
|
|
|
|
version: "3.4"
|
|
|
|
services:
|
|
|
|
web:
|
|
|
|
configs:
|
|
|
|
- source: appconfig
|
|
|
|
mode: $theint
|
|
|
|
secrets:
|
|
|
|
- source: super
|
|
|
|
mode: $theint
|
|
|
|
healthcheck:
|
|
|
|
retries: ${theint}
|
|
|
|
disable: $thebool
|
|
|
|
deploy:
|
|
|
|
replicas: $theint
|
|
|
|
update_config:
|
|
|
|
parallelism: $theint
|
|
|
|
max_failure_ratio: $thefloat
|
|
|
|
restart_policy:
|
|
|
|
max_attempts: $theint
|
|
|
|
ports:
|
|
|
|
- $theint
|
|
|
|
- "34567"
|
|
|
|
- target: $theint
|
|
|
|
published: $theint
|
|
|
|
ulimits:
|
2017-10-04 16:51:48 -04:00
|
|
|
nproc: $theint
|
|
|
|
nofile:
|
|
|
|
hard: $theint
|
2017-10-03 18:03:20 -04:00
|
|
|
soft: $theint
|
|
|
|
privileged: $thebool
|
|
|
|
read_only: $thebool
|
|
|
|
stdin_open: ${thebool}
|
|
|
|
tty: $thebool
|
|
|
|
volumes:
|
|
|
|
- source: data
|
2017-10-04 16:51:48 -04:00
|
|
|
type: volume
|
2017-10-03 18:03:20 -04:00
|
|
|
read_only: $thebool
|
|
|
|
volume:
|
|
|
|
nocopy: $thebool
|
|
|
|
|
|
|
|
configs:
|
|
|
|
appconfig:
|
|
|
|
external: $thebool
|
|
|
|
secrets:
|
|
|
|
super:
|
|
|
|
external: $thebool
|
|
|
|
volumes:
|
|
|
|
data:
|
|
|
|
external: $thebool
|
|
|
|
networks:
|
|
|
|
front:
|
|
|
|
external: $thebool
|
|
|
|
internal: $thebool
|
|
|
|
attachable: $thebool
|
|
|
|
|
|
|
|
`))
|
|
|
|
require.NoError(t, err)
|
|
|
|
env := map[string]string{
|
|
|
|
"theint": "555",
|
|
|
|
"thefloat": "3.14",
|
|
|
|
"thebool": "true",
|
|
|
|
}
|
|
|
|
|
|
|
|
config, err := Load(buildConfigDetails(dict, env))
|
|
|
|
require.NoError(t, err)
|
2017-10-04 16:51:48 -04:00
|
|
|
expected := &types.Config{
|
2017-09-29 08:21:40 -04:00
|
|
|
Filename: "filename.yml",
|
2017-10-04 16:51:48 -04:00
|
|
|
Services: []types.ServiceConfig{
|
|
|
|
{
|
|
|
|
Name: "web",
|
|
|
|
Configs: []types.ServiceConfigObjConfig{
|
|
|
|
{
|
|
|
|
Source: "appconfig",
|
|
|
|
Mode: uint32Ptr(555),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Secrets: []types.ServiceSecretConfig{
|
|
|
|
{
|
|
|
|
Source: "super",
|
|
|
|
Mode: uint32Ptr(555),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
HealthCheck: &types.HealthCheckConfig{
|
|
|
|
Retries: uint64Ptr(555),
|
|
|
|
Disable: true,
|
|
|
|
},
|
|
|
|
Deploy: types.DeployConfig{
|
|
|
|
Replicas: uint64Ptr(555),
|
|
|
|
UpdateConfig: &types.UpdateConfig{
|
|
|
|
Parallelism: uint64Ptr(555),
|
|
|
|
MaxFailureRatio: 3.14,
|
|
|
|
},
|
|
|
|
RestartPolicy: &types.RestartPolicy{
|
|
|
|
MaxAttempts: uint64Ptr(555),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Ports: []types.ServicePortConfig{
|
|
|
|
{Target: 555, Mode: "ingress", Protocol: "tcp"},
|
|
|
|
{Target: 34567, Mode: "ingress", Protocol: "tcp"},
|
|
|
|
{Target: 555, Published: 555},
|
|
|
|
},
|
|
|
|
Ulimits: map[string]*types.UlimitsConfig{
|
|
|
|
"nproc": {Single: 555},
|
|
|
|
"nofile": {Hard: 555, Soft: 555},
|
|
|
|
},
|
|
|
|
Privileged: true,
|
|
|
|
ReadOnly: true,
|
|
|
|
StdinOpen: true,
|
|
|
|
Tty: true,
|
|
|
|
Volumes: []types.ServiceVolumeConfig{
|
|
|
|
{
|
|
|
|
Source: "data",
|
|
|
|
Type: "volume",
|
|
|
|
ReadOnly: true,
|
|
|
|
Volume: &types.ServiceVolumeVolume{NoCopy: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Environment: types.MappingWithEquals{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Configs: map[string]types.ConfigObjConfig{
|
2017-11-10 08:35:29 -05:00
|
|
|
"appconfig": {External: types.External{External: true}, Name: "appconfig"},
|
2017-10-04 16:51:48 -04:00
|
|
|
},
|
|
|
|
Secrets: map[string]types.SecretConfig{
|
2017-11-10 08:35:29 -05:00
|
|
|
"super": {External: types.External{External: true}, Name: "super"},
|
2017-10-04 16:51:48 -04:00
|
|
|
},
|
|
|
|
Volumes: map[string]types.VolumeConfig{
|
2017-11-06 17:03:43 -05:00
|
|
|
"data": {External: types.External{External: true}, Name: "data"},
|
2017-10-04 16:51:48 -04:00
|
|
|
},
|
|
|
|
Networks: map[string]types.NetworkConfig{
|
|
|
|
"front": {
|
2017-11-22 14:21:32 -05:00
|
|
|
External: types.External{External: true},
|
|
|
|
Name: "front",
|
2017-10-04 16:51:48 -04:00
|
|
|
Internal: true,
|
|
|
|
Attachable: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-10-03 18:03:20 -04:00
|
|
|
assert.Equal(t, expected, config)
|
|
|
|
}
|
|
|
|
|
2016-12-20 16:26:49 -05:00
|
|
|
func TestUnsupportedProperties(t *testing.T) {
|
|
|
|
dict, err := ParseYAML([]byte(`
|
|
|
|
version: "3"
|
|
|
|
services:
|
|
|
|
web:
|
|
|
|
image: web
|
2017-10-03 18:03:20 -04:00
|
|
|
build:
|
2017-09-13 10:47:17 -04:00
|
|
|
context: ./web
|
2016-12-20 16:26:49 -05:00
|
|
|
links:
|
|
|
|
- bar
|
2017-12-27 06:45:19 -05:00
|
|
|
pid: host
|
2016-12-20 16:26:49 -05:00
|
|
|
db:
|
|
|
|
image: db
|
2017-10-03 18:03:20 -04:00
|
|
|
build:
|
2017-09-13 10:47:17 -04:00
|
|
|
context: ./db
|
2016-12-20 16:26:49 -05:00
|
|
|
`))
|
2017-09-19 10:48:52 -04:00
|
|
|
require.NoError(t, err)
|
2016-12-20 16:26:49 -05:00
|
|
|
|
2017-02-07 04:44:47 -05:00
|
|
|
configDetails := buildConfigDetails(dict, nil)
|
2016-12-20 16:26:49 -05:00
|
|
|
|
|
|
|
_, err = Load(configDetails)
|
2017-09-19 10:48:52 -04:00
|
|
|
require.NoError(t, err)
|
2016-12-20 16:26:49 -05:00
|
|
|
|
2017-09-29 08:21:40 -04:00
|
|
|
unsupported := GetUnsupportedProperties(dict)
|
2017-12-27 06:45:19 -05:00
|
|
|
assert.Equal(t, []string{"build", "links", "pid"}, unsupported)
|
2016-12-20 16:26:49 -05:00
|
|
|
}
|
|
|
|
|
2017-09-20 05:42:08 -04:00
|
|
|
func TestBuildProperties(t *testing.T) {
|
|
|
|
dict, err := ParseYAML([]byte(`
|
|
|
|
version: "3"
|
|
|
|
services:
|
|
|
|
web:
|
|
|
|
image: web
|
|
|
|
build: .
|
|
|
|
links:
|
|
|
|
- bar
|
|
|
|
db:
|
|
|
|
image: db
|
|
|
|
build:
|
|
|
|
context: ./db
|
|
|
|
`))
|
|
|
|
require.NoError(t, err)
|
|
|
|
configDetails := buildConfigDetails(dict, nil)
|
|
|
|
_, err = Load(configDetails)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2016-12-20 16:26:49 -05:00
|
|
|
func TestDeprecatedProperties(t *testing.T) {
|
|
|
|
dict, err := ParseYAML([]byte(`
|
|
|
|
version: "3"
|
|
|
|
services:
|
|
|
|
web:
|
|
|
|
image: web
|
|
|
|
container_name: web
|
|
|
|
db:
|
|
|
|
image: db
|
|
|
|
container_name: db
|
|
|
|
expose: ["5434"]
|
|
|
|
`))
|
2017-09-19 10:48:52 -04:00
|
|
|
require.NoError(t, err)
|
2016-12-20 16:26:49 -05:00
|
|
|
|
2017-02-07 04:44:47 -05:00
|
|
|
configDetails := buildConfigDetails(dict, nil)
|
2016-12-20 16:26:49 -05:00
|
|
|
|
|
|
|
_, err = Load(configDetails)
|
2017-09-19 10:48:52 -04:00
|
|
|
require.NoError(t, err)
|
2016-12-20 16:26:49 -05:00
|
|
|
|
2017-09-29 08:21:40 -04:00
|
|
|
deprecated := GetDeprecatedProperties(dict)
|
2017-09-19 10:48:52 -04:00
|
|
|
assert.Len(t, deprecated, 2)
|
2016-12-20 16:26:49 -05:00
|
|
|
assert.Contains(t, deprecated, "container_name")
|
|
|
|
assert.Contains(t, deprecated, "expose")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestForbiddenProperties(t *testing.T) {
|
|
|
|
_, err := loadYAML(`
|
|
|
|
version: "3"
|
|
|
|
services:
|
|
|
|
foo:
|
|
|
|
image: busybox
|
|
|
|
volumes:
|
|
|
|
- /data
|
|
|
|
volume_driver: some-driver
|
|
|
|
bar:
|
|
|
|
extends:
|
|
|
|
service: foo
|
2017-03-14 12:39:26 -04:00
|
|
|
`)
|
2016-12-20 16:26:49 -05:00
|
|
|
|
2017-09-19 10:48:52 -04:00
|
|
|
require.Error(t, err)
|
2016-12-20 16:26:49 -05:00
|
|
|
assert.IsType(t, &ForbiddenPropertiesError{}, err)
|
|
|
|
fmt.Println(err)
|
|
|
|
forbidden := err.(*ForbiddenPropertiesError).Properties
|
|
|
|
|
2017-09-19 10:48:52 -04:00
|
|
|
assert.Len(t, forbidden, 2)
|
2016-12-20 16:26:49 -05:00
|
|
|
assert.Contains(t, forbidden, "volume_driver")
|
|
|
|
assert.Contains(t, forbidden, "extends")
|
|
|
|
}
|
|
|
|
|
2017-08-17 18:41:21 -04:00
|
|
|
func TestInvalidResource(t *testing.T) {
|
|
|
|
_, err := loadYAML(`
|
|
|
|
version: "3"
|
|
|
|
services:
|
|
|
|
foo:
|
|
|
|
image: busybox
|
|
|
|
deploy:
|
|
|
|
resources:
|
|
|
|
impossible:
|
|
|
|
x: 1
|
|
|
|
`)
|
2017-09-19 10:48:52 -04:00
|
|
|
require.Error(t, err)
|
2017-08-17 18:41:21 -04:00
|
|
|
assert.Contains(t, err.Error(), "Additional property impossible is not allowed")
|
|
|
|
}
|
|
|
|
|
2017-02-20 01:12:36 -05:00
|
|
|
func TestInvalidExternalAndDriverCombination(t *testing.T) {
|
|
|
|
_, err := loadYAML(`
|
|
|
|
version: "3"
|
|
|
|
volumes:
|
|
|
|
external_volume:
|
|
|
|
external: true
|
|
|
|
driver: foobar
|
|
|
|
`)
|
|
|
|
|
2017-09-19 10:48:52 -04:00
|
|
|
require.Error(t, err)
|
2017-02-20 01:12:36 -05:00
|
|
|
assert.Contains(t, err.Error(), "conflicting parameters \"external\" and \"driver\" specified for volume")
|
|
|
|
assert.Contains(t, err.Error(), "external_volume")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInvalidExternalAndDirverOptsCombination(t *testing.T) {
|
|
|
|
_, err := loadYAML(`
|
|
|
|
version: "3"
|
|
|
|
volumes:
|
|
|
|
external_volume:
|
|
|
|
external: true
|
|
|
|
driver_opts:
|
|
|
|
beep: boop
|
|
|
|
`)
|
|
|
|
|
2017-09-19 10:48:52 -04:00
|
|
|
require.Error(t, err)
|
2017-02-20 01:12:36 -05:00
|
|
|
assert.Contains(t, err.Error(), "conflicting parameters \"external\" and \"driver_opts\" specified for volume")
|
|
|
|
assert.Contains(t, err.Error(), "external_volume")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInvalidExternalAndLabelsCombination(t *testing.T) {
|
|
|
|
_, err := loadYAML(`
|
|
|
|
version: "3"
|
|
|
|
volumes:
|
|
|
|
external_volume:
|
|
|
|
external: true
|
|
|
|
labels:
|
|
|
|
- beep=boop
|
|
|
|
`)
|
|
|
|
|
2017-09-19 10:48:52 -04:00
|
|
|
require.Error(t, err)
|
2017-02-20 01:12:36 -05:00
|
|
|
assert.Contains(t, err.Error(), "conflicting parameters \"external\" and \"labels\" specified for volume")
|
|
|
|
assert.Contains(t, err.Error(), "external_volume")
|
|
|
|
}
|
|
|
|
|
2017-11-22 14:21:32 -05:00
|
|
|
func TestLoadVolumeInvalidExternalNameAndNameCombination(t *testing.T) {
|
2017-06-29 19:25:50 -04:00
|
|
|
_, err := loadYAML(`
|
|
|
|
version: "3.4"
|
|
|
|
volumes:
|
|
|
|
external_volume:
|
|
|
|
name: user_specified_name
|
|
|
|
external:
|
|
|
|
name: external_name
|
|
|
|
`)
|
|
|
|
|
2017-09-19 10:48:52 -04:00
|
|
|
require.Error(t, err)
|
2017-06-29 19:25:50 -04:00
|
|
|
assert.Contains(t, err.Error(), "volume.external.name and volume.name conflict; only use volume.name")
|
|
|
|
assert.Contains(t, err.Error(), "external_volume")
|
|
|
|
}
|
|
|
|
|
2016-12-20 16:26:49 -05:00
|
|
|
func durationPtr(value time.Duration) *time.Duration {
|
|
|
|
return &value
|
|
|
|
}
|
|
|
|
|
|
|
|
func uint64Ptr(value uint64) *uint64 {
|
|
|
|
return &value
|
|
|
|
}
|
|
|
|
|
2017-10-04 16:51:48 -04:00
|
|
|
func uint32Ptr(value uint32) *uint32 {
|
|
|
|
return &value
|
|
|
|
}
|
|
|
|
|
2016-12-20 16:26:49 -05:00
|
|
|
func TestFullExample(t *testing.T) {
|
|
|
|
bytes, err := ioutil.ReadFile("full-example.yml")
|
2017-09-19 10:48:52 -04:00
|
|
|
require.NoError(t, err)
|
2016-12-20 16:26:49 -05:00
|
|
|
|
2017-02-07 04:44:47 -05:00
|
|
|
homeDir := "/home/foo"
|
2017-03-14 12:39:26 -04:00
|
|
|
env := map[string]string{"HOME": homeDir, "QUX": "qux_from_environment"}
|
|
|
|
config, err := loadYAMLWithEnv(string(bytes), env)
|
2017-09-19 10:48:52 -04:00
|
|
|
require.NoError(t, err)
|
2016-12-20 16:26:49 -05:00
|
|
|
|
|
|
|
workingDir, err := os.Getwd()
|
2017-09-19 10:48:52 -04:00
|
|
|
require.NoError(t, err)
|
2016-12-20 16:26:49 -05:00
|
|
|
|
|
|
|
stopGracePeriod := time.Duration(20 * time.Second)
|
|
|
|
|
|
|
|
expectedServiceConfig := types.ServiceConfig{
|
|
|
|
Name: "foo",
|
|
|
|
|
2017-09-13 10:47:17 -04:00
|
|
|
Build: types.BuildConfig{
|
|
|
|
Context: "./dir",
|
|
|
|
Dockerfile: "Dockerfile",
|
|
|
|
Args: map[string]*string{"foo": strPtr("bar")},
|
|
|
|
Target: "foo",
|
|
|
|
Network: "foo",
|
|
|
|
CacheFrom: []string{"foo", "bar"},
|
|
|
|
Labels: map[string]string{"FOO": "BAR"},
|
|
|
|
},
|
2016-12-20 16:26:49 -05:00
|
|
|
CapAdd: []string{"ALL"},
|
|
|
|
CapDrop: []string{"NET_ADMIN", "SYS_ADMIN"},
|
|
|
|
CgroupParent: "m-executor-abcd",
|
|
|
|
Command: []string{"bundle", "exec", "thin", "-p", "3000"},
|
|
|
|
ContainerName: "my-web-container",
|
|
|
|
DependsOn: []string{"db", "redis"},
|
|
|
|
Deploy: types.DeployConfig{
|
|
|
|
Mode: "replicated",
|
|
|
|
Replicas: uint64Ptr(6),
|
|
|
|
Labels: map[string]string{"FOO": "BAR"},
|
|
|
|
UpdateConfig: &types.UpdateConfig{
|
|
|
|
Parallelism: uint64Ptr(3),
|
|
|
|
Delay: time.Duration(10 * time.Second),
|
|
|
|
FailureAction: "continue",
|
|
|
|
Monitor: time.Duration(60 * time.Second),
|
|
|
|
MaxFailureRatio: 0.3,
|
2017-07-19 04:55:34 -04:00
|
|
|
Order: "start-first",
|
2016-12-20 16:26:49 -05:00
|
|
|
},
|
|
|
|
Resources: types.Resources{
|
|
|
|
Limits: &types.Resource{
|
|
|
|
NanoCPUs: "0.001",
|
|
|
|
MemoryBytes: 50 * 1024 * 1024,
|
|
|
|
},
|
|
|
|
Reservations: &types.Resource{
|
|
|
|
NanoCPUs: "0.0001",
|
|
|
|
MemoryBytes: 20 * 1024 * 1024,
|
2017-11-17 17:05:59 -05:00
|
|
|
GenericResources: []types.GenericResource{
|
|
|
|
{
|
|
|
|
DiscreteResourceSpec: &types.DiscreteGenericResource{
|
|
|
|
Kind: "gpu",
|
|
|
|
Value: 2,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
DiscreteResourceSpec: &types.DiscreteGenericResource{
|
|
|
|
Kind: "ssd",
|
|
|
|
Value: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2016-12-20 16:26:49 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
RestartPolicy: &types.RestartPolicy{
|
2017-06-28 11:08:44 -04:00
|
|
|
Condition: "on-failure",
|
2016-12-20 16:26:49 -05:00
|
|
|
Delay: durationPtr(5 * time.Second),
|
|
|
|
MaxAttempts: uint64Ptr(3),
|
|
|
|
Window: durationPtr(2 * time.Minute),
|
|
|
|
},
|
|
|
|
Placement: types.Placement{
|
|
|
|
Constraints: []string{"node=foo"},
|
2017-05-06 09:49:42 -04:00
|
|
|
Preferences: []types.PlacementPreferences{
|
|
|
|
{
|
|
|
|
Spread: "node.labels.az",
|
|
|
|
},
|
|
|
|
},
|
2016-12-20 16:26:49 -05:00
|
|
|
},
|
2017-04-03 17:42:16 -04:00
|
|
|
EndpointMode: "dnsrr",
|
2016-12-20 16:26:49 -05:00
|
|
|
},
|
|
|
|
Devices: []string{"/dev/ttyUSB0:/dev/ttyUSB0"},
|
|
|
|
DNS: []string{"8.8.8.8", "9.9.9.9"},
|
|
|
|
DNSSearch: []string{"dc1.example.com", "dc2.example.com"},
|
|
|
|
DomainName: "foo.com",
|
|
|
|
Entrypoint: []string{"/code/entrypoint.sh", "-p", "3000"},
|
2017-03-14 12:39:26 -04:00
|
|
|
Environment: map[string]*string{
|
|
|
|
"FOO": strPtr("foo_from_env_file"),
|
|
|
|
"BAR": strPtr("bar_from_env_file_2"),
|
|
|
|
"BAZ": strPtr("baz_from_service_def"),
|
|
|
|
"QUX": strPtr("qux_from_environment"),
|
2016-12-20 16:26:49 -05:00
|
|
|
},
|
2017-01-18 15:27:02 -05:00
|
|
|
EnvFile: []string{
|
|
|
|
"./example1.env",
|
|
|
|
"./example2.env",
|
|
|
|
},
|
2016-12-20 16:26:49 -05:00
|
|
|
Expose: []string{"3000", "8000"},
|
|
|
|
ExternalLinks: []string{
|
|
|
|
"redis_1",
|
|
|
|
"project_db_1:mysql",
|
|
|
|
"project_db_1:postgresql",
|
|
|
|
},
|
Preserve sort-order of extra hosts, and allow duplicate entries
Extra hosts (`extra_hosts` in compose-file, or `--hosts` in services) adds
custom host/ip mappings to the container's `/etc/hosts`.
The current implementation used a `map[string]string{}` as intermediate
storage, and sorted the results alphabetically when converting to a service-spec.
As a result, duplicate hosts were removed, and order of host/ip mappings was not
preserved (in case the compose-file used a list instead of a map).
According to the **host.conf(5)** man page (http://man7.org/linux/man-pages/man5/host.conf.5.html)
multi Valid values are on and off. If set to on, the resolver
library will return all valid addresses for a host that
appears in the /etc/hosts file, instead of only the first.
This is off by default, as it may cause a substantial
performance loss at sites with large hosts files.
Multiple entries for a host are allowed, and even required for some situations,
for example, to add mappings for IPv4 and IPv6 addreses for a host, as illustrated
by the example hosts file in the **hosts(5)** man page (http://man7.org/linux/man-pages/man5/hosts.5.html):
# The following lines are desirable for IPv4 capable hosts
127.0.0.1 localhost
# 127.0.1.1 is often used for the FQDN of the machine
127.0.1.1 thishost.mydomain.org thishost
192.168.1.10 foo.mydomain.org foo
192.168.1.13 bar.mydomain.org bar
146.82.138.7 master.debian.org master
209.237.226.90 www.opensource.org
# The following lines are desirable for IPv6 capable hosts
::1 localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
This patch changes the intermediate storage format to use a `[]string`, and only
sorts entries if the input format in the compose file is a mapping. If the input
format is a list, the original sort-order is preserved.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2017-10-29 20:33:23 -04:00
|
|
|
ExtraHosts: []string{
|
|
|
|
"somehost:162.242.195.82",
|
|
|
|
"otherhost:50.31.209.229",
|
2016-12-20 16:26:49 -05:00
|
|
|
},
|
|
|
|
HealthCheck: &types.HealthCheckConfig{
|
2017-08-19 22:39:05 -04:00
|
|
|
Test: types.HealthCheckTest([]string{"CMD-SHELL", "echo \"hello world\""}),
|
2017-08-29 21:09:06 -04:00
|
|
|
Interval: durationPtr(10 * time.Second),
|
|
|
|
Timeout: durationPtr(1 * time.Second),
|
2017-08-19 22:39:05 -04:00
|
|
|
Retries: uint64Ptr(5),
|
2017-08-29 21:09:06 -04:00
|
|
|
StartPeriod: durationPtr(15 * time.Second),
|
2016-12-20 16:26:49 -05:00
|
|
|
},
|
|
|
|
Hostname: "foo",
|
|
|
|
Image: "redis",
|
|
|
|
Ipc: "host",
|
|
|
|
Labels: map[string]string{
|
|
|
|
"com.example.description": "Accounting webapp",
|
|
|
|
"com.example.number": "42",
|
|
|
|
"com.example.empty-label": "",
|
|
|
|
},
|
|
|
|
Links: []string{
|
|
|
|
"db",
|
|
|
|
"db:database",
|
|
|
|
"redis",
|
|
|
|
},
|
|
|
|
Logging: &types.LoggingConfig{
|
|
|
|
Driver: "syslog",
|
|
|
|
Options: map[string]string{
|
|
|
|
"syslog-address": "tcp://192.168.0.42:123",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
MacAddress: "02:42:ac:11:65:43",
|
|
|
|
NetworkMode: "container:0cfeab0f748b9a743dc3da582046357c6ef497631c1a016d28d2bf9b4f899f7b",
|
|
|
|
Networks: map[string]*types.ServiceNetworkConfig{
|
|
|
|
"some-network": {
|
|
|
|
Aliases: []string{"alias1", "alias3"},
|
|
|
|
Ipv4Address: "",
|
|
|
|
Ipv6Address: "",
|
|
|
|
},
|
|
|
|
"other-network": {
|
|
|
|
Ipv4Address: "172.16.238.10",
|
|
|
|
Ipv6Address: "2001:3984:3989::10",
|
|
|
|
},
|
|
|
|
"other-other-network": nil,
|
|
|
|
},
|
|
|
|
Pid: "host",
|
2017-01-31 15:45:45 -05:00
|
|
|
Ports: []types.ServicePortConfig{
|
|
|
|
//"3000",
|
|
|
|
{
|
|
|
|
Mode: "ingress",
|
|
|
|
Target: 3000,
|
|
|
|
Protocol: "tcp",
|
|
|
|
},
|
|
|
|
//"3000-3005",
|
|
|
|
{
|
|
|
|
Mode: "ingress",
|
|
|
|
Target: 3000,
|
|
|
|
Protocol: "tcp",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Mode: "ingress",
|
|
|
|
Target: 3001,
|
|
|
|
Protocol: "tcp",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Mode: "ingress",
|
|
|
|
Target: 3002,
|
|
|
|
Protocol: "tcp",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Mode: "ingress",
|
|
|
|
Target: 3003,
|
|
|
|
Protocol: "tcp",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Mode: "ingress",
|
|
|
|
Target: 3004,
|
|
|
|
Protocol: "tcp",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Mode: "ingress",
|
|
|
|
Target: 3005,
|
|
|
|
Protocol: "tcp",
|
|
|
|
},
|
|
|
|
//"8000:8000",
|
|
|
|
{
|
|
|
|
Mode: "ingress",
|
|
|
|
Target: 8000,
|
|
|
|
Published: 8000,
|
|
|
|
Protocol: "tcp",
|
|
|
|
},
|
|
|
|
//"9090-9091:8080-8081",
|
|
|
|
{
|
|
|
|
Mode: "ingress",
|
|
|
|
Target: 8080,
|
|
|
|
Published: 9090,
|
|
|
|
Protocol: "tcp",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Mode: "ingress",
|
|
|
|
Target: 8081,
|
|
|
|
Published: 9091,
|
|
|
|
Protocol: "tcp",
|
|
|
|
},
|
|
|
|
//"49100:22",
|
|
|
|
{
|
|
|
|
Mode: "ingress",
|
|
|
|
Target: 22,
|
|
|
|
Published: 49100,
|
|
|
|
Protocol: "tcp",
|
|
|
|
},
|
|
|
|
//"127.0.0.1:8001:8001",
|
|
|
|
{
|
|
|
|
Mode: "ingress",
|
|
|
|
Target: 8001,
|
|
|
|
Published: 8001,
|
|
|
|
Protocol: "tcp",
|
|
|
|
},
|
|
|
|
//"127.0.0.1:5000-5010:5000-5010",
|
|
|
|
{
|
|
|
|
Mode: "ingress",
|
|
|
|
Target: 5000,
|
|
|
|
Published: 5000,
|
|
|
|
Protocol: "tcp",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Mode: "ingress",
|
|
|
|
Target: 5001,
|
|
|
|
Published: 5001,
|
|
|
|
Protocol: "tcp",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Mode: "ingress",
|
|
|
|
Target: 5002,
|
|
|
|
Published: 5002,
|
|
|
|
Protocol: "tcp",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Mode: "ingress",
|
|
|
|
Target: 5003,
|
|
|
|
Published: 5003,
|
|
|
|
Protocol: "tcp",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Mode: "ingress",
|
|
|
|
Target: 5004,
|
|
|
|
Published: 5004,
|
|
|
|
Protocol: "tcp",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Mode: "ingress",
|
|
|
|
Target: 5005,
|
|
|
|
Published: 5005,
|
|
|
|
Protocol: "tcp",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Mode: "ingress",
|
|
|
|
Target: 5006,
|
|
|
|
Published: 5006,
|
|
|
|
Protocol: "tcp",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Mode: "ingress",
|
|
|
|
Target: 5007,
|
|
|
|
Published: 5007,
|
|
|
|
Protocol: "tcp",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Mode: "ingress",
|
|
|
|
Target: 5008,
|
|
|
|
Published: 5008,
|
|
|
|
Protocol: "tcp",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Mode: "ingress",
|
|
|
|
Target: 5009,
|
|
|
|
Published: 5009,
|
|
|
|
Protocol: "tcp",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Mode: "ingress",
|
|
|
|
Target: 5010,
|
|
|
|
Published: 5010,
|
|
|
|
Protocol: "tcp",
|
|
|
|
},
|
2016-12-20 16:26:49 -05:00
|
|
|
},
|
|
|
|
Privileged: true,
|
|
|
|
ReadOnly: true,
|
|
|
|
Restart: "always",
|
|
|
|
SecurityOpt: []string{
|
|
|
|
"label=level:s0:c100,c200",
|
|
|
|
"label=type:svirt_apache_t",
|
|
|
|
},
|
|
|
|
StdinOpen: true,
|
|
|
|
StopSignal: "SIGUSR1",
|
|
|
|
StopGracePeriod: &stopGracePeriod,
|
|
|
|
Tmpfs: []string{"/run", "/tmp"},
|
|
|
|
Tty: true,
|
|
|
|
Ulimits: map[string]*types.UlimitsConfig{
|
|
|
|
"nproc": {
|
|
|
|
Single: 65535,
|
|
|
|
},
|
|
|
|
"nofile": {
|
|
|
|
Soft: 20000,
|
|
|
|
Hard: 40000,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
User: "someone",
|
2017-01-24 12:09:53 -05:00
|
|
|
Volumes: []types.ServiceVolumeConfig{
|
|
|
|
{Target: "/var/lib/mysql", Type: "volume"},
|
|
|
|
{Source: "/opt/data", Target: "/var/lib/mysql", Type: "bind"},
|
|
|
|
{Source: workingDir, Target: "/code", Type: "bind"},
|
|
|
|
{Source: workingDir + "/static", Target: "/var/www/html", Type: "bind"},
|
|
|
|
{Source: homeDir + "/configs", Target: "/etc/configs/", Type: "bind", ReadOnly: true},
|
|
|
|
{Source: "datavolume", Target: "/var/lib/mysql", Type: "volume"},
|
2017-04-06 11:37:55 -04:00
|
|
|
{Source: workingDir + "/opt", Target: "/opt", Consistency: "cached", Type: "bind"},
|
2018-01-16 11:52:26 -05:00
|
|
|
{Target: "/opt", Type: "tmpfs", Tmpfs: &types.ServiceVolumeTmpfs{
|
|
|
|
Size: int64(10000),
|
|
|
|
}},
|
2016-12-20 16:26:49 -05:00
|
|
|
},
|
|
|
|
WorkingDir: "/code",
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, []types.ServiceConfig{expectedServiceConfig}, config.Services)
|
|
|
|
|
|
|
|
expectedNetworkConfig := map[string]types.NetworkConfig{
|
|
|
|
"some-network": {},
|
|
|
|
|
|
|
|
"other-network": {
|
|
|
|
Driver: "overlay",
|
|
|
|
DriverOpts: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
"baz": "1",
|
|
|
|
},
|
|
|
|
Ipam: types.IPAMConfig{
|
|
|
|
Driver: "overlay",
|
|
|
|
Config: []*types.IPAMPool{
|
|
|
|
{Subnet: "172.16.238.0/24"},
|
|
|
|
{Subnet: "2001:3984:3989::/64"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
"external-network": {
|
2017-11-22 14:21:32 -05:00
|
|
|
Name: "external-network",
|
|
|
|
External: types.External{External: true},
|
2016-12-20 16:26:49 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
"other-external-network": {
|
2017-11-22 14:21:32 -05:00
|
|
|
Name: "my-cool-network",
|
|
|
|
External: types.External{External: true},
|
2016-12-20 16:26:49 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, expectedNetworkConfig, config.Networks)
|
|
|
|
|
|
|
|
expectedVolumeConfig := map[string]types.VolumeConfig{
|
|
|
|
"some-volume": {},
|
|
|
|
"other-volume": {
|
|
|
|
Driver: "flocker",
|
|
|
|
DriverOpts: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
"baz": "1",
|
|
|
|
},
|
|
|
|
},
|
2017-06-29 19:25:50 -04:00
|
|
|
"another-volume": {
|
|
|
|
Name: "user_specified_name",
|
|
|
|
Driver: "vsphere",
|
|
|
|
DriverOpts: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
"baz": "1",
|
|
|
|
},
|
|
|
|
},
|
2016-12-20 16:26:49 -05:00
|
|
|
"external-volume": {
|
2017-11-06 17:03:43 -05:00
|
|
|
Name: "external-volume",
|
|
|
|
External: types.External{External: true},
|
2016-12-20 16:26:49 -05:00
|
|
|
},
|
|
|
|
"other-external-volume": {
|
2017-11-06 17:03:43 -05:00
|
|
|
Name: "my-cool-volume",
|
|
|
|
External: types.External{External: true},
|
2016-12-20 16:26:49 -05:00
|
|
|
},
|
2017-06-29 19:25:50 -04:00
|
|
|
"external-volume3": {
|
2017-11-06 17:03:43 -05:00
|
|
|
Name: "this-is-volume3",
|
|
|
|
External: types.External{External: true},
|
2017-06-29 19:25:50 -04:00
|
|
|
},
|
2016-12-20 16:26:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, expectedVolumeConfig, config.Volumes)
|
|
|
|
}
|
|
|
|
|
2018-01-16 11:52:26 -05:00
|
|
|
func TestLoadTmpfsVolume(t *testing.T) {
|
|
|
|
config, err := loadYAML(`
|
|
|
|
version: "3.6"
|
|
|
|
services:
|
|
|
|
tmpfs:
|
|
|
|
image: nginx:latest
|
|
|
|
volumes:
|
|
|
|
- type: tmpfs
|
|
|
|
target: /app
|
|
|
|
tmpfs:
|
|
|
|
size: 10000
|
|
|
|
`)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
expected := types.ServiceVolumeConfig{
|
|
|
|
Target: "/app",
|
|
|
|
Type: "tmpfs",
|
|
|
|
Tmpfs: &types.ServiceVolumeTmpfs{
|
|
|
|
Size: int64(10000),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
require.Len(t, config.Services, 1)
|
|
|
|
assert.Len(t, config.Services[0].Volumes, 1)
|
|
|
|
assert.Equal(t, expected, config.Services[0].Volumes[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadTmpfsVolumeAdditionalPropertyNotAllowed(t *testing.T) {
|
|
|
|
_, err := loadYAML(`
|
|
|
|
version: "3.5"
|
|
|
|
services:
|
|
|
|
tmpfs:
|
|
|
|
image: nginx:latest
|
|
|
|
volumes:
|
|
|
|
- type: tmpfs
|
|
|
|
target: /app
|
|
|
|
tmpfs:
|
|
|
|
size: 10000
|
|
|
|
`)
|
|
|
|
require.Error(t, err)
|
|
|
|
assert.Contains(t, err.Error(), "services.tmpfs.volumes.0 Additional property tmpfs is not allowed")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadTmpfsVolumeSizeCanBeZero(t *testing.T) {
|
|
|
|
config, err := loadYAML(`
|
|
|
|
version: "3.6"
|
|
|
|
services:
|
|
|
|
tmpfs:
|
|
|
|
image: nginx:latest
|
|
|
|
volumes:
|
|
|
|
- type: tmpfs
|
|
|
|
target: /app
|
|
|
|
tmpfs:
|
|
|
|
size: 0
|
|
|
|
`)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
expected := types.ServiceVolumeConfig{
|
|
|
|
Target: "/app",
|
|
|
|
Type: "tmpfs",
|
|
|
|
Tmpfs: &types.ServiceVolumeTmpfs{},
|
|
|
|
}
|
|
|
|
|
|
|
|
require.Len(t, config.Services, 1)
|
|
|
|
assert.Len(t, config.Services[0].Volumes, 1)
|
|
|
|
assert.Equal(t, expected, config.Services[0].Volumes[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadTmpfsVolumeSizeMustBeGTEQZero(t *testing.T) {
|
|
|
|
_, err := loadYAML(`
|
|
|
|
version: "3.6"
|
|
|
|
services:
|
|
|
|
tmpfs:
|
|
|
|
image: nginx:latest
|
|
|
|
volumes:
|
|
|
|
- type: tmpfs
|
|
|
|
target: /app
|
|
|
|
tmpfs:
|
|
|
|
size: -1
|
|
|
|
`)
|
|
|
|
require.Error(t, err)
|
|
|
|
assert.Contains(t, err.Error(), "services.tmpfs.volumes.0.tmpfs.size Must be greater than or equal to 0")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadTmpfsVolumeSizeMustBeInteger(t *testing.T) {
|
|
|
|
_, err := loadYAML(`
|
|
|
|
version: "3.6"
|
|
|
|
services:
|
|
|
|
tmpfs:
|
|
|
|
image: nginx:latest
|
|
|
|
volumes:
|
|
|
|
- type: tmpfs
|
|
|
|
target: /app
|
|
|
|
tmpfs:
|
|
|
|
size: 0.0001
|
|
|
|
`)
|
|
|
|
require.Error(t, err)
|
|
|
|
assert.Contains(t, err.Error(), "services.tmpfs.volumes.0.tmpfs.size must be a integer")
|
|
|
|
}
|
|
|
|
|
2016-12-20 16:26:49 -05:00
|
|
|
func serviceSort(services []types.ServiceConfig) []types.ServiceConfig {
|
|
|
|
sort.Sort(servicesByName(services))
|
|
|
|
return services
|
|
|
|
}
|
|
|
|
|
|
|
|
type servicesByName []types.ServiceConfig
|
|
|
|
|
|
|
|
func (sbn servicesByName) Len() int { return len(sbn) }
|
|
|
|
func (sbn servicesByName) Swap(i, j int) { sbn[i], sbn[j] = sbn[j], sbn[i] }
|
|
|
|
func (sbn servicesByName) Less(i, j int) bool { return sbn[i].Name < sbn[j].Name }
|
2017-02-04 16:55:28 -05:00
|
|
|
|
|
|
|
func TestLoadAttachableNetwork(t *testing.T) {
|
|
|
|
config, err := loadYAML(`
|
2017-03-13 15:05:30 -04:00
|
|
|
version: "3.2"
|
2017-02-04 16:55:28 -05:00
|
|
|
networks:
|
|
|
|
mynet1:
|
|
|
|
driver: overlay
|
|
|
|
attachable: true
|
|
|
|
mynet2:
|
|
|
|
driver: bridge
|
|
|
|
`)
|
2017-09-19 10:48:52 -04:00
|
|
|
require.NoError(t, err)
|
2017-02-04 16:55:28 -05:00
|
|
|
|
|
|
|
expected := map[string]types.NetworkConfig{
|
|
|
|
"mynet1": {
|
|
|
|
Driver: "overlay",
|
|
|
|
Attachable: true,
|
|
|
|
},
|
|
|
|
"mynet2": {
|
|
|
|
Driver: "bridge",
|
|
|
|
Attachable: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, expected, config.Networks)
|
|
|
|
}
|
2017-01-31 15:45:45 -05:00
|
|
|
|
|
|
|
func TestLoadExpandedPortFormat(t *testing.T) {
|
|
|
|
config, err := loadYAML(`
|
2017-03-13 15:05:30 -04:00
|
|
|
version: "3.2"
|
2017-01-31 15:45:45 -05:00
|
|
|
services:
|
|
|
|
web:
|
|
|
|
image: busybox
|
|
|
|
ports:
|
|
|
|
- "80-82:8080-8082"
|
|
|
|
- "90-92:8090-8092/udp"
|
|
|
|
- "85:8500"
|
|
|
|
- 8600
|
|
|
|
- protocol: udp
|
|
|
|
target: 53
|
|
|
|
published: 10053
|
|
|
|
- mode: host
|
|
|
|
target: 22
|
|
|
|
published: 10022
|
|
|
|
`)
|
2017-09-19 10:48:52 -04:00
|
|
|
require.NoError(t, err)
|
2017-01-31 15:45:45 -05:00
|
|
|
|
|
|
|
expected := []types.ServicePortConfig{
|
|
|
|
{
|
|
|
|
Mode: "ingress",
|
|
|
|
Target: 8080,
|
|
|
|
Published: 80,
|
|
|
|
Protocol: "tcp",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Mode: "ingress",
|
|
|
|
Target: 8081,
|
|
|
|
Published: 81,
|
|
|
|
Protocol: "tcp",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Mode: "ingress",
|
|
|
|
Target: 8082,
|
|
|
|
Published: 82,
|
|
|
|
Protocol: "tcp",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Mode: "ingress",
|
|
|
|
Target: 8090,
|
|
|
|
Published: 90,
|
|
|
|
Protocol: "udp",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Mode: "ingress",
|
|
|
|
Target: 8091,
|
|
|
|
Published: 91,
|
|
|
|
Protocol: "udp",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Mode: "ingress",
|
|
|
|
Target: 8092,
|
|
|
|
Published: 92,
|
|
|
|
Protocol: "udp",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Mode: "ingress",
|
|
|
|
Target: 8500,
|
|
|
|
Published: 85,
|
|
|
|
Protocol: "tcp",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Mode: "ingress",
|
|
|
|
Target: 8600,
|
|
|
|
Published: 0,
|
|
|
|
Protocol: "tcp",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Target: 53,
|
|
|
|
Published: 10053,
|
|
|
|
Protocol: "udp",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Mode: "host",
|
|
|
|
Target: 22,
|
|
|
|
Published: 10022,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-09-19 10:48:52 -04:00
|
|
|
assert.Len(t, config.Services, 1)
|
2017-01-31 15:45:45 -05:00
|
|
|
assert.Equal(t, expected, config.Services[0].Ports)
|
|
|
|
}
|
2017-01-24 12:09:53 -05:00
|
|
|
|
|
|
|
func TestLoadExpandedMountFormat(t *testing.T) {
|
|
|
|
config, err := loadYAML(`
|
2017-03-13 15:05:30 -04:00
|
|
|
version: "3.2"
|
2017-01-24 12:09:53 -05:00
|
|
|
services:
|
|
|
|
web:
|
|
|
|
image: busybox
|
|
|
|
volumes:
|
|
|
|
- type: volume
|
|
|
|
source: foo
|
|
|
|
target: /target
|
|
|
|
read_only: true
|
|
|
|
volumes:
|
|
|
|
foo: {}
|
|
|
|
`)
|
2017-09-19 10:48:52 -04:00
|
|
|
require.NoError(t, err)
|
2017-01-24 12:09:53 -05:00
|
|
|
|
|
|
|
expected := types.ServiceVolumeConfig{
|
|
|
|
Type: "volume",
|
|
|
|
Source: "foo",
|
|
|
|
Target: "/target",
|
|
|
|
ReadOnly: true,
|
|
|
|
}
|
|
|
|
|
2017-09-19 10:48:52 -04:00
|
|
|
require.Len(t, config.Services, 1)
|
|
|
|
assert.Len(t, config.Services[0].Volumes, 1)
|
2017-01-24 12:09:53 -05:00
|
|
|
assert.Equal(t, expected, config.Services[0].Volumes[0])
|
|
|
|
}
|
Preserve sort-order of extra hosts, and allow duplicate entries
Extra hosts (`extra_hosts` in compose-file, or `--hosts` in services) adds
custom host/ip mappings to the container's `/etc/hosts`.
The current implementation used a `map[string]string{}` as intermediate
storage, and sorted the results alphabetically when converting to a service-spec.
As a result, duplicate hosts were removed, and order of host/ip mappings was not
preserved (in case the compose-file used a list instead of a map).
According to the **host.conf(5)** man page (http://man7.org/linux/man-pages/man5/host.conf.5.html)
multi Valid values are on and off. If set to on, the resolver
library will return all valid addresses for a host that
appears in the /etc/hosts file, instead of only the first.
This is off by default, as it may cause a substantial
performance loss at sites with large hosts files.
Multiple entries for a host are allowed, and even required for some situations,
for example, to add mappings for IPv4 and IPv6 addreses for a host, as illustrated
by the example hosts file in the **hosts(5)** man page (http://man7.org/linux/man-pages/man5/hosts.5.html):
# The following lines are desirable for IPv4 capable hosts
127.0.0.1 localhost
# 127.0.1.1 is often used for the FQDN of the machine
127.0.1.1 thishost.mydomain.org thishost
192.168.1.10 foo.mydomain.org foo
192.168.1.13 bar.mydomain.org bar
146.82.138.7 master.debian.org master
209.237.226.90 www.opensource.org
# The following lines are desirable for IPv6 capable hosts
::1 localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
This patch changes the intermediate storage format to use a `[]string`, and only
sorts entries if the input format in the compose file is a mapping. If the input
format is a list, the original sort-order is preserved.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2017-10-29 20:33:23 -04:00
|
|
|
|
|
|
|
func TestLoadExtraHostsMap(t *testing.T) {
|
|
|
|
config, err := loadYAML(`
|
|
|
|
version: "3.2"
|
|
|
|
services:
|
|
|
|
web:
|
|
|
|
image: busybox
|
|
|
|
extra_hosts:
|
|
|
|
"zulu": "162.242.195.82"
|
|
|
|
"alpha": "50.31.209.229"
|
|
|
|
`)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
expected := types.HostsList{
|
|
|
|
"alpha:50.31.209.229",
|
|
|
|
"zulu:162.242.195.82",
|
|
|
|
}
|
|
|
|
|
|
|
|
require.Len(t, config.Services, 1)
|
|
|
|
assert.Equal(t, expected, config.Services[0].ExtraHosts)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadExtraHostsList(t *testing.T) {
|
|
|
|
config, err := loadYAML(`
|
|
|
|
version: "3.2"
|
|
|
|
services:
|
|
|
|
web:
|
|
|
|
image: busybox
|
|
|
|
extra_hosts:
|
|
|
|
- "zulu:162.242.195.82"
|
|
|
|
- "alpha:50.31.209.229"
|
|
|
|
- "zulu:ff02::1"
|
|
|
|
`)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
expected := types.HostsList{
|
|
|
|
"zulu:162.242.195.82",
|
|
|
|
"alpha:50.31.209.229",
|
|
|
|
"zulu:ff02::1",
|
|
|
|
}
|
|
|
|
|
|
|
|
require.Len(t, config.Services, 1)
|
|
|
|
assert.Equal(t, expected, config.Services[0].ExtraHosts)
|
|
|
|
}
|
2017-11-06 17:03:43 -05:00
|
|
|
|
|
|
|
func TestLoadVolumesWarnOnDeprecatedExternalNameVersion34(t *testing.T) {
|
|
|
|
buf, cleanup := patchLogrus()
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
source := map[string]interface{}{
|
|
|
|
"foo": map[string]interface{}{
|
|
|
|
"external": map[string]interface{}{
|
|
|
|
"name": "oops",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
volumes, err := LoadVolumes(source, "3.4")
|
|
|
|
require.NoError(t, err)
|
|
|
|
expected := map[string]types.VolumeConfig{
|
|
|
|
"foo": {
|
|
|
|
Name: "oops",
|
|
|
|
External: types.External{External: true},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
assert.Equal(t, expected, volumes)
|
|
|
|
assert.Contains(t, buf.String(), "volume.external.name is deprecated")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func patchLogrus() (*bytes.Buffer, func()) {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
out := logrus.StandardLogger().Out
|
|
|
|
logrus.SetOutput(buf)
|
|
|
|
return buf, func() { logrus.SetOutput(out) }
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadVolumesWarnOnDeprecatedExternalNameVersion33(t *testing.T) {
|
|
|
|
buf, cleanup := patchLogrus()
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
source := map[string]interface{}{
|
|
|
|
"foo": map[string]interface{}{
|
|
|
|
"external": map[string]interface{}{
|
|
|
|
"name": "oops",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
volumes, err := LoadVolumes(source, "3.3")
|
|
|
|
require.NoError(t, err)
|
|
|
|
expected := map[string]types.VolumeConfig{
|
|
|
|
"foo": {
|
|
|
|
Name: "oops",
|
|
|
|
External: types.External{External: true},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
assert.Equal(t, expected, volumes)
|
|
|
|
assert.Equal(t, "", buf.String())
|
2017-11-17 09:31:13 -05:00
|
|
|
}
|
2017-11-06 17:03:43 -05:00
|
|
|
|
2017-11-17 09:31:13 -05:00
|
|
|
func TestLoadV35(t *testing.T) {
|
|
|
|
actual, err := loadYAML(`
|
|
|
|
version: "3.5"
|
|
|
|
services:
|
|
|
|
foo:
|
|
|
|
image: busybox
|
|
|
|
isolation: process
|
|
|
|
configs:
|
2017-11-11 00:11:29 -05:00
|
|
|
foo:
|
|
|
|
name: fooqux
|
2017-11-17 09:31:13 -05:00
|
|
|
external: true
|
2017-11-11 00:11:29 -05:00
|
|
|
bar:
|
|
|
|
name: barqux
|
|
|
|
file: ./example1.env
|
|
|
|
secrets:
|
|
|
|
foo:
|
|
|
|
name: fooqux
|
|
|
|
external: true
|
|
|
|
bar:
|
|
|
|
name: barqux
|
|
|
|
file: ./full-example.yml
|
2017-11-17 09:31:13 -05:00
|
|
|
`)
|
|
|
|
require.NoError(t, err)
|
2017-11-11 00:11:29 -05:00
|
|
|
assert.Len(t, actual.Services, 1)
|
|
|
|
assert.Len(t, actual.Secrets, 2)
|
|
|
|
assert.Len(t, actual.Configs, 2)
|
2017-11-17 09:31:13 -05:00
|
|
|
assert.Equal(t, "process", actual.Services[0].Isolation)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadV35InvalidIsolation(t *testing.T) {
|
|
|
|
// validation should be done only on the daemon side
|
|
|
|
actual, err := loadYAML(`
|
|
|
|
version: "3.5"
|
|
|
|
services:
|
|
|
|
foo:
|
|
|
|
image: busybox
|
|
|
|
isolation: invalid
|
|
|
|
configs:
|
|
|
|
super:
|
|
|
|
external: true
|
|
|
|
`)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, actual.Services, 1)
|
|
|
|
assert.Equal(t, "invalid", actual.Services[0].Isolation)
|
2017-11-06 17:03:43 -05:00
|
|
|
}
|
2017-11-10 08:35:29 -05:00
|
|
|
|
2017-11-22 14:21:32 -05:00
|
|
|
func TestLoadSecretInvalidExternalNameAndNameCombination(t *testing.T) {
|
2017-11-10 08:35:29 -05:00
|
|
|
_, err := loadYAML(`
|
|
|
|
version: "3.5"
|
|
|
|
secrets:
|
|
|
|
external_secret:
|
|
|
|
name: user_specified_name
|
|
|
|
external:
|
|
|
|
name: external_name
|
|
|
|
`)
|
|
|
|
|
|
|
|
require.Error(t, err)
|
|
|
|
assert.Contains(t, err.Error(), "secret.external.name and secret.name conflict; only use secret.name")
|
|
|
|
assert.Contains(t, err.Error(), "external_secret")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadSecretsWarnOnDeprecatedExternalNameVersion35(t *testing.T) {
|
|
|
|
buf, cleanup := patchLogrus()
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
source := map[string]interface{}{
|
|
|
|
"foo": map[string]interface{}{
|
|
|
|
"external": map[string]interface{}{
|
|
|
|
"name": "oops",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2017-11-11 00:11:29 -05:00
|
|
|
details := types.ConfigDetails{
|
|
|
|
Version: "3.5",
|
2017-11-10 08:35:29 -05:00
|
|
|
}
|
|
|
|
secrets, err := LoadSecrets(source, details)
|
|
|
|
require.NoError(t, err)
|
|
|
|
expected := map[string]types.SecretConfig{
|
|
|
|
"foo": {
|
|
|
|
Name: "oops",
|
|
|
|
External: types.External{External: true},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
assert.Equal(t, expected, secrets)
|
|
|
|
assert.Contains(t, buf.String(), "secret.external.name is deprecated")
|
|
|
|
}
|
2017-11-22 14:21:32 -05:00
|
|
|
|
|
|
|
func TestLoadNetworksWarnOnDeprecatedExternalNameVersion35(t *testing.T) {
|
|
|
|
buf, cleanup := patchLogrus()
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
source := map[string]interface{}{
|
|
|
|
"foo": map[string]interface{}{
|
|
|
|
"external": map[string]interface{}{
|
|
|
|
"name": "oops",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
networks, err := LoadNetworks(source, "3.5")
|
|
|
|
require.NoError(t, err)
|
|
|
|
expected := map[string]types.NetworkConfig{
|
|
|
|
"foo": {
|
|
|
|
Name: "oops",
|
|
|
|
External: types.External{External: true},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
assert.Equal(t, expected, networks)
|
|
|
|
assert.Contains(t, buf.String(), "network.external.name is deprecated")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadNetworksWarnOnDeprecatedExternalNameVersion34(t *testing.T) {
|
|
|
|
buf, cleanup := patchLogrus()
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
source := map[string]interface{}{
|
|
|
|
"foo": map[string]interface{}{
|
|
|
|
"external": map[string]interface{}{
|
|
|
|
"name": "oops",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
networks, err := LoadNetworks(source, "3.4")
|
|
|
|
require.NoError(t, err)
|
|
|
|
expected := map[string]types.NetworkConfig{
|
|
|
|
"foo": {
|
|
|
|
Name: "oops",
|
|
|
|
External: types.External{External: true},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
assert.Equal(t, expected, networks)
|
|
|
|
assert.Equal(t, "", buf.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadNetworkInvalidExternalNameAndNameCombination(t *testing.T) {
|
|
|
|
_, err := loadYAML(`
|
|
|
|
version: "3.5"
|
|
|
|
networks:
|
|
|
|
foo:
|
|
|
|
name: user_specified_name
|
|
|
|
external:
|
|
|
|
name: external_name
|
|
|
|
`)
|
|
|
|
|
|
|
|
require.Error(t, err)
|
|
|
|
assert.Contains(t, err.Error(), "network.external.name and network.name conflict; only use network.name")
|
|
|
|
assert.Contains(t, err.Error(), "foo")
|
|
|
|
}
|