hs-test: check container state after startup 04/41204/7
authorAdrian Villin <[email protected]>
Tue, 23 Jul 2024 10:14:19 +0000 (12:14 +0200)
committerDave Wallace <[email protected]>
Wed, 24 Jul 2024 23:41:04 +0000 (23:41 +0000)
- tests will now fail if a container exits right after startup
- fixed MirroringTest (still broken with multiple workers)

Type: test

Change-Id: I47b51c2bcf53f535aa2d06c2f5b09a9559631117
Signed-off-by: Adrian Villin <[email protected]>
extras/hs-test/docker/Dockerfile.nginx-server
extras/hs-test/infra/container.go

index 1971158..0720c6f 100644 (file)
@@ -9,4 +9,4 @@ RUN apt-get update \
 COPY resources/nginx/nginx_server_mirroring.conf /nginx.conf
 
 
-ENTRYPOINT ["nginx", "-c", "/nginx.conf"]
+ENTRYPOINT nginx -c /nginx.conf
index 46cee97..7317499 100644 (file)
@@ -14,6 +14,7 @@ import (
        "time"
 
        containerTypes "github.com/docker/docker/api/types/container"
+       "github.com/docker/docker/api/types/filters"
        "github.com/docker/docker/api/types/image"
        "github.com/docker/docker/pkg/stdcopy"
        "github.com/edwarnicke/exechelper"
@@ -179,6 +180,7 @@ func (c *Container) Create() error {
        resp, err := c.Suite.Docker.ContainerCreate(
                c.ctx,
                &containerTypes.Config{
+                       Hostname: c.Name,
                        Image: c.Image,
                        Env:   c.getEnvVars(),
                        Cmd:   strings.Split(c.ExtraRunningArgs, " "),
@@ -216,7 +218,9 @@ func (c *Container) allocateCpus() {
 // Starts a container
 func (c *Container) Start() error {
        var err error
-       for nTries := 0; nTries < 5; nTries++ {
+       var nTries int
+
+       for nTries = 0; nTries < 5; nTries++ {
                err = c.Suite.Docker.ContainerStart(c.ctx, c.ID, containerTypes.StartOptions{})
                if err == nil {
                        continue
@@ -224,6 +228,25 @@ func (c *Container) Start() error {
                c.Suite.Log("Error while starting " + c.Name + ". Retrying...")
                time.Sleep(1 * time.Second)
        }
+       if nTries >= 5 {
+               return err
+       }
+
+       // wait for container to start
+       time.Sleep(1 * time.Second)
+
+       // check if container exited right after startup
+       containers, err := c.Suite.Docker.ContainerList(c.ctx, containerTypes.ListOptions{
+               All:     true,
+               Filters: filters.NewArgs(filters.Arg("name", c.Name)),
+       })
+       if err != nil {
+               return err
+       }
+       if containers[0].State == "exited" {
+               c.Suite.Log("Container details: " + fmt.Sprint(containers[0]))
+               return fmt.Errorf("Container %s exited: '%s'", c.Name, containers[0].Status)
+       }
 
        return err
 }