Skip to content
Merged
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
5 changes: 4 additions & 1 deletion loader/extends.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ func applyServiceExtends(ctx context.Context, name string, services map[string]a
)
switch v := extends.(type) {
case map[string]any:
ref = v["service"].(string)
ref, ok = v["service"].(string)
if !ok {
return nil, fmt.Errorf("extends.%s.service is required", name)
Copy link
Collaborator

Choose a reason for hiding this comment

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

json schema already declares "required": ["service"], so why do we end up seing this?

}
file = v["file"]
opts.ProcessEvent("extends", v)
case string:
Expand Down
27 changes: 27 additions & 0 deletions loader/extends_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,33 @@ services:
assert.NilError(t, err)
}

func TestExtendsWihtMissingService(t *testing.T) {
yaml := `
name: test-extends-port
services:
test:
image: test
extends:
file: testdata/extends/base.yaml
`
abs, err := filepath.Abs(".")
assert.NilError(t, err)

_, err = LoadWithContext(context.Background(), types.ConfigDetails{
ConfigFiles: []types.ConfigFile{
{
Content: []byte(yaml),
Filename: "(inline)",
},
},
WorkingDir: abs,
}, func(options *Options) {
options.ResolvePaths = false
options.SkipValidation = true
})
assert.Error(t, err, "extends.test.service is required")
}

func TestIncludeWithExtends(t *testing.T) {
yaml := `
name: test-include-with-extends
Expand Down
7 changes: 7 additions & 0 deletions loader/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,13 @@ func checkConsistency(project *types.Project) error { //nolint:gocyclo
s.Deploy.Replicas = s.Scale
}

if s.Scale != nil && *s.Scale < 0 {
return fmt.Errorf("services.%s.scale: must be greater than or equal to 0", s.Name)
}
if s.Deploy != nil && s.Deploy.Replicas != nil && *s.Deploy.Replicas < 0 {
return fmt.Errorf("services.%s.deploy.replicas: must be greater than or equal to 0", s.Name)
}

if s.CPUS != 0 && s.Deploy != nil {
if s.Deploy.Resources.Limits != nil && s.Deploy.Resources.Limits.NanoCPUs.Value() != s.CPUS {
return fmt.Errorf("services.%s: can't set distinct values on 'cpus' and 'deploy.resources.limits.cpus': %w",
Expand Down
28 changes: 28 additions & 0 deletions loader/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,3 +436,31 @@ func TestValidateMountConflict(t *testing.T) {
err := checkConsistency(project)
assert.Error(t, err, "services.myservice.volumes[1]: target /conflict already mounted as services.myservice.tmpfs[1]")
}

func TestValidateNegativeScale(t *testing.T) {
project := &types.Project{
Services: types.Services{
"myservice": {
Name: "myservice",
Image: "scratch",
Scale: ptr(-1),
},
},
}
err := checkConsistency(project)
assert.Error(t, err, "services.myservice.scale: must be greater than or equal to 0")

project = &types.Project{
Services: types.Services{
"myservice": {
Name: "myservice",
Image: "scratch",
Deploy: &types.DeployConfig{
Replicas: ptr(-1),
},
},
},
}
err = checkConsistency(project)
assert.Error(t, err, "services.myservice.deploy.replicas: must be greater than or equal to 0")
}