util_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. Copyright 2014 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 securitycontext
  14. import (
  15. "testing"
  16. "k8s.io/kubernetes/pkg/api"
  17. )
  18. func TestParseSELinuxOptions(t *testing.T) {
  19. cases := []struct {
  20. name string
  21. input string
  22. expected *api.SELinuxOptions
  23. }{
  24. {
  25. name: "simple",
  26. input: "user_t:role_t:type_t:s0",
  27. expected: &api.SELinuxOptions{
  28. User: "user_t",
  29. Role: "role_t",
  30. Type: "type_t",
  31. Level: "s0",
  32. },
  33. },
  34. {
  35. name: "simple + categories",
  36. input: "user_t:role_t:type_t:s0:c0",
  37. expected: &api.SELinuxOptions{
  38. User: "user_t",
  39. Role: "role_t",
  40. Type: "type_t",
  41. Level: "s0:c0",
  42. },
  43. },
  44. {
  45. name: "not enough fields",
  46. input: "type_t:s0:c0",
  47. },
  48. }
  49. for _, tc := range cases {
  50. result, err := ParseSELinuxOptions(tc.input)
  51. if err != nil {
  52. if tc.expected == nil {
  53. continue
  54. } else {
  55. t.Errorf("%v: unexpected error: %v", tc.name, err)
  56. }
  57. }
  58. compareContexts(tc.name, tc.expected, result, t)
  59. }
  60. }
  61. func compareContexts(name string, ex, ac *api.SELinuxOptions, t *testing.T) {
  62. if e, a := ex.User, ac.User; e != a {
  63. t.Errorf("%v: expected user: %v, got: %v", name, e, a)
  64. }
  65. if e, a := ex.Role, ac.Role; e != a {
  66. t.Errorf("%v: expected role: %v, got: %v", name, e, a)
  67. }
  68. if e, a := ex.Type, ac.Type; e != a {
  69. t.Errorf("%v: expected type: %v, got: %v", name, e, a)
  70. }
  71. if e, a := ex.Level, ac.Level; e != a {
  72. t.Errorf("%v: expected level: %v, got: %v", name, e, a)
  73. }
  74. }
  75. func containerWithUser(ptr *int64) *api.Container {
  76. return &api.Container{SecurityContext: &api.SecurityContext{RunAsUser: ptr}}
  77. }
  78. func TestHaRootUID(t *testing.T) {
  79. var nonRoot int64 = 1
  80. var root int64 = 0
  81. tests := map[string]struct {
  82. container *api.Container
  83. expect bool
  84. }{
  85. "nil sc": {
  86. container: &api.Container{SecurityContext: nil},
  87. },
  88. "nil runAsuser": {
  89. container: containerWithUser(nil),
  90. },
  91. "runAsUser non-root": {
  92. container: containerWithUser(&nonRoot),
  93. },
  94. "runAsUser root": {
  95. container: containerWithUser(&root),
  96. expect: true,
  97. },
  98. }
  99. for k, v := range tests {
  100. actual := HasRootUID(v.container)
  101. if actual != v.expect {
  102. t.Errorf("%s failed, expected %t but received %t", k, v.expect, actual)
  103. }
  104. }
  105. }
  106. func TestHasRunAsUser(t *testing.T) {
  107. var runAsUser int64 = 0
  108. tests := map[string]struct {
  109. container *api.Container
  110. expect bool
  111. }{
  112. "nil sc": {
  113. container: &api.Container{SecurityContext: nil},
  114. },
  115. "nil runAsUser": {
  116. container: containerWithUser(nil),
  117. },
  118. "valid runAsUser": {
  119. container: containerWithUser(&runAsUser),
  120. expect: true,
  121. },
  122. }
  123. for k, v := range tests {
  124. actual := HasRunAsUser(v.container)
  125. if actual != v.expect {
  126. t.Errorf("%s failed, expected %t but received %t", k, v.expect, actual)
  127. }
  128. }
  129. }
  130. func TestHasRootRunAsUser(t *testing.T) {
  131. var nonRoot int64 = 1
  132. var root int64 = 0
  133. tests := map[string]struct {
  134. container *api.Container
  135. expect bool
  136. }{
  137. "nil sc": {
  138. container: &api.Container{SecurityContext: nil},
  139. },
  140. "nil runAsuser": {
  141. container: containerWithUser(nil),
  142. },
  143. "runAsUser non-root": {
  144. container: containerWithUser(&nonRoot),
  145. },
  146. "runAsUser root": {
  147. container: containerWithUser(&root),
  148. expect: true,
  149. },
  150. }
  151. for k, v := range tests {
  152. actual := HasRootRunAsUser(v.container)
  153. if actual != v.expect {
  154. t.Errorf("%s failed, expected %t but received %t", k, v.expect, actual)
  155. }
  156. }
  157. }