2020-09-22 05:16:05 -04:00
|
|
|
package stack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/cli/cli/compose/loader"
|
|
|
|
composetypes "github.com/docker/cli/cli/compose/types"
|
|
|
|
"github.com/docker/cli/internal/test"
|
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestConfigWithEmptyComposeFile(t *testing.T) {
|
|
|
|
cmd := newConfigCommand(test.NewFakeCli(&fakeClient{}))
|
|
|
|
cmd.SetOut(io.Discard)
|
|
|
|
|
2024-04-26 14:16:51 -04:00
|
|
|
assert.ErrorContains(t, cmd.Execute(), `Specify a Compose file`)
|
2020-09-22 05:16:05 -04:00
|
|
|
}
|
|
|
|
|
2022-11-16 19:11:06 -05:00
|
|
|
func TestConfigMergeInterpolation(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
skipInterpolation bool
|
|
|
|
fileOne string
|
|
|
|
fileTwo string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "With Interpolation",
|
|
|
|
skipInterpolation: false,
|
|
|
|
fileOne: `version: "3.7"
|
2020-09-22 05:16:05 -04:00
|
|
|
services:
|
|
|
|
foo:
|
|
|
|
image: busybox:latest
|
|
|
|
command: cat file1.txt
|
|
|
|
`,
|
2022-11-16 19:11:06 -05:00
|
|
|
fileTwo: `version: "3.7"
|
2020-09-22 05:16:05 -04:00
|
|
|
services:
|
|
|
|
foo:
|
|
|
|
image: busybox:${VERSION}
|
|
|
|
command: cat file2.txt
|
|
|
|
`,
|
2022-11-16 19:11:06 -05:00
|
|
|
expected: `version: "3.7"
|
2020-09-22 05:16:05 -04:00
|
|
|
services:
|
|
|
|
foo:
|
|
|
|
command:
|
|
|
|
- cat
|
|
|
|
- file2.txt
|
|
|
|
image: busybox:1.0
|
|
|
|
`,
|
2022-11-16 19:11:06 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Without Interpolation",
|
|
|
|
skipInterpolation: true,
|
|
|
|
fileOne: `version: "3.7"
|
2020-09-22 05:16:05 -04:00
|
|
|
services:
|
|
|
|
foo:
|
|
|
|
image: busybox:latest
|
|
|
|
command: cat file1.txt
|
|
|
|
`,
|
2022-11-16 19:11:06 -05:00
|
|
|
fileTwo: `version: "3.7"
|
2020-09-22 05:16:05 -04:00
|
|
|
services:
|
|
|
|
foo:
|
|
|
|
image: busybox:${VERSION}
|
|
|
|
command: cat file2.txt
|
|
|
|
`,
|
2022-11-16 19:11:06 -05:00
|
|
|
expected: `version: "3.7"
|
2020-09-22 05:16:05 -04:00
|
|
|
services:
|
|
|
|
foo:
|
|
|
|
command:
|
|
|
|
- cat
|
|
|
|
- file2.txt
|
|
|
|
image: busybox:${VERSION}
|
|
|
|
`,
|
2022-11-16 19:11:06 -05:00
|
|
|
},
|
|
|
|
}
|
2020-09-22 05:16:05 -04:00
|
|
|
|
2022-11-16 19:11:06 -05:00
|
|
|
for _, tc := range tests {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
firstConfigData, err := loader.ParseYAML([]byte(tc.fileOne))
|
|
|
|
assert.Check(t, err)
|
|
|
|
secondConfigData, err := loader.ParseYAML([]byte(tc.fileTwo))
|
|
|
|
assert.Check(t, err)
|
2020-09-22 05:16:05 -04:00
|
|
|
|
2022-11-16 19:11:06 -05:00
|
|
|
actual, err := outputConfig(composetypes.ConfigDetails{
|
2020-09-22 05:16:05 -04:00
|
|
|
ConfigFiles: []composetypes.ConfigFile{
|
|
|
|
{Config: firstConfigData, Filename: "firstConfig"},
|
|
|
|
{Config: secondConfigData, Filename: "secondConfig"},
|
|
|
|
},
|
2022-11-16 19:11:06 -05:00
|
|
|
Environment: map[string]string{
|
|
|
|
"VERSION": "1.0",
|
|
|
|
},
|
|
|
|
}, tc.skipInterpolation)
|
|
|
|
assert.Check(t, err)
|
|
|
|
assert.Equal(t, tc.expected, actual)
|
2020-09-22 05:16:05 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|