test: replace context.Background()/context.TODO() with t.Context()

Replace manual context creation with t.Context() which is automatically
cancelled when the test completes.

Go 1.24 modernization pattern.

Assisted-By: cagent
Signed-off-by: David Gageot <david.gageot@docker.com>
This commit is contained in:
David Gageot
2026-01-20 10:30:31 +01:00
committed by Guillaume Lours
parent b92b87dd9c
commit 093205121c
19 changed files with 56 additions and 96 deletions

View File

@@ -18,7 +18,6 @@ package compose
import (
"bytes"
"context"
"fmt"
"io"
"os"
@@ -244,16 +243,11 @@ services:
}
// Set up the context with necessary environment variables
ctx := context.Background()
_ = os.Setenv("TEST_VAR", "test-value")
_ = os.Setenv("API_KEY", "123456")
defer func() {
_ = os.Unsetenv("TEST_VAR")
_ = os.Unsetenv("API_KEY")
}()
t.Setenv("TEST_VAR", "test-value")
t.Setenv("API_KEY", "123456")
// Extract variables from the model
info, noVariables, err := extractInterpolationVariablesFromModel(ctx, cli, projectOptions, []string{})
info, noVariables, err := extractInterpolationVariablesFromModel(t.Context(), cli, projectOptions, []string{})
require.NoError(t, err)
require.False(t, noVariables)

View File

@@ -17,7 +17,6 @@
package desktop
import (
"context"
"os"
"testing"
"time"
@@ -34,9 +33,6 @@ func TestClientPing(t *testing.T) {
t.Skip("Skipping - COMPOSE_TEST_DESKTOP_ENDPOINT not defined")
}
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
client := NewClient(desktopEndpoint)
t.Cleanup(func() {
_ = client.Close()
@@ -44,7 +40,7 @@ func TestClientPing(t *testing.T) {
now := time.Now()
ret, err := client.Ping(ctx)
ret, err := client.Ping(t.Context())
require.NoError(t, err)
serverTime := time.Unix(0, ret.ServerTime)

View File

@@ -17,7 +17,6 @@
package compose
import (
"context"
"fmt"
"strings"
"testing"
@@ -95,7 +94,7 @@ func TestServiceLinks(t *testing.T) {
c := testContainer("db", dbContainerName, false)
apiClient.EXPECT().ContainerList(gomock.Any(), containerListOptions).Return([]container.Summary{c}, nil)
links, err := tested.(*composeService).getLinks(context.Background(), testProject, s, 1)
links, err := tested.(*composeService).getLinks(t.Context(), testProject, s, 1)
assert.NilError(t, err)
assert.Equal(t, len(links), 3)
@@ -118,7 +117,7 @@ func TestServiceLinks(t *testing.T) {
c := testContainer("db", dbContainerName, false)
apiClient.EXPECT().ContainerList(gomock.Any(), containerListOptions).Return([]container.Summary{c}, nil)
links, err := tested.(*composeService).getLinks(context.Background(), testProject, s, 1)
links, err := tested.(*composeService).getLinks(t.Context(), testProject, s, 1)
assert.NilError(t, err)
assert.Equal(t, len(links), 3)
@@ -141,7 +140,7 @@ func TestServiceLinks(t *testing.T) {
c := testContainer("db", dbContainerName, false)
apiClient.EXPECT().ContainerList(gomock.Any(), containerListOptions).Return([]container.Summary{c}, nil)
links, err := tested.(*composeService).getLinks(context.Background(), testProject, s, 1)
links, err := tested.(*composeService).getLinks(t.Context(), testProject, s, 1)
assert.NilError(t, err)
assert.Equal(t, len(links), 3)
@@ -165,7 +164,7 @@ func TestServiceLinks(t *testing.T) {
c := testContainer("db", dbContainerName, false)
apiClient.EXPECT().ContainerList(gomock.Any(), containerListOptions).Return([]container.Summary{c}, nil)
links, err := tested.(*composeService).getLinks(context.Background(), testProject, s, 1)
links, err := tested.(*composeService).getLinks(t.Context(), testProject, s, 1)
assert.NilError(t, err)
assert.Equal(t, len(links), 4)
@@ -202,7 +201,7 @@ func TestServiceLinks(t *testing.T) {
}
apiClient.EXPECT().ContainerList(gomock.Any(), containerListOptionsOneOff).Return([]container.Summary{c}, nil)
links, err := tested.(*composeService).getLinks(context.Background(), testProject, s, 1)
links, err := tested.(*composeService).getLinks(t.Context(), testProject, s, 1)
assert.NilError(t, err)
assert.Equal(t, len(links), 3)
@@ -233,7 +232,7 @@ func TestWaitDependencies(t *testing.T) {
"db": {Condition: ServiceConditionRunningOrHealthy},
"redis": {Condition: ServiceConditionRunningOrHealthy},
}
assert.NilError(t, tested.(*composeService).waitDependencies(context.Background(), &project, "", dependencies, nil, 0))
assert.NilError(t, tested.(*composeService).waitDependencies(t.Context(), &project, "", dependencies, nil, 0))
})
t.Run("should skip dependencies with condition service_started", func(t *testing.T) {
dbService := types.ServiceConfig{Name: "db", Scale: intPtr(1)}
@@ -246,7 +245,7 @@ func TestWaitDependencies(t *testing.T) {
"db": {Condition: types.ServiceConditionStarted, Required: true},
"redis": {Condition: types.ServiceConditionStarted, Required: true},
}
assert.NilError(t, tested.(*composeService).waitDependencies(context.Background(), &project, "", dependencies, nil, 0))
assert.NilError(t, tested.(*composeService).waitDependencies(t.Context(), &project, "", dependencies, nil, 0))
})
}
@@ -260,7 +259,7 @@ func TestIsServiceHealthy(t *testing.T) {
assert.NilError(t, err)
cli.EXPECT().Client().Return(apiClient).AnyTimes()
ctx := context.Background()
ctx := t.Context()
t.Run("disabled healthcheck with fallback to running", func(t *testing.T) {
containerID := "test-container-id"
@@ -475,7 +474,7 @@ func TestCreateMobyContainer(t *testing.T) {
Aliases: []string{"bork-test-0"},
}))
_, err = tested.(*composeService).createMobyContainer(context.Background(), &project, service, "test", 0, nil, createOptions{
_, err = tested.(*composeService).createMobyContainer(t.Context(), &project, service, "test", 0, nil, createOptions{
Labels: make(types.Labels),
})
assert.NilError(t, err)
@@ -561,7 +560,7 @@ func TestCreateMobyContainer(t *testing.T) {
NetworkSettings: &container.NetworkSettings{},
}, nil)
_, err = tested.(*composeService).createMobyContainer(context.Background(), &project, service, "test", 0, nil, createOptions{
_, err = tested.(*composeService).createMobyContainer(t.Context(), &project, service, "test", 0, nil, createOptions{
Labels: make(types.Labels),
})
assert.NilError(t, err)

View File

@@ -17,7 +17,6 @@
package compose
import (
"context"
"os"
"path/filepath"
"sort"
@@ -164,7 +163,7 @@ func TestBuildContainerMountOptions(t *testing.T) {
}
mock.EXPECT().ImageInspect(gomock.Any(), "myProject-myService").AnyTimes().Return(image.InspectResponse{}, nil)
mounts, err := s.buildContainerMountOptions(context.TODO(), project, project.Services["myService"], inherit)
mounts, err := s.buildContainerMountOptions(t.Context(), project, project.Services["myService"], inherit)
sort.Slice(mounts, func(i, j int) bool {
return mounts[i].Target < mounts[j].Target
})
@@ -176,7 +175,7 @@ func TestBuildContainerMountOptions(t *testing.T) {
assert.Equal(t, mounts[2].VolumeOptions.Subpath, "etc")
assert.Equal(t, mounts[3].Target, "\\\\.\\pipe\\docker_engine")
mounts, err = s.buildContainerMountOptions(context.TODO(), project, project.Services["myService"], inherit)
mounts, err = s.buildContainerMountOptions(t.Context(), project, project.Services["myService"], inherit)
sort.Slice(mounts, func(i, j int) bool {
return mounts[i].Target < mounts[j].Target
})
@@ -435,7 +434,7 @@ volumes:
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p, err := composeloader.LoadWithContext(context.TODO(), composetypes.ConfigDetails{
p, err := composeloader.LoadWithContext(t.Context(), composetypes.ConfigDetails{
ConfigFiles: []composetypes.ConfigFile{
{
Filename: "test",
@@ -448,7 +447,7 @@ volumes:
})
assert.NilError(t, err)
s := &composeService{}
binds, mounts, err := s.buildContainerVolumes(context.TODO(), *p, p.Services["test"], nil)
binds, mounts, err := s.buildContainerVolumes(t.Context(), *p, p.Services["test"], nil)
assert.NilError(t, err)
assert.DeepEqual(t, tt.binds, binds)
assert.DeepEqual(t, tt.mounts, mounts)

View File

@@ -71,9 +71,6 @@ func TestTraversalWithMultipleParents(t *testing.T) {
project.Services[name] = svc
}
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
svc := make(chan string, 10)
seen := make(map[string]int)
done := make(chan struct{})
@@ -84,7 +81,7 @@ func TestTraversalWithMultipleParents(t *testing.T) {
done <- struct{}{}
}()
err := InDependencyOrder(ctx, &project, func(ctx context.Context, service string) error {
err := InDependencyOrder(t.Context(), &project, func(ctx context.Context, service string) error {
svc <- service
return nil
})
@@ -99,11 +96,8 @@ func TestTraversalWithMultipleParents(t *testing.T) {
}
func TestInDependencyUpCommandOrder(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
var order []string
err := InDependencyOrder(ctx, createTestProject(), func(ctx context.Context, service string) error {
err := InDependencyOrder(t.Context(), createTestProject(), func(ctx context.Context, service string) error {
order = append(order, service)
return nil
})
@@ -112,11 +106,8 @@ func TestInDependencyUpCommandOrder(t *testing.T) {
}
func TestInDependencyReverseDownCommandOrder(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
var order []string
err := InReverseDependencyOrder(ctx, createTestProject(), func(ctx context.Context, service string) error {
err := InReverseDependencyOrder(t.Context(), createTestProject(), func(ctx context.Context, service string) error {
order = append(order, service)
return nil
})
@@ -429,7 +420,7 @@ func TestWith_RootNodesAndUp(t *testing.T) {
return nil
})
WithRootNodesAndDown(tt.nodes)(gt)
err := gt.visit(context.TODO(), graph)
err := gt.visit(t.Context(), graph)
assert.NilError(t, err)
sort.Strings(visited)
assert.DeepEqual(t, tt.want, visited)

View File

@@ -17,7 +17,6 @@
package compose
import (
"context"
"fmt"
"os"
"strings"
@@ -90,7 +89,7 @@ func TestDown(t *testing.T) {
api.EXPECT().NetworkRemove(gomock.Any(), "abc123").Return(nil)
api.EXPECT().NetworkRemove(gomock.Any(), "def456").Return(nil)
err = tested.Down(context.Background(), strings.ToLower(testProject), compose.DownOptions{})
err = tested.Down(t.Context(), strings.ToLower(testProject), compose.DownOptions{})
assert.NilError(t, err)
}
@@ -139,7 +138,7 @@ func TestDownWithGivenServices(t *testing.T) {
api.EXPECT().NetworkInspect(gomock.Any(), "abc123", gomock.Any()).Return(network.Inspect{ID: "abc123"}, nil)
api.EXPECT().NetworkRemove(gomock.Any(), "abc123").Return(nil)
err = tested.Down(context.Background(), strings.ToLower(testProject), compose.DownOptions{
err = tested.Down(t.Context(), strings.ToLower(testProject), compose.DownOptions{
Services: []string{"service1", "not-running-service"},
})
assert.NilError(t, err)
@@ -175,7 +174,7 @@ func TestDownWithSpecifiedServiceButTheServicesAreNotRunning(t *testing.T) {
{ID: "def456", Name: "myProject_default", Labels: map[string]string{compose.NetworkLabel: "default"}},
}, nil)
err = tested.Down(context.Background(), strings.ToLower(testProject), compose.DownOptions{
err = tested.Down(t.Context(), strings.ToLower(testProject), compose.DownOptions{
Services: []string{"not-running-service1", "not-running-service2"},
})
assert.NilError(t, err)
@@ -227,7 +226,7 @@ func TestDownRemoveOrphans(t *testing.T) {
api.EXPECT().NetworkInspect(gomock.Any(), "abc123", gomock.Any()).Return(network.Inspect{ID: "abc123"}, nil)
api.EXPECT().NetworkRemove(gomock.Any(), "abc123").Return(nil)
err = tested.Down(context.Background(), strings.ToLower(testProject), compose.DownOptions{RemoveOrphans: true})
err = tested.Down(t.Context(), strings.ToLower(testProject), compose.DownOptions{RemoveOrphans: true})
assert.NilError(t, err)
}
@@ -259,7 +258,7 @@ func TestDownRemoveVolumes(t *testing.T) {
api.EXPECT().VolumeRemove(gomock.Any(), "myProject_volume", true).Return(nil)
err = tested.Down(context.Background(), strings.ToLower(testProject), compose.DownOptions{Volumes: true})
err = tested.Down(t.Context(), strings.ToLower(testProject), compose.DownOptions{Volumes: true})
assert.NilError(t, err)
}
@@ -346,7 +345,7 @@ func TestDownRemoveImages(t *testing.T) {
t.Log("-> docker compose down --rmi=local")
opts.Images = "local"
err = tested.Down(context.Background(), strings.ToLower(testProject), opts)
err = tested.Down(t.Context(), strings.ToLower(testProject), opts)
assert.NilError(t, err)
otherImagesToBeRemoved := []string{
@@ -361,7 +360,7 @@ func TestDownRemoveImages(t *testing.T) {
t.Log("-> docker compose down --rmi=all")
opts.Images = "all"
err = tested.Down(context.Background(), strings.ToLower(testProject), opts)
err = tested.Down(t.Context(), strings.ToLower(testProject), opts)
assert.NilError(t, err)
}
@@ -406,7 +405,7 @@ func TestDownRemoveImages_NoLabel(t *testing.T) {
api.EXPECT().ImageRemove(gomock.Any(), "testproject-service1:latest", image.RemoveOptions{}).Return(nil, nil)
err = tested.Down(context.Background(), strings.ToLower(testProject), compose.DownOptions{Images: "local"})
err = tested.Down(t.Context(), strings.ToLower(testProject), compose.DownOptions{Images: "local"})
assert.NilError(t, err)
}

View File

@@ -17,7 +17,6 @@
package compose
import (
"context"
"strings"
"testing"
"time"
@@ -40,7 +39,6 @@ func TestImages(t *testing.T) {
tested, err := NewComposeService(cli)
assert.NilError(t, err)
ctx := context.Background()
args := filters.NewArgs(projectFilter(strings.ToLower(testProject)))
listOpts := container.ListOptions{All: true, Filters: args}
api.EXPECT().ServerVersion(gomock.Any()).Return(types.Version{APIVersion: "1.96"}, nil).AnyTimes()
@@ -56,9 +54,9 @@ func TestImages(t *testing.T) {
c2 := containerDetail("service1", "456", "running", "bar:2")
c2.Ports = []container.Port{{PublicPort: 80, PrivatePort: 90, IP: "localhost"}}
c3 := containerDetail("service2", "789", "exited", "foo:1")
api.EXPECT().ContainerList(ctx, listOpts).Return([]container.Summary{c1, c2, c3}, nil)
api.EXPECT().ContainerList(t.Context(), listOpts).Return([]container.Summary{c1, c2, c3}, nil)
images, err := tested.Images(ctx, strings.ToLower(testProject), compose.ImagesOptions{})
images, err := tested.Images(t.Context(), strings.ToLower(testProject), compose.ImagesOptions{})
expected := map[string]compose.ImageSummary{
"123": {

View File

@@ -45,8 +45,7 @@ func TestKillAll(t *testing.T) {
name := strings.ToLower(testProject)
ctx := context.Background()
api.EXPECT().ContainerList(ctx, container.ListOptions{
api.EXPECT().ContainerList(t.Context(), container.ListOptions{
Filters: filters.NewArgs(projectFilter(name), hasConfigHashLabel()),
}).Return(
[]container.Summary{testContainer("service1", "123", false), testContainer("service1", "456", false), testContainer("service2", "789", false)}, nil)
@@ -64,7 +63,7 @@ func TestKillAll(t *testing.T) {
api.EXPECT().ContainerKill(anyCancellableContext(), "456", "").Return(nil)
api.EXPECT().ContainerKill(anyCancellableContext(), "789", "").Return(nil)
err = tested.Kill(ctx, name, compose.KillOptions{})
err = tested.Kill(t.Context(), name, compose.KillOptions{})
assert.NilError(t, err)
}
@@ -82,8 +81,7 @@ func TestKillSignal(t *testing.T) {
Filters: filters.NewArgs(projectFilter(name), serviceFilter(serviceName), hasConfigHashLabel()),
}
ctx := context.Background()
api.EXPECT().ContainerList(ctx, listOptions).Return([]container.Summary{testContainer(serviceName, "123", false)}, nil)
api.EXPECT().ContainerList(t.Context(), listOptions).Return([]container.Summary{testContainer(serviceName, "123", false)}, nil)
api.EXPECT().VolumeList(
gomock.Any(),
volume.ListOptions{
@@ -96,7 +94,7 @@ func TestKillSignal(t *testing.T) {
}, nil)
api.EXPECT().ContainerKill(anyCancellableContext(), "123", "SIGTERM").Return(nil)
err = tested.Kill(ctx, name, compose.KillOptions{Services: []string{serviceName}, Signal: "SIGTERM"})
err = tested.Kill(t.Context(), name, compose.KillOptions{Services: []string{serviceName}, Signal: "SIGTERM"})
assert.NilError(t, err)
}

View File

@@ -17,7 +17,6 @@
package compose
import (
"context"
"io"
"strings"
"sync"
@@ -44,8 +43,7 @@ func TestComposeService_Logs_Demux(t *testing.T) {
name := strings.ToLower(testProject)
ctx := context.Background()
api.EXPECT().ContainerList(ctx, containerType.ListOptions{
api.EXPECT().ContainerList(t.Context(), containerType.ListOptions{
All: true,
Filters: filters.NewArgs(oneOffFilter(false), projectFilter(name), hasConfigHashLabel()),
}).Return(
@@ -87,7 +85,7 @@ func TestComposeService_Logs_Demux(t *testing.T) {
}
consumer := &testLogConsumer{}
err = tested.Logs(ctx, name, consumer, opts)
err = tested.Logs(t.Context(), name, consumer, opts)
require.NoError(t, err)
require.Equal(
@@ -114,8 +112,7 @@ func TestComposeService_Logs_ServiceFiltering(t *testing.T) {
name := strings.ToLower(testProject)
ctx := context.Background()
api.EXPECT().ContainerList(ctx, containerType.ListOptions{
api.EXPECT().ContainerList(t.Context(), containerType.ListOptions{
All: true,
Filters: filters.NewArgs(oneOffFilter(false), projectFilter(name), hasConfigHashLabel()),
}).Return(
@@ -157,7 +154,7 @@ func TestComposeService_Logs_ServiceFiltering(t *testing.T) {
opts := compose.LogOptions{
Project: proj,
}
err = tested.Logs(ctx, name, consumer, opts)
err = tested.Logs(t.Context(), name, consumer, opts)
require.NoError(t, err)
require.Equal(t, []string{"hello c1"}, consumer.LogsForContainer("c1"))

View File

@@ -17,7 +17,6 @@
package compose
import (
"context"
"strings"
"testing"
@@ -37,7 +36,6 @@ func TestPs(t *testing.T) {
tested, err := NewComposeService(cli)
assert.NilError(t, err)
ctx := context.Background()
args := filters.NewArgs(projectFilter(strings.ToLower(testProject)), hasConfigHashLabel())
args.Add("label", "com.docker.compose.oneoff=False")
listOpts := containerType.ListOptions{Filters: args, All: false}
@@ -45,12 +43,12 @@ func TestPs(t *testing.T) {
c2, inspect2 := containerDetails("service1", "456", containerType.StateRunning, "", 0)
c2.Ports = []containerType.Port{{PublicPort: 80, PrivatePort: 90, IP: "localhost"}}
c3, inspect3 := containerDetails("service2", "789", containerType.StateExited, "", 130)
api.EXPECT().ContainerList(ctx, listOpts).Return([]containerType.Summary{c1, c2, c3}, nil)
api.EXPECT().ContainerList(t.Context(), listOpts).Return([]containerType.Summary{c1, c2, c3}, nil)
api.EXPECT().ContainerInspect(anyCancellableContext(), "123").Return(inspect1, nil)
api.EXPECT().ContainerInspect(anyCancellableContext(), "456").Return(inspect2, nil)
api.EXPECT().ContainerInspect(anyCancellableContext(), "789").Return(inspect3, nil)
containers, err := tested.Ps(ctx, strings.ToLower(testProject), compose.PsOptions{})
containers, err := tested.Ps(t.Context(), strings.ToLower(testProject), compose.PsOptions{})
expected := []compose.ContainerSummary{
{

View File

@@ -17,7 +17,6 @@
package compose
import (
"context"
"slices"
"testing"
@@ -32,7 +31,7 @@ import (
)
func Test_createLayers(t *testing.T) {
project, err := loader.LoadWithContext(context.TODO(), types.ConfigDetails{
project, err := loader.LoadWithContext(t.Context(), types.ConfigDetails{
WorkingDir: "testdata/publish/",
Environment: types.Mapping{},
ConfigFiles: []types.ConfigFile{
@@ -45,7 +44,7 @@ func Test_createLayers(t *testing.T) {
project.ComposeFiles = []string{"testdata/publish/compose.yaml"}
service := &composeService{}
layers, err := service.createLayers(context.TODO(), project, api.PublishOptions{
layers, err := service.createLayers(t.Context(), project, api.PublishOptions{
WithEnvironment: true,
})
assert.NilError(t, err)

View File

@@ -17,7 +17,6 @@
package compose
import (
"context"
"strings"
"testing"
"time"
@@ -41,7 +40,6 @@ func TestStopTimeout(t *testing.T) {
tested, err := NewComposeService(cli)
assert.NilError(t, err)
ctx := context.Background()
api.EXPECT().ContainerList(gomock.Any(), projectFilterListOpt(false)).Return(
[]container.Summary{
testContainer("service1", "123", false),
@@ -63,7 +61,7 @@ func TestStopTimeout(t *testing.T) {
api.EXPECT().ContainerStop(gomock.Any(), "456", stopConfig).Return(nil)
api.EXPECT().ContainerStop(gomock.Any(), "789", stopConfig).Return(nil)
err = tested.Stop(ctx, strings.ToLower(testProject), compose.StopOptions{
err = tested.Stop(t.Context(), strings.ToLower(testProject), compose.StopOptions{
Timeout: &timeout,
})
assert.NilError(t, err)

View File

@@ -17,7 +17,6 @@
package compose
import (
"context"
"strconv"
"testing"
@@ -119,10 +118,8 @@ func TestViz(t *testing.T) {
tested, err := NewComposeService(cli)
require.NoError(t, err)
ctx := context.Background()
t.Run("viz (no ports, networks or image)", func(t *testing.T) {
graphStr, err := tested.Viz(ctx, &project, compose.VizOptions{
graphStr, err := tested.Viz(t.Context(), &project, compose.VizOptions{
Indentation: " ",
IncludePorts: false,
IncludeImageName: false,
@@ -181,7 +178,7 @@ func TestViz(t *testing.T) {
})
t.Run("viz (with ports, networks and image)", func(t *testing.T) {
graphStr, err := tested.Viz(ctx, &project, compose.VizOptions{
graphStr, err := tested.Viz(t.Context(), &project, compose.VizOptions{
Indentation: "\t",
IncludePorts: true,
IncludeImageName: true,

View File

@@ -17,7 +17,6 @@
package compose
import (
"context"
"testing"
"github.com/docker/docker/api/types/container"
@@ -58,7 +57,6 @@ func TestVolumes(t *testing.T) {
},
}
ctx := context.Background()
args := filters.NewArgs(projectFilter(testProject))
listOpts := container.ListOptions{Filters: args}
volumeListArgs := filters.NewArgs(projectFilter(testProject))
@@ -68,20 +66,19 @@ func TestVolumes(t *testing.T) {
}
containerReturn := []container.Summary{c1, c2}
// Mock API calls
mockApi.EXPECT().ContainerList(ctx, listOpts).Times(2).Return(containerReturn, nil)
mockApi.EXPECT().VolumeList(ctx, volumeListOpts).Times(2).Return(volumeReturn, nil)
mockApi.EXPECT().ContainerList(t.Context(), listOpts).Times(2).Return(containerReturn, nil)
mockApi.EXPECT().VolumeList(t.Context(), volumeListOpts).Times(2).Return(volumeReturn, nil)
// Test without service filter - should return all project volumes
volumeOptions := api.VolumesOptions{}
volumes, err := tested.Volumes(ctx, testProject, volumeOptions)
volumes, err := tested.Volumes(t.Context(), testProject, volumeOptions)
expected := []api.VolumesSummary{vol1, vol2, vol3}
assert.NilError(t, err)
assert.DeepEqual(t, volumes, expected)
// Test with service filter - should only return volumes used by service1
volumeOptions = api.VolumesOptions{Services: []string{"service1"}}
volumes, err = tested.Volumes(ctx, testProject, volumeOptions)
volumes, err = tested.Volumes(t.Context(), testProject, volumeOptions)
expected = []api.VolumesSummary{vol1, vol2}
assert.NilError(t, err)
assert.DeepEqual(t, volumes, expected)

View File

@@ -95,7 +95,7 @@ func TestWatch_Sync(t *testing.T) {
//
cli.EXPECT().Client().Return(apiClient).AnyTimes()
ctx, cancelFunc := context.WithCancel(context.Background())
ctx, cancelFunc := context.WithCancel(t.Context())
t.Cleanup(cancelFunc)
proj := types.Project{

View File

@@ -39,7 +39,7 @@ func TestComposeCancel(t *testing.T) {
t.Run("metrics on cancel Compose build", func(t *testing.T) {
const buildProjectPath = "fixtures/build-infinite/compose.yaml"
ctx, cancel := context.WithCancel(context.Background())
ctx, cancel := context.WithCancel(t.Context())
defer cancel()
// require a separate groupID from the process running tests, in order to simulate ctrl+C from a terminal.

View File

@@ -75,7 +75,7 @@ func TestUpDependenciesNotStopped(t *testing.T) {
"app",
)
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
ctx, cancel := context.WithTimeout(t.Context(), 15*time.Second)
t.Cleanup(cancel)
cmd, err := StartWithNewGroupID(ctx, testCmd, upOut, nil)

View File

@@ -27,7 +27,7 @@ import (
func Test_BatchDebounceEvents(t *testing.T) {
ch := make(chan FileEvent)
clock := clockwork.NewFakeClock()
ctx, stop := context.WithCancel(context.Background())
ctx, stop := context.WithCancel(t.Context())
t.Cleanup(stop)
eventBatchCh := BatchDebounceEvents(ctx, clock, ch)

View File

@@ -501,7 +501,7 @@ type notifyFixture struct {
func newNotifyFixture(t *testing.T) *notifyFixture {
out := bytes.NewBuffer(nil)
ctx, cancel := context.WithCancel(context.Background())
ctx, cancel := context.WithCancel(t.Context())
nf := &notifyFixture{
ctx: ctx,
cancel: cancel,