Skip to content

Commit 1f8a052

Browse files
committed
e2e test to run provider with env variable injection
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 84a4049 commit 1f8a052

File tree

10 files changed

+91
-25
lines changed

10 files changed

+91
-25
lines changed

Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ install: binary
7474
install $(or $(DESTDIR),./bin/build)/docker-compose ~/.docker/cli-plugins/docker-compose
7575

7676
.PHONY: e2e-compose
77-
e2e-compose: ## Run end to end local tests in plugin mode. Set E2E_TEST=TestName to run a single test
77+
e2e-compose: example-provider
78+
## Run end to end local tests in plugin mode. Set E2E_TEST=TestName to run a single test
7879
go run gotest.tools/gotestsum@latest --format testname --junitfile "/tmp/report/report.xml" -- -v $(TEST_FLAGS) -count=1 ./pkg/e2e
7980

8081
.PHONY: e2e-compose-standalone
@@ -156,3 +157,6 @@ pre-commit: validate check-dependencies lint build test e2e-compose
156157
help: ## Show help
157158
@echo Please specify a build target. The choices are:
158159
@grep -E '^[0-9a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
160+
161+
example-provider:
162+
go build -o bin/build/example-provider docs/examples/provider.go

docs/examples/provider.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,30 @@ func main() {
3939
}
4040
}
4141

42+
type options struct {
43+
db string
44+
size int
45+
}
46+
4247
func composeCommand() *cobra.Command {
4348
c := &cobra.Command{
4449
Use: "compose EVENT",
4550
TraverseChildren: true,
4651
}
4752
c.PersistentFlags().String("project-name", "", "compose project name") // unused
53+
54+
var options options
55+
4856
upCmd := &cobra.Command{
49-
Use: "up",
50-
Run: up,
57+
Use: "up",
58+
Run: func(_ *cobra.Command, args []string) {
59+
up(options, args)
60+
},
5161
Args: cobra.ExactArgs(1),
5262
}
53-
upCmd.Flags().String("type", "", "Database type (mysql, postgres, etc.)")
63+
upCmd.Flags().StringVar(&options.db, "type", "", "Database type (mysql, postgres, etc.)")
5464
_ = upCmd.MarkFlagRequired("type")
55-
upCmd.Flags().Int("size", 10, "Database size in GB")
65+
upCmd.Flags().IntVar(&options.size, "size", 10, "Database size in GB")
5666
upCmd.Flags().String("name", "", "Name of the database to be created")
5767
_ = upCmd.MarkFlagRequired("name")
5868

@@ -71,13 +81,13 @@ func composeCommand() *cobra.Command {
7181

7282
const lineSeparator = "\n"
7383

74-
func up(_ *cobra.Command, args []string) {
84+
func up(options options, args []string) {
7585
servicename := args[0]
7686
fmt.Printf(`{ "type": "debug", "message": "Starting %s" }%s`, servicename, lineSeparator)
7787

78-
for i := 0; i < 100; i += 10 {
88+
for i := 0; i < options.size; i++ {
7989
time.Sleep(1 * time.Second)
80-
fmt.Printf(`{ "type": "info", "message": "Processing ... %d%%" }%s`, i, lineSeparator)
90+
fmt.Printf(`{ "type": "info", "message": "Processing ... %d%%" }%s`, i*100/options.size, lineSeparator)
8191
}
8292
fmt.Printf(`{ "type": "setenv", "message": "URL=https://magic.cloud/%s" }%s`, servicename, lineSeparator)
8393
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,4 +213,4 @@ exclude (
213213
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2
214214
)
215215

216-
replace github.com/compose-spec/compose-go/v2 => github.com/ndeloof/compose-go/v2 v2.0.1-0.20250618092235-a694d81af63d
216+
replace github.com/compose-spec/compose-go/v2 => github.com/ndeloof/compose-go/v2 v2.0.1-0.20250618152329-47e2a42e5ab4

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,8 +359,8 @@ github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8m
359359
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
360360
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f h1:y5//uYreIhSUg3J1GEMiLbxo1LJaP8RfCpH6pymGZus=
361361
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f/go.mod h1:ZdcZmHo+o7JKHSa8/e818NopupXU1YMK5fe1lsApnBw=
362-
github.com/ndeloof/compose-go/v2 v2.0.1-0.20250618092235-a694d81af63d h1:fLSNfPyqfQC2uWAL9KxGmAwQm1DZaf6FoshlcJpLIcs=
363-
github.com/ndeloof/compose-go/v2 v2.0.1-0.20250618092235-a694d81af63d/go.mod h1:TmjkIB9W73fwVxkYY+u2uhMbMUakjiif79DlYgXsyvU=
362+
github.com/ndeloof/compose-go/v2 v2.0.1-0.20250618152329-47e2a42e5ab4 h1:n01lYAdi3slIjYGd2XHi9jMJrx/BUqpx/6eiiaxtnmg=
363+
github.com/ndeloof/compose-go/v2 v2.0.1-0.20250618152329-47e2a42e5ab4/go.mod h1:TmjkIB9W73fwVxkYY+u2uhMbMUakjiif79DlYgXsyvU=
364364
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
365365
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
366366
github.com/onsi/ginkgo v1.12.0 h1:Iw5WCbBcaAAd0fpRb1c9r5YCylv4XDoCSigm1zLevwU=
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
FOO=BAR
1+
FOO=FOO-from-dot-env

pkg/e2e/fixtures/interpolation/compose.yaml

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,18 @@ services:
33
image: alpine
44
command: env
55
environment:
6-
FOO: ${FOO}
7-
BAR: bar_from_environment
6+
- FOO
7+
- BAR=bar_from_environment
8+
- BY_PROVIDER_FROM_ENV=${EXAMPLE_URL}
89
env_file:
9-
- env_file.env
10+
- env_file.env
11+
depends_on:
12+
- example
13+
14+
example:
15+
provider:
16+
type: example-provider
17+
options:
18+
type: test
19+
name: example
20+
size: 0
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
ZOT=${FOO:-ZOT}
22
QIX=some ${FOO} value
3-
BAR_FROM_ENV_FILE=${BAR}
3+
BAR_FROM_ENV_FILE=${BAR}
4+
BY_PROVIDER_FROM_ENV_FILE: ${EXAMPLE_URL}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
include:
2+
- path: ../compose.yaml
3+
env_file: include.env
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
FOO=FOO-from-include

pkg/e2e/interpolation_test.go

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ package e2e
1818

1919
import (
2020
"bufio"
21+
"fmt"
22+
"os"
23+
"path/filepath"
2124
"slices"
2225
"strings"
2326
"testing"
@@ -26,26 +29,59 @@ import (
2629
)
2730

2831
func Test_interpolation(t *testing.T) {
29-
c := NewParallelCLI(t)
32+
provider, err := findExecutable("example-provider")
33+
assert.NilError(t, err)
34+
path := fmt.Sprintf("%s%s%s", os.Getenv("PATH"), string(os.PathListSeparator), filepath.Dir(provider))
35+
c := NewParallelCLI(t, WithEnv("PATH="+path))
36+
3037
const projectName = "interpolation"
3138
t.Cleanup(func() {
3239
c.cleanupWithDown(t, projectName)
3340
})
3441

35-
res := c.RunDockerComposeCmdNoCheck(t, "-f", "fixtures/interpolation/compose.yaml", "--project-name", projectName, "up")
42+
res := c.RunDockerComposeCmd(t, "-f", "fixtures/interpolation/compose.yaml", "--project-name", projectName, "up")
43+
env := getEnv(res.Combined())
44+
45+
assert.Check(t, slices.Contains(env, "FOO=FOO-from-dot-env"))
46+
assert.Check(t, slices.Contains(env, "ZOT=FOO-from-dot-env"))
47+
assert.Check(t, slices.Contains(env, "QIX=some FOO-from-dot-env value"))
48+
assert.Check(t, slices.Contains(env, "BAR_FROM_ENV_FILE=bar_from_environment"))
49+
50+
assert.Check(t, slices.Contains(env, "BY_PROVIDER_FROM_ENV=https://magic.cloud/example"))
51+
assert.Check(t, slices.Contains(env, "BY_PROVIDER_FROM_ENV_FILE=https://magic.cloud/example"))
52+
}
53+
54+
func Test_interpolationWithInclude(t *testing.T) {
55+
provider, err := findExecutable("example-provider")
56+
assert.NilError(t, err)
57+
path := fmt.Sprintf("%s%s%s", os.Getenv("PATH"), string(os.PathListSeparator), filepath.Dir(provider))
58+
c := NewParallelCLI(t, WithEnv("PATH="+path))
59+
60+
const projectName = "interpolation-include"
61+
t.Cleanup(func() {
62+
c.cleanupWithDown(t, projectName)
63+
})
64+
65+
res := c.RunDockerComposeCmd(t, "-f", "fixtures/interpolation/include/compose.yaml", "--project-name", projectName, "up")
66+
env := getEnv(res.Combined())
67+
68+
assert.Check(t, slices.Contains(env, "FOO=FOO-from-include"))
69+
assert.Check(t, slices.Contains(env, "ZOT=FOO-from-include"))
70+
assert.Check(t, slices.Contains(env, "QIX=some FOO-from-include value"))
71+
72+
assert.Check(t, slices.Contains(env, "BY_PROVIDER_FROM_ENV=https://magic.cloud/example"))
73+
assert.Check(t, slices.Contains(env, "BY_PROVIDER_FROM_ENV_FILE=https://magic.cloud/example"))
74+
}
75+
76+
func getEnv(out string) []string {
3677
var env []string
37-
scanner := bufio.NewScanner(strings.NewReader(res.Stdout()))
78+
scanner := bufio.NewScanner(strings.NewReader(out))
3879
for scanner.Scan() {
3980
line := scanner.Text()
4081
if strings.HasPrefix(line, "test-1 | ") {
4182
env = append(env, line[10:])
4283
}
4384
}
4485
slices.Sort(env)
45-
46-
assert.Check(t, slices.Contains(env, "FOO=BAR"))
47-
assert.Check(t, slices.Contains(env, "ZOT=BAR"))
48-
assert.Check(t, slices.Contains(env, "QIX=some BAR value"))
49-
assert.Check(t, slices.Contains(env, "BAR_FROM_ENV_FILE=bar_from_environment"))
50-
86+
return env
5187
}

0 commit comments

Comments
 (0)