|
| 1 | +// Copyright 2026 Google LLC |
| 2 | +// |
| 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 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package instances |
| 16 | + |
| 17 | +import ( |
| 18 | + "net/http" |
| 19 | + "net/http/httputil" |
| 20 | + "testing" |
| 21 | + "time" |
| 22 | +) |
| 23 | + |
| 24 | +type mockHostClient struct { |
| 25 | + getFunc func(string, string, *HostResponse) (int, error) |
| 26 | +} |
| 27 | + |
| 28 | +func (c *mockHostClient) Get(path, query string, res *HostResponse) (int, error) { |
| 29 | + return c.getFunc(path, query, res) |
| 30 | +} |
| 31 | + |
| 32 | +func (c *mockHostClient) Post(path, query string, body any, res *HostResponse) (int, error) { |
| 33 | + return 0, nil |
| 34 | +} |
| 35 | + |
| 36 | + |
| 37 | +func (c *mockHostClient) GetReverseProxy() *httputil.ReverseProxy { |
| 38 | + return nil |
| 39 | +} |
| 40 | + |
| 41 | +func TestWaitForHostReadySucceeds(t *testing.T) { |
| 42 | + client := &mockHostClient{ |
| 43 | + getFunc: func(path, query string, res *HostResponse) (int, error) { |
| 44 | + return http.StatusOK, nil |
| 45 | + }, |
| 46 | + } |
| 47 | + |
| 48 | + err := WaitForHostReady(&defaultReadinessChecker{client}, 100*time.Millisecond, 1*time.Millisecond) |
| 49 | + if err != nil { |
| 50 | + t.Errorf("unexpected error: %v", err) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +func TestWaitForHostReadyRetriesOnBadGateway(t *testing.T) { |
| 55 | + calls := 0 |
| 56 | + client := &mockHostClient{ |
| 57 | + getFunc: func(path, query string, res *HostResponse) (int, error) { |
| 58 | + calls++ |
| 59 | + if calls == 1 { |
| 60 | + return http.StatusBadGateway, nil |
| 61 | + } |
| 62 | + return http.StatusOK, nil |
| 63 | + }, |
| 64 | + } |
| 65 | + |
| 66 | + err := WaitForHostReady(&defaultReadinessChecker{client}, 100*time.Millisecond, 1*time.Millisecond) |
| 67 | + if err != nil { |
| 68 | + t.Errorf("unexpected error: %v", err) |
| 69 | + } |
| 70 | + if calls != 2 { |
| 71 | + t.Errorf("expected 2 calls, got %d", calls) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +func TestWaitForHostReadyRetriesOnServiceUnavailable(t *testing.T) { |
| 76 | + calls := 0 |
| 77 | + client := &mockHostClient{ |
| 78 | + getFunc: func(path, query string, res *HostResponse) (int, error) { |
| 79 | + calls++ |
| 80 | + if calls == 1 { |
| 81 | + return http.StatusServiceUnavailable, nil |
| 82 | + } |
| 83 | + return http.StatusOK, nil |
| 84 | + }, |
| 85 | + } |
| 86 | + |
| 87 | + err := WaitForHostReady(&defaultReadinessChecker{client}, 100*time.Millisecond, 1*time.Millisecond) |
| 88 | + if err != nil { |
| 89 | + t.Errorf("unexpected error: %v", err) |
| 90 | + } |
| 91 | + if calls != 2 { |
| 92 | + t.Errorf("expected 2 calls, got %d", calls) |
| 93 | + } |
| 94 | +} |
0 commit comments