helpers_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package kuberuntime
  14. import (
  15. "path/filepath"
  16. "testing"
  17. "github.com/stretchr/testify/assert"
  18. "github.com/stretchr/testify/require"
  19. "k8s.io/api/core/v1"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. utilfeature "k8s.io/apiserver/pkg/util/feature"
  22. featuregatetesting "k8s.io/component-base/featuregate/testing"
  23. runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
  24. runtimetesting "k8s.io/cri-api/pkg/apis/testing"
  25. "k8s.io/kubernetes/pkg/features"
  26. kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
  27. )
  28. func TestStableKey(t *testing.T) {
  29. container := &v1.Container{
  30. Name: "test_container",
  31. Image: "foo/image:v1",
  32. }
  33. pod := &v1.Pod{
  34. ObjectMeta: metav1.ObjectMeta{
  35. Name: "test_pod",
  36. Namespace: "test_pod_namespace",
  37. UID: "test_pod_uid",
  38. },
  39. Spec: v1.PodSpec{
  40. Containers: []v1.Container{*container},
  41. },
  42. }
  43. oldKey := getStableKey(pod, container)
  44. // Updating the container image should change the key.
  45. container.Image = "foo/image:v2"
  46. newKey := getStableKey(pod, container)
  47. assert.NotEqual(t, oldKey, newKey)
  48. }
  49. func TestToKubeContainer(t *testing.T) {
  50. c := &runtimeapi.Container{
  51. Id: "test-id",
  52. Metadata: &runtimeapi.ContainerMetadata{
  53. Name: "test-name",
  54. Attempt: 1,
  55. },
  56. Image: &runtimeapi.ImageSpec{Image: "test-image"},
  57. ImageRef: "test-image-ref",
  58. State: runtimeapi.ContainerState_CONTAINER_RUNNING,
  59. Annotations: map[string]string{
  60. containerHashLabel: "1234",
  61. },
  62. }
  63. expect := &kubecontainer.Container{
  64. ID: kubecontainer.ContainerID{
  65. Type: runtimetesting.FakeRuntimeName,
  66. ID: "test-id",
  67. },
  68. Name: "test-name",
  69. ImageID: "test-image-ref",
  70. Image: "test-image",
  71. Hash: uint64(0x1234),
  72. State: kubecontainer.ContainerStateRunning,
  73. }
  74. _, _, m, err := createTestRuntimeManager()
  75. assert.NoError(t, err)
  76. got, err := m.toKubeContainer(c)
  77. assert.NoError(t, err)
  78. assert.Equal(t, expect, got)
  79. }
  80. func TestGetImageUser(t *testing.T) {
  81. _, i, m, err := createTestRuntimeManager()
  82. assert.NoError(t, err)
  83. type image struct {
  84. name string
  85. uid *runtimeapi.Int64Value
  86. username string
  87. }
  88. type imageUserValues struct {
  89. // getImageUser can return (*int64)(nil) so comparing with *uid will break
  90. // type cannot be *int64 as Golang does not allow to take the address of a numeric constant"
  91. uid interface{}
  92. username string
  93. err error
  94. }
  95. tests := []struct {
  96. description string
  97. originalImage image
  98. expectedImageUserValues imageUserValues
  99. }{
  100. {
  101. "image without username and uid should return (new(int64), \"\", nil)",
  102. image{
  103. name: "test-image-ref1",
  104. uid: (*runtimeapi.Int64Value)(nil),
  105. username: "",
  106. },
  107. imageUserValues{
  108. uid: int64(0),
  109. username: "",
  110. err: nil,
  111. },
  112. },
  113. {
  114. "image with username and no uid should return ((*int64)nil, imageStatus.Username, nil)",
  115. image{
  116. name: "test-image-ref2",
  117. uid: (*runtimeapi.Int64Value)(nil),
  118. username: "testUser",
  119. },
  120. imageUserValues{
  121. uid: (*int64)(nil),
  122. username: "testUser",
  123. err: nil,
  124. },
  125. },
  126. {
  127. "image with uid should return (*int64, \"\", nil)",
  128. image{
  129. name: "test-image-ref3",
  130. uid: &runtimeapi.Int64Value{
  131. Value: 2,
  132. },
  133. username: "whatever",
  134. },
  135. imageUserValues{
  136. uid: int64(2),
  137. username: "",
  138. err: nil,
  139. },
  140. },
  141. }
  142. i.SetFakeImages([]string{"test-image-ref1", "test-image-ref2", "test-image-ref3"})
  143. for j, test := range tests {
  144. i.Images[test.originalImage.name].Username = test.originalImage.username
  145. i.Images[test.originalImage.name].Uid = test.originalImage.uid
  146. uid, username, err := m.getImageUser(test.originalImage.name)
  147. assert.NoError(t, err, "TestCase[%d]", j)
  148. if test.expectedImageUserValues.uid == (*int64)(nil) {
  149. assert.Equal(t, test.expectedImageUserValues.uid, uid, "TestCase[%d]", j)
  150. } else {
  151. assert.Equal(t, test.expectedImageUserValues.uid, *uid, "TestCase[%d]", j)
  152. }
  153. assert.Equal(t, test.expectedImageUserValues.username, username, "TestCase[%d]", j)
  154. }
  155. }
  156. func TestGetSeccompProfileFromAnnotations(t *testing.T) {
  157. _, _, m, err := createTestRuntimeManager()
  158. require.NoError(t, err)
  159. tests := []struct {
  160. description string
  161. annotation map[string]string
  162. containerName string
  163. expectedProfile string
  164. }{
  165. {
  166. description: "no seccomp should return empty string",
  167. expectedProfile: "",
  168. },
  169. {
  170. description: "no seccomp with containerName should return exmpty string",
  171. containerName: "container1",
  172. expectedProfile: "",
  173. },
  174. {
  175. description: "pod runtime/default seccomp profile should return runtime/default",
  176. annotation: map[string]string{
  177. v1.SeccompPodAnnotationKey: v1.SeccompProfileRuntimeDefault,
  178. },
  179. expectedProfile: v1.SeccompProfileRuntimeDefault,
  180. },
  181. {
  182. description: "pod docker/default seccomp profile should return docker/default",
  183. annotation: map[string]string{
  184. v1.SeccompPodAnnotationKey: v1.DeprecatedSeccompProfileDockerDefault,
  185. },
  186. expectedProfile: v1.DeprecatedSeccompProfileDockerDefault,
  187. },
  188. {
  189. description: "pod runtime/default seccomp profile with containerName should return runtime/default",
  190. annotation: map[string]string{
  191. v1.SeccompPodAnnotationKey: v1.SeccompProfileRuntimeDefault,
  192. },
  193. containerName: "container1",
  194. expectedProfile: v1.SeccompProfileRuntimeDefault,
  195. },
  196. {
  197. description: "pod docker/default seccomp profile with containerName should return docker/default",
  198. annotation: map[string]string{
  199. v1.SeccompPodAnnotationKey: v1.DeprecatedSeccompProfileDockerDefault,
  200. },
  201. containerName: "container1",
  202. expectedProfile: v1.DeprecatedSeccompProfileDockerDefault,
  203. },
  204. {
  205. description: "pod unconfined seccomp profile should return unconfined",
  206. annotation: map[string]string{
  207. v1.SeccompPodAnnotationKey: "unconfined",
  208. },
  209. expectedProfile: "unconfined",
  210. },
  211. {
  212. description: "pod unconfined seccomp profile with containerName should return unconfined",
  213. annotation: map[string]string{
  214. v1.SeccompPodAnnotationKey: "unconfined",
  215. },
  216. containerName: "container1",
  217. expectedProfile: "unconfined",
  218. },
  219. {
  220. description: "pod localhost seccomp profile should return local profile path",
  221. annotation: map[string]string{
  222. v1.SeccompPodAnnotationKey: "localhost/chmod.json",
  223. },
  224. expectedProfile: "localhost/" + filepath.Join(fakeSeccompProfileRoot, "chmod.json"),
  225. },
  226. {
  227. description: "pod localhost seccomp profile with containerName should return local profile path",
  228. annotation: map[string]string{
  229. v1.SeccompPodAnnotationKey: "localhost/chmod.json",
  230. },
  231. containerName: "container1",
  232. expectedProfile: "localhost/" + filepath.Join(fakeSeccompProfileRoot, "chmod.json"),
  233. },
  234. {
  235. description: "container localhost seccomp profile with containerName should return local profile path",
  236. annotation: map[string]string{
  237. v1.SeccompContainerAnnotationKeyPrefix + "container1": "localhost/chmod.json",
  238. },
  239. containerName: "container1",
  240. expectedProfile: "localhost/" + filepath.Join(fakeSeccompProfileRoot, "chmod.json"),
  241. },
  242. {
  243. description: "container localhost seccomp profile should override pod profile",
  244. annotation: map[string]string{
  245. v1.SeccompPodAnnotationKey: "unconfined",
  246. v1.SeccompContainerAnnotationKeyPrefix + "container1": "localhost/chmod.json",
  247. },
  248. containerName: "container1",
  249. expectedProfile: "localhost/" + filepath.Join(fakeSeccompProfileRoot, "chmod.json"),
  250. },
  251. {
  252. description: "container localhost seccomp profile with unmatched containerName should return empty string",
  253. annotation: map[string]string{
  254. v1.SeccompContainerAnnotationKeyPrefix + "container1": "localhost/chmod.json",
  255. },
  256. containerName: "container2",
  257. expectedProfile: "",
  258. },
  259. }
  260. for i, test := range tests {
  261. seccompProfile := m.getSeccompProfileFromAnnotations(test.annotation, test.containerName)
  262. assert.Equal(t, test.expectedProfile, seccompProfile, "TestCase[%d]", i)
  263. }
  264. }
  265. func TestNamespacesForPod(t *testing.T) {
  266. defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.PodShareProcessNamespace, true)()
  267. for desc, test := range map[string]struct {
  268. input *v1.Pod
  269. expected *runtimeapi.NamespaceOption
  270. }{
  271. "nil pod -> default v1 namespaces": {
  272. nil,
  273. &runtimeapi.NamespaceOption{
  274. Ipc: runtimeapi.NamespaceMode_POD,
  275. Network: runtimeapi.NamespaceMode_POD,
  276. Pid: runtimeapi.NamespaceMode_CONTAINER,
  277. },
  278. },
  279. "v1.Pod default namespaces": {
  280. &v1.Pod{},
  281. &runtimeapi.NamespaceOption{
  282. Ipc: runtimeapi.NamespaceMode_POD,
  283. Network: runtimeapi.NamespaceMode_POD,
  284. Pid: runtimeapi.NamespaceMode_CONTAINER,
  285. },
  286. },
  287. "Host Namespaces": {
  288. &v1.Pod{
  289. Spec: v1.PodSpec{
  290. HostIPC: true,
  291. HostNetwork: true,
  292. HostPID: true,
  293. },
  294. },
  295. &runtimeapi.NamespaceOption{
  296. Ipc: runtimeapi.NamespaceMode_NODE,
  297. Network: runtimeapi.NamespaceMode_NODE,
  298. Pid: runtimeapi.NamespaceMode_NODE,
  299. },
  300. },
  301. "Shared Process Namespace (feature enabled)": {
  302. &v1.Pod{
  303. Spec: v1.PodSpec{
  304. ShareProcessNamespace: &[]bool{true}[0],
  305. },
  306. },
  307. &runtimeapi.NamespaceOption{
  308. Ipc: runtimeapi.NamespaceMode_POD,
  309. Network: runtimeapi.NamespaceMode_POD,
  310. Pid: runtimeapi.NamespaceMode_POD,
  311. },
  312. },
  313. "Shared Process Namespace, redundant flag (feature enabled)": {
  314. &v1.Pod{
  315. Spec: v1.PodSpec{
  316. ShareProcessNamespace: &[]bool{false}[0],
  317. },
  318. },
  319. &runtimeapi.NamespaceOption{
  320. Ipc: runtimeapi.NamespaceMode_POD,
  321. Network: runtimeapi.NamespaceMode_POD,
  322. Pid: runtimeapi.NamespaceMode_CONTAINER,
  323. },
  324. },
  325. } {
  326. t.Logf("TestCase: %s", desc)
  327. actual := namespacesForPod(test.input)
  328. assert.Equal(t, test.expected, actual)
  329. }
  330. defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.PodShareProcessNamespace, false)()
  331. for desc, test := range map[string]struct {
  332. input *v1.Pod
  333. expected *runtimeapi.NamespaceOption
  334. }{
  335. "v1.Pod default namespaces": {
  336. &v1.Pod{},
  337. &runtimeapi.NamespaceOption{
  338. Ipc: runtimeapi.NamespaceMode_POD,
  339. Network: runtimeapi.NamespaceMode_POD,
  340. Pid: runtimeapi.NamespaceMode_CONTAINER,
  341. },
  342. },
  343. "Shared Process Namespace (feature disabled)": {
  344. &v1.Pod{
  345. Spec: v1.PodSpec{
  346. ShareProcessNamespace: &[]bool{true}[0],
  347. },
  348. },
  349. &runtimeapi.NamespaceOption{
  350. Ipc: runtimeapi.NamespaceMode_POD,
  351. Network: runtimeapi.NamespaceMode_POD,
  352. Pid: runtimeapi.NamespaceMode_CONTAINER,
  353. },
  354. },
  355. "Shared Process Namespace, redundant flag (feature disabled)": {
  356. &v1.Pod{
  357. Spec: v1.PodSpec{
  358. ShareProcessNamespace: &[]bool{false}[0],
  359. },
  360. },
  361. &runtimeapi.NamespaceOption{
  362. Ipc: runtimeapi.NamespaceMode_POD,
  363. Network: runtimeapi.NamespaceMode_POD,
  364. Pid: runtimeapi.NamespaceMode_CONTAINER,
  365. },
  366. },
  367. } {
  368. t.Logf("TestCase: %s", desc)
  369. actual := namespacesForPod(test.input)
  370. assert.Equal(t, test.expected, actual)
  371. }
  372. }