Skip to content

Commit 5b599d1

Browse files
authored
fix: update docker test to log build and run output (#59)
Modifies `docker_test.go` to log the output from the Docker build step as well as the logs from the test run.
1 parent e86ee18 commit 5b599d1

File tree

2 files changed

+51
-35
lines changed

2 files changed

+51
-35
lines changed

docker-compose.yml

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,28 +22,3 @@ services:
2222
interval: 5s
2323
timeout: 10s
2424
retries: 5
25-
26-
depends_on:
27-
- db
28-
# TODO_UPNEXT(#20): add a health check that looks for component ready states from the /healthz endpoint
29-
30-
# This DB container is provided for local development. It is not recommended
31-
# for production use. Operator should set up their own secure DB instance.
32-
db:
33-
image: postgres:latest
34-
container_name: db
35-
restart: always
36-
ports:
37-
- 5432:5432
38-
volumes:
39-
# Initializes the database with the minimum required tables from the base schema.
40-
- ./db/driver/sqlc/schema.sql:/docker-entrypoint-initdb.d/init.sql
41-
# For local development, persist the database data in a .gitignore'd tmp directory.
42-
- ./tmp/db:/var/lib/postgresql/data
43-
environment:
44-
POSTGRES_PASSWORD: pgpassword
45-
POSTGRES_DB: postgres
46-
healthcheck:
47-
test: pg_isready -U postgres
48-
interval: 5s
49-
retries: 3

e2e/docker_test.go

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ import (
1818
/* -------------------- Dockertest Ephemeral PATH Container Setup -------------------- */
1919

2020
const (
21+
imageName = "path-image"
2122
containerName = "path"
2223
internalPathPort = "3000"
23-
dockerfilePath = "../Dockerfile"
24+
buildContextDir = ".."
25+
dockerfileName = "Dockerfile"
2426
configMountPoint = ":/app/.config.yaml"
2527
containerEnvImageTag = "IMAGE_TAG=test"
2628
containerExtraHost = "host.docker.internal:host-gateway" // allows the container to access the host machine's Docker daemon
@@ -77,29 +79,68 @@ func setupPathInstance(t *testing.T, configFilePath string) (containerPort strin
7779
func setupPathDocker(t *testing.T, configFilePath string) (*dockertest.Pool, *dockertest.Resource, string) {
7880
t.Helper()
7981

80-
// eg. {file_path}/path/e2e/.config.test.yaml:/app/.config.yaml
81-
containerConfigMount := filepath.Join(os.Getenv("PWD"), configFilePath) + configMountPoint
82+
// eg. {file_path}/path/e2e/.shannon.config.yaml
83+
configFilePath = filepath.Join(os.Getenv("PWD"), configFilePath)
8284

83-
opts := &dockertest.RunOptions{
84-
Name: containerName,
85-
Mounts: []string{containerConfigMount},
86-
Env: []string{containerEnvImageTag},
87-
ExposedPorts: []string{containerPortAndProtocol},
88-
ExtraHosts: []string{containerExtraHost},
85+
// Check if config file exists and exit if it does not
86+
if _, err := os.Stat(configFilePath); os.IsNotExist(err) {
87+
t.Fatalf("config file does not exist: %s", configFilePath)
8988
}
9089

90+
// eg. {file_path}/path/e2e/.shannon.config.yaml:/app/.config.yaml
91+
containerConfigMount := configFilePath + configMountPoint
92+
93+
// Initialize the dockertest pool
9194
pool, err := dockertest.NewPool("")
9295
if err != nil {
9396
t.Fatalf("Could not construct pool: %s", err)
9497
}
95-
resource, err := pool.BuildAndRunWithOptions(dockerfilePath, opts, func(config *docker.HostConfig) {
98+
99+
// Build the image and log build output
100+
buildOptions := docker.BuildImageOptions{
101+
Name: imageName,
102+
ContextDir: buildContextDir,
103+
Dockerfile: dockerfileName,
104+
OutputStream: os.Stdout,
105+
SuppressOutput: false,
106+
NoCache: false,
107+
}
108+
if err := pool.Client.BuildImage(buildOptions); err != nil {
109+
t.Fatalf("could not build path image: %s", err)
110+
}
111+
112+
// Run the built image
113+
runOpts := &dockertest.RunOptions{
114+
Name: containerName,
115+
Repository: imageName,
116+
Mounts: []string{containerConfigMount},
117+
Env: []string{containerEnvImageTag},
118+
ExposedPorts: []string{containerPortAndProtocol},
119+
ExtraHosts: []string{containerExtraHost},
120+
}
121+
resource, err := pool.RunWithOptions(runOpts, func(config *docker.HostConfig) {
96122
config.AutoRemove = true
97123
config.RestartPolicy = docker.RestartPolicy{Name: "no"}
98124
})
99125
if err != nil {
100126
t.Fatalf("Could not start resource: %s", err)
101127
}
102128

129+
// Print container logs in a goroutine to prevent blocking
130+
go func() {
131+
if err := pool.Client.Logs(docker.LogsOptions{
132+
Container: resource.Container.ID,
133+
OutputStream: os.Stdout,
134+
ErrorStream: os.Stderr,
135+
Stdout: true,
136+
Stderr: true,
137+
Follow: true,
138+
}); err != nil {
139+
fmt.Printf("could not fetch logs for PATH container: %s", err)
140+
}
141+
}()
142+
143+
// Handle termination signals
103144
c := make(chan os.Signal, 1)
104145
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
105146
go func() {

0 commit comments

Comments
 (0)