Change compose file handling to require valid service specifications

Added the checks and some tests
One of the tests wasn't valid docker-compose.yml, that's been changed.
Bad config throws an error and has a test

Signed-off-by: Jack Laxson <jackjrabbit@gmail.com>
This commit is contained in:
Jack Laxson 2019-05-18 05:53:44 -04:00 committed by Tibor Vass
parent 61a6fdd767
commit 4b2666b9d6
3 changed files with 49 additions and 1 deletions

View File

@ -1,6 +1,8 @@
package bake package bake
import ( import (
"fmt"
"github.com/docker/cli/cli/compose/loader" "github.com/docker/cli/cli/compose/loader"
composetypes "github.com/docker/cli/cli/compose/types" composetypes "github.com/docker/cli/cli/compose/types"
) )
@ -33,7 +35,7 @@ func ParseCompose(dt []byte) (*Config, error) {
var g Group var g Group
for _, s := range cfg.Services { for _, s := range cfg.Services {
g.Targets = append(g.Targets, s.Name)
var contextPathP *string var contextPathP *string
if s.Build.Context != "" { if s.Build.Context != "" {
contextPath := s.Build.Context contextPath := s.Build.Context
@ -44,6 +46,15 @@ func ParseCompose(dt []byte) (*Config, error) {
dockerfilePath := s.Build.Dockerfile dockerfilePath := s.Build.Dockerfile
dockerfilePathP = &dockerfilePath dockerfilePathP = &dockerfilePath
} }
// Check if there's actually a dockerfile mentioned
if dockerfilePathP == nil && contextPathP == nil {
// if not make sure they're setting an image or it's invalid d-c.yml
if s.Image == "" {
return nil, fmt.Errorf("Compose file invalid: Service %s has neither an image nor a build context specified. At least one must be provided.", s.Name)
}
break
}
g.Targets = append(g.Targets, s.Name)
t := Target{ t := Target{
Context: contextPathP, Context: contextPathP,
Dockerfile: dockerfilePathP, Dockerfile: dockerfilePathP,

View File

@ -40,6 +40,22 @@ services:
require.Equal(t, "123", c.Target["webapp"].Args["buildno"]) require.Equal(t, "123", c.Target["webapp"].Args["buildno"])
} }
func TestNoBuildOutOfTreeService(t *testing.T) {
var dt = []byte(`
version: "3.7"
services:
external:
image: "verycooldb:1337"
webapp:
build: ./db
`)
c, err := ParseCompose(dt)
require.NoError(t, err)
require.Equal(t, 1, len(c.Group))
}
func TestParseComposeTarget(t *testing.T) { func TestParseComposeTarget(t *testing.T) {
var dt = []byte(` var dt = []byte(`
version: "3.7" version: "3.7"
@ -47,9 +63,11 @@ version: "3.7"
services: services:
db: db:
build: build:
context: ./db
target: db target: db
webapp: webapp:
build: build:
context: .
target: webapp target: webapp
`) `)
@ -59,3 +77,21 @@ services:
require.Equal(t, "db", *c.Target["db"].Target) require.Equal(t, "db", *c.Target["db"].Target)
require.Equal(t, "webapp", *c.Target["webapp"].Target) require.Equal(t, "webapp", *c.Target["webapp"].Target)
} }
func TestBogusCompose(t *testing.T) {
var dt = []byte(`
version: "3.7"
services:
db:
build:
target: db
webapp:
build:
context: .
target: webapp
`)
_, err := ParseCompose(dt)
require.Error(t, err)
}

View File

@ -7,6 +7,7 @@ services:
image: docker.io/tonistiigi/db image: docker.io/tonistiigi/db
webapp: webapp:
build: build:
context: .
dockerfile: Dockerfile.webapp dockerfile: Dockerfile.webapp
args: args:
buildno: 1 buildno: 1