| 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 frameworkscip-go gomod sigs.k8s.io/apiserver-network-proxy . `sigs.k8s.io/apiserver-network-proxy/tests/framework`/ |
| 18 | |
| 19 | import ( |
| 20 | "fmtscip-go gomod github.com/golang/go/src go1.23.6 fmt/" |
| 21 | "netscip-go gomod github.com/golang/go/src go1.23.6 net/" |
| 22 | "net/httpscip-go gomod github.com/golang/go/src go1.23.6 `net/http`/" |
| 23 | "strconvscip-go gomod github.com/golang/go/src go1.23.6 strconv/" |
| 24 | "timescip-go gomod github.com/golang/go/src go1.23.6 time/" |
| 25 | ) |
| 26 | |
| 27 | var ForeverTestTimeoutscip-go gomod sigs.k8s.io/apiserver-network-proxy . `sigs.k8s.io/apiserver-network-proxy/tests/framework`/ForeverTestTimeout. = timescip-go gomod github.com/golang/go/src go1.23.6 time/.Minutescip-go gomod github.com/golang/go/src go1.23.6 time/Minute. |
| 28 | |
| 29 | func checkReadinessscip-go gomod sigs.k8s.io/apiserver-network-proxy . `sigs.k8s.io/apiserver-network-proxy/tests/framework`/checkReadiness().(addr string) bool { |
| 30 | resp, err := httpscip-go gomod github.com/golang/go/src go1.23.6 `net/http`/.Getscip-go gomod github.com/golang/go/src go1.23.6 `net/http`/Get().(fmtscip-go gomod github.com/golang/go/src go1.23.6 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.23.6 fmt/Sprintf().("http://%s/readyz", addr)) |
| 31 | if err != nil { |
| 32 | return false |
| 33 | } |
| 34 | resp.Bodyscip-go gomod github.com/golang/go/src go1.23.6 `net/http`/Response#Body..Closescip-go gomod github.com/golang/go/src go1.23.6 io/Closer#Close().() |
| 35 | return resp.StatusCodescip-go gomod github.com/golang/go/src go1.23.6 `net/http`/Response#StatusCode. == httpscip-go gomod github.com/golang/go/src go1.23.6 `net/http`/.StatusOKscip-go gomod github.com/golang/go/src go1.23.6 `net/http`/StatusOK. |
| 36 | } |
| 37 | |
| 38 | func checkLivenessscip-go gomod sigs.k8s.io/apiserver-network-proxy . `sigs.k8s.io/apiserver-network-proxy/tests/framework`/checkLiveness().(addr string) bool { |
| 39 | resp, err := httpscip-go gomod github.com/golang/go/src go1.23.6 `net/http`/.Getscip-go gomod github.com/golang/go/src go1.23.6 `net/http`/Get().(fmtscip-go gomod github.com/golang/go/src go1.23.6 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.23.6 fmt/Sprintf().("http://%s/healthz", addr)) |
| 40 | if err != nil { |
| 41 | return false |
| 42 | } |
| 43 | resp.Bodyscip-go gomod github.com/golang/go/src go1.23.6 `net/http`/Response#Body..Closescip-go gomod github.com/golang/go/src go1.23.6 io/Closer#Close().() |
| 44 | return resp.StatusCodescip-go gomod github.com/golang/go/src go1.23.6 `net/http`/Response#StatusCode. == httpscip-go gomod github.com/golang/go/src go1.23.6 `net/http`/.StatusOKscip-go gomod github.com/golang/go/src go1.23.6 `net/http`/StatusOK. |
| 45 | } |
| 46 | |
| 47 | // FreePorts finds [count] available ports. |
| 48 | func FreePortsscip-go gomod sigs.k8s.io/apiserver-network-proxy . `sigs.k8s.io/apiserver-network-proxy/tests/framework`/FreePorts().(count int) ([]int, error) { |
| 49 | ports := make([]int, count) |
| 50 | for i := 0; i < count; i++ { |
| 51 | l, err := netscip-go gomod github.com/golang/go/src go1.23.6 net/.Listenscip-go gomod github.com/golang/go/src go1.23.6 net/Listen().("tcp", "127.0.0.1:0") |
| 52 | if err != nil { |
| 53 | return nil, fmtscip-go gomod github.com/golang/go/src go1.23.6 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.23.6 fmt/Errorf().("failed to reserve ports: %w", err) |
| 54 | } |
| 55 | defer l.Closescip-go gomod github.com/golang/go/src go1.23.6 net/Listener#Close().() |
| 56 | _, p, err := netscip-go gomod github.com/golang/go/src go1.23.6 net/.SplitHostPortscip-go gomod github.com/golang/go/src go1.23.6 net/SplitHostPort().(l.Addrscip-go gomod github.com/golang/go/src go1.23.6 net/Listener#Addr().().Stringscip-go gomod github.com/golang/go/src go1.23.6 net/Addr#String().()) |
| 57 | if err != nil { |
| 58 | return nil, fmtscip-go gomod github.com/golang/go/src go1.23.6 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.23.6 fmt/Errorf().("failed to reserve ports: %w", err) |
| 59 | } |
| 60 | ports[i], err = strconvscip-go gomod github.com/golang/go/src go1.23.6 strconv/.Atoiscip-go gomod github.com/golang/go/src go1.23.6 strconv/Atoi().(p) |
| 61 | if err != nil { |
| 62 | return nil, fmtscip-go gomod github.com/golang/go/src go1.23.6 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.23.6 fmt/Errorf().("failed to reserve ports: %w", err) |
| 63 | } |
| 64 | } |
| 65 | return ports, nil |
| 66 | } |