externalsecret_controller_watch_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. Copyright © The ESO 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. https://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 externalsecret
  14. import (
  15. "testing"
  16. "github.com/stretchr/testify/assert"
  17. "github.com/stretchr/testify/require"
  18. "k8s.io/apimachinery/pkg/runtime/schema"
  19. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  20. )
  21. func TestGetTargetResourceIndex(t *testing.T) {
  22. tests := []struct {
  23. name string
  24. es *esv1.ExternalSecret
  25. expectedValues []string
  26. }{
  27. {
  28. name: "ConfigMap target",
  29. es: &esv1.ExternalSecret{
  30. Spec: esv1.ExternalSecretSpec{
  31. Target: esv1.ExternalSecretTarget{
  32. Name: "my-configmap",
  33. Manifest: &esv1.ManifestReference{
  34. APIVersion: "v1",
  35. Kind: "ConfigMap",
  36. },
  37. },
  38. },
  39. },
  40. expectedValues: []string{"/v1/ConfigMap/my-configmap"},
  41. },
  42. {
  43. name: "ArgoCD Application target",
  44. es: &esv1.ExternalSecret{
  45. Spec: esv1.ExternalSecretSpec{
  46. Target: esv1.ExternalSecretTarget{
  47. Name: "my-app",
  48. Manifest: &esv1.ManifestReference{
  49. APIVersion: "argoproj.io/v1alpha1",
  50. Kind: "Application",
  51. },
  52. },
  53. },
  54. },
  55. expectedValues: []string{"argoproj.io/v1alpha1/Application/my-app"},
  56. },
  57. {
  58. name: "Secret target (no manifest)",
  59. es: &esv1.ExternalSecret{
  60. Spec: esv1.ExternalSecretSpec{
  61. Target: esv1.ExternalSecretTarget{
  62. Name: "my-secret",
  63. Manifest: nil,
  64. },
  65. },
  66. },
  67. expectedValues: nil, // Secrets don't get indexed for generic resources
  68. },
  69. }
  70. for _, tt := range tests {
  71. t.Run(tt.name, func(t *testing.T) {
  72. if !isGenericTarget(tt.es) {
  73. assert.Nil(t, tt.expectedValues)
  74. return
  75. }
  76. gvk := getTargetGVK(tt.es)
  77. targetName := getTargetName(tt.es)
  78. indexValue := gvk.Group + "/" + gvk.Version + "/" + gvk.Kind + "/" + targetName
  79. require.Len(t, tt.expectedValues, 1)
  80. assert.Equal(t, tt.expectedValues[0], indexValue)
  81. })
  82. }
  83. }
  84. func TestGVKFromManifestTarget(t *testing.T) {
  85. tests := []struct {
  86. name string
  87. manifest *esv1.ManifestReference
  88. expectedGVK schema.GroupVersionKind
  89. }{
  90. {
  91. name: "Core v1 ConfigMap",
  92. manifest: &esv1.ManifestReference{
  93. APIVersion: "v1",
  94. Kind: "ConfigMap",
  95. },
  96. expectedGVK: schema.GroupVersionKind{
  97. Group: "",
  98. Version: "v1",
  99. Kind: "ConfigMap",
  100. },
  101. },
  102. {
  103. name: "ArgoCD Application",
  104. manifest: &esv1.ManifestReference{
  105. APIVersion: "argoproj.io/v1alpha1",
  106. Kind: "Application",
  107. },
  108. expectedGVK: schema.GroupVersionKind{
  109. Group: "argoproj.io",
  110. Version: "v1alpha1",
  111. Kind: "Application",
  112. },
  113. },
  114. {
  115. name: "Networking v1 Ingress",
  116. manifest: &esv1.ManifestReference{
  117. APIVersion: "networking.k8s.io/v1",
  118. Kind: "Ingress",
  119. },
  120. expectedGVK: schema.GroupVersionKind{
  121. Group: "networking.k8s.io",
  122. Version: "v1",
  123. Kind: "Ingress",
  124. },
  125. },
  126. }
  127. for _, tt := range tests {
  128. t.Run(tt.name, func(t *testing.T) {
  129. es := &esv1.ExternalSecret{
  130. Spec: esv1.ExternalSecretSpec{
  131. Target: esv1.ExternalSecretTarget{
  132. Manifest: tt.manifest,
  133. },
  134. },
  135. }
  136. gvk := getTargetGVK(es)
  137. assert.Equal(t, tt.expectedGVK.Group, gvk.Group)
  138. assert.Equal(t, tt.expectedGVK.Version, gvk.Version)
  139. assert.Equal(t, tt.expectedGVK.Kind, gvk.Kind)
  140. })
  141. }
  142. }