mustrunas_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 user
  14. import (
  15. policy "k8s.io/api/policy/v1beta1"
  16. api "k8s.io/kubernetes/pkg/apis/core"
  17. "strings"
  18. "testing"
  19. )
  20. func TestNewMustRunAs(t *testing.T) {
  21. tests := map[string]struct {
  22. opts *policy.RunAsUserStrategyOptions
  23. pass bool
  24. }{
  25. "nil opts": {
  26. opts: nil,
  27. pass: false,
  28. },
  29. "invalid opts": {
  30. opts: &policy.RunAsUserStrategyOptions{},
  31. pass: false,
  32. },
  33. "valid opts": {
  34. opts: &policy.RunAsUserStrategyOptions{
  35. Ranges: []policy.IDRange{
  36. {Min: 1, Max: 1},
  37. },
  38. },
  39. pass: true,
  40. },
  41. }
  42. for name, tc := range tests {
  43. _, err := NewMustRunAs(tc.opts)
  44. if err != nil && tc.pass {
  45. t.Errorf("%s expected to pass but received error %#v", name, err)
  46. }
  47. if err == nil && !tc.pass {
  48. t.Errorf("%s expected to fail but did not receive an error", name)
  49. }
  50. }
  51. }
  52. func TestGenerate(t *testing.T) {
  53. opts := &policy.RunAsUserStrategyOptions{
  54. Ranges: []policy.IDRange{
  55. {Min: 1, Max: 1},
  56. },
  57. }
  58. mustRunAs, err := NewMustRunAs(opts)
  59. if err != nil {
  60. t.Fatalf("unexpected error initializing NewMustRunAs %v", err)
  61. }
  62. generated, err := mustRunAs.Generate(nil, nil)
  63. if err != nil {
  64. t.Fatalf("unexpected error generating runAsUser %v", err)
  65. }
  66. if *generated != opts.Ranges[0].Min {
  67. t.Errorf("generated runAsUser does not equal configured runAsUser")
  68. }
  69. }
  70. func TestValidate(t *testing.T) {
  71. opts := &policy.RunAsUserStrategyOptions{
  72. Ranges: []policy.IDRange{
  73. {Min: 1, Max: 1},
  74. {Min: 10, Max: 20},
  75. },
  76. }
  77. validID := int64(15)
  78. invalidID := int64(21)
  79. tests := map[string]struct {
  80. container *api.Container
  81. expectedMsg string
  82. }{
  83. "good container": {
  84. container: &api.Container{
  85. SecurityContext: &api.SecurityContext{
  86. RunAsUser: &validID,
  87. },
  88. },
  89. },
  90. "nil run as user": {
  91. container: &api.Container{
  92. SecurityContext: &api.SecurityContext{
  93. RunAsUser: nil,
  94. },
  95. },
  96. expectedMsg: "runAsUser: Required",
  97. },
  98. "invalid id": {
  99. container: &api.Container{
  100. SecurityContext: &api.SecurityContext{
  101. RunAsUser: &invalidID,
  102. },
  103. },
  104. expectedMsg: "runAsUser: Invalid",
  105. },
  106. }
  107. for name, tc := range tests {
  108. mustRunAs, err := NewMustRunAs(opts)
  109. if err != nil {
  110. t.Errorf("unexpected error initializing NewMustRunAs for testcase %s: %#v", name, err)
  111. continue
  112. }
  113. errs := mustRunAs.Validate(nil, nil, nil, tc.container.SecurityContext.RunAsNonRoot, tc.container.SecurityContext.RunAsUser)
  114. //should've passed but didn't
  115. if len(tc.expectedMsg) == 0 && len(errs) > 0 {
  116. t.Errorf("%s expected no errors but received %v", name, errs)
  117. }
  118. //should've failed but didn't
  119. if len(tc.expectedMsg) != 0 && len(errs) == 0 {
  120. t.Errorf("%s expected error %s but received no errors", name, tc.expectedMsg)
  121. }
  122. //failed with additional messages
  123. if len(tc.expectedMsg) != 0 && len(errs) > 1 {
  124. t.Errorf("%s expected error %s but received multiple errors: %v", name, tc.expectedMsg, errs)
  125. }
  126. //check that we got the right message
  127. if len(tc.expectedMsg) != 0 && len(errs) == 1 {
  128. if !strings.Contains(errs[0].Error(), tc.expectedMsg) {
  129. t.Errorf("%s expected error to contain %s but it did not: %v", name, tc.expectedMsg, errs)
  130. }
  131. }
  132. }
  133. }