mayrunas_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. Copyright 2018 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 group
  14. import (
  15. "fmt"
  16. "strings"
  17. "testing"
  18. policy "k8s.io/api/policy/v1beta1"
  19. "k8s.io/apimachinery/pkg/util/validation/field"
  20. )
  21. func TestMayRunAsOptions(t *testing.T) {
  22. tests := map[string]struct {
  23. ranges []policy.IDRange
  24. pass bool
  25. }{
  26. "empty": {
  27. ranges: []policy.IDRange{},
  28. },
  29. "ranges": {
  30. ranges: []policy.IDRange{
  31. {Min: 1, Max: 1},
  32. },
  33. pass: true,
  34. },
  35. }
  36. for k, v := range tests {
  37. _, err := NewMayRunAs(v.ranges)
  38. if v.pass && err != nil {
  39. t.Errorf("error creating strategy for %s: %v", k, err)
  40. }
  41. if !v.pass && err == nil {
  42. t.Errorf("expected error for %s but got none", k)
  43. }
  44. }
  45. }
  46. func TestMayRunAsValidate(t *testing.T) {
  47. tests := map[string]struct {
  48. ranges []policy.IDRange
  49. groups []int64
  50. expectedErrors []string
  51. }{
  52. "empty groups": {
  53. ranges: []policy.IDRange{
  54. {Min: 1, Max: 3},
  55. },
  56. },
  57. "not in range": {
  58. groups: []int64{5},
  59. ranges: []policy.IDRange{
  60. {Min: 1, Max: 3},
  61. {Min: 4, Max: 4},
  62. },
  63. expectedErrors: []string{"group 5 must be in the ranges: [{1 3} {4 4}]"},
  64. },
  65. "not in ranges - multiple groups": {
  66. groups: []int64{5, 10, 2020},
  67. ranges: []policy.IDRange{
  68. {Min: 1, Max: 3},
  69. {Min: 15, Max: 70},
  70. },
  71. expectedErrors: []string{
  72. "group 5 must be in the ranges: [{1 3} {15 70}]",
  73. "group 10 must be in the ranges: [{1 3} {15 70}]",
  74. "group 2020 must be in the ranges: [{1 3} {15 70}]",
  75. },
  76. },
  77. "not in ranges - one of multiple groups does not match": {
  78. groups: []int64{5, 10, 2020},
  79. ranges: []policy.IDRange{
  80. {Min: 1, Max: 5},
  81. {Min: 8, Max: 12},
  82. {Min: 15, Max: 70},
  83. },
  84. expectedErrors: []string{
  85. "group 2020 must be in the ranges: [{1 5} {8 12} {15 70}]",
  86. },
  87. },
  88. "in range 1": {
  89. groups: []int64{2},
  90. ranges: []policy.IDRange{
  91. {Min: 1, Max: 3},
  92. },
  93. },
  94. "in range boundary min": {
  95. groups: []int64{1},
  96. ranges: []policy.IDRange{
  97. {Min: 1, Max: 3},
  98. },
  99. },
  100. "in range boundary max": {
  101. groups: []int64{3},
  102. ranges: []policy.IDRange{
  103. {Min: 1, Max: 3},
  104. },
  105. },
  106. "singular range": {
  107. groups: []int64{4},
  108. ranges: []policy.IDRange{
  109. {Min: 4, Max: 4},
  110. },
  111. },
  112. "in one of multiple ranges": {
  113. groups: []int64{4},
  114. ranges: []policy.IDRange{
  115. {Min: 1, Max: 4},
  116. {Min: 10, Max: 15},
  117. },
  118. },
  119. "multiple groups matches one range": {
  120. groups: []int64{4, 8, 12},
  121. ranges: []policy.IDRange{
  122. {Min: 1, Max: 20},
  123. },
  124. },
  125. "multiple groups match multiple ranges": {
  126. groups: []int64{4, 8, 12},
  127. ranges: []policy.IDRange{
  128. {Min: 1, Max: 4},
  129. {Min: 200, Max: 2000},
  130. {Min: 7, Max: 11},
  131. {Min: 5, Max: 7},
  132. {Min: 17, Max: 53},
  133. {Min: 12, Max: 71},
  134. },
  135. },
  136. }
  137. for k, v := range tests {
  138. s, err := NewMayRunAs(v.ranges)
  139. if err != nil {
  140. t.Errorf("error creating strategy for %s: %v", k, err)
  141. }
  142. errs := s.Validate(field.NewPath(""), nil, v.groups)
  143. if len(v.expectedErrors) != len(errs) {
  144. // number of expected errors is different from actual, includes cases when we expected errors and they appeared or vice versa
  145. t.Errorf("number of expected errors for '%s' does not match with errors received:\n"+
  146. "expected:\n%s\nbut got:\n%s",
  147. k, concatenateStrings(v.expectedErrors), concatenateErrors(errs))
  148. } else if len(v.expectedErrors) > 0 {
  149. // check that the errors received match the expectations
  150. for i, s := range v.expectedErrors {
  151. if !strings.Contains(errs[i].Error(), s) {
  152. t.Errorf("expected errors in particular order for '%s':\n%s\nbut got:\n%s",
  153. k, concatenateStrings(v.expectedErrors), concatenateErrors(errs))
  154. break
  155. }
  156. }
  157. }
  158. }
  159. }
  160. func concatenateErrors(errs field.ErrorList) string {
  161. var errStrings []string
  162. for _, e := range errs {
  163. errStrings = append(errStrings, e.Error())
  164. }
  165. return concatenateStrings(errStrings)
  166. }
  167. func concatenateStrings(ss []string) string {
  168. var ret string
  169. for i, v := range ss {
  170. ret += fmt.Sprintf("%d: %s\n", i+1, v)
  171. }
  172. return ret
  173. }