volumes_test.go 2.9 KB

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