@@ -30,7 +30,7 @@ import (
30
30
"github.com/opencontainers/runtime-tools/generate"
31
31
"github.com/stretchr/testify/assert"
32
32
"github.com/stretchr/testify/require"
33
- "k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1/ runtime"
33
+ runtime "k8s.io/kubernetes/pkg/kubelet/apis/cri/runtime/v1alpha2 "
34
34
35
35
"github.com/containerd/cri-containerd/pkg/annotations"
36
36
ostesting "github.com/containerd/cri-containerd/pkg/os/testing"
@@ -169,6 +169,10 @@ func getCreateContainerTestData() (*runtime.ContainerConfig, *runtime.PodSandbox
169
169
Type : runtimespec .UTSNamespace ,
170
170
Path : getUTSNamespace (sandboxPid ),
171
171
})
172
+ assert .Contains (t , spec .Linux .Namespaces , runtimespec.LinuxNamespace {
173
+ Type : runtimespec .PIDNamespace ,
174
+ Path : getPIDNamespace (sandboxPid ),
175
+ })
172
176
173
177
t .Logf ("Check PodSandbox annotations" )
174
178
assert .Contains (t , spec .Annotations , annotations .SandboxID )
@@ -543,7 +547,7 @@ func TestGenerateContainerMounts(t *testing.T) {
543
547
},
544
548
"should use host /dev/shm when host ipc is set" : {
545
549
securityContext : & runtime.LinuxContainerSecurityContext {
546
- NamespaceOptions : & runtime.NamespaceOption {HostIpc : true },
550
+ NamespaceOptions : & runtime.NamespaceOption {Ipc : runtime . NamespaceMode_NODE },
547
551
},
548
552
expectedMounts : []* runtime.Mount {
549
553
{
@@ -748,25 +752,39 @@ func TestPidNamespace(t *testing.T) {
748
752
testID := "test-id"
749
753
testPid := uint32 (1234 )
750
754
testSandboxID := "sandbox-id"
751
- config , sandboxConfig , imageConfig , specCheck := getCreateContainerTestData ()
755
+ config , sandboxConfig , imageConfig , _ := getCreateContainerTestData ()
752
756
c := newTestCRIContainerdService ()
753
- t .Logf ("should not set pid namespace when host pid is true" )
754
- config .Linux .SecurityContext .NamespaceOptions = & runtime.NamespaceOption {HostPid : true }
755
- spec , err := c .generateContainerSpec (testID , testSandboxID , testPid , config , sandboxConfig , imageConfig , nil )
756
- require .NoError (t , err )
757
- specCheck (t , testID , testSandboxID , testPid , spec )
758
- for _ , ns := range spec .Linux .Namespaces {
759
- assert .NotEqual (t , ns .Type , runtimespec .PIDNamespace )
757
+ for desc , test := range map [string ]struct {
758
+ pidNS runtime.NamespaceMode
759
+ expected runtimespec.LinuxNamespace
760
+ }{
761
+ "node namespace mode" : {
762
+ pidNS : runtime .NamespaceMode_NODE ,
763
+ expected : runtimespec.LinuxNamespace {
764
+ Type : runtimespec .PIDNamespace ,
765
+ Path : getPIDNamespace (testPid ),
766
+ },
767
+ },
768
+ "container namespace mode" : {
769
+ pidNS : runtime .NamespaceMode_CONTAINER ,
770
+ expected : runtimespec.LinuxNamespace {
771
+ Type : runtimespec .PIDNamespace ,
772
+ },
773
+ },
774
+ "pod namespace mode" : {
775
+ pidNS : runtime .NamespaceMode_POD ,
776
+ expected : runtimespec.LinuxNamespace {
777
+ Type : runtimespec .PIDNamespace ,
778
+ Path : getPIDNamespace (testPid ),
779
+ },
780
+ },
781
+ } {
782
+ t .Logf ("TestCase %q" , desc )
783
+ config .Linux .SecurityContext .NamespaceOptions = & runtime.NamespaceOption {Pid : test .pidNS }
784
+ spec , err := c .generateContainerSpec (testID , testSandboxID , testPid , config , sandboxConfig , imageConfig , nil )
785
+ require .NoError (t , err )
786
+ assert .Contains (t , spec .Linux .Namespaces , test .expected )
760
787
}
761
-
762
- t .Logf ("should set pid namespace when host pid is false" )
763
- config .Linux .SecurityContext .NamespaceOptions = & runtime.NamespaceOption {HostPid : false }
764
- spec , err = c .generateContainerSpec (testID , testSandboxID , testPid , config , sandboxConfig , imageConfig , nil )
765
- require .NoError (t , err )
766
- specCheck (t , testID , testSandboxID , testPid , spec )
767
- assert .Contains (t , spec .Linux .Namespaces , runtimespec.LinuxNamespace {
768
- Type : runtimespec .PIDNamespace ,
769
- })
770
788
}
771
789
772
790
func TestDefaultRuntimeSpec (t * testing.T ) {
0 commit comments