...
1 package helper
2
3 import (
4 "crypto/tls"
5 "fmt"
6 "io"
7 "net/http"
8 "strings"
9 "time"
10
11 . "github.com/onsi/ginkgo/v2"
12 )
13
14
15
16 func HttpWaitForWithStatus(url string, match string, maxRetry int, interval int, expectedCode int) {
17 fmt.Fprintf(GinkgoWriter, "Checking %s, for %s\n", url, match)
18
19 var body []byte
20
21 for i := 0; i < maxRetry; i++ {
22 fmt.Fprintf(GinkgoWriter, "try %d of %d\n", i, maxRetry)
23
24
25
26 transporter := &http.Transport{
27 TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
28 }
29 client := &http.Client{Transport: transporter}
30 resp, err := client.Get(url)
31 if err != nil {
32
33 fmt.Fprintln(GinkgoWriter, "error while requesting:", err.Error())
34 time.Sleep(time.Duration(interval) * time.Second)
35 continue
36 }
37 defer func() {
38 if cErr := resp.Body.Close(); cErr != nil {
39 fmt.Fprintf(GinkgoWriter, "[warn] error closing response body: %v\n", cErr)
40 }
41 }()
42 if resp.StatusCode == expectedCode {
43 body, _ = io.ReadAll(resp.Body)
44 if strings.Contains(string(body), match) {
45 return
46 }
47
48 }
49 time.Sleep(time.Duration(interval) * time.Second)
50 }
51 fmt.Fprintf(GinkgoWriter, "Last output from %s: %s\n", url, string(body))
52 Fail(fmt.Sprintf("Failed after %d retries. Content in %s doesn't include '%s'.", maxRetry, url, match))
53 }
54
55
56
57
58
59
60 func GetCustomStartPort() int {
61 return 30000 + 100*GinkgoParallelProcess()
62 }
63
View as plain text