Files
odo/tests/helper/helper_http.go
dependabot[bot] 50cb5c9b3b Go: Bump github.com/securego/gosec/v2 from 2.14.0 to 2.15.0 (#6686)
* Go: Bump github.com/securego/gosec/v2 from 2.14.0 to 2.15.0

Bumps [github.com/securego/gosec/v2](https://github.com/securego/gosec) from 2.14.0 to 2.15.0.
- [Release notes](https://github.com/securego/gosec/releases)
- [Changelog](https://github.com/securego/gosec/blob/master/.goreleaser.yml)
- [Commits](https://github.com/securego/gosec/compare/v2.14.0...v2.15.0)

---
updated-dependencies:
- dependency-name: github.com/securego/gosec/v2
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>

* Make sure to handle error returned by io.Closer.Close() in 'defer' statements

This is considered unsafe by gosec otherwise.

[1] https://github.com/securego/gosec/issues/512
[2] https://github.com/securego/gosec/issues/714
[3] https://www.joeshaw.org/dont-defer-close-on-writable-files/

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Armel Soro <asoro@redhat.com>
2023-05-16 09:51:30 -04:00

63 lines
2.1 KiB
Go

package helper
import (
"crypto/tls"
"fmt"
"io"
"net/http"
"strings"
"time"
. "github.com/onsi/ginkgo/v2"
)
// HttpWaitForWithStatus periodically (every interval) calls GET to given url
// ends when result response contains match string and status code, or after the maxRetry
func HttpWaitForWithStatus(url string, match string, maxRetry int, interval int, expectedCode int) {
fmt.Fprintf(GinkgoWriter, "Checking %s, for %s\n", url, match)
var body []byte
for i := 0; i < maxRetry; i++ {
fmt.Fprintf(GinkgoWriter, "try %d of %d\n", i, maxRetry)
// #nosec
// gosec:G107, G402 -> This is safe since it's just used for testing.
transporter := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: transporter}
resp, err := client.Get(url)
if err != nil {
// we log the error and sleep again because this could mean the component is not up yet
fmt.Fprintln(GinkgoWriter, "error while requesting:", err.Error())
time.Sleep(time.Duration(interval) * time.Second)
continue
}
defer func() {
if cErr := resp.Body.Close(); cErr != nil {
fmt.Fprintf(GinkgoWriter, "[warn] error closing response body: %v\n", cErr)
}
}()
if resp.StatusCode == expectedCode {
body, _ = io.ReadAll(resp.Body)
if strings.Contains(string(body), match) {
return
}
}
time.Sleep(time.Duration(interval) * time.Second)
}
fmt.Fprintf(GinkgoWriter, "Last output from %s: %s\n", url, string(body))
Fail(fmt.Sprintf("Failed after %d retries. Content in %s doesn't include '%s'.", maxRetry, url, match))
}
// GetCustomStartPort returns a port that can be used as starting value for custom port mapping.
// Because of the way Ginkgo runs specs in parallel (by isolating them in different processes),
// this function needs to be called in a Before* node or test spec.
// It returns a starting value that aims at minimizing the probability of collisions.
// Callers can then safely increment the returned value in their specs if needed.
func GetCustomStartPort() int {
return 30000 + 100*GinkgoParallelProcess()
}