2021-02-15 13:54:55 +01:00
|
|
|
/*
|
|
|
|
Copyright 2020 Docker Compose CLI authors
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package compose
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-08-18 10:50:43 -03:00
|
|
|
"strings"
|
2021-02-15 13:54:55 +01:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2025-02-12 09:34:07 +01:00
|
|
|
"github.com/docker/docker/api/types/container"
|
2022-04-11 10:58:20 +02:00
|
|
|
"github.com/docker/docker/api/types/filters"
|
2024-06-07 17:57:54 +02:00
|
|
|
"github.com/docker/docker/api/types/network"
|
2022-04-11 10:58:20 +02:00
|
|
|
"github.com/docker/docker/api/types/volume"
|
2023-12-13 14:01:36 +01:00
|
|
|
"go.uber.org/mock/gomock"
|
2021-02-15 13:54:55 +01:00
|
|
|
"gotest.tools/v3/assert"
|
2025-02-12 09:34:07 +01:00
|
|
|
|
|
|
|
compose "github.com/docker/compose/v2/pkg/api"
|
|
|
|
"github.com/docker/compose/v2/pkg/utils"
|
2021-02-15 13:54:55 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestStopTimeout(t *testing.T) {
|
|
|
|
mockCtrl := gomock.NewController(t)
|
|
|
|
defer mockCtrl.Finish()
|
2022-03-03 14:19:01 +01:00
|
|
|
|
2023-03-08 15:11:32 +00:00
|
|
|
api, cli := prepareMocks(mockCtrl)
|
2022-09-08 16:26:00 -04:00
|
|
|
tested := composeService{
|
|
|
|
dockerCli: cli,
|
|
|
|
}
|
2021-02-15 13:54:55 +01:00
|
|
|
|
|
|
|
ctx := context.Background()
|
2022-04-11 10:58:20 +02:00
|
|
|
api.EXPECT().ContainerList(gomock.Any(), projectFilterListOpt(false)).Return(
|
2025-02-12 09:34:07 +01:00
|
|
|
[]container.Summary{
|
2021-09-20 08:16:59 +02:00
|
|
|
testContainer("service1", "123", false),
|
|
|
|
testContainer("service1", "456", false),
|
|
|
|
testContainer("service2", "789", false),
|
2021-02-15 13:54:55 +01:00
|
|
|
}, nil)
|
2023-06-06 16:55:54 -04:00
|
|
|
api.EXPECT().VolumeList(
|
|
|
|
gomock.Any(),
|
|
|
|
volume.ListOptions{
|
|
|
|
Filters: filters.NewArgs(projectFilter(strings.ToLower(testProject))),
|
|
|
|
}).
|
2022-10-18 16:57:53 +02:00
|
|
|
Return(volume.ListResponse{}, nil)
|
2024-06-07 17:57:54 +02:00
|
|
|
api.EXPECT().NetworkList(gomock.Any(), network.ListOptions{Filters: filters.NewArgs(projectFilter(strings.ToLower(testProject)))}).
|
|
|
|
Return([]network.Summary{}, nil)
|
2021-02-15 13:54:55 +01:00
|
|
|
|
2022-10-18 15:21:11 +02:00
|
|
|
timeout := 2 * time.Second
|
2025-02-12 09:34:07 +01:00
|
|
|
stopConfig := container.StopOptions{Timeout: utils.DurationSecondToInt(&timeout)}
|
2022-10-18 15:21:11 +02:00
|
|
|
api.EXPECT().ContainerStop(gomock.Any(), "123", stopConfig).Return(nil)
|
|
|
|
api.EXPECT().ContainerStop(gomock.Any(), "456", stopConfig).Return(nil)
|
|
|
|
api.EXPECT().ContainerStop(gomock.Any(), "789", stopConfig).Return(nil)
|
2021-02-15 13:54:55 +01:00
|
|
|
|
2022-02-05 10:27:52 +03:30
|
|
|
err := tested.Stop(ctx, strings.ToLower(testProject), compose.StopOptions{
|
2021-02-15 13:54:55 +01:00
|
|
|
Timeout: &timeout,
|
|
|
|
})
|
|
|
|
assert.NilError(t, err)
|
|
|
|
}
|