2023-01-30 09:20:08 +00:00
|
|
|
package dockertestutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
|
|
|
|
"github.com/ory/dockertest/v3"
|
|
|
|
"github.com/ory/dockertest/v3/docker"
|
|
|
|
)
|
|
|
|
|
|
|
|
const filePerm = 0o644
|
|
|
|
|
|
|
|
func SaveLog(
|
|
|
|
pool *dockertest.Pool,
|
|
|
|
resource *dockertest.Resource,
|
|
|
|
basePath string,
|
2024-09-11 10:00:32 +00:00
|
|
|
) (string, string, error) {
|
2023-01-30 09:20:08 +00:00
|
|
|
err := os.MkdirAll(basePath, os.ModePerm)
|
|
|
|
if err != nil {
|
2024-09-11 10:00:32 +00:00
|
|
|
return "", "", err
|
2023-01-30 09:20:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var stdout bytes.Buffer
|
|
|
|
var stderr bytes.Buffer
|
|
|
|
|
|
|
|
err = pool.Client.Logs(
|
|
|
|
docker.LogsOptions{
|
|
|
|
Context: context.TODO(),
|
|
|
|
Container: resource.Container.ID,
|
|
|
|
OutputStream: &stdout,
|
|
|
|
ErrorStream: &stderr,
|
|
|
|
Tail: "all",
|
|
|
|
RawTerminal: false,
|
|
|
|
Stdout: true,
|
|
|
|
Stderr: true,
|
|
|
|
Follow: false,
|
|
|
|
Timestamps: false,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
2024-09-11 10:00:32 +00:00
|
|
|
return "", "", err
|
2023-01-30 09:20:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Saving logs for %s to %s\n", resource.Container.Name, basePath)
|
|
|
|
|
2024-09-11 10:00:32 +00:00
|
|
|
stdoutPath := path.Join(basePath, resource.Container.Name+".stdout.log")
|
2023-01-30 09:20:08 +00:00
|
|
|
err = os.WriteFile(
|
2024-09-11 10:00:32 +00:00
|
|
|
stdoutPath,
|
2023-01-30 09:20:08 +00:00
|
|
|
stdout.Bytes(),
|
|
|
|
filePerm,
|
|
|
|
)
|
|
|
|
if err != nil {
|
2024-09-11 10:00:32 +00:00
|
|
|
return "", "", err
|
2023-01-30 09:20:08 +00:00
|
|
|
}
|
|
|
|
|
2024-09-11 10:00:32 +00:00
|
|
|
stderrPath := path.Join(basePath, resource.Container.Name+".stderr.log")
|
2023-01-30 09:20:08 +00:00
|
|
|
err = os.WriteFile(
|
2024-09-11 10:00:32 +00:00
|
|
|
stderrPath,
|
2023-01-30 09:20:08 +00:00
|
|
|
stderr.Bytes(),
|
|
|
|
filePerm,
|
|
|
|
)
|
|
|
|
if err != nil {
|
2024-09-11 10:00:32 +00:00
|
|
|
return "", "", err
|
2023-01-30 09:20:08 +00:00
|
|
|
}
|
|
|
|
|
2024-09-11 10:00:32 +00:00
|
|
|
return stdoutPath, stderrPath, nil
|
2023-01-30 09:20:08 +00:00
|
|
|
}
|