Skip to content

Commit 483f2f9

Browse files
authored
Ensure docker-compose runs down process if any error setting up service or agents (#3207)
Ensures that the tear down process (specially docker-compose down command) is executed if there is any failure in the SetUp process of Service and Agent deployers based on docker-compose. Running this tear down process ensures that there are no stale docker resources, e.g. docker networks, if containers failed to start.
1 parent 5685a34 commit 483f2f9

File tree

5 files changed

+71
-13
lines changed

5 files changed

+71
-13
lines changed

‎internal/agentdeployer/agent.go‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,26 @@ func (d *DockerComposeAgentDeployer) SetUp(ctx context.Context, agentInfo AgentI
177177
ExtraArgs: []string{"--build", "-d"},
178178
}
179179

180+
defer func() {
181+
if err == nil {
182+
return
183+
}
184+
// If running with --setup or --tear-down flags or a regular test system execution,
185+
// force to tear down the service in case of setup error.
186+
if d.runTestsOnly {
187+
// In case of running only tests (--no-provision flag), container logs are still useful for debugging.
188+
processAgentContainerLogs(context.WithoutCancel(ctx), p, compose.CommandOptions{
189+
Env: opts.Env,
190+
}, agentInfo.Name)
191+
logger.Debug("Skipping tearing down service due to runTestsOnly flag")
192+
return
193+
}
194+
logger.Debug("Tearing down service due to setup error")
195+
// Update svcInfo with the latest info before tearing down
196+
agent.agentInfo = agentInfo
197+
agent.TearDown(context.WithoutCancel(ctx))
198+
}()
199+
180200
if d.runTestsOnly || d.runTearDown {
181201
logger.Debug("Skipping bringing up docker-compose project and connect container to network (non setup steps)")
182202
} else {
@@ -194,9 +214,6 @@ func (d *DockerComposeAgentDeployer) SetUp(ctx context.Context, agentInfo AgentI
194214
// requires to be connected the service to the stack network
195215
err = p.WaitForHealthy(ctx, opts)
196216
if err != nil {
197-
processAgentContainerLogs(ctx, p, compose.CommandOptions{
198-
Env: opts.Env,
199-
}, agentName)
200217
return nil, fmt.Errorf("service is unhealthy: %w", err)
201218
}
202219

‎internal/cobraext/flags.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ const (
275275
TearDownFlagDescription = "trigger just the tear-down phase of testing"
276276

277277
NoProvisionFlagName = "no-provision"
278-
NoProvisionFlagDescription = "trigger just system tests wihout setup nor teardown"
278+
NoProvisionFlagDescription = "trigger just system tests without setup nor teardown"
279279

280280
UpdateScriptTestArchiveFlagName = "update"
281281
UpdateScriptTestArchiveFlagDescription = "update archive file if a cmp fails"

‎internal/servicedeployer/compose.go‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,26 @@ func (d *DockerComposeServiceDeployer) SetUp(ctx context.Context, svcInfo Servic
118118
ExtraArgs: []string{"--build", "-d"},
119119
}
120120

121+
defer func() {
122+
if err == nil {
123+
return
124+
}
125+
// If running with --setup or --tear-down flags or a regular test system execution,
126+
// force to tear down the service in case of setup error.
127+
if d.runTestsOnly {
128+
// In case of running only tests (--no-provision flag), container logs are still useful for debugging.
129+
processServiceContainerLogs(context.WithoutCancel(ctx), p, compose.CommandOptions{
130+
Env: opts.Env,
131+
}, svcInfo.Name)
132+
logger.Debug("Skipping tearing down service due to runTestsOnly flag")
133+
return
134+
}
135+
logger.Debug("Tearing down service due to setup error")
136+
// Update svcInfo with the latest info before tearing down
137+
service.svcInfo = svcInfo
138+
service.TearDown(context.WithoutCancel(ctx))
139+
}()
140+
121141
serviceName := svcInfo.Name
122142
if d.runTearDown || d.runTestsOnly {
123143
logger.Debug("Skipping bringing up docker-compose custom agent project")
@@ -130,9 +150,6 @@ func (d *DockerComposeServiceDeployer) SetUp(ctx context.Context, svcInfo Servic
130150

131151
err = p.WaitForHealthy(ctx, opts)
132152
if err != nil {
133-
processServiceContainerLogs(context.WithoutCancel(ctx), p, compose.CommandOptions{
134-
Env: opts.Env,
135-
}, svcInfo.Name)
136153
return nil, fmt.Errorf("service is unhealthy: %w", err)
137154
}
138155

‎internal/servicedeployer/custom_agent.go‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,26 @@ func (d *CustomAgentDeployer) SetUp(ctx context.Context, svcInfo ServiceInfo) (D
156156
ExtraArgs: []string{"--build", "-d"},
157157
}
158158

159+
defer func() {
160+
if err == nil {
161+
return
162+
}
163+
// If running with --setup or --tear-down flags or a regular test system execution,
164+
// force to tear down the service in case of setup error.
165+
if d.runTestsOnly {
166+
// In case of running only tests (--no-provision flag), container logs are still useful for debugging.
167+
processServiceContainerLogs(context.WithoutCancel(ctx), p, compose.CommandOptions{
168+
Env: opts.Env,
169+
}, svcInfo.Name)
170+
logger.Debug("Skipping tearing down service due to runTestsOnly flag")
171+
return
172+
}
173+
logger.Debug("Tearing down service due to setup error")
174+
// Update svcInfo with the latest info before tearing down
175+
service.svcInfo = svcInfo
176+
service.TearDown(context.WithoutCancel(ctx))
177+
}()
178+
159179
if d.runTestsOnly || d.runTearDown {
160180
logger.Debug("Skipping bringing up docker-compose project and connect container to network (non setup steps)")
161181
} else {
@@ -177,9 +197,6 @@ func (d *CustomAgentDeployer) SetUp(ctx context.Context, svcInfo ServiceInfo) (D
177197
// requires to be connected the service to the stack network
178198
err = p.WaitForHealthy(ctx, opts)
179199
if err != nil {
180-
processServiceContainerLogs(ctx, p, compose.CommandOptions{
181-
Env: opts.Env,
182-
}, svcInfo.Name)
183200
return nil, fmt.Errorf("service is unhealthy: %w", err)
184201
}
185202

‎internal/servicedeployer/terraform.go‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,16 @@ func (tsd TerraformServiceDeployer) SetUp(ctx context.Context, svcInfo ServiceIn
147147
return nil, fmt.Errorf("can't build Terraform aliases: %w", err)
148148
}
149149

150+
defer func() {
151+
if err == nil {
152+
return
153+
}
154+
logger.Debug("Tearing down service due to setup error")
155+
// Update svcInfo with the latest info before tearing down
156+
service.svcInfo = svcInfo
157+
service.TearDown(context.WithoutCancel(ctx))
158+
}()
159+
150160
// Boot up service
151161
opts = compose.CommandOptions{
152162
Env: service.env,
@@ -159,9 +169,6 @@ func (tsd TerraformServiceDeployer) SetUp(ctx context.Context, svcInfo ServiceIn
159169

160170
err = p.WaitForHealthy(ctx, opts)
161171
if err != nil {
162-
processServiceContainerLogs(ctx, p, compose.CommandOptions{
163-
Env: opts.Env,
164-
}, svcInfo.Name)
165172
//lint:ignore ST1005 error starting with product name can be capitalized
166173
return nil, fmt.Errorf("Terraform deployer is unhealthy: %w", err)
167174
}

0 commit comments

Comments
 (0)