From ab3a0f58d9cc2dfb1ffc982d7bd65309bf0f0d6c Mon Sep 17 00:00:00 2001 From: Steven Hartland Date: Wed, 7 Aug 2024 10:40:21 +0100 Subject: [PATCH] test: fix image label test (#2689) Fix test for image label setting which could fail if the volume already existed. --- mounts_test.go | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/mounts_test.go b/mounts_test.go index ecf91943dd..533b584feb 100644 --- a/mounts_test.go +++ b/mounts_test.go @@ -217,6 +217,15 @@ func TestMountsReceiveRyukLabels(t *testing.T) { } ctx := context.Background() + client, err := testcontainers.NewDockerClientWithOpts(ctx) + require.NoError(t, err) + defer client.Close() + + // Ensure the volume is removed before creating the container + // otherwise the volume will be reused and the labels won't be set. + err = client.VolumeRemove(ctx, "app-data", true) + require.NoError(t, err) + c, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{ ContainerRequest: req, Started: true, @@ -224,12 +233,8 @@ func TestMountsReceiveRyukLabels(t *testing.T) { require.NoError(t, err) terminateContainerOnEnd(t, ctx, c) - // Check if volume is created with the expected labels - client, err := testcontainers.NewDockerClientWithOpts(ctx) - require.NoError(t, err) - defer client.Close() - + // Check if volume is created with the expected labels. volume, err := client.VolumeInspect(ctx, "app-data") require.NoError(t, err) - assert.Equal(t, testcontainers.GenericLabels(), volume.Labels) + require.Equal(t, testcontainers.GenericLabels(), volume.Labels) }