44package container
55
66import (
7+ "context"
78 "fmt"
89 "net/http"
910 "net/http/httptest"
1011
12+ ncTypes "github.com/containerd/nerdctl/v2/pkg/api/types"
1113 "github.com/containerd/nerdctl/v2/pkg/config"
1214 "github.com/golang/mock/gomock"
1315 "github.com/gorilla/mux"
@@ -42,15 +44,15 @@ var _ = Describe("Container Start API ", func() {
4244 Context ("handler" , func () {
4345 It ("should return 204 as success response" , func () {
4446 // service mock returns nil to mimic handler started the container successfully.
45- service .EXPECT ().Start (gomock .Any (), "123" ).Return (nil )
47+ service .EXPECT ().Start (gomock .Any (), gomock . Any (), gomock . Any () ).Return (nil )
4648
4749 h .start (rr , req )
4850 Expect (rr ).Should (HaveHTTPStatus (http .StatusNoContent ))
4951 })
5052
5153 It ("should return 404 not found response" , func () {
5254 // service mock returns not found error to mimic user trying to start container that does not exist
53- service .EXPECT ().Start (gomock .Any (), "123" ).Return (
55+ service .EXPECT ().Start (gomock .Any (), gomock . Any (), gomock . Any () ).Return (
5456 errdefs .NewNotFound (fmt .Errorf ("container not found" )))
5557 h .start (rr , req )
5658 Expect (rr ).Should (HaveHTTPStatus (http .StatusNotFound ))
@@ -59,7 +61,7 @@ var _ = Describe("Container Start API ", func() {
5961 It ("should return 500 internal error response" , func () {
6062 // service mock return error to mimic a user trying to start a container with an id that has
6163 // multiple containers with same prefix.
62- service .EXPECT ().Start (gomock .Any (), "123" ).Return (
64+ service .EXPECT ().Start (gomock .Any (), gomock . Any (), gomock . Any () ).Return (
6365 fmt .Errorf ("multiple IDs found with provided prefix" ))
6466
6567 h .start (rr , req )
@@ -68,11 +70,38 @@ var _ = Describe("Container Start API ", func() {
6870 })
6971 It ("should return 304 not-modified error when container is already running" , func () {
7072 // service mock returns not found error to mimic user trying to start container that is running
71- service .EXPECT ().Start (gomock .Any (), "123" ).Return (
73+ service .EXPECT ().Start (gomock .Any (), gomock . Any (), gomock . Any () ).Return (
7274 errdefs .NewNotModified (fmt .Errorf ("container already running" )))
7375
7476 h .start (rr , req )
7577 Expect (rr ).Should (HaveHTTPStatus (http .StatusNotModified ))
7678 })
79+ It ("should pass detachKeys to the service" , func () {
80+ // Set up the request with detachKeys query parameter
81+ req , _ = http .NewRequest (http .MethodPost , "/containers/123/start?detachKeys=ctrl-p,ctrl-q" , nil )
82+ req = mux .SetURLVars (req , map [string ]string {"id" : "123" })
83+
84+ // Expect the service to be called with the correct options
85+ service .EXPECT ().Start (
86+ gomock .Any (),
87+ gomock .Any (),
88+ gomock .Any (),
89+ ).DoAndReturn (func (_ context.Context , cid string , options ncTypes.ContainerStartOptions ) error {
90+ Expect (cid ).To (Equal ("123" ))
91+ Expect (options .DetachKeys ).To (Equal ("ctrl-p,ctrl-q" ))
92+ return nil
93+ })
94+
95+ h .start (rr , req )
96+ Expect (rr ).Should (HaveHTTPStatus (http .StatusNoContent ))
97+ })
98+ It ("should return 400 Bad Request for invalid ctrl- combination" , func () {
99+ req , _ = http .NewRequest (http .MethodPost , "/containers/123/start?detachKeys=ctrl-1" , nil )
100+ req = mux .SetURLVars (req , map [string ]string {"id" : "123" })
101+ service .EXPECT ().Start (gomock .Any (), gomock .Any (), gomock .Any ()).Times (0 )
102+ h .start (rr , req )
103+ Expect (rr ).Should (HaveHTTPStatus (http .StatusBadRequest ))
104+ Expect (rr .Body ).Should (MatchJSON (`{"message": "Invalid detach keys: invalid ctrl key: 1 - must be one of abcdefghijklmnopqrstuvwxyz@[\\]^_"}` ))
105+ })
77106 })
78107})
0 commit comments