helpers_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. /*
  2. Copyright 2015 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 container
  14. import (
  15. "reflect"
  16. "testing"
  17. "github.com/stretchr/testify/assert"
  18. "k8s.io/api/core/v1"
  19. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  20. )
  21. func TestEnvVarsToMap(t *testing.T) {
  22. vars := []EnvVar{
  23. {
  24. Name: "foo",
  25. Value: "bar",
  26. },
  27. {
  28. Name: "zoo",
  29. Value: "baz",
  30. },
  31. }
  32. varMap := EnvVarsToMap(vars)
  33. if e, a := len(vars), len(varMap); e != a {
  34. t.Errorf("Unexpected map length; expected: %d, got %d", e, a)
  35. }
  36. if a := varMap["foo"]; a != "bar" {
  37. t.Errorf("Unexpected value of key 'foo': %v", a)
  38. }
  39. if a := varMap["zoo"]; a != "baz" {
  40. t.Errorf("Unexpected value of key 'zoo': %v", a)
  41. }
  42. }
  43. func TestExpandCommandAndArgs(t *testing.T) {
  44. cases := []struct {
  45. name string
  46. container *v1.Container
  47. envs []EnvVar
  48. expectedCommand []string
  49. expectedArgs []string
  50. }{
  51. {
  52. name: "none",
  53. container: &v1.Container{},
  54. },
  55. {
  56. name: "command expanded",
  57. container: &v1.Container{
  58. Command: []string{"foo", "$(VAR_TEST)", "$(VAR_TEST2)"},
  59. },
  60. envs: []EnvVar{
  61. {
  62. Name: "VAR_TEST",
  63. Value: "zoo",
  64. },
  65. {
  66. Name: "VAR_TEST2",
  67. Value: "boo",
  68. },
  69. },
  70. expectedCommand: []string{"foo", "zoo", "boo"},
  71. },
  72. {
  73. name: "args expanded",
  74. container: &v1.Container{
  75. Args: []string{"zap", "$(VAR_TEST)", "$(VAR_TEST2)"},
  76. },
  77. envs: []EnvVar{
  78. {
  79. Name: "VAR_TEST",
  80. Value: "hap",
  81. },
  82. {
  83. Name: "VAR_TEST2",
  84. Value: "trap",
  85. },
  86. },
  87. expectedArgs: []string{"zap", "hap", "trap"},
  88. },
  89. {
  90. name: "both expanded",
  91. container: &v1.Container{
  92. Command: []string{"$(VAR_TEST2)--$(VAR_TEST)", "foo", "$(VAR_TEST3)"},
  93. Args: []string{"foo", "$(VAR_TEST)", "$(VAR_TEST2)"},
  94. },
  95. envs: []EnvVar{
  96. {
  97. Name: "VAR_TEST",
  98. Value: "zoo",
  99. },
  100. {
  101. Name: "VAR_TEST2",
  102. Value: "boo",
  103. },
  104. {
  105. Name: "VAR_TEST3",
  106. Value: "roo",
  107. },
  108. },
  109. expectedCommand: []string{"boo--zoo", "foo", "roo"},
  110. expectedArgs: []string{"foo", "zoo", "boo"},
  111. },
  112. }
  113. for _, tc := range cases {
  114. actualCommand, actualArgs := ExpandContainerCommandAndArgs(tc.container, tc.envs)
  115. if e, a := tc.expectedCommand, actualCommand; !reflect.DeepEqual(e, a) {
  116. t.Errorf("%v: unexpected command; expected %v, got %v", tc.name, e, a)
  117. }
  118. if e, a := tc.expectedArgs, actualArgs; !reflect.DeepEqual(e, a) {
  119. t.Errorf("%v: unexpected args; expected %v, got %v", tc.name, e, a)
  120. }
  121. }
  122. }
  123. func TestExpandVolumeMountsWithSubpath(t *testing.T) {
  124. cases := []struct {
  125. name string
  126. container *v1.Container
  127. envs []EnvVar
  128. expectedSubPath string
  129. expectedMountPath string
  130. expectedOk bool
  131. }{
  132. {
  133. name: "subpath with no expansion",
  134. container: &v1.Container{
  135. VolumeMounts: []v1.VolumeMount{{SubPathExpr: "foo"}},
  136. },
  137. expectedSubPath: "foo",
  138. expectedMountPath: "",
  139. expectedOk: true,
  140. },
  141. {
  142. name: "volumes with expanded subpath",
  143. container: &v1.Container{
  144. VolumeMounts: []v1.VolumeMount{{SubPathExpr: "foo/$(POD_NAME)"}},
  145. },
  146. envs: []EnvVar{
  147. {
  148. Name: "POD_NAME",
  149. Value: "bar",
  150. },
  151. },
  152. expectedSubPath: "foo/bar",
  153. expectedMountPath: "",
  154. expectedOk: true,
  155. },
  156. {
  157. name: "volumes expanded with empty subpath",
  158. container: &v1.Container{
  159. VolumeMounts: []v1.VolumeMount{{SubPathExpr: ""}},
  160. },
  161. envs: []EnvVar{
  162. {
  163. Name: "POD_NAME",
  164. Value: "bar",
  165. },
  166. },
  167. expectedSubPath: "",
  168. expectedMountPath: "",
  169. expectedOk: true,
  170. },
  171. {
  172. name: "volumes expanded with no envs subpath",
  173. container: &v1.Container{
  174. VolumeMounts: []v1.VolumeMount{{SubPathExpr: "/foo/$(POD_NAME)"}},
  175. },
  176. expectedSubPath: "/foo/$(POD_NAME)",
  177. expectedMountPath: "",
  178. expectedOk: false,
  179. },
  180. {
  181. name: "volumes expanded with leading environment variable",
  182. container: &v1.Container{
  183. VolumeMounts: []v1.VolumeMount{{SubPathExpr: "$(POD_NAME)/bar"}},
  184. },
  185. envs: []EnvVar{
  186. {
  187. Name: "POD_NAME",
  188. Value: "foo",
  189. },
  190. },
  191. expectedSubPath: "foo/bar",
  192. expectedMountPath: "",
  193. expectedOk: true,
  194. },
  195. {
  196. name: "volumes with volume and subpath",
  197. container: &v1.Container{
  198. VolumeMounts: []v1.VolumeMount{{MountPath: "/foo", SubPathExpr: "$(POD_NAME)/bar"}},
  199. },
  200. envs: []EnvVar{
  201. {
  202. Name: "POD_NAME",
  203. Value: "foo",
  204. },
  205. },
  206. expectedSubPath: "foo/bar",
  207. expectedMountPath: "/foo",
  208. expectedOk: true,
  209. },
  210. {
  211. name: "volumes with volume and no subpath",
  212. container: &v1.Container{
  213. VolumeMounts: []v1.VolumeMount{{MountPath: "/foo"}},
  214. },
  215. envs: []EnvVar{
  216. {
  217. Name: "POD_NAME",
  218. Value: "foo",
  219. },
  220. },
  221. expectedSubPath: "",
  222. expectedMountPath: "/foo",
  223. expectedOk: true,
  224. },
  225. {
  226. name: "subpaths with empty environment variable",
  227. container: &v1.Container{
  228. VolumeMounts: []v1.VolumeMount{{SubPathExpr: "foo/$(POD_NAME)/$(ANNOTATION)"}},
  229. },
  230. envs: []EnvVar{
  231. {
  232. Name: "ANNOTATION",
  233. Value: "",
  234. },
  235. },
  236. expectedSubPath: "foo/$(POD_NAME)/$(ANNOTATION)",
  237. expectedMountPath: "",
  238. expectedOk: false,
  239. },
  240. {
  241. name: "subpaths with missing env variables",
  242. container: &v1.Container{
  243. VolumeMounts: []v1.VolumeMount{{SubPathExpr: "foo/$(ODD_NAME)/$(POD_NAME)"}},
  244. },
  245. envs: []EnvVar{
  246. {
  247. Name: "ODD_NAME",
  248. Value: "bar",
  249. },
  250. },
  251. expectedSubPath: "foo/$(ODD_NAME)/$(POD_NAME)",
  252. expectedMountPath: "",
  253. expectedOk: false,
  254. },
  255. {
  256. name: "subpaths with empty expansion",
  257. container: &v1.Container{
  258. VolumeMounts: []v1.VolumeMount{{SubPathExpr: "$()"}},
  259. },
  260. expectedSubPath: "$()",
  261. expectedMountPath: "",
  262. expectedOk: false,
  263. },
  264. {
  265. name: "subpaths with nested expandable envs",
  266. container: &v1.Container{
  267. VolumeMounts: []v1.VolumeMount{{SubPathExpr: "$(POD_NAME$(ANNOTATION))"}},
  268. },
  269. envs: []EnvVar{
  270. {
  271. Name: "POD_NAME",
  272. Value: "foo",
  273. },
  274. {
  275. Name: "ANNOTATION",
  276. Value: "bar",
  277. },
  278. },
  279. expectedSubPath: "$(POD_NAME$(ANNOTATION))",
  280. expectedMountPath: "",
  281. expectedOk: false,
  282. },
  283. }
  284. for _, tc := range cases {
  285. actualSubPath, err := ExpandContainerVolumeMounts(tc.container.VolumeMounts[0], tc.envs)
  286. ok := err == nil
  287. if e, a := tc.expectedOk, ok; !reflect.DeepEqual(e, a) {
  288. t.Errorf("%v: unexpected validation failure of subpath; expected %v, got %v", tc.name, e, a)
  289. }
  290. if !ok {
  291. // if ExpandContainerVolumeMounts returns an error, we don't care what the actualSubPath value is
  292. continue
  293. }
  294. if e, a := tc.expectedSubPath, actualSubPath; !reflect.DeepEqual(e, a) {
  295. t.Errorf("%v: unexpected subpath; expected %v, got %v", tc.name, e, a)
  296. }
  297. if e, a := tc.expectedMountPath, tc.container.VolumeMounts[0].MountPath; !reflect.DeepEqual(e, a) {
  298. t.Errorf("%v: unexpected mountpath; expected %v, got %v", tc.name, e, a)
  299. }
  300. }
  301. }
  302. func TestShouldContainerBeRestarted(t *testing.T) {
  303. pod := &v1.Pod{
  304. ObjectMeta: metav1.ObjectMeta{
  305. UID: "12345678",
  306. Name: "foo",
  307. Namespace: "new",
  308. },
  309. Spec: v1.PodSpec{
  310. Containers: []v1.Container{
  311. {Name: "no-history"},
  312. {Name: "alive"},
  313. {Name: "succeed"},
  314. {Name: "failed"},
  315. {Name: "unknown"},
  316. },
  317. },
  318. }
  319. podStatus := &PodStatus{
  320. ID: pod.UID,
  321. Name: pod.Name,
  322. Namespace: pod.Namespace,
  323. ContainerStatuses: []*ContainerStatus{
  324. {
  325. Name: "alive",
  326. State: ContainerStateRunning,
  327. },
  328. {
  329. Name: "succeed",
  330. State: ContainerStateExited,
  331. ExitCode: 0,
  332. },
  333. {
  334. Name: "failed",
  335. State: ContainerStateExited,
  336. ExitCode: 1,
  337. },
  338. {
  339. Name: "alive",
  340. State: ContainerStateExited,
  341. ExitCode: 2,
  342. },
  343. {
  344. Name: "unknown",
  345. State: ContainerStateUnknown,
  346. },
  347. {
  348. Name: "failed",
  349. State: ContainerStateExited,
  350. ExitCode: 3,
  351. },
  352. },
  353. }
  354. policies := []v1.RestartPolicy{
  355. v1.RestartPolicyNever,
  356. v1.RestartPolicyOnFailure,
  357. v1.RestartPolicyAlways,
  358. }
  359. expected := map[string][]bool{
  360. "no-history": {true, true, true},
  361. "alive": {false, false, false},
  362. "succeed": {false, false, true},
  363. "failed": {false, true, true},
  364. "unknown": {true, true, true},
  365. }
  366. for _, c := range pod.Spec.Containers {
  367. for i, policy := range policies {
  368. pod.Spec.RestartPolicy = policy
  369. e := expected[c.Name][i]
  370. r := ShouldContainerBeRestarted(&c, pod, podStatus)
  371. if r != e {
  372. t.Errorf("Restart for container %q with restart policy %q expected %t, got %t",
  373. c.Name, policy, e, r)
  374. }
  375. }
  376. }
  377. }
  378. func TestHasPrivilegedContainer(t *testing.T) {
  379. newBoolPtr := func(b bool) *bool {
  380. return &b
  381. }
  382. tests := map[string]struct {
  383. securityContext *v1.SecurityContext
  384. expected bool
  385. }{
  386. "nil security context": {
  387. securityContext: nil,
  388. expected: false,
  389. },
  390. "nil privileged": {
  391. securityContext: &v1.SecurityContext{},
  392. expected: false,
  393. },
  394. "false privileged": {
  395. securityContext: &v1.SecurityContext{Privileged: newBoolPtr(false)},
  396. expected: false,
  397. },
  398. "true privileged": {
  399. securityContext: &v1.SecurityContext{Privileged: newBoolPtr(true)},
  400. expected: true,
  401. },
  402. }
  403. for k, v := range tests {
  404. pod := &v1.Pod{
  405. Spec: v1.PodSpec{
  406. Containers: []v1.Container{
  407. {SecurityContext: v.securityContext},
  408. },
  409. },
  410. }
  411. actual := HasPrivilegedContainer(pod)
  412. if actual != v.expected {
  413. t.Errorf("%s expected %t but got %t", k, v.expected, actual)
  414. }
  415. }
  416. // Test init containers as well.
  417. for k, v := range tests {
  418. pod := &v1.Pod{
  419. Spec: v1.PodSpec{
  420. InitContainers: []v1.Container{
  421. {SecurityContext: v.securityContext},
  422. },
  423. },
  424. }
  425. actual := HasPrivilegedContainer(pod)
  426. if actual != v.expected {
  427. t.Errorf("%s expected %t but got %t", k, v.expected, actual)
  428. }
  429. }
  430. }
  431. func TestMakePortMappings(t *testing.T) {
  432. port := func(name string, protocol v1.Protocol, containerPort, hostPort int32, ip string) v1.ContainerPort {
  433. return v1.ContainerPort{
  434. Name: name,
  435. Protocol: protocol,
  436. ContainerPort: containerPort,
  437. HostPort: hostPort,
  438. HostIP: ip,
  439. }
  440. }
  441. portMapping := func(name string, protocol v1.Protocol, containerPort, hostPort int, ip string) PortMapping {
  442. return PortMapping{
  443. Name: name,
  444. Protocol: protocol,
  445. ContainerPort: containerPort,
  446. HostPort: hostPort,
  447. HostIP: ip,
  448. }
  449. }
  450. tests := []struct {
  451. container *v1.Container
  452. expectedPortMappings []PortMapping
  453. }{
  454. {
  455. &v1.Container{
  456. Name: "fooContainer",
  457. Ports: []v1.ContainerPort{
  458. port("", v1.ProtocolTCP, 80, 8080, "127.0.0.1"),
  459. port("", v1.ProtocolTCP, 443, 4343, "192.168.0.1"),
  460. port("foo", v1.ProtocolUDP, 555, 5555, ""),
  461. // Duplicated, should be ignored.
  462. port("foo", v1.ProtocolUDP, 888, 8888, ""),
  463. // Duplicated, should be ignored.
  464. port("", v1.ProtocolTCP, 80, 8888, ""),
  465. },
  466. },
  467. []PortMapping{
  468. portMapping("fooContainer-TCP:80", v1.ProtocolTCP, 80, 8080, "127.0.0.1"),
  469. portMapping("fooContainer-TCP:443", v1.ProtocolTCP, 443, 4343, "192.168.0.1"),
  470. portMapping("fooContainer-foo", v1.ProtocolUDP, 555, 5555, ""),
  471. },
  472. },
  473. }
  474. for i, tt := range tests {
  475. actual := MakePortMappings(tt.container)
  476. assert.Equal(t, tt.expectedPortMappings, actual, "[%d]", i)
  477. }
  478. }