Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add composefile v1 / v2 parsing and validation support #573

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions cli/compose/loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ func Load(configDetails types.ConfigDetails) (*types.Config, error) {
return nil, err
}

if _, ok := configDict["version"]; !ok {
configDict = map[string]interface{}{
"services": configDict,
}
}

cfg := types.Config{}

config, err := interpolateConfig(configDict, configDetails.LookupEnv)
Expand Down
38 changes: 6 additions & 32 deletions cli/compose/loader/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/docker/cli/cli/compose/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"path/filepath"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should move this import up with other standard imports.

)

func buildConfigDetails(source map[string]interface{}, env map[string]string) types.ConfigDetails {
Expand All @@ -20,7 +21,7 @@ func buildConfigDetails(source map[string]interface{}, env map[string]string) ty
}

return types.ConfigDetails{
WorkingDir: workingDir,
WorkingDir: filepath.Join(workingDir, "testdata"),
ConfigFiles: []types.ConfigFile{
{Filename: "filename.yml", Config: source},
},
Expand Down Expand Up @@ -299,26 +300,6 @@ services:
require.NoError(t, err)
}

func TestUnsupportedVersion(t *testing.T) {
_, err := loadYAML(`
version: "2"
services:
foo:
image: busybox
`)
require.Error(t, err)
assert.Contains(t, err.Error(), "version")

_, err = loadYAML(`
version: "2.0"
services:
foo:
image: busybox
`)
require.Error(t, err)
assert.Contains(t, err.Error(), "version")
}

func TestInvalidVersion(t *testing.T) {
_, err := loadYAML(`
version: 3
Expand All @@ -330,14 +311,6 @@ services:
assert.Contains(t, err.Error(), "version must be a string")
}

func TestV1Unsupported(t *testing.T) {
_, err := loadYAML(`
foo:
image: busybox
`)
assert.Error(t, err)
}

func TestNonMappingObject(t *testing.T) {
_, err := loadYAML(`
version: "3"
Expand Down Expand Up @@ -680,7 +653,7 @@ func uint64Ptr(value uint64) *uint64 {
}

func TestFullExample(t *testing.T) {
bytes, err := ioutil.ReadFile("full-example.yml")
bytes, err := ioutil.ReadFile("testdata/v3-full-example.yml")
require.NoError(t, err)

homeDir := "/home/foo"
Expand All @@ -690,6 +663,7 @@ func TestFullExample(t *testing.T) {

workingDir, err := os.Getwd()
require.NoError(t, err)
workingDir = filepath.Join(workingDir, "testdata")

stopGracePeriod := time.Duration(20 * time.Second)

Expand Down Expand Up @@ -761,8 +735,8 @@ func TestFullExample(t *testing.T) {
"QUX": strPtr("qux_from_environment"),
},
EnvFile: []string{
"./example1.env",
"./example2.env",
"example1.env",
"example2.env",
},
Expose: []string{"3000", "8000"},
ExternalLinks: []string{
Expand Down
37 changes: 37 additions & 0 deletions cli/compose/loader/loader_v1_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package loader

import (
"testing"
"github.com/stretchr/testify/require"
)


func TestUnsupportedVersion(t *testing.T) {
_, err := loadYAML(`
version: "0.1"
services:
foo:
image: busybox
`)
require.Error(t, err)
require.Contains(t, err.Error(), "version")

_, err = loadYAML(`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why testing the same error again?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are right, I'll remove 😛

version: "0.1"
services:
foo:
image: busybox
`)
require.Error(t, err)
require.Contains(t, err.Error(), "version")
}


func TestV1Supported(t *testing.T) {
actual, err := loadYAML(`
foo:
image: busybox
`)
require.NoError(t, err)
require.Len(t, actual.Services, 1)
}
18 changes: 18 additions & 0 deletions cli/compose/loader/loader_v2_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package loader

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestV2Supported(t *testing.T) {
actual, err := loadYAML(`
version: "2.0"
services:
foo:
image: busybox`)

require.NoError(t, err)
require.Len(t, actual.Services, 1)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ FOO=foo_from_env_file
# overridden in example2.env
BAR=bar_from_env_file

# overridden in full-example.yml
# overridden in v3-full-example.yml
BAZ=baz_from_env_file
10 changes: 10 additions & 0 deletions cli/compose/loader/testdata/v1-full.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
net:
image: busybox
volume:
image: busybox
volumes:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if this is a "full" test it should probably include all the fields supported by v1 and one service with build, one with image

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dnephin yeah I just copied the file from `docker/compose so far 😛

- /data
app:
image: busybox
net: "container:net"
volumes_from: ["volume"]
23 changes: 23 additions & 0 deletions cli/compose/loader/testdata/v2-full.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
version: "2"

volumes:
data:
driver: local

networks:
front: {}

services:
web:
build: .
networks:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here, should include all the fields

- front
- default
volumes_from:
- other

other:
image: busybox:latest
command: top
volumes:
- /data
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ services:
# String or list
# env_file: .env
env_file:
- ./example1.env
- ./example2.env
- example1.env
- example2.env

# Mapping or list
# Mapping values can be strings, numbers or null
Expand Down
148 changes: 143 additions & 5 deletions cli/compose/schema/bindata.go

Large diffs are not rendered by default.

Loading