Skip to content
This repository has been archived by the owner on Jun 16, 2021. It is now read-only.

Commit

Permalink
Refactoring a little bit the yaml package
Browse files Browse the repository at this point in the history
- Split into smaller files (with test) to make it easier to read
- Add a Build type that handle some Build magic
- Add an External type that handle some external magic (networks,
  volumes)
- Add Networks/Network type that handle some Networks magic
- Refactor the Ulimit types to be simpler and not user reflection

Also adds a merge_fixtures_test.go and testdata folder with some
existing compose file to make sure it parses it well (should serve as
quick regression tests in the future too).

Signed-off-by: Vincent Demeester <vincent@sbr.pm>
  • Loading branch information
vdemeester committed Jun 1, 2016
1 parent 8161ca7 commit f336219
Show file tree
Hide file tree
Showing 37 changed files with 1,201 additions and 309 deletions.
7 changes: 5 additions & 2 deletions config/convert.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package config

import "github.com/docker/libcompose/utils"
import (
"github.com/docker/libcompose/utils"
"github.com/docker/libcompose/yaml"
)

// ConvertServices converts a set of v1 service configs to v2 service configs
func ConvertServices(v1Services map[string]*ServiceConfigV1) (map[string]*ServiceConfig, error) {
Expand All @@ -9,7 +12,7 @@ func ConvertServices(v1Services map[string]*ServiceConfigV1) (map[string]*Servic

for name, service := range v1Services {
replacementFields[name] = &ServiceConfig{
Build: Build{
Build: yaml.Build{
Context: service.Build,
Dockerfile: service.Dockerfile,
},
Expand Down
4 changes: 3 additions & 1 deletion config/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package config
import (
"reflect"
"testing"

"github.com/docker/libcompose/yaml"
)

func TestBuild(t *testing.T) {
Expand All @@ -18,7 +20,7 @@ func TestBuild(t *testing.T) {

v2Config := v2Services["test"]

expectedBuild := Build{
expectedBuild := yaml.Build{
Context: ".",
Dockerfile: "Dockerfile",
}
Expand Down
27 changes: 27 additions & 0 deletions config/merge_fixtures_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package config

import (
"io/ioutil"
"path/filepath"
"testing"
)

func TestMergeOnValidFixtures(t *testing.T) {
files, err := ioutil.ReadDir("testdata/")
if err != nil {
t.Fatal(err)
}
for _, file := range files {
if file.IsDir() {
continue
}
data, err := ioutil.ReadFile(filepath.Join("testdata", file.Name()))
if err != nil {
t.Fatalf("error reading %q: %v", file.Name(), err)
}
_, _, _, _, err = Merge(NewServiceConfigs(), nil, nil, file.Name(), data, nil)
if err != nil {
t.Errorf("error loading %q: %v\n %v", file.Name(), string(data), err)
}
}
}
4 changes: 3 additions & 1 deletion config/merge_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package config

import "testing"
import (
"testing"
)

type NullLookup struct {
}
Expand Down
9 changes: 8 additions & 1 deletion config/merge_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,14 @@ func resolveContextV2(inFile string, serviceData RawService) RawService {
if _, ok := serviceData["build"]; !ok {
return serviceData
}
build := serviceData["build"].(map[interface{}]interface{})
var build map[interface{}]interface{}
if buildAsString, ok := serviceData["build"].(string); ok {
build = map[interface{}]interface{}{
"context": buildAsString,
}
} else {
build = serviceData["build"].(map[interface{}]interface{})
}
context := asString(build["context"])
if context == "" {
return serviceData
Expand Down
6 changes: 6 additions & 0 deletions config/testdata/build-image.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: "2"

services:
simple:
build: .
image: myimage
5 changes: 5 additions & 0 deletions config/testdata/build.v1.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
simple1:
build: .
simple2:
build: .
dockerfile: alternate
18 changes: 18 additions & 0 deletions config/testdata/build.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
version: "2"

services:
simple1:
build: .
simple2:
context: ./dir
simple3:
context: ./another
dockerfile: alternate
args:
buildno: 1
user: vincent
simple4:
context: ./another
args:
buildno: 2
user: josh
6 changes: 6 additions & 0 deletions config/testdata/command.v1.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
simple1:
image: myimage
command: bundle exec thi-p 3000
simple2:
image: myimage
command: [bundle, exec, thin, -p, "3000"]
11 changes: 11 additions & 0 deletions config/testdata/depends-on.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: '2'
services:
web:
build: .
depends_on:
- db
- redis
redis:
image: redis
db:
image: postgres
6 changes: 6 additions & 0 deletions config/testdata/dns.v1.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
simple:
build: .
dns: 8.8.8.8
dns:
- 8.8.8.8
- 9.9.9.9
12 changes: 12 additions & 0 deletions config/testdata/entrypoint.v1.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
simple1:
build: .
entrypoint:
- php
- -d
- zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20100525/xdebug.so
- -d
- memory_limit=-1
- vendor/bin/phpunit
simple2:
build: .
entrypoint: /code/entrypoint.sh
14 changes: 14 additions & 0 deletions config/testdata/entrypoint.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
version: "2"
services:
simple1:
build: .
entrypoint:
- php
- -d
- zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20100525/xdebug.so
- -d
- memory_limit=-1
- vendor/bin/phpunit
simple2:
build: .
entrypoint: /code/entrypoint.sh
8 changes: 8 additions & 0 deletions config/testdata/logging.v1.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
simple1:
image: myimage
log_driver: syslog
log_opt:
syslog-address: "tcp://192.168.0.42:123"
simple2:
image: myimage
log_driver: "none"
12 changes: 12 additions & 0 deletions config/testdata/logging.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: "2"
services:
simple1:
image: myimage
logging:
driver: syslog
options:
syslog-address: "tcp://192.168.0.42:123"
simple2:
image: myimage
logging:
driver: "none"
17 changes: 17 additions & 0 deletions config/testdata/network-mode.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
version: "2"
services:
simple1:
image: myimage
network_mode: bridge
simple2:
image: myimage
network_mode: "service:bridge"
simple3:
image: myimage
network_mode: "container:test_container"
simple4:
image: myimage
network_mode: host
simple5:
image: myimage
network_mode: none
21 changes: 21 additions & 0 deletions config/testdata/networks-definition.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
version: "2"
networks:
default:
driver: custom-driver
network1:
driver: bridge
driver_opts:
com.docker.network.enable_ipv6: "true"
ipam:
driver: default
config:
- subnet: 172.16.238.0/24
gateway: 172.16.238.1
- subnet: 2001:3984:3989::/64
gateway: 2001:3984:3989::1
network2:
external: true
network3:
external:
name: name-of-network
network3: {}
29 changes: 29 additions & 0 deletions config/testdata/networks.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
version: "2"
services:
simple1:
image: myimage
networks:
- network1
- network2
simple2:
image: myimage
networks:
network1:
aliases:
- alias1
- alias3
network2:
aliases:
- alias2
simple3:
image: myimage
networks:
network1:
ipv4_address: 172.16.238.10
ipv6_address: 2001:3984:3989::10
simple4:
image: myimage
networks: ["network1"]
simple5:
image: myimage
networks: ["network1", "network2"]
7 changes: 7 additions & 0 deletions config/testdata/ulimits.v1.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
simple1:
image: myimage
ulimits:
nproc: 65535
nofile:
soft: 20000
hard: 40000
9 changes: 9 additions & 0 deletions config/testdata/ulimits.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
version: "2"
services:
simple1:
image: myimage
ulimits:
nproc: 65535
nofile:
soft: 20000
hard: 40000
14 changes: 14 additions & 0 deletions config/testdata/volumes-definition.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
version: "2"
volumes:
volume1:
driver: foo
volume2:
driver: bar
driver_opts:
foo: "bar"
baz: ""
volume3:
external: true
volume4:
external:
name: name-of-volume
14 changes: 14 additions & 0 deletions config/testdata/volumes.v1.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
simple1:
image: myimage
volumes:
- /var/lib/mysql
- /opt/data:/var/lib/mysql
- ./cache:/tmp/cache
- ~configs:/etc/configs/:ro
- datavolume:/var/lib/mysql
volume_driver: mydriver
volumes_from:
- service_name
- service_name:ro
- container_name
- container_name:rw
11 changes: 11 additions & 0 deletions config/testdata/volumes.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: "2"
services:
simple1:
image: myimage
volumes:
- /var/lib/mysql
- /opt/data:/var/lib/mysql
- ./cache:/tmp/cache
- ~configs:/etc/configs/:ro
- datavolume:/var/lib/mysql
volume_driver: mydriver
Loading

0 comments on commit f336219

Please sign in to comment.