From 559a1b0ccb5f46b9e322ca553b80f73598c43c5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Thu, 24 Nov 2022 07:13:51 +0100 Subject: [PATCH] fix: avoid panics when checking container state and container.raw is nil (#635) --- docker.go | 5 ++++- docker_test.go | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/docker.go b/docker.go index 6e3b681942..9618288465 100644 --- a/docker.go +++ b/docker.go @@ -355,7 +355,10 @@ func (c *DockerContainer) Name(ctx context.Context) (string, error) { func (c *DockerContainer) State(ctx context.Context) (*types.ContainerState, error) { inspect, err := c.inspectRawContainer(ctx) if err != nil { - return c.raw.State, err + if c.raw != nil { + return c.raw.State, err + } + return nil, err } return inspect.State, nil } diff --git a/docker_test.go b/docker_test.go index d49cb05bf5..4a9bed97d2 100644 --- a/docker_test.go +++ b/docker_test.go @@ -460,6 +460,64 @@ func TestContainerTerminationResetsState(t *testing.T) { } } +func TestContainerStateAfterTermination(t *testing.T) { + createContainerFn := func(ctx context.Context) (Container, error) { + return GenericContainer(ctx, GenericContainerRequest{ + ProviderType: providerType, + ContainerRequest: ContainerRequest{ + Image: nginxAlpineImage, + ExposedPorts: []string{ + nginxDefaultPort, + }, + }, + Started: true, + }) + } + + t.Run("Nil State after termination", func(t *testing.T) { + ctx := context.Background() + nginx, err := createContainerFn(ctx) + if err != nil { + t.Fatal(err) + } + + // terminate the container before the raw state is set + err = nginx.Terminate(ctx) + if err != nil { + t.Fatal(err) + } + + state, err := nginx.State(ctx) + assert.Error(t, err, "expected error from container inspect.") + + assert.Nil(t, state, "expected nil container inspect.") + }) + + t.Run("Non-nil State after termination if raw as already set", func(t *testing.T) { + ctx := context.Background() + nginx, err := createContainerFn(ctx) + if err != nil { + t.Fatal(err) + } + + state, err := nginx.State(ctx) + assert.NoError(t, err, "unexpected error from container inspect before container termination.") + + assert.NotNil(t, state, "unexpected nil container inspect before container termination.") + + // terminate the container before the raw state is set + err = nginx.Terminate(ctx) + if err != nil { + t.Fatal(err) + } + + state, err = nginx.State(ctx) + assert.Error(t, err, "expected error from container inspect after container termination.") + + assert.NotNil(t, state, "unexpected nil container inspect after container termination.") + }) +} + func TestContainerStopWithReaper(t *testing.T) { ctx := context.Background()