webhook_test.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 TestWaitForExternalSecretWebhookReadyRetriesUntilOK(t *testing.T) {
  22. t.Helper()
  23. originalURL := externalSecretWebhookURL
  24. originalPollInterval := webhookReadyPollInterval
  25. originalTimeout := webhookReadyTimeout
  26. originalContext := webhookReadyContext
  27. t.Cleanup(func() {
  28. externalSecretWebhookURL = originalURL
  29. webhookReadyPollInterval = originalPollInterval
  30. webhookReadyTimeout = originalTimeout
  31. webhookReadyContext = originalContext
  32. })
  33. attempts := 0
  34. server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  35. attempts++
  36. if attempts < 3 {
  37. http.Error(w, "not ready", http.StatusServiceUnavailable)
  38. return
  39. }
  40. w.WriteHeader(http.StatusOK)
  41. }))
  42. defer server.Close()
  43. externalSecretWebhookURL = func(string) string { return server.URL }
  44. webhookReadyPollInterval = 10 * time.Millisecond
  45. webhookReadyTimeout = time.Second
  46. webhookReadyContext = context.Background
  47. if err := waitForExternalSecretWebhookReady("external-secrets-system"); err != nil {
  48. t.Fatalf("waitForExternalSecretWebhookReady returned error: %v", err)
  49. }
  50. if attempts != 3 {
  51. t.Fatalf("expected 3 webhook attempts, got %d", attempts)
  52. }
  53. }
  54. func TestWaitForExternalSecretWebhookReadyTimesOut(t *testing.T) {
  55. t.Helper()
  56. originalURL := externalSecretWebhookURL
  57. originalPollInterval := webhookReadyPollInterval
  58. originalTimeout := webhookReadyTimeout
  59. originalContext := webhookReadyContext
  60. t.Cleanup(func() {
  61. externalSecretWebhookURL = originalURL
  62. webhookReadyPollInterval = originalPollInterval
  63. webhookReadyTimeout = originalTimeout
  64. webhookReadyContext = originalContext
  65. })
  66. server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  67. http.Error(w, "not ready", http.StatusServiceUnavailable)
  68. }))
  69. defer server.Close()
  70. externalSecretWebhookURL = func(string) string { return server.URL }
  71. webhookReadyPollInterval = 10 * time.Millisecond
  72. webhookReadyTimeout = 50 * time.Millisecond
  73. webhookReadyContext = context.Background
  74. if err := waitForExternalSecretWebhookReady("external-secrets-system"); err == nil {
  75. t.Fatal("expected waitForExternalSecretWebhookReady to time out")
  76. }
  77. }