@@ -18,6 +18,7 @@ package container
1818
1919import (
2020 "bytes"
21+ "context"
2122 "fmt"
2223 "os"
2324 "path/filepath"
@@ -27,11 +28,14 @@ import (
2728 "gotest.tools/v3/assert"
2829
2930 "github.com/containerd/cgroups/v3"
31+ containerd "github.com/containerd/containerd/v2/client"
3032 "github.com/containerd/continuity/testutil/loopback"
3133
3234 "github.com/containerd/nerdctl/v2/pkg/cmd/container"
35+ "github.com/containerd/nerdctl/v2/pkg/idutil/containerwalker"
3336 "github.com/containerd/nerdctl/v2/pkg/testutil"
3437 "github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest"
38+ "github.com/containerd/nerdctl/v2/pkg/testutil/test"
3539)
3640
3741func TestRunCgroupV2 (t * testing.T ) {
@@ -170,6 +174,53 @@ func TestRunCgroupV1(t *testing.T) {
170174 base .Cmd ("run" , "--rm" , "--cpu-quota" , "42000" , "--cpu-period" , "100000" , "--cpuset-mems" , "0" , "--memory" , "42m" , "--memory-reservation" , "6m" , "--memory-swap" , "100m" , "--memory-swappiness" , "0" , "--pids-limit" , "42" , "--cpu-shares" , "2000" , "--cpuset-cpus" , "0-1" , testutil .AlpineImage , "cat" , quota , period , cpusetMems , memoryLimit , memoryReservation , memorySwap , memorySwappiness , pidsLimit , cpuShare , cpusetCpus ).AssertOutExactly (expected )
171175}
172176
177+ // TestIssue3781 tests https://github.com/containerd/nerdctl/issues/3781
178+ func TestIssue3781 (t * testing.T ) {
179+ t .Parallel ()
180+ testCase := nerdtest .Setup ()
181+ testCase .Require = test .Not (nerdtest .Docker )
182+
183+ base := testutil .NewBase (t )
184+ info := base .Info ()
185+ switch info .CgroupDriver {
186+ case "none" , "" :
187+ t .Skip ("test requires cgroup driver" )
188+ }
189+ containerName := testutil .Identifier (t )
190+ base .Cmd ("run" , "-d" , "--name" , containerName , testutil .AlpineImage , "sleep" , "infinity" ).AssertOK ()
191+ defer func () {
192+ base .Cmd ("rm" , "-f" , containerName )
193+ }()
194+ base .Cmd ("update" , "--cpuset-cpus" , "0-1" , containerName ).AssertOK ()
195+ addr := base .ContainerdAddress ()
196+ client , err := containerd .New (addr , containerd .WithDefaultNamespace (testutil .Namespace ))
197+ assert .NilError (base .T , err )
198+ ctx := context .Background ()
199+
200+ // get container id by container name.
201+ var cid string
202+ var args []string
203+ args = append (args , containerName )
204+ walker := & containerwalker.ContainerWalker {
205+ Client : client ,
206+ OnFound : func (ctx context.Context , found containerwalker.Found ) error {
207+ if found .MatchCount > 1 {
208+ return fmt .Errorf ("multiple IDs found with provided prefix: %s" , found .Req )
209+ }
210+ cid = found .Container .ID ()
211+ return nil
212+ },
213+ }
214+ err = walker .WalkAll (ctx , args , true )
215+ assert .NilError (base .T , err )
216+
217+ container , err := client .LoadContainer (ctx , cid )
218+ assert .NilError (base .T , err )
219+ spec , err := container .Spec (ctx )
220+ assert .NilError (base .T , err )
221+ assert .Equal (t , spec .Linux .Resources .Pids == nil , true )
222+ }
223+
173224func TestRunDevice (t * testing.T ) {
174225 if os .Geteuid () != 0 || userns .RunningInUserNS () {
175226 t .Skip ("test requires the root in the initial user namespace" )
0 commit comments