util_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. "reflect"
  16. "testing"
  17. "k8s.io/api/core/v1"
  18. )
  19. func TestAddNoNewPrivileges(t *testing.T) {
  20. pfalse := false
  21. ptrue := true
  22. tests := map[string]struct {
  23. sc *v1.SecurityContext
  24. expect bool
  25. }{
  26. "allowPrivilegeEscalation nil security context nil": {
  27. sc: nil,
  28. expect: false,
  29. },
  30. "allowPrivilegeEscalation nil": {
  31. sc: &v1.SecurityContext{
  32. AllowPrivilegeEscalation: nil,
  33. },
  34. expect: false,
  35. },
  36. "allowPrivilegeEscalation false": {
  37. sc: &v1.SecurityContext{
  38. AllowPrivilegeEscalation: &pfalse,
  39. },
  40. expect: true,
  41. },
  42. "allowPrivilegeEscalation true": {
  43. sc: &v1.SecurityContext{
  44. AllowPrivilegeEscalation: &ptrue,
  45. },
  46. expect: false,
  47. },
  48. }
  49. for k, v := range tests {
  50. actual := AddNoNewPrivileges(v.sc)
  51. if actual != v.expect {
  52. t.Errorf("%s failed, expected %t but received %t", k, v.expect, actual)
  53. }
  54. }
  55. }
  56. func TestConvertToRuntimeMaskedPaths(t *testing.T) {
  57. dPM := v1.DefaultProcMount
  58. uPM := v1.UnmaskedProcMount
  59. tests := map[string]struct {
  60. pm *v1.ProcMountType
  61. expect []string
  62. }{
  63. "procMount nil": {
  64. pm: nil,
  65. expect: defaultMaskedPaths,
  66. },
  67. "procMount default": {
  68. pm: &dPM,
  69. expect: defaultMaskedPaths,
  70. },
  71. "procMount unmasked": {
  72. pm: &uPM,
  73. expect: []string{},
  74. },
  75. }
  76. for k, v := range tests {
  77. actual := ConvertToRuntimeMaskedPaths(v.pm)
  78. if !reflect.DeepEqual(actual, v.expect) {
  79. t.Errorf("%s failed, expected %#v but received %#v", k, v.expect, actual)
  80. }
  81. }
  82. }
  83. func TestConvertToRuntimeReadonlyPaths(t *testing.T) {
  84. dPM := v1.DefaultProcMount
  85. uPM := v1.UnmaskedProcMount
  86. tests := map[string]struct {
  87. pm *v1.ProcMountType
  88. expect []string
  89. }{
  90. "procMount nil": {
  91. pm: nil,
  92. expect: defaultReadonlyPaths,
  93. },
  94. "procMount default": {
  95. pm: &dPM,
  96. expect: defaultReadonlyPaths,
  97. },
  98. "procMount unmasked": {
  99. pm: &uPM,
  100. expect: []string{},
  101. },
  102. }
  103. for k, v := range tests {
  104. actual := ConvertToRuntimeReadonlyPaths(v.pm)
  105. if !reflect.DeepEqual(actual, v.expect) {
  106. t.Errorf("%s failed, expected %#v but received %#v", k, v.expect, actual)
  107. }
  108. }
  109. }