| 1 | /* |
| 2 | Copyright 2023 The Kubernetes Authors. |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package framework |
| 18 | |
| 19 | import ( |
| 20 | "fmt" |
| 21 | "net" |
| 22 | "net/http" |
| 23 | "strconv" |
| 24 | "time" |
| 25 | ) |
| 26 | |
| 27 | var ForeverTestTimeout = time.Minute |
| 28 | |
| 29 | func checkReadiness(addr string) bool { |
| 30 | resp, err := http.Get(fmt.Sprintf("http://%s/readyz", addr)) |
| 31 | if err != nil { |
| 32 | return false |
| 33 | } |
| 34 | resp.Body.Close() |
| 35 | return resp.StatusCode == http.StatusOK |
| 36 | } |
| 37 | |
| 38 | func checkLiveness(addr string) bool { |
| 39 | resp, err := http.Get(fmt.Sprintf("http://%s/healthz", addr)) |
| 40 | if err != nil { |
| 41 | return false |
| 42 | } |
| 43 | resp.Body.Close() |
| 44 | return resp.StatusCode == http.StatusOK |
| 45 | } |
| 46 | |
| 47 | // FreePorts finds [count] available ports. |
| 48 | func FreePorts(count int) ([]int, error) { |
| 49 | ports := make([]int, count) |
| 50 | for i := 0; i < count; i++ { |
| 51 | l, err := net.Listen("tcp", "127.0.0.1:0") |
| 52 | if err != nil { |
| 53 | return nil, fmt.Errorf("failed to reserve ports: %w", err) |
| 54 | } |
| 55 | defer l.Close() |
| 56 | _, p, err := net.SplitHostPort(l.Addr().String()) |
| 57 | if err != nil { |
| 58 | return nil, fmt.Errorf("failed to reserve ports: %w", err) |
| 59 | } |
| 60 | ports[i], err = strconv.Atoi(p) |
| 61 | if err != nil { |
| 62 | return nil, fmt.Errorf("failed to reserve ports: %w", err) |
| 63 | } |
| 64 | } |
| 65 | return ports, nil |
| 66 | } |