1package docker
2
3import (
4 "testing"
5 "time"
6
7 "github.com/stretchr/testify/assert"
8 "github.com/stretchr/testify/require"
9)
10
11// TestStopTimeout_ConvertsGraceToAPISeconds proves the runtime API never
12// receives a truncated or negative grace: a non-positive grace keeps the
13// runtime default (nil), and a fractional grace rounds up so the
14// container always gets at least the declared time.
15func TestStopTimeout_ConvertsGraceToAPISeconds(t *testing.T) {
16 cases := []struct {
17 name string
18 grace time.Duration
19 want *int
20 }{
21 {name: "zero keeps the runtime default", grace: 0, want: nil},
22 {name: "negative keeps the runtime default", grace: -time.Second, want: nil},
23 {name: "default app grace", grace: 10 * time.Second, want: intPtr(10)},
24 {name: "sub-second rounds up", grace: 500 * time.Millisecond, want: intPtr(1)},
25 {name: "fractional rounds up", grace: 1500 * time.Millisecond, want: intPtr(2)},
26 {name: "exact seconds are preserved", grace: 25 * time.Second, want: intPtr(25)},
27 {name: "the app cap is preserved", grace: 5 * time.Minute, want: intPtr(300)},
28 }
29 for _, tc := range cases {
30 t.Run(tc.name, func(t *testing.T) {
31 got := stopTimeout(tc.grace)
32 if tc.want == nil {
33 assert.Nil(t, got)
34 return
35 }
36 require.NotNil(t, got)
37 assert.Equal(t, *tc.want, *got)
38 })
39 }
40}
41
42func intPtr(v int) *int { return &v }