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 depends_on cycle detection during validation phase #546

Merged
merged 1 commit into from
Jan 29, 2024
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
14 changes: 14 additions & 0 deletions loader/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2552,6 +2552,20 @@ func TestLoadWithIncludeCycle(t *testing.T) {
assert.Check(t, strings.HasPrefix(err.Error(), "include cycle detected"))
}

func TestLoadDependsOnCycle(t *testing.T) {
workingDir, err := os.Getwd()
assert.NilError(t, err)
_, err = LoadWithContext(context.Background(), types.ConfigDetails{
WorkingDir: filepath.Join(workingDir, "testdata"),
ConfigFiles: []types.ConfigFile{
{
Filename: filepath.Join(workingDir, "testdata", "compose-depends-on-cycle.yaml"),
},
},
})
assert.Error(t, err, "dependency cycle detected: service2 -> service3 -> service1", err)
}

func TestLoadWithMultipleInclude(t *testing.T) {
// include same service twice should not trigger an error
p, err := Load(buildConfigDetails(`
Expand Down
14 changes: 14 additions & 0 deletions loader/testdata/compose-depends-on-cycle.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name : depends-on-cycle
services:
service1:
image: service1
depends_on:
- service2
service2:
image: service2
depends_on:
- service3
service3:
image: service3
depends_on:
- service1
14 changes: 14 additions & 0 deletions loader/testdata/compose-depends-on-profile-no-cycle.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name : depends-on-cycle
services:
service1:
image: service1
depends_on:
- service2
service2:
image: service2
depends_on:
- service3
service3:
image: service3
depends_on:
- service1
8 changes: 8 additions & 0 deletions loader/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
package loader

import (
"context"
"errors"
"fmt"
"strings"

"github.com/compose-spec/compose-go/v2/errdefs"
"github.com/compose-spec/compose-go/v2/graph"
"github.com/compose-spec/compose-go/v2/types"
)

Expand Down Expand Up @@ -73,6 +75,12 @@ func checkConsistency(project *types.Project) error {
return fmt.Errorf("service %q depends on undefined service %s: %w", s.Name, dependedService, errdefs.ErrInvalid)
}
}
// Check there isn't a cycle in depends_on declarations
if err := graph.InDependencyOrder(context.Background(), project, func(ctx context.Context, s string, config types.ServiceConfig) error {
return nil
}); err != nil {
return err
}

if strings.HasPrefix(s.NetworkMode, types.ServicePrefix) {
serviceName := s.NetworkMode[len(types.ServicePrefix):]
Expand Down