Skip to content

Commit c57493a

Browse files
committed
e2e test to run provider with env variable injection
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
1 parent 84a4049 commit c57493a

File tree

5 files changed

+51
-13
lines changed

5 files changed

+51
-13
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
}

pkg/e2e/fixtures/interpolation/compose.yaml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,16 @@ services:
55
environment:
66
FOO: ${FOO}
77
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}

pkg/e2e/interpolation_test.go

Lines changed: 15 additions & 3 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,15 +29,21 @@ 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")
3643
var env []string
37-
scanner := bufio.NewScanner(strings.NewReader(res.Stdout()))
44+
out := res.Combined()
45+
fmt.Println(out)
46+
scanner := bufio.NewScanner(strings.NewReader(out))
3847
for scanner.Scan() {
3948
line := scanner.Text()
4049
if strings.HasPrefix(line, "test-1 | ") {
@@ -48,4 +57,7 @@ func Test_interpolation(t *testing.T) {
4857
assert.Check(t, slices.Contains(env, "QIX=some BAR value"))
4958
assert.Check(t, slices.Contains(env, "BAR_FROM_ENV_FILE=bar_from_environment"))
5059

60+
assert.Check(t, slices.Contains(env, "BY_PROVIDER_FROM_ENV=https://magic.cloud/example"))
61+
assert.Check(t, slices.Contains(env, "BY_PROVIDER_FROM_ENV_FILE=https://magic.cloud/example"))
62+
5163
}

0 commit comments

Comments
 (0)