webhook_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 addon
  14. import (
  15. "context"
  16. "net/http"
  17. "net/http/httptest"
  18. "testing"
  19. "time"
  20. )
  21. func TestExternalSecretWebhookURLUsesReleaseName(t *testing.T) {
  22. t.Helper()
  23. got := externalSecretWebhookURL("default", "eso")
  24. want := "https://eso-external-secrets-webhook.default.svc.cluster.local/validate-external-secrets-io-v1-externalsecret"
  25. if got != want {
  26. t.Fatalf("unexpected webhook URL: got %q want %q", got, want)
  27. }
  28. }
  29. func TestExternalSecretWebhookURLUsesHelmFullnameWhenReleaseContainsChartName(t *testing.T) {
  30. t.Helper()
  31. got := externalSecretWebhookURL("external-secrets-system", "external-secrets")
  32. want := "https://external-secrets-webhook.external-secrets-system.svc.cluster.local/validate-external-secrets-io-v1-externalsecret"
  33. if got != want {
  34. t.Fatalf("unexpected webhook URL: got %q want %q", got, want)
  35. }
  36. }
  37. func TestWaitForExternalSecretWebhookReadyRetriesUntilOK(t *testing.T) {
  38. t.Helper()
  39. originalURL := externalSecretWebhookURL
  40. originalPollInterval := webhookReadyPollInterval
  41. originalTimeout := webhookReadyTimeout
  42. originalContext := webhookReadyContext
  43. t.Cleanup(func() {
  44. externalSecretWebhookURL = originalURL
  45. webhookReadyPollInterval = originalPollInterval
  46. webhookReadyTimeout = originalTimeout
  47. webhookReadyContext = originalContext
  48. })
  49. attempts := 0
  50. server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  51. attempts++
  52. if attempts < 3 {
  53. http.Error(w, "not ready", http.StatusServiceUnavailable)
  54. return
  55. }
  56. w.WriteHeader(http.StatusOK)
  57. }))
  58. defer server.Close()
  59. externalSecretWebhookURL = func(string, string) string { return server.URL }
  60. webhookReadyPollInterval = 10 * time.Millisecond
  61. webhookReadyTimeout = time.Second
  62. webhookReadyContext = context.Background
  63. if err := waitForExternalSecretWebhookReady("external-secrets-system", "external-secrets"); err != nil {
  64. t.Fatalf("waitForExternalSecretWebhookReady returned error: %v", err)
  65. }
  66. if attempts != 3 {
  67. t.Fatalf("expected 3 webhook attempts, got %d", attempts)
  68. }
  69. }
  70. func TestWaitForExternalSecretWebhookReadyTimesOut(t *testing.T) {
  71. t.Helper()
  72. originalURL := externalSecretWebhookURL
  73. originalPollInterval := webhookReadyPollInterval
  74. originalTimeout := webhookReadyTimeout
  75. originalContext := webhookReadyContext
  76. t.Cleanup(func() {
  77. externalSecretWebhookURL = originalURL
  78. webhookReadyPollInterval = originalPollInterval
  79. webhookReadyTimeout = originalTimeout
  80. webhookReadyContext = originalContext
  81. })
  82. server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  83. http.Error(w, "not ready", http.StatusServiceUnavailable)
  84. }))
  85. defer server.Close()
  86. externalSecretWebhookURL = func(string, string) string { return server.URL }
  87. webhookReadyPollInterval = 10 * time.Millisecond
  88. webhookReadyTimeout = 50 * time.Millisecond
  89. webhookReadyContext = context.Background
  90. if err := waitForExternalSecretWebhookReady("external-secrets-system", "external-secrets"); err == nil {
  91. t.Fatal("expected waitForExternalSecretWebhookReady to time out")
  92. }
  93. }