webhook_test.go 3.6 KB

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