strategy_test.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 apparmor
  14. import (
  15. "testing"
  16. "github.com/davecgh/go-spew/spew"
  17. "github.com/stretchr/testify/assert"
  18. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  19. api "k8s.io/kubernetes/pkg/apis/core"
  20. "k8s.io/kubernetes/pkg/security/apparmor"
  21. "k8s.io/kubernetes/pkg/util/maps"
  22. )
  23. const (
  24. containerName = "test-c"
  25. )
  26. var (
  27. withoutAppArmor = map[string]string{"foo": "bar"}
  28. withDefault = map[string]string{
  29. "foo": "bar",
  30. apparmor.ContainerAnnotationKeyPrefix + containerName: apparmor.ProfileRuntimeDefault,
  31. }
  32. withLocal = map[string]string{
  33. "foo": "bar",
  34. apparmor.ContainerAnnotationKeyPrefix + containerName: apparmor.ProfileNamePrefix + "foo",
  35. }
  36. withDisallowed = map[string]string{
  37. "foo": "bar",
  38. apparmor.ContainerAnnotationKeyPrefix + containerName: apparmor.ProfileNamePrefix + "bad",
  39. }
  40. noAppArmor = map[string]string{"foo": "bar"}
  41. unconstrainedWithDefault = map[string]string{
  42. apparmor.DefaultProfileAnnotationKey: apparmor.ProfileRuntimeDefault,
  43. }
  44. constrained = map[string]string{
  45. apparmor.AllowedProfilesAnnotationKey: apparmor.ProfileRuntimeDefault + "," +
  46. apparmor.ProfileNamePrefix + "foo",
  47. }
  48. constrainedWithDefault = map[string]string{
  49. apparmor.DefaultProfileAnnotationKey: apparmor.ProfileRuntimeDefault,
  50. apparmor.AllowedProfilesAnnotationKey: apparmor.ProfileRuntimeDefault + "," +
  51. apparmor.ProfileNamePrefix + "foo",
  52. }
  53. container = api.Container{
  54. Name: containerName,
  55. Image: "busybox",
  56. }
  57. )
  58. func TestGenerate(t *testing.T) {
  59. type testcase struct {
  60. pspAnnotations map[string]string
  61. podAnnotations map[string]string
  62. expected map[string]string
  63. }
  64. tests := []testcase{{
  65. pspAnnotations: noAppArmor,
  66. podAnnotations: withoutAppArmor,
  67. expected: withoutAppArmor,
  68. }, {
  69. pspAnnotations: unconstrainedWithDefault,
  70. podAnnotations: withoutAppArmor,
  71. expected: withDefault,
  72. }, {
  73. pspAnnotations: constrained,
  74. podAnnotations: withoutAppArmor,
  75. expected: withoutAppArmor,
  76. }, {
  77. pspAnnotations: constrainedWithDefault,
  78. podAnnotations: withoutAppArmor,
  79. expected: withDefault,
  80. }}
  81. // Add unchanging permutations.
  82. for _, podAnnotations := range []map[string]string{withDefault, withLocal} {
  83. for _, pspAnnotations := range []map[string]string{noAppArmor, unconstrainedWithDefault, constrained, constrainedWithDefault} {
  84. tests = append(tests, testcase{
  85. pspAnnotations: pspAnnotations,
  86. podAnnotations: podAnnotations,
  87. expected: podAnnotations,
  88. })
  89. }
  90. }
  91. for i, test := range tests {
  92. s := NewStrategy(test.pspAnnotations)
  93. msgAndArgs := []interface{}{"testcase[%d]: %s", i, spew.Sdump(test)}
  94. actual, err := s.Generate(test.podAnnotations, &container)
  95. assert.NoError(t, err, msgAndArgs...)
  96. assert.Equal(t, test.expected, actual, msgAndArgs...)
  97. }
  98. }
  99. func TestValidate(t *testing.T) {
  100. type testcase struct {
  101. pspAnnotations map[string]string
  102. podAnnotations map[string]string
  103. expectErr bool
  104. }
  105. tests := []testcase{}
  106. // Valid combinations
  107. for _, podAnnotations := range []map[string]string{withDefault, withLocal} {
  108. for _, pspAnnotations := range []map[string]string{noAppArmor, unconstrainedWithDefault, constrained, constrainedWithDefault} {
  109. tests = append(tests, testcase{
  110. pspAnnotations: pspAnnotations,
  111. podAnnotations: podAnnotations,
  112. expectErr: false,
  113. })
  114. }
  115. }
  116. for _, podAnnotations := range []map[string]string{withoutAppArmor, withDisallowed} {
  117. for _, pspAnnotations := range []map[string]string{noAppArmor, unconstrainedWithDefault} {
  118. tests = append(tests, testcase{
  119. pspAnnotations: pspAnnotations,
  120. podAnnotations: podAnnotations,
  121. expectErr: false,
  122. })
  123. }
  124. }
  125. // Invalid combinations
  126. for _, podAnnotations := range []map[string]string{withoutAppArmor, withDisallowed} {
  127. for _, pspAnnotations := range []map[string]string{constrained, constrainedWithDefault} {
  128. tests = append(tests, testcase{
  129. pspAnnotations: pspAnnotations,
  130. podAnnotations: podAnnotations,
  131. expectErr: true,
  132. })
  133. }
  134. }
  135. for i, test := range tests {
  136. s := NewStrategy(test.pspAnnotations)
  137. pod, container := makeTestPod(test.podAnnotations)
  138. msgAndArgs := []interface{}{"testcase[%d]: %s", i, spew.Sdump(test)}
  139. errs := s.Validate(pod, container)
  140. if test.expectErr {
  141. assert.Len(t, errs, 1, msgAndArgs...)
  142. } else {
  143. assert.Len(t, errs, 0, msgAndArgs...)
  144. }
  145. }
  146. }
  147. func makeTestPod(annotations map[string]string) (*api.Pod, *api.Container) {
  148. return &api.Pod{
  149. ObjectMeta: metav1.ObjectMeta{
  150. Name: "test-pod",
  151. Annotations: maps.CopySS(annotations),
  152. },
  153. Spec: api.PodSpec{
  154. Containers: []api.Container{container},
  155. },
  156. }, &container
  157. }