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
commit aee7a3c16a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 12 deletions

View File

@ -1221,19 +1221,31 @@ func writeYaml(content string, fileName string) error {
return nil 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 { func GetPort() int {
a, err := net.ResolveTCPAddr("tcp", "localhost:0") portMin := 5000
if err != nil { portMax := 5999
Fail(fmt.Sprintf("unable to get free port: %v", err)) 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) Fail(fmt.Sprintf("unable to get free port: %v", err))
if err != nil { return 0 // notreached
Fail(fmt.Sprintf("unable to get free port: %v", err))
}
defer l.Close()
return l.Addr().(*net.TCPAddr).Port
} }
func ncz(port int) bool { func ncz(port int) bool {

View File

@ -318,7 +318,7 @@ registries = ['{{.Host}}:{{.Port}}']`
Expect(search).Should(Exit(125)) Expect(search).Should(Exit(125))
Expect(search.OutputToString()).Should(BeEmpty()) 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 // cleanup
resetRegistriesConfigEnv() resetRegistriesConfigEnv()
@ -363,13 +363,14 @@ registries = ['{{.Host}}:{{.Port}}']`
Expect(search).Should(Exit(125)) Expect(search).Should(Exit(125))
Expect(search.OutputToString()).Should(BeEmpty()) 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 // cleanup
resetRegistriesConfigEnv() resetRegistriesConfigEnv()
}) })
It("podman search doesn't attempt HTTP if one registry is not listed as insecure", func() { 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" { if podmanTest.Host.Arch == "ppc64le" {
Skip("No registry image for ppc64le") Skip("No registry image for ppc64le")
} }