Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion cmd/compose/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"strings"

"github.com/compose-spec/compose-go/v2/cli"
"github.com/compose-spec/compose-go/v2/loader"
"github.com/compose-spec/compose-go/v2/template"
"github.com/compose-spec/compose-go/v2/types"
"github.com/docker/cli/cli/command"
Expand Down Expand Up @@ -519,7 +520,7 @@ func runConfigImages(ctx context.Context, dockerCli command.Cli, opts configOpti

func runVariables(ctx context.Context, dockerCli command.Cli, opts configOptions, services []string) error {
opts.noInterpolate = true
model, err := opts.ToModel(ctx, dockerCli, services, cli.WithoutEnvironmentResolution)
model, err := opts.ToModel(ctx, dockerCli, services, cli.WithoutEnvironmentResolution, cli.WithLoadOptions(loader.WithSkipValidation))
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/compose/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"text/tabwriter"

"github.com/compose-spec/compose-go/v2/cli"
"github.com/compose-spec/compose-go/v2/loader"
"github.com/compose-spec/compose-go/v2/template"
"github.com/compose-spec/compose-go/v2/types"
"github.com/docker/cli/cli/command"
Expand Down Expand Up @@ -165,7 +166,7 @@ func extractInterpolationVariablesFromModel(ctx context.Context, dockerCli comma
ProjectOptions: projectOptions,
}

model, err := opts.ToModel(ctx, dockerCli, nil, cli.WithoutEnvironmentResolution)
model, err := opts.ToModel(ctx, dockerCli, nil, cli.WithoutEnvironmentResolution, cli.WithLoadOptions(loader.WithSkipValidation))
if err != nil {
return nil, false, err
}
Expand Down
71 changes: 71 additions & 0 deletions cmd/compose/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,77 @@ services:
"\nExpected:\n%s\nGot:\n%s", expected, actualOutput)
}

func TestExtractInterpolationVariablesFromModelAllowsTemplatedPortFields(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
cli := mocks.NewMockCli(ctrl)

dir := t.TempDir()
composePath := filepath.Join(dir, "compose.yaml")
assert.NilError(t, os.WriteFile(composePath, []byte(`
name: remote-defaults
services:
web:
image: nginx
ports:
- host_ip: "${LXKNS_ADDRESS:-127.0.0.1}"
published: "${LXKNS_PORT:-5010}"
target: 80
protocol: tcp
`), 0o600))

projectOptions := &ProjectOptions{
ConfigPaths: []string{composePath},
ProjectDir: dir,
}
info, noVariables, err := extractInterpolationVariablesFromModel(t.Context(), cli, projectOptions, []string{})
assert.NilError(t, err)
assert.Assert(t, noVariables == false)

values := map[string]string{}
for _, variable := range info {
values[variable.name] = variable.defaultValue
}
assert.Equal(t, values["LXKNS_ADDRESS"], "127.0.0.1")
assert.Equal(t, values["LXKNS_PORT"], "5010")
}

func TestRunVariablesAllowsTemplatedPortFields(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

dir := t.TempDir()
composePath := filepath.Join(dir, "compose.yaml")
assert.NilError(t, os.WriteFile(composePath, []byte(`
name: remote-defaults
services:
web:
image: nginx
ports:
- host_ip: "${LXKNS_ADDRESS:-127.0.0.1}"
published: "${LXKNS_PORT:-5010}"
target: 80
protocol: tcp
`), 0o600))

buf := new(bytes.Buffer)
cli := mocks.NewMockCli(ctrl)
cli.EXPECT().Out().Return(streams.NewOut(buf)).AnyTimes()

opts := configOptions{
Format: "json",
ProjectOptions: &ProjectOptions{
ConfigPaths: []string{composePath},
ProjectDir: dir,
},
}
assert.NilError(t, runVariables(t.Context(), cli, opts, nil))

output := buf.String()
assert.Assert(t, strings.Contains(output, `"LXKNS_ADDRESS"`), output)
assert.Assert(t, strings.Contains(output, `"LXKNS_PORT"`), output)
}

func TestConfirmRemoteIncludes(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
Expand Down
Loading