Skip to content

Commit d1f5b20

Browse files
committed
Move service container status to a file
1 parent ceb7b21 commit d1f5b20

File tree

6 files changed

+84
-13
lines changed

6 files changed

+84
-13
lines changed

.buildkite/pipeline.trigger.integration.tests.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ for test in ${CHECK_PACKAGES_TESTS[@]}; do
4444
echo " - build/test-results/*.xml"
4545
echo " - build/elastic-stack-dump/check-*/logs/*.log"
4646
echo " - build/elastic-stack-dump/check-*/logs/fleet-server-internal/**/*"
47+
echo " - build/container-status/*.log"
4748
if [[ $test =~ with-kind$ ]]; then
4849
echo " - build/kubectl-dump.txt"
4950
fi
@@ -61,6 +62,7 @@ for package in $(find . -maxdepth 1 -mindepth 1 -type d) ; do
6162
echo " provider: \"gcp\""
6263
echo " artifact_paths:"
6364
echo " - build/test-results/*.xml"
65+
echo " - build/container-status/*.log"
6466
done
6567

6668
popd > /dev/null

internal/benchrunner/runners/system/servicedeployer/compose.go

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package servicedeployer
66

77
import (
8+
"bytes"
89
"fmt"
910
"os"
1011
"path/filepath"
@@ -73,7 +74,10 @@ func (d *DockerComposeServiceDeployer) SetUp(inCtxt ServiceContext) (DeployedSer
7374
if err != nil {
7475
return nil, fmt.Errorf("could not boot up service using Docker Compose: %w", err)
7576
}
76-
err = p.WaitForHealthy(opts)
77+
statuses, err := p.WaitForHealthy(opts)
78+
if statusErr := writeServiceContainerStatus(outCtxt.Name, statuses); statusErr != nil {
79+
logger.Errorf("failed to create container status file: %v", statusErr)
80+
}
7781
if err != nil {
7882
processServiceContainerLogs(p, compose.CommandOptions{
7983
Env: opts.Env,
@@ -205,11 +209,38 @@ func writeServiceContainerLogs(serviceName string, content []byte) error {
205209
return fmt.Errorf("can't create directory for service container logs (path: %s): %w", containerLogsDir, err)
206210
}
207211

208-
containerLogsFilepath := filepath.Join(containerLogsDir, fmt.Sprintf("%s-%d.log", serviceName, time.Now().UnixNano()))
212+
containerLogsFilepath := filepath.Join(containerLogsDir, fmt.Sprintf("benchmark-%s-%d.log", serviceName, time.Now().UnixNano()))
209213
logger.Infof("Write container logs to file: %s", containerLogsFilepath)
210214
err = os.WriteFile(containerLogsFilepath, content, 0644)
211215
if err != nil {
212216
return fmt.Errorf("can't write container logs to file (path: %s): %w", containerLogsFilepath, err)
213217
}
214218
return nil
215219
}
220+
221+
func writeServiceContainerStatus(serviceName string, content [][]byte) error {
222+
if len(content) == 0 {
223+
return nil
224+
}
225+
226+
buildDir, err := builder.BuildDirectory()
227+
if err != nil {
228+
return fmt.Errorf("locating build directory failed: %w", err)
229+
}
230+
231+
containerStatusDir := filepath.Join(buildDir, "container-status")
232+
err = os.MkdirAll(containerStatusDir, 0755)
233+
if err != nil {
234+
return fmt.Errorf("can't create directory for service container status (path: %s): %w", containerStatusDir, err)
235+
}
236+
237+
containerStatusFilepath := filepath.Join(containerStatusDir, fmt.Sprintf("benchmark-%s-%d.log", serviceName, time.Now().UnixNano()))
238+
logger.Infof("Write container status to file: %s", containerStatusFilepath)
239+
240+
contentFile := bytes.Join(content, []byte("\n"))
241+
err = os.WriteFile(containerStatusFilepath, contentFile, 0644)
242+
if err != nil {
243+
return fmt.Errorf("can't write container status to file (path: %s): %w", containerStatusFilepath, err)
244+
}
245+
return nil
246+
}

internal/compose/compose.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -314,28 +314,29 @@ func (p *Project) Logs(opts CommandOptions) ([]byte, error) {
314314
}
315315

316316
// WaitForHealthy method waits until all containers are healthy.
317-
func (p *Project) WaitForHealthy(opts CommandOptions) error {
317+
func (p *Project) WaitForHealthy(opts CommandOptions) ([][]byte, error) {
318318
// Read container IDs
319319
args := p.baseArgs()
320320
args = append(args, "ps")
321321
args = append(args, "-q")
322322

323323
var b bytes.Buffer
324324
if err := p.runDockerComposeCmd(dockerComposeOptions{args: args, env: opts.Env, stdout: &b}); err != nil {
325-
return err
325+
return nil, err
326326
}
327327

328328
startTime := time.Now()
329329
timeout := startTime.Add(waitForHealthyTimeout)
330330

331331
containerIDs := strings.Fields(b.String())
332+
containerStatuses := [][]byte{}
332333
for {
333334
if time.Now().After(timeout) {
334-
return errors.New("timeout waiting for healthy container")
335+
return containerStatuses, errors.New("timeout waiting for healthy container")
335336
}
336337

337338
if signal.SIGINT() {
338-
return errors.New("SIGINT: cancel waiting for policy assigned")
339+
return containerStatuses, errors.New("SIGINT: cancel waiting for policy assigned")
339340
}
340341

341342
// NOTE: healthy must be reinitialized at each iteration
@@ -344,11 +345,11 @@ func (p *Project) WaitForHealthy(opts CommandOptions) error {
344345
logger.Debugf("Wait for healthy containers: %s", strings.Join(containerIDs, ","))
345346
descriptions, err := docker.InspectContainers(containerIDs...)
346347
if err != nil {
347-
return err
348+
return containerStatuses, err
348349
}
349350

350351
for _, containerDescription := range descriptions {
351-
logger.Debugf("Container status: %s", containerDescription.String())
352+
containerStatuses = append(containerStatuses, []byte(containerDescription.String()))
352353

353354
// No healthcheck defined for service
354355
if containerDescription.State.Status == "running" && containerDescription.State.Health == nil {
@@ -367,7 +368,7 @@ func (p *Project) WaitForHealthy(opts CommandOptions) error {
367368

368369
// Container exited with code > 0
369370
if containerDescription.State.Status == "exited" && containerDescription.State.ExitCode > 0 {
370-
return fmt.Errorf("container (ID: %s) exited with code %d", containerDescription.ID, containerDescription.State.ExitCode)
371+
return containerStatuses, fmt.Errorf("container (ID: %s) exited with code %d", containerDescription.ID, containerDescription.State.ExitCode)
371372
}
372373

373374
// Any different status is considered unhealthy
@@ -383,7 +384,7 @@ func (p *Project) WaitForHealthy(opts CommandOptions) error {
383384
time.Sleep(waitForHealthyInterval)
384385
}
385386

386-
return nil
387+
return containerStatuses, nil
387388
}
388389

389390
func (p *Project) baseArgs() []string {

internal/testrunner/runners/system/servicedeployer/compose.go

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package servicedeployer
66

77
import (
8+
"bytes"
89
"fmt"
910
"os"
1011
"path/filepath"
@@ -89,7 +90,10 @@ func (d *DockerComposeServiceDeployer) SetUp(inCtxt ServiceContext) (DeployedSer
8990
return nil, errors.Wrap(err, "could not boot up service using Docker Compose")
9091
}
9192

92-
err = p.WaitForHealthy(opts)
93+
statuses, err := p.WaitForHealthy(opts)
94+
if statusErr := writeServiceContainerStatus(outCtxt.Name, statuses); statusErr != nil {
95+
logger.Errorf("failed to create container status file: %v", statusErr)
96+
}
9397
if err != nil {
9498
processServiceContainerLogs(p, compose.CommandOptions{
9599
Env: opts.Env,
@@ -234,3 +238,30 @@ func writeServiceContainerLogs(serviceName string, content []byte) error {
234238
}
235239
return nil
236240
}
241+
242+
func writeServiceContainerStatus(serviceName string, content [][]byte) error {
243+
if len(content) == 0 {
244+
return nil
245+
}
246+
247+
buildDir, err := builder.BuildDirectory()
248+
if err != nil {
249+
return fmt.Errorf("locating build directory failed: %w", err)
250+
}
251+
252+
containerStatusDir := filepath.Join(buildDir, "container-status")
253+
err = os.MkdirAll(containerStatusDir, 0755)
254+
if err != nil {
255+
return fmt.Errorf("can't create directory for service container status (path: %s): %w", containerStatusDir, err)
256+
}
257+
258+
containerStatusFilepath := filepath.Join(containerStatusDir, fmt.Sprintf("%s-%d.log", serviceName, time.Now().UnixNano()))
259+
logger.Infof("Write container status to file: %s", containerStatusFilepath)
260+
261+
contentFile := bytes.Join(content, []byte("\n"))
262+
err = os.WriteFile(containerStatusFilepath, contentFile, 0644)
263+
if err != nil {
264+
return fmt.Errorf("can't write container status to file (path: %s): %w", containerStatusFilepath, err)
265+
}
266+
return nil
267+
}

internal/testrunner/runners/system/servicedeployer/custom_agent.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,10 @@ func (d *CustomAgentDeployer) SetUp(inCtxt ServiceContext) (DeployedService, err
129129
return nil, errors.Wrapf(err, "can't attach service container to the stack network")
130130
}
131131

132-
err = p.WaitForHealthy(opts)
132+
statuses, err := p.WaitForHealthy(opts)
133+
if statusErr := writeServiceContainerStatus(outCtxt.Name, statuses); statusErr != nil {
134+
logger.Errorf("failed to create container status file: %v", statusErr)
135+
}
133136
if err != nil {
134137
processServiceContainerLogs(p, compose.CommandOptions{
135138
Env: opts.Env,

internal/testrunner/runners/system/servicedeployer/terraform.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,10 @@ func (tsd TerraformServiceDeployer) SetUp(inCtxt ServiceContext) (DeployedServic
148148
return nil, errors.Wrap(err, "could not boot up service using Docker Compose")
149149
}
150150

151-
err = p.WaitForHealthy(opts)
151+
statuses, err := p.WaitForHealthy(opts)
152+
if statusErr := writeServiceContainerStatus(outCtxt.Name, statuses); statusErr != nil {
153+
logger.Errorf("failed to create container status file: %v", statusErr)
154+
}
152155
if err != nil {
153156
processServiceContainerLogs(p, compose.CommandOptions{
154157
Env: opts.Env,

0 commit comments

Comments
 (0)