@@ -12,6 +12,7 @@ import (
1212 . "github.com/onsi/ginkgo/v2"
1313 . "github.com/onsi/gomega"
1414
15+ ncTypes "github.com/containerd/nerdctl/v2/pkg/api/types"
1516 "github.com/runfinch/finch-daemon/api/handlers/container"
1617 "github.com/runfinch/finch-daemon/mocks/mocks_archive"
1718 "github.com/runfinch/finch-daemon/mocks/mocks_backend"
@@ -32,6 +33,7 @@ var _ = Describe("Container Stop API ", func() {
3233 cid string
3334 tarExtractor * mocks_archive.MockTarExtractor
3435 service container.Service
36+ stopOptions ncTypes.ContainerStopOptions
3537 )
3638 BeforeEach (func () {
3739 ctx = context .Background ()
@@ -43,6 +45,7 @@ var _ = Describe("Container Stop API ", func() {
4345 con = mocks_container .NewMockContainer (mockCtrl )
4446 con .EXPECT ().ID ().Return (cid ).AnyTimes ()
4547 tarExtractor = mocks_archive .NewMockTarExtractor (mockCtrl )
48+ stopOptions = ncTypes.ContainerStopOptions {}
4649
4750 service = NewService (cdClient , mockNerdctlService {ncClient , nil }, logger , nil , nil , tarExtractor )
4851 })
@@ -53,10 +56,10 @@ var _ = Describe("Container Stop API ", func() {
5356 cdClient .EXPECT ().SearchContainer (gomock .Any (), gomock .Any ()).Return (
5457 []containerd.Container {con }, nil )
5558
56- ncClient .EXPECT ().StopContainer (ctx , con , gomock .Any ())
59+ ncClient .EXPECT ().StopContainer (ctx , con . ID () , gomock .Any ())
5760 logger .EXPECT ().Debugf ("successfully stopped: %s" , cid )
5861
59- err := service .Stop (ctx , cid , nil )
62+ err := service .Stop (ctx , cid , stopOptions )
6063 Expect (err ).Should (BeNil ())
6164 })
6265 It ("should return not found error" , func () {
@@ -66,7 +69,7 @@ var _ = Describe("Container Stop API ", func() {
6669 logger .EXPECT ().Debugf ("no such container: %s" , cid )
6770
6871 // service should return NotFound error
69- err := service .Stop (ctx , cid , nil )
72+ err := service .Stop (ctx , cid , stopOptions )
7073 Expect (errdefs .IsNotFound (err )).Should (BeTrue ())
7174 })
7275 It ("should return multiple containers found error" , func () {
@@ -76,7 +79,7 @@ var _ = Describe("Container Stop API ", func() {
7679 logger .EXPECT ().Debugf ("multiple IDs found with provided prefix: %s, total containers found: %d" , cid , 2 )
7780
7881 // service should return error
79- err := service .Stop (ctx , cid , nil )
82+ err := service .Stop (ctx , cid , stopOptions )
8083 Expect (err ).Should (Not (BeNil ()))
8184 })
8285 It ("should return not modified error as container is stopped already" , func () {
@@ -86,7 +89,7 @@ var _ = Describe("Container Stop API ", func() {
8689 []containerd.Container {con }, nil )
8790
8891 // service should return not modified error.
89- err := service .Stop (ctx , cid , nil )
92+ err := service .Stop (ctx , cid , stopOptions )
9093 Expect (errdefs .IsNotModified (err )).Should (BeTrue ())
9194 })
9295 It ("should return not modified error as container is not running" , func () {
@@ -96,7 +99,7 @@ var _ = Describe("Container Stop API ", func() {
9699 []containerd.Container {con }, nil )
97100
98101 // service should return not modified error.
99- err := service .Stop (ctx , cid , nil )
102+ err := service .Stop (ctx , cid , stopOptions )
100103 Expect (errdefs .IsNotModified (err )).Should (BeTrue ())
101104 })
102105 It ("should fail due to nerdctl client error" , func () {
@@ -106,12 +109,27 @@ var _ = Describe("Container Stop API ", func() {
106109 []containerd.Container {con }, nil )
107110
108111 expectedErr := fmt .Errorf ("nerdctl error" )
109- ncClient .EXPECT ().StopContainer (ctx , con , gomock .Any ()).Return (expectedErr )
112+ ncClient .EXPECT ().StopContainer (ctx , con . ID () , gomock .Any ()).Return (expectedErr )
110113 logger .EXPECT ().Errorf ("Failed to stop container: %s. Error: %v" , cid , expectedErr )
111114
112115 // service should return not modified error.
113- err := service .Stop (ctx , cid , nil )
116+ err := service .Stop (ctx , cid , stopOptions )
114117 Expect (err ).Should (Equal (expectedErr ))
115118 })
119+ It ("should stop container with custom signal" , func () {
120+ stopOptions .Signal = "SIGKILL"
121+
122+ // set up the mock to return a container that is in running state
123+ cdClient .EXPECT ().GetContainerStatus (gomock .Any (), gomock .Any ()).Return (containerd .Running )
124+ cdClient .EXPECT ().SearchContainer (gomock .Any (), gomock .Any ()).Return (
125+ []containerd.Container {con }, nil )
126+
127+ // Expect StopContainer to be called with the custom options containing SIGKILL
128+ ncClient .EXPECT ().StopContainer (ctx , con .ID (), stopOptions )
129+ logger .EXPECT ().Debugf ("successfully stopped: %s" , cid )
130+
131+ err := service .Stop (ctx , cid , stopOptions )
132+ Expect (err ).Should (BeNil ())
133+ })
116134 })
117135})
0 commit comments