Skip to content

Implement test which checks env relationships #516

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

Closed
wants to merge 3 commits into from
Closed
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
4 changes: 4 additions & 0 deletions .github/workflows/releaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ jobs:
-
name: Test
run: go test -v ./...
env:
TESTCONTAINERS_RYUK_CONNECTION_TIMEOUT: 5m
TESTCONTAINERS_RYUK_RECONNECTION_TIMEOUT: 30s

- name: Validate build
run: go run .
-
Expand Down
222 changes: 222 additions & 0 deletions envs/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,22 @@
package envs

import (
"fmt"
"net/url"
"os"
"path/filepath"
"strings"

"context"

"github.com/docker/compose/v2/pkg/api"
docker "github.com/docker/docker/client"
"github.com/mitchellh/go-homedir"
"github.com/stretchr/testify/require"
"github.com/symfony-cli/symfony-cli/local/platformsh"

tc "github.com/testcontainers/testcontainers-go/modules/compose"
"github.com/testcontainers/testcontainers-go/wait"
. "gopkg.in/check.v1"
)

Expand Down Expand Up @@ -98,3 +109,214 @@ func (s *LocalSuite) TestRelationships(c *C) {
"PGHOST": "127.0.0.1",
})
}

func (s *LocalSuite) TestGenericContainer(c *C) {
os.Setenv("COMPOSE_PROJECT_NAME", "generic")
identifier := tc.StackIdentifier("generic")
compose, err := tc.NewDockerComposeWith(tc.WithStackFiles("./testdata/local_project/generic_compose.yaml"), identifier)
require.NoError(c, err, "NewDockerComposeAPIWith()")

ctx := context.Background()

require.NoError(c, compose.Up(ctx, tc.WithRecreate(api.RecreateNever), tc.Wait(true)), "compose.Up()")

compose.WaitForService("generic", wait.ForListeningPort("3000/tcp"))

host := os.Getenv(docker.EnvOverrideHost)
if host == "" || strings.HasPrefix(host, "unix://") {
host = "127.0.0.1"
} else {
u, err := url.Parse(host)
if err != nil {
host = "127.0.0.1"
} else {
host = u.Hostname()
}
}

container, err := compose.ServiceContainer(ctx, "generic")
if err != nil {
c.Errorf("Could not get service with name %s", "generic")
c.FailNow()
}

mappedPort, err := container.MappedPort(ctx, "3000/tcp")

if err != nil {
c.Errorf("Could not get mapped port of container: %s", err)
c.FailNow()
}

c.Assert(extractRelationshipsEnvs(&Local{}), DeepEquals, Envs{
"GENERIC_HOST": host,
"GENERIC_IP": host,
"GENERIC_PORT": mappedPort.Port(),
"GENERIC_URL": fmt.Sprintf("tcp://127.0.0.1:%s", mappedPort.Port()),
"GENERIC_SCHEME": "tcp",
})

defer func() {
require.NoError(c, compose.Down(context.Background(), tc.RemoveOrphans(true), tc.RemoveImagesLocal), "compose.Down()")
}()
}

func (s *LocalSuite) TestGenericContainerWithServicePrefix(c *C) {
os.Setenv("COMPOSE_PROJECT_NAME", "genericcustom")
identifier := tc.StackIdentifier("genericcustom")
compose, err := tc.NewDockerComposeWith(tc.WithStackFiles("./testdata/local_project/generic_service_compose.yaml"), identifier)
require.NoError(c, err, "NewDockerComposeAPIWith()")

ctx := context.Background()

require.NoError(c, compose.Up(ctx, tc.WithRecreate(api.RecreateNever), tc.Wait(true)), "compose.Up()")

compose.WaitForService("generic", wait.ForListeningPort("3000/tcp"))

host := os.Getenv(docker.EnvOverrideHost)
if host == "" || strings.HasPrefix(host, "unix://") {
host = "127.0.0.1"
} else {
u, err := url.Parse(host)
if err != nil {
host = "127.0.0.1"
} else {
host = u.Hostname()
}
}

container, err := compose.ServiceContainer(ctx, "generic")
if err != nil {
c.Errorf("Could not get service with name %s", "generic")
c.FailNow()
}

mappedPort, err := container.MappedPort(ctx, "3000/tcp")

if err != nil {
c.Errorf("Could not get mapped port of container: %s", err)
c.FailNow()
}

c.Assert(extractRelationshipsEnvs(&Local{}), DeepEquals, Envs{
"CUSTOM_HOST": host,
"CUSTOM_IP": host,
"CUSTOM_PORT": mappedPort.Port(),
"CUSTOM_URL": fmt.Sprintf("tcp://127.0.0.1:%s", mappedPort.Port()),
"CUSTOM_SCHEME": "tcp",
})

defer func() {
require.NoError(c, compose.Down(context.Background(), tc.RemoveOrphans(true), tc.RemoveImagesLocal), "compose.Down()")
}()
}

func (s *LocalSuite) TestRedisContainer(c *C) {

os.Setenv("COMPOSE_PROJECT_NAME", "redis")
identifier := tc.StackIdentifier("redis")
compose, err := tc.NewDockerComposeWith(tc.WithStackFiles("./testdata/local_project/redis_compose.yaml"), identifier)
require.NoError(c, err, "NewDockerComposeAPIWith()")

ctx := context.Background()

require.NoError(c, compose.Up(ctx, tc.WithRecreate(api.RecreateNever), tc.Wait(true)), "compose.Up()")

compose.WaitForService("redis", wait.ForListeningPort("6379/tcp"))

host := os.Getenv(docker.EnvOverrideHost)
if host == "" || strings.HasPrefix(host, "unix://") {
host = "127.0.0.1"
} else {
u, err := url.Parse(host)
if err != nil {
host = "127.0.0.1"
} else {
host = u.Hostname()
}
}

container, err := compose.ServiceContainer(ctx, "redis")
if err != nil {
c.Errorf("Could not get service with name %s", "redis")
c.FailNow()
}

mappedPort, err := container.MappedPort(ctx, "6379/tcp")

if err != nil {
c.Errorf("Could not get mapped port of container: %s", err)
c.FailNow()
}

c.Assert(extractRelationshipsEnvs(&Local{}), DeepEquals, Envs{
"REDIS_HOST": host,
"REDIS_PORT": mappedPort.Port(),
"REDIS_URL": fmt.Sprintf("redis://127.0.0.1:%s", mappedPort.Port()),
"REDIS_SCHEME": "redis",
})

defer func() {
require.NoError(c, compose.Down(context.Background(), tc.RemoveOrphans(true), tc.RemoveImagesLocal), "compose.Down()")
}()
}

func (s *LocalSuite) TestDatabaseContainer(c *C) {
os.Setenv("COMPOSE_PROJECT_NAME", "postgres")
identifier := tc.StackIdentifier("postgres")
compose, err := tc.NewDockerComposeWith(tc.WithStackFiles("./testdata/local_project/postgres_compose.yaml"), identifier)
require.NoError(c, err, "NewDockerComposeAPIWith()")

ctx := context.Background()

require.NoError(c, compose.Up(ctx, tc.WithRecreate(api.RecreateNever), tc.Wait(true)), "compose.Up()")

compose.WaitForService("database", wait.ForListeningPort("5432/tcp"))

host := os.Getenv(docker.EnvOverrideHost)
if host == "" || strings.HasPrefix(host, "unix://") {
host = "127.0.0.1"
} else {
u, err := url.Parse(host)
if err != nil {
host = "127.0.0.1"
} else {
host = u.Hostname()
}
}

container, err := compose.ServiceContainer(ctx, "database")
if err != nil {
c.Errorf("Could not get service with name %s", "database")
c.FailNow()
}

mappedPort, err := container.MappedPort(ctx, "5432/tcp")

if err != nil {
c.Errorf("Could not get mapped port of container: %s", err)
c.FailNow()
}

c.Assert(extractRelationshipsEnvs(&Local{}), DeepEquals, Envs{
"PGHOST": host,
"PGUSER": "app",
"PGDATABASE": "app",
"PGPASSWORD": "password",
"PGPORT": mappedPort.Port(),
"DATABASE_DRIVER": "postgres",
"DATABASE_VERSION": "16.3",
"DATABASE_USERNAME": "app",
"DATABASE_USER": "app",
"DATABASE_NAME": "app",
"DATABASE_DATABASE": "app",
"DATABASE_PASSWORD": "password",
"DATABASE_SERVER": fmt.Sprintf("postgres://127.0.0.1:%s", mappedPort.Port()),
"DATABASE_HOST": host,
"DATABASE_PORT": mappedPort.Port(),
"DATABASE_URL": fmt.Sprintf("postgres://%s:%s@127.0.0.1:%s/%s%s", "app", "password", mappedPort.Port(), "app", "?sslmode=disable&charset=utf8&serverVersion=16.3"),
})

defer func() {
require.NoError(c, compose.Down(context.Background(), tc.RemoveOrphans(true), tc.RemoveImagesLocal), "compose.Down()")
}()
}
5 changes: 5 additions & 0 deletions envs/testdata/local_project/generic_compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
services:
generic:
image: "traefik:v3.1"
ports:
- 3000/tcp
7 changes: 7 additions & 0 deletions envs/testdata/local_project/generic_service_compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
services:
generic:
image: "traefik:v3.1"
ports:
- 3000/tcp
labels:
com.symfony.server.service-prefix: CUSTOM
8 changes: 8 additions & 0 deletions envs/testdata/local_project/postgres_compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
services:
database:
image: postgres:16.3-alpine
ports:
- 5432/tcp
environment:
POSTGRES_USER: app
POSTGRES_PASSWORD: password
5 changes: 5 additions & 0 deletions envs/testdata/local_project/redis_compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
services:
redis:
image: redis:latest
ports:
- 6379/tcp
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy