helpers_test.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
  22. runtimetesting "k8s.io/cri-api/pkg/apis/testing"
  23. kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
  24. )
  25. func TestStableKey(t *testing.T) {
  26. container := &v1.Container{
  27. Name: "test_container",
  28. Image: "foo/image:v1",
  29. }
  30. pod := &v1.Pod{
  31. ObjectMeta: metav1.ObjectMeta{
  32. Name: "test_pod",
  33. Namespace: "test_pod_namespace",
  34. UID: "test_pod_uid",
  35. },
  36. Spec: v1.PodSpec{
  37. Containers: []v1.Container{*container},
  38. },
  39. }
  40. oldKey := getStableKey(pod, container)
  41. // Updating the container image should change the key.
  42. container.Image = "foo/image:v2"
  43. newKey := getStableKey(pod, container)
  44. assert.NotEqual(t, oldKey, newKey)
  45. }
  46. func TestToKubeContainer(t *testing.T) {
  47. c := &runtimeapi.Container{
  48. Id: "test-id",
  49. Metadata: &runtimeapi.ContainerMetadata{
  50. Name: "test-name",
  51. Attempt: 1,
  52. },
  53. Image: &runtimeapi.ImageSpec{Image: "test-image"},
  54. ImageRef: "test-image-ref",
  55. State: runtimeapi.ContainerState_CONTAINER_RUNNING,
  56. Annotations: map[string]string{
  57. containerHashLabel: "1234",
  58. },
  59. }
  60. expect := &kubecontainer.Container{
  61. ID: kubecontainer.ContainerID{
  62. Type: runtimetesting.FakeRuntimeName,
  63. ID: "test-id",
  64. },
  65. Name: "test-name",
  66. ImageID: "test-image-ref",
  67. Image: "test-image",
  68. Hash: uint64(0x1234),
  69. State: kubecontainer.ContainerStateRunning,
  70. }
  71. _, _, m, err := createTestRuntimeManager()
  72. assert.NoError(t, err)
  73. got, err := m.toKubeContainer(c)
  74. assert.NoError(t, err)
  75. assert.Equal(t, expect, got)
  76. }
  77. func TestGetImageUser(t *testing.T) {
  78. _, i, m, err := createTestRuntimeManager()
  79. assert.NoError(t, err)
  80. type image struct {
  81. name string
  82. uid *runtimeapi.Int64Value
  83. username string
  84. }
  85. type imageUserValues struct {
  86. // getImageUser can return (*int64)(nil) so comparing with *uid will break
  87. // type cannot be *int64 as Golang does not allow to take the address of a numeric constant"
  88. uid interface{}
  89. username string
  90. err error
  91. }
  92. tests := []struct {
  93. description string
  94. originalImage image
  95. expectedImageUserValues imageUserValues
  96. }{
  97. {
  98. "image without username and uid should return (new(int64), \"\", nil)",
  99. image{
  100. name: "test-image-ref1",
  101. uid: (*runtimeapi.Int64Value)(nil),
  102. username: "",
  103. },
  104. imageUserValues{
  105. uid: int64(0),
  106. username: "",
  107. err: nil,
  108. },
  109. },
  110. {
  111. "image with username and no uid should return ((*int64)nil, imageStatus.Username, nil)",
  112. image{
  113. name: "test-image-ref2",
  114. uid: (*runtimeapi.Int64Value)(nil),
  115. username: "testUser",
  116. },
  117. imageUserValues{
  118. uid: (*int64)(nil),
  119. username: "testUser",
  120. err: nil,
  121. },
  122. },
  123. {
  124. "image with uid should return (*int64, \"\", nil)",
  125. image{
  126. name: "test-image-ref3",
  127. uid: &runtimeapi.Int64Value{
  128. Value: 2,
  129. },
  130. username: "whatever",
  131. },
  132. imageUserValues{
  133. uid: int64(2),
  134. username: "",
  135. err: nil,
  136. },
  137. },
  138. }
  139. i.SetFakeImages([]string{"test-image-ref1", "test-image-ref2", "test-image-ref3"})
  140. for j, test := range tests {
  141. i.Images[test.originalImage.name].Username = test.originalImage.username
  142. i.Images[test.originalImage.name].Uid = test.originalImage.uid
  143. uid, username, err := m.getImageUser(test.originalImage.name)
  144. assert.NoError(t, err, "TestCase[%d]", j)
  145. if test.expectedImageUserValues.uid == (*int64)(nil) {
  146. assert.Equal(t, test.expectedImageUserValues.uid, uid, "TestCase[%d]", j)
  147. } else {
  148. assert.Equal(t, test.expectedImageUserValues.uid, *uid, "TestCase[%d]", j)
  149. }
  150. assert.Equal(t, test.expectedImageUserValues.username, username, "TestCase[%d]", j)
  151. }
  152. }
  153. func TestGetSeccompProfileFromAnnotations(t *testing.T) {
  154. _, _, m, err := createTestRuntimeManager()
  155. require.NoError(t, err)
  156. tests := []struct {
  157. description string
  158. annotation map[string]string
  159. containerName string
  160. expectedProfile string
  161. }{
  162. {
  163. description: "no seccomp should return empty string",
  164. expectedProfile: "",
  165. },
  166. {
  167. description: "no seccomp with containerName should return exmpty string",
  168. containerName: "container1",
  169. expectedProfile: "",
  170. },
  171. {
  172. description: "pod runtime/default seccomp profile should return runtime/default",
  173. annotation: map[string]string{
  174. v1.SeccompPodAnnotationKey: v1.SeccompProfileRuntimeDefault,
  175. },
  176. expectedProfile: v1.SeccompProfileRuntimeDefault,
  177. },
  178. {
  179. description: "pod docker/default seccomp profile should return docker/default",
  180. annotation: map[string]string{
  181. v1.SeccompPodAnnotationKey: v1.DeprecatedSeccompProfileDockerDefault,
  182. },
  183. expectedProfile: v1.DeprecatedSeccompProfileDockerDefault,
  184. },
  185. {
  186. description: "pod runtime/default seccomp profile with containerName should return runtime/default",
  187. annotation: map[string]string{
  188. v1.SeccompPodAnnotationKey: v1.SeccompProfileRuntimeDefault,
  189. },
  190. containerName: "container1",
  191. expectedProfile: v1.SeccompProfileRuntimeDefault,
  192. },
  193. {
  194. description: "pod docker/default seccomp profile with containerName should return docker/default",
  195. annotation: map[string]string{
  196. v1.SeccompPodAnnotationKey: v1.DeprecatedSeccompProfileDockerDefault,
  197. },
  198. containerName: "container1",
  199. expectedProfile: v1.DeprecatedSeccompProfileDockerDefault,
  200. },
  201. {
  202. description: "pod unconfined seccomp profile should return unconfined",
  203. annotation: map[string]string{
  204. v1.SeccompPodAnnotationKey: "unconfined",
  205. },
  206. expectedProfile: "unconfined",
  207. },
  208. {
  209. description: "pod unconfined seccomp profile with containerName should return unconfined",
  210. annotation: map[string]string{
  211. v1.SeccompPodAnnotationKey: "unconfined",
  212. },
  213. containerName: "container1",
  214. expectedProfile: "unconfined",
  215. },
  216. {
  217. description: "pod localhost seccomp profile should return local profile path",
  218. annotation: map[string]string{
  219. v1.SeccompPodAnnotationKey: "localhost/chmod.json",
  220. },
  221. expectedProfile: "localhost/" + filepath.Join(fakeSeccompProfileRoot, "chmod.json"),
  222. },
  223. {
  224. description: "pod localhost seccomp profile with containerName should return local profile path",
  225. annotation: map[string]string{
  226. v1.SeccompPodAnnotationKey: "localhost/chmod.json",
  227. },
  228. containerName: "container1",
  229. expectedProfile: "localhost/" + filepath.Join(fakeSeccompProfileRoot, "chmod.json"),
  230. },
  231. {
  232. description: "container localhost seccomp profile with containerName should return local profile path",
  233. annotation: map[string]string{
  234. v1.SeccompContainerAnnotationKeyPrefix + "container1": "localhost/chmod.json",
  235. },
  236. containerName: "container1",
  237. expectedProfile: "localhost/" + filepath.Join(fakeSeccompProfileRoot, "chmod.json"),
  238. },
  239. {
  240. description: "container localhost seccomp profile should override pod profile",
  241. annotation: map[string]string{
  242. v1.SeccompPodAnnotationKey: "unconfined",
  243. v1.SeccompContainerAnnotationKeyPrefix + "container1": "localhost/chmod.json",
  244. },
  245. containerName: "container1",
  246. expectedProfile: "localhost/" + filepath.Join(fakeSeccompProfileRoot, "chmod.json"),
  247. },
  248. {
  249. description: "container localhost seccomp profile with unmatched containerName should return empty string",
  250. annotation: map[string]string{
  251. v1.SeccompContainerAnnotationKeyPrefix + "container1": "localhost/chmod.json",
  252. },
  253. containerName: "container2",
  254. expectedProfile: "",
  255. },
  256. }
  257. for i, test := range tests {
  258. seccompProfile := m.getSeccompProfileFromAnnotations(test.annotation, test.containerName)
  259. assert.Equal(t, test.expectedProfile, seccompProfile, "TestCase[%d]", i)
  260. }
  261. }
  262. func TestNamespacesForPod(t *testing.T) {
  263. for desc, test := range map[string]struct {
  264. input *v1.Pod
  265. expected *runtimeapi.NamespaceOption
  266. }{
  267. "nil pod -> default v1 namespaces": {
  268. nil,
  269. &runtimeapi.NamespaceOption{
  270. Ipc: runtimeapi.NamespaceMode_POD,
  271. Network: runtimeapi.NamespaceMode_POD,
  272. Pid: runtimeapi.NamespaceMode_CONTAINER,
  273. },
  274. },
  275. "v1.Pod default namespaces": {
  276. &v1.Pod{},
  277. &runtimeapi.NamespaceOption{
  278. Ipc: runtimeapi.NamespaceMode_POD,
  279. Network: runtimeapi.NamespaceMode_POD,
  280. Pid: runtimeapi.NamespaceMode_CONTAINER,
  281. },
  282. },
  283. "Host Namespaces": {
  284. &v1.Pod{
  285. Spec: v1.PodSpec{
  286. HostIPC: true,
  287. HostNetwork: true,
  288. HostPID: true,
  289. },
  290. },
  291. &runtimeapi.NamespaceOption{
  292. Ipc: runtimeapi.NamespaceMode_NODE,
  293. Network: runtimeapi.NamespaceMode_NODE,
  294. Pid: runtimeapi.NamespaceMode_NODE,
  295. },
  296. },
  297. "Shared Process Namespace (feature enabled)": {
  298. &v1.Pod{
  299. Spec: v1.PodSpec{
  300. ShareProcessNamespace: &[]bool{true}[0],
  301. },
  302. },
  303. &runtimeapi.NamespaceOption{
  304. Ipc: runtimeapi.NamespaceMode_POD,
  305. Network: runtimeapi.NamespaceMode_POD,
  306. Pid: runtimeapi.NamespaceMode_POD,
  307. },
  308. },
  309. "Shared Process Namespace, redundant flag (feature enabled)": {
  310. &v1.Pod{
  311. Spec: v1.PodSpec{
  312. ShareProcessNamespace: &[]bool{false}[0],
  313. },
  314. },
  315. &runtimeapi.NamespaceOption{
  316. Ipc: runtimeapi.NamespaceMode_POD,
  317. Network: runtimeapi.NamespaceMode_POD,
  318. Pid: runtimeapi.NamespaceMode_CONTAINER,
  319. },
  320. },
  321. } {
  322. t.Logf("TestCase: %s", desc)
  323. actual := namespacesForPod(test.input)
  324. assert.Equal(t, test.expected, actual)
  325. }
  326. }