Skip to content

Commit 00ccddb

Browse files
leoperegrinondeloof
authored andcommitted
add volumes command test cases
Signed-off-by: Leonardo Peregrino <[email protected]>
1 parent 63b4414 commit 00ccddb

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

pkg/compose/volumes_test.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
Copyright 2020 Docker Compose CLI authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package compose
18+
19+
import (
20+
"context"
21+
"testing"
22+
23+
"github.com/compose-spec/compose-go/v2/types"
24+
"github.com/docker/compose/v2/pkg/api"
25+
"github.com/docker/docker/api/types/container"
26+
"github.com/docker/docker/api/types/filters"
27+
"github.com/docker/docker/api/types/volume"
28+
"go.uber.org/mock/gomock"
29+
"gotest.tools/v3/assert"
30+
)
31+
32+
func TestVolumes(t *testing.T) {
33+
mockCtrl := gomock.NewController(t)
34+
defer mockCtrl.Finish()
35+
36+
mockApi, mockCli := prepareMocks(mockCtrl)
37+
tested := composeService{
38+
dockerCli: mockCli,
39+
}
40+
41+
// Create test volumes
42+
vol1 := &volume.Volume{Name: testProject + "_vol1"}
43+
vol2 := &volume.Volume{Name: testProject + "_vol2"}
44+
vol3 := &volume.Volume{Name: testProject + "_vol3"}
45+
46+
// Create test containers with volume mounts
47+
c1 := container.Summary{
48+
Labels: map[string]string{api.ServiceLabel: "service1"},
49+
Mounts: []container.MountPoint{
50+
{Name: testProject + "_vol1"},
51+
{Name: testProject + "_vol2"},
52+
},
53+
}
54+
c2 := container.Summary{
55+
Labels: map[string]string{api.ServiceLabel: "service2"},
56+
Mounts: []container.MountPoint{
57+
{Name: testProject + "_vol3"},
58+
},
59+
}
60+
61+
ctx := context.Background()
62+
project := &types.Project{Name: testProject}
63+
args := filters.NewArgs(projectFilter(testProject))
64+
listOpts := container.ListOptions{Filters: args}
65+
volumeListArgs := filters.NewArgs(projectFilter(testProject))
66+
volumeListOpts := volume.ListOptions{Filters: volumeListArgs}
67+
volumeReturn := volume.ListResponse{
68+
Volumes: []*volume.Volume{vol1, vol2, vol3},
69+
}
70+
containerReturn := []container.Summary{c1, c2}
71+
72+
// Mock API calls
73+
mockApi.EXPECT().ContainerList(ctx, listOpts).Times(2).Return(containerReturn, nil)
74+
mockApi.EXPECT().VolumeList(ctx, volumeListOpts).Times(2).Return(volumeReturn, nil)
75+
76+
// Test without service filter - should return all project volumes
77+
volumeOptions := api.VolumesOptions{}
78+
volumes, err := tested.Volumes(ctx, project, volumeOptions)
79+
expected := []api.VolumesSummary{vol1, vol2, vol3}
80+
assert.NilError(t, err)
81+
assert.DeepEqual(t, volumes, expected)
82+
83+
// Test with service filter - should only return volumes used by service1
84+
volumeOptions = api.VolumesOptions{Services: []string{"service1"}}
85+
volumes, err = tested.Volumes(ctx, project, volumeOptions)
86+
expected = []api.VolumesSummary{vol1, vol2}
87+
assert.NilError(t, err)
88+
assert.DeepEqual(t, volumes, expected)
89+
}

0 commit comments

Comments
 (0)