util_test.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 util
  14. import (
  15. "reflect"
  16. "testing"
  17. policy "k8s.io/api/policy/v1beta1"
  18. api "k8s.io/kubernetes/pkg/apis/core"
  19. )
  20. // TestVolumeSourceFSTypeDrift ensures that for every known type of volume source (by the fields on
  21. // a VolumeSource object that GetVolumeFSType is returning a good value. This ensures both that we're
  22. // returning an FSType for the VolumeSource field (protect the GetVolumeFSType method) and that we
  23. // haven't drifted (ensure new fields in VolumeSource are covered).
  24. func TestVolumeSourceFSTypeDrift(t *testing.T) {
  25. allFSTypes := GetAllFSTypesAsSet()
  26. val := reflect.ValueOf(api.VolumeSource{})
  27. for i := 0; i < val.NumField(); i++ {
  28. fieldVal := val.Type().Field(i)
  29. volumeSource := api.VolumeSource{}
  30. volumeSourceVolume := reflect.New(fieldVal.Type.Elem())
  31. reflect.ValueOf(&volumeSource).Elem().FieldByName(fieldVal.Name).Set(volumeSourceVolume)
  32. fsType, err := GetVolumeFSType(api.Volume{VolumeSource: volumeSource})
  33. if err != nil {
  34. t.Errorf("error getting fstype for field %s. This likely means that drift has occurred between FSType and VolumeSource. Please update the api and getVolumeFSType", fieldVal.Name)
  35. }
  36. if !allFSTypes.Has(string(fsType)) {
  37. t.Errorf("%s was missing from GetFSTypesAsSet", fsType)
  38. }
  39. }
  40. }
  41. func TestPSPAllowsFSType(t *testing.T) {
  42. tests := map[string]struct {
  43. psp *policy.PodSecurityPolicy
  44. fsType policy.FSType
  45. allows bool
  46. }{
  47. "nil psp": {
  48. psp: nil,
  49. fsType: policy.HostPath,
  50. allows: false,
  51. },
  52. "empty volumes": {
  53. psp: &policy.PodSecurityPolicy{},
  54. fsType: policy.HostPath,
  55. allows: false,
  56. },
  57. "non-matching": {
  58. psp: &policy.PodSecurityPolicy{
  59. Spec: policy.PodSecurityPolicySpec{
  60. Volumes: []policy.FSType{policy.AWSElasticBlockStore},
  61. },
  62. },
  63. fsType: policy.HostPath,
  64. allows: false,
  65. },
  66. "match on FSTypeAll": {
  67. psp: &policy.PodSecurityPolicy{
  68. Spec: policy.PodSecurityPolicySpec{
  69. Volumes: []policy.FSType{policy.All},
  70. },
  71. },
  72. fsType: policy.HostPath,
  73. allows: true,
  74. },
  75. "match on direct match": {
  76. psp: &policy.PodSecurityPolicy{
  77. Spec: policy.PodSecurityPolicySpec{
  78. Volumes: []policy.FSType{policy.HostPath},
  79. },
  80. },
  81. fsType: policy.HostPath,
  82. allows: true,
  83. },
  84. }
  85. for k, v := range tests {
  86. allows := PSPAllowsFSType(v.psp, v.fsType)
  87. if v.allows != allows {
  88. t.Errorf("%s expected PSPAllowsFSType to return %t but got %t", k, v.allows, allows)
  89. }
  90. }
  91. }
  92. func TestAllowsHostVolumePath(t *testing.T) {
  93. tests := map[string]struct {
  94. psp *policy.PodSecurityPolicy
  95. path string
  96. allows bool
  97. mustBeReadOnly bool
  98. }{
  99. "nil psp": {
  100. psp: nil,
  101. path: "/test",
  102. allows: false,
  103. mustBeReadOnly: false,
  104. },
  105. "empty allowed paths": {
  106. psp: &policy.PodSecurityPolicy{},
  107. path: "/test",
  108. allows: true,
  109. mustBeReadOnly: false,
  110. },
  111. "non-matching": {
  112. psp: &policy.PodSecurityPolicy{
  113. Spec: policy.PodSecurityPolicySpec{
  114. AllowedHostPaths: []policy.AllowedHostPath{
  115. {
  116. PathPrefix: "/foo",
  117. ReadOnly: true,
  118. },
  119. },
  120. },
  121. },
  122. path: "/foobar",
  123. allows: false,
  124. mustBeReadOnly: false,
  125. },
  126. "match on direct match": {
  127. psp: &policy.PodSecurityPolicy{
  128. Spec: policy.PodSecurityPolicySpec{
  129. AllowedHostPaths: []policy.AllowedHostPath{
  130. {
  131. PathPrefix: "/foo",
  132. ReadOnly: true,
  133. },
  134. },
  135. },
  136. },
  137. path: "/foo",
  138. allows: true,
  139. mustBeReadOnly: true,
  140. },
  141. "match with trailing slash on host path": {
  142. psp: &policy.PodSecurityPolicy{
  143. Spec: policy.PodSecurityPolicySpec{
  144. AllowedHostPaths: []policy.AllowedHostPath{
  145. {PathPrefix: "/foo"},
  146. },
  147. },
  148. },
  149. path: "/foo/",
  150. allows: true,
  151. mustBeReadOnly: false,
  152. },
  153. "match with trailing slash on allowed path": {
  154. psp: &policy.PodSecurityPolicy{
  155. Spec: policy.PodSecurityPolicySpec{
  156. AllowedHostPaths: []policy.AllowedHostPath{
  157. {PathPrefix: "/foo/"},
  158. },
  159. },
  160. },
  161. path: "/foo",
  162. allows: true,
  163. mustBeReadOnly: false,
  164. },
  165. "match child directory": {
  166. psp: &policy.PodSecurityPolicy{
  167. Spec: policy.PodSecurityPolicySpec{
  168. AllowedHostPaths: []policy.AllowedHostPath{
  169. {
  170. PathPrefix: "/foo/",
  171. ReadOnly: true,
  172. },
  173. },
  174. },
  175. },
  176. path: "/foo/bar",
  177. allows: true,
  178. mustBeReadOnly: true,
  179. },
  180. "non-matching parent directory": {
  181. psp: &policy.PodSecurityPolicy{
  182. Spec: policy.PodSecurityPolicySpec{
  183. AllowedHostPaths: []policy.AllowedHostPath{
  184. {PathPrefix: "/foo/bar"},
  185. },
  186. },
  187. },
  188. path: "/foo",
  189. allows: false,
  190. mustBeReadOnly: false,
  191. },
  192. }
  193. for k, v := range tests {
  194. allows, mustBeReadOnly := AllowsHostVolumePath(v.psp, v.path)
  195. if v.allows != allows {
  196. t.Errorf("allows: %s expected %t but got %t", k, v.allows, allows)
  197. }
  198. if v.mustBeReadOnly != mustBeReadOnly {
  199. t.Errorf("mustBeReadOnly: %s expected %t but got %t", k, v.mustBeReadOnly, mustBeReadOnly)
  200. }
  201. }
  202. }
  203. func TestEqualStringSlices(t *testing.T) {
  204. tests := map[string]struct {
  205. arg1 []string
  206. arg2 []string
  207. expectedResult bool
  208. }{
  209. "nil equals to nil": {
  210. arg1: nil,
  211. arg2: nil,
  212. expectedResult: true,
  213. },
  214. "equal by size": {
  215. arg1: []string{"1", "1"},
  216. arg2: []string{"1", "1"},
  217. expectedResult: true,
  218. },
  219. "not equal by size": {
  220. arg1: []string{"1"},
  221. arg2: []string{"1", "1"},
  222. expectedResult: false,
  223. },
  224. "not equal by elements": {
  225. arg1: []string{"1", "1"},
  226. arg2: []string{"1", "2"},
  227. expectedResult: false,
  228. },
  229. }
  230. for k, v := range tests {
  231. if result := EqualStringSlices(v.arg1, v.arg2); result != v.expectedResult {
  232. t.Errorf("%s expected to return %t but got %t", k, v.expectedResult, result)
  233. }
  234. }
  235. }