mirror of https://github.com/containers/podman.git
Merge pull request #18769 from edsantiago/getport_rewrite
e2e: GetPort(): safer allocation of random ports
This commit is contained in:
commit
aee7a3c16a
|
@ -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)
|
|
||||||
if err != nil {
|
|
||||||
Fail(fmt.Sprintf("unable to get free port: %v", err))
|
Fail(fmt.Sprintf("unable to get free port: %v", err))
|
||||||
}
|
return 0 // notreached
|
||||||
defer l.Close()
|
|
||||||
return l.Addr().(*net.TCPAddr).Port
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func ncz(port int) bool {
|
func ncz(port int) bool {
|
||||||
|
|
|
@ -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")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue