|
| 1 | +package container_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + |
| 6 | + "github.com/cri-o/cri-o/internal/config/nsmgr" |
| 7 | + nsmgrtest "github.com/cri-o/cri-o/internal/config/nsmgr/test" |
| 8 | + "github.com/cri-o/cri-o/internal/lib/sandbox" |
| 9 | + . "github.com/onsi/ginkgo" |
| 10 | + . "github.com/onsi/gomega" |
| 11 | + rspec "github.com/opencontainers/runtime-spec/specs-go" |
| 12 | + types "k8s.io/cri-api/pkg/apis/runtime/v1" |
| 13 | +) |
| 14 | + |
| 15 | +var _ = t.Describe("Container:SpecAddNamespaces", func() { |
| 16 | + It("should inherit pod namespaces", func() { |
| 17 | + // Given |
| 18 | + config := &types.ContainerConfig{ |
| 19 | + Metadata: &types.ContainerMetadata{Name: "name"}, |
| 20 | + Linux: &types.LinuxContainerConfig{ |
| 21 | + SecurityContext: &types.LinuxContainerSecurityContext{ |
| 22 | + NamespaceOptions: &types.NamespaceOption{ |
| 23 | + Network: types.NamespaceMode_POD, |
| 24 | + Ipc: types.NamespaceMode_POD, |
| 25 | + Pid: types.NamespaceMode_CONTAINER, |
| 26 | + }, |
| 27 | + }, |
| 28 | + }, |
| 29 | + } |
| 30 | + |
| 31 | + sboxConfig := &types.PodSandboxConfig{} |
| 32 | + sb := &sandbox.Sandbox{} |
| 33 | + sb.AddManagedNamespaces(nsmgrtest.AllSpoofedNamespaces) |
| 34 | + sut.Spec().ClearLinuxNamespaces() |
| 35 | + |
| 36 | + // When |
| 37 | + Expect(sut.SetConfig(config, sboxConfig)).To(BeNil()) |
| 38 | + Expect(sut.SpecAddNamespaces(sb)).To(BeNil()) |
| 39 | + |
| 40 | + // Then |
| 41 | + spec := sut.Spec() |
| 42 | + Expect(len(spec.Config.Linux.Namespaces)).To(Equal(len(nsmgrtest.AllSpoofedNamespaces))) |
| 43 | + for _, ns := range nsmgrtest.AllSpoofedNamespaces { |
| 44 | + found := false |
| 45 | + for _, specNs := range spec.Config.Linux.Namespaces { |
| 46 | + if specNs.Path == ns.Path() { |
| 47 | + found = true |
| 48 | + } |
| 49 | + } |
| 50 | + Expect(found).To(Equal(true)) |
| 51 | + } |
| 52 | + }) |
| 53 | + It("should drop network if hostNet", func() { |
| 54 | + // Given |
| 55 | + config := &types.ContainerConfig{ |
| 56 | + Metadata: &types.ContainerMetadata{Name: "name"}, |
| 57 | + Linux: &types.LinuxContainerConfig{ |
| 58 | + SecurityContext: &types.LinuxContainerSecurityContext{ |
| 59 | + NamespaceOptions: &types.NamespaceOption{ |
| 60 | + Network: types.NamespaceMode_NODE, |
| 61 | + Ipc: types.NamespaceMode_POD, |
| 62 | + Pid: types.NamespaceMode_CONTAINER, |
| 63 | + }, |
| 64 | + }, |
| 65 | + }, |
| 66 | + } |
| 67 | + |
| 68 | + sboxConfig := &types.PodSandboxConfig{} |
| 69 | + sb := &sandbox.Sandbox{} |
| 70 | + sb.AddManagedNamespaces(nsmgrtest.AllSpoofedNamespaces) |
| 71 | + |
| 72 | + // When |
| 73 | + Expect(sut.SetConfig(config, sboxConfig)).To(BeNil()) |
| 74 | + sut.Spec().ClearLinuxNamespaces() |
| 75 | + Expect(sut.SpecAddNamespaces(sb)).To(BeNil()) |
| 76 | + |
| 77 | + // Then |
| 78 | + spec := sut.Spec() |
| 79 | + Expect(len(spec.Config.Linux.Namespaces)).To(Equal(len(nsmgrtest.AllSpoofedNamespaces) - 1)) |
| 80 | + |
| 81 | + for _, specNs := range spec.Config.Linux.Namespaces { |
| 82 | + Expect(specNs.Type).NotTo(Equal(rspec.NetworkNamespace)) |
| 83 | + } |
| 84 | + }) |
| 85 | + It("should drop PID if hostPID", func() { |
| 86 | + // Given |
| 87 | + config := &types.ContainerConfig{ |
| 88 | + Metadata: &types.ContainerMetadata{Name: "name"}, |
| 89 | + Linux: &types.LinuxContainerConfig{ |
| 90 | + SecurityContext: &types.LinuxContainerSecurityContext{ |
| 91 | + NamespaceOptions: &types.NamespaceOption{ |
| 92 | + Network: types.NamespaceMode_POD, |
| 93 | + Ipc: types.NamespaceMode_POD, |
| 94 | + Pid: types.NamespaceMode_NODE, |
| 95 | + }, |
| 96 | + }, |
| 97 | + }, |
| 98 | + } |
| 99 | + |
| 100 | + sboxConfig := &types.PodSandboxConfig{} |
| 101 | + sb := &sandbox.Sandbox{} |
| 102 | + sb.AddManagedNamespaces(nsmgrtest.AllSpoofedNamespaces) |
| 103 | + |
| 104 | + // When |
| 105 | + Expect(sut.SetConfig(config, sboxConfig)).To(BeNil()) |
| 106 | + sut.Spec().ClearLinuxNamespaces() |
| 107 | + Expect(sut.SpecAddNamespaces(sb)).To(BeNil()) |
| 108 | + |
| 109 | + // Then |
| 110 | + spec := sut.Spec() |
| 111 | + Expect(len(spec.Config.Linux.Namespaces)).To(Equal(len(nsmgrtest.AllSpoofedNamespaces))) |
| 112 | + |
| 113 | + for _, specNs := range spec.Config.Linux.Namespaces { |
| 114 | + Expect(specNs.Type).NotTo(Equal(rspec.PIDNamespace)) |
| 115 | + } |
| 116 | + }) |
| 117 | + It("should use pod PID", func() { |
| 118 | + // Given |
| 119 | + config := &types.ContainerConfig{ |
| 120 | + Metadata: &types.ContainerMetadata{Name: "name"}, |
| 121 | + Linux: &types.LinuxContainerConfig{ |
| 122 | + SecurityContext: &types.LinuxContainerSecurityContext{ |
| 123 | + NamespaceOptions: &types.NamespaceOption{ |
| 124 | + Network: types.NamespaceMode_POD, |
| 125 | + Ipc: types.NamespaceMode_POD, |
| 126 | + Pid: types.NamespaceMode_POD, |
| 127 | + }, |
| 128 | + }, |
| 129 | + }, |
| 130 | + } |
| 131 | + |
| 132 | + sboxConfig := &types.PodSandboxConfig{ |
| 133 | + Linux: &types.LinuxPodSandboxConfig{ |
| 134 | + SecurityContext: &types.LinuxSandboxSecurityContext{ |
| 135 | + NamespaceOptions: &types.NamespaceOption{ |
| 136 | + Network: types.NamespaceMode_POD, |
| 137 | + Ipc: types.NamespaceMode_POD, |
| 138 | + Pid: types.NamespaceMode_POD, |
| 139 | + }, |
| 140 | + }, |
| 141 | + }, |
| 142 | + } |
| 143 | + sb := &sandbox.Sandbox{} |
| 144 | + infra, err := nsmgrtest.ContainerWithPid(os.Getpid()) |
| 145 | + Expect(err).To(BeNil()) |
| 146 | + Expect(sb.SetInfraContainer(infra)).To(BeNil()) |
| 147 | + sb.AddManagedNamespaces(nsmgrtest.AllSpoofedNamespaces) |
| 148 | + |
| 149 | + // When |
| 150 | + Expect(sut.SetConfig(config, sboxConfig)).To(BeNil()) |
| 151 | + sut.Spec().ClearLinuxNamespaces() |
| 152 | + Expect(sut.SpecAddNamespaces(sb)).To(BeNil()) |
| 153 | + |
| 154 | + // Then |
| 155 | + spec := sut.Spec() |
| 156 | + Expect(len(spec.Config.Linux.Namespaces)).To(Equal(len(nsmgrtest.AllSpoofedNamespaces) + 1)) |
| 157 | + |
| 158 | + found := false |
| 159 | + for _, specNs := range spec.Config.Linux.Namespaces { |
| 160 | + if specNs.Type == rspec.PIDNamespace { |
| 161 | + found = true |
| 162 | + } |
| 163 | + } |
| 164 | + Expect(found).To(Equal(true)) |
| 165 | + }) |
| 166 | + It("should ignore if empty", func() { |
| 167 | + // Given |
| 168 | + config := &types.ContainerConfig{ |
| 169 | + Metadata: &types.ContainerMetadata{Name: "name"}, |
| 170 | + Linux: &types.LinuxContainerConfig{ |
| 171 | + SecurityContext: &types.LinuxContainerSecurityContext{ |
| 172 | + NamespaceOptions: &types.NamespaceOption{ |
| 173 | + Network: types.NamespaceMode_POD, |
| 174 | + Ipc: types.NamespaceMode_POD, |
| 175 | + Pid: types.NamespaceMode_CONTAINER, |
| 176 | + }, |
| 177 | + }, |
| 178 | + }, |
| 179 | + } |
| 180 | + |
| 181 | + sboxConfig := &types.PodSandboxConfig{} |
| 182 | + sb := &sandbox.Sandbox{} |
| 183 | + sb.AddManagedNamespaces([]nsmgr.Namespace{&nsmgrtest.SpoofedNamespace{ |
| 184 | + NsType: nsmgr.IPCNS, |
| 185 | + EmptyPath: true, |
| 186 | + }}) |
| 187 | + |
| 188 | + // When |
| 189 | + Expect(sut.SetConfig(config, sboxConfig)).To(BeNil()) |
| 190 | + sut.Spec().ClearLinuxNamespaces() |
| 191 | + Expect(sut.SpecAddNamespaces(sb)).To(BeNil()) |
| 192 | + |
| 193 | + // Then |
| 194 | + spec := sut.Spec() |
| 195 | + Expect(len(spec.Config.Linux.Namespaces)).To(Equal(0)) |
| 196 | + }) |
| 197 | +}) |
0 commit comments