Merge pull request #18769 from edsantiago/getport_rewrite

e2e: GetPort(): safer allocation of random ports
This commit is contained in:
OpenShift Merge Robot
2023-06-02 15:39:43 -04:00
committed by GitHub
2 changed files with 25 additions and 12 deletions

View File

@ -1221,19 +1221,31 @@ func writeYaml(content string, fileName string) error {
return nil
}
// GetPort finds an unused port on the system
// GetPort finds an unused TCP/IP port on the system, in the range 5000-5999
func GetPort() int {
a, err := net.ResolveTCPAddr("tcp", "localhost:0")
if err != nil {
Fail(fmt.Sprintf("unable to get free port: %v", err))
portMin := 5000
portMax := 5999
rng := rand.New(rand.NewSource(time.Now().UnixNano()))
// Avoid dup-allocation races between parallel ginkgo processes
nProcs := GinkgoT().ParallelTotal()
myProc := GinkgoT().ParallelProcess() - 1
for i := 0; i < 50; i++ {
// Random port within that range
port := portMin + rng.Intn((portMax-portMin)/nProcs)*nProcs + myProc
used, err := net.Listen("tcp", "localhost:"+strconv.Itoa(port))
if err == nil {
// it's open. Return it.
err = used.Close()
Expect(err).ToNot(HaveOccurred(), "closing random port")
return port
}
}
l, err := net.ListenTCP("tcp", a)
if err != nil {
Fail(fmt.Sprintf("unable to get free port: %v", err))
}
defer l.Close()
return l.Addr().(*net.TCPAddr).Port
Fail(fmt.Sprintf("unable to get free port: %v", err))
return 0 // notreached
}
func ncz(port int) bool {

View File

@ -318,7 +318,7 @@ registries = ['{{.Host}}:{{.Port}}']`
Expect(search).Should(Exit(125))
Expect(search.OutputToString()).Should(BeEmpty())
Expect(search.ErrorToString()).To(ContainSubstring("error"))
Expect(search.ErrorToString()).To(ContainSubstring("http: server gave HTTP response to HTTPS client"))
// cleanup
resetRegistriesConfigEnv()
@ -363,13 +363,14 @@ registries = ['{{.Host}}:{{.Port}}']`
Expect(search).Should(Exit(125))
Expect(search.OutputToString()).Should(BeEmpty())
Expect(search.ErrorToString()).To(ContainSubstring("error"))
Expect(search.ErrorToString()).To(ContainSubstring("http: server gave HTTP response to HTTPS client"))
// cleanup
resetRegistriesConfigEnv()
})
It("podman search doesn't attempt HTTP if one registry is not listed as insecure", func() {
Skip("FIXME FIXME FIXME #18768: This test is a NOP")
if podmanTest.Host.Arch == "ppc64le" {
Skip("No registry image for ppc64le")
}