provider_runtime_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 common
  14. import (
  15. "strings"
  16. "testing"
  17. corev1 "k8s.io/api/core/v1"
  18. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  19. "github.com/external-secrets/external-secrets-e2e/framework"
  20. esv1alpha1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1alpha1"
  21. )
  22. func TestClusterProviderExternalSecretRuntimeSupportsAuthLifecycle(t *testing.T) {
  23. runtimeWithoutHooks := &ClusterProviderExternalSecretRuntime{}
  24. if runtimeWithoutHooks.SupportsAuthLifecycle() {
  25. t.Fatalf("expected SupportsAuthLifecycle to return false when both hooks are nil")
  26. }
  27. runtimeWithBreakOnly := &ClusterProviderExternalSecretRuntime{
  28. BreakAuth: func() {},
  29. }
  30. if runtimeWithBreakOnly.SupportsAuthLifecycle() {
  31. t.Fatalf("expected SupportsAuthLifecycle to return false when RepairAuth is nil")
  32. }
  33. runtimeWithRepairOnly := &ClusterProviderExternalSecretRuntime{
  34. RepairAuth: func() {},
  35. }
  36. if runtimeWithRepairOnly.SupportsAuthLifecycle() {
  37. t.Fatalf("expected SupportsAuthLifecycle to return false when BreakAuth is nil")
  38. }
  39. runtimeWithBothHooks := &ClusterProviderExternalSecretRuntime{
  40. BreakAuth: func() {},
  41. RepairAuth: func() {},
  42. }
  43. if !runtimeWithBothHooks.SupportsAuthLifecycle() {
  44. t.Fatalf("expected SupportsAuthLifecycle to return true when both hooks are present")
  45. }
  46. }
  47. func TestClusterProviderPushRuntimeSupportsAuthLifecycle(t *testing.T) {
  48. runtimeWithoutHooks := &ClusterProviderPushRuntime{}
  49. if runtimeWithoutHooks.SupportsAuthLifecycle() {
  50. t.Fatalf("expected SupportsAuthLifecycle to return false when both hooks are nil")
  51. }
  52. runtimeWithBreakOnly := &ClusterProviderPushRuntime{
  53. BreakAuth: func() {},
  54. }
  55. if runtimeWithBreakOnly.SupportsAuthLifecycle() {
  56. t.Fatalf("expected SupportsAuthLifecycle to return false when RepairAuth is nil")
  57. }
  58. runtimeWithRepairOnly := &ClusterProviderPushRuntime{
  59. RepairAuth: func() {},
  60. }
  61. if runtimeWithRepairOnly.SupportsAuthLifecycle() {
  62. t.Fatalf("expected SupportsAuthLifecycle to return false when BreakAuth is nil")
  63. }
  64. runtimeWithBothHooks := &ClusterProviderPushRuntime{
  65. BreakAuth: func() {},
  66. RepairAuth: func() {},
  67. }
  68. if !runtimeWithBothHooks.SupportsAuthLifecycle() {
  69. t.Fatalf("expected SupportsAuthLifecycle to return true when both hooks are present")
  70. }
  71. }
  72. func TestClusterProviderPushRuntimeSupportsRemoteAbsenceAssertions(t *testing.T) {
  73. runtimeWithoutExpectation := &ClusterProviderPushRuntime{}
  74. if runtimeWithoutExpectation.SupportsRemoteAbsenceAssertions() {
  75. t.Fatalf("expected SupportsRemoteAbsenceAssertions to return false when ExpectNoRemoteSecret is nil")
  76. }
  77. runtimeWithExpectation := &ClusterProviderPushRuntime{
  78. ExpectNoRemoteSecret: func(_, _ string) {},
  79. }
  80. if !runtimeWithExpectation.SupportsRemoteAbsenceAssertions() {
  81. t.Fatalf("expected SupportsRemoteAbsenceAssertions to return true when ExpectNoRemoteSecret is present")
  82. }
  83. }
  84. func TestClusterProviderPushRuntimeSupportsRemoteNamespaceOverrides(t *testing.T) {
  85. runtimeWithoutFactory := &ClusterProviderPushRuntime{}
  86. if runtimeWithoutFactory.SupportsRemoteNamespaceOverrides() {
  87. t.Fatalf("expected SupportsRemoteNamespaceOverrides to return false when CreateWritableRemoteScope is nil")
  88. }
  89. runtimeWithFactory := &ClusterProviderPushRuntime{
  90. CreateWritableRemoteScope: func(_ string) string { return "override-namespace" },
  91. }
  92. if !runtimeWithFactory.SupportsRemoteNamespaceOverrides() {
  93. t.Fatalf("expected SupportsRemoteNamespaceOverrides to return true when CreateWritableRemoteScope is present")
  94. }
  95. }
  96. func TestApplyClusterProviderPushSecretPanicsWithClearMessageWhenRuntimeNil(t *testing.T) {
  97. defer func() {
  98. recovered := recover()
  99. if recovered == nil {
  100. t.Fatalf("expected panic when runtime is nil")
  101. }
  102. message, ok := recovered.(string)
  103. if !ok {
  104. t.Fatalf("expected panic message to be string, got %T", recovered)
  105. }
  106. if !strings.Contains(message, "cluster provider push harness returned nil runtime") {
  107. t.Fatalf("expected panic message to mention nil runtime guard, got %q", message)
  108. }
  109. }()
  110. applyClusterProviderPushSecret(nil, nil, "remote-secret")
  111. }
  112. func TestApplyClusterProviderPushSecretUsesSafeObjectNameIndependentOfRemoteKey(t *testing.T) {
  113. tc := &framework.TestCase{
  114. PushSecret: &esv1alpha1.PushSecret{},
  115. PushSecretSource: &corev1.Secret{
  116. ObjectMeta: metav1.ObjectMeta{
  117. Name: "push-provider-source",
  118. },
  119. },
  120. }
  121. runtime := &ClusterProviderPushRuntime{
  122. ClusterProviderName: "push-provider-cluster-provider",
  123. }
  124. applyClusterProviderPushSecret(tc, runtime, "/e2e/test-ns/push-provider-remote")
  125. if got, want := tc.PushSecret.ObjectMeta.Name, "push-provider-source-push-secret"; got != want {
  126. t.Fatalf("expected PushSecret name %q, got %q", want, got)
  127. }
  128. if got, want := tc.PushSecret.Spec.Data[0].Match.RemoteRef.RemoteKey, "/e2e/test-ns/push-provider-remote"; got != want {
  129. t.Fatalf("expected remote key %q, got %q", want, got)
  130. }
  131. }