From f76f30d06cafcd492c8c9e3a814947ea19471cf5 Mon Sep 17 00:00:00 2001 From: Stefan Prodan Date: Fri, 23 Aug 2024 14:56:23 +0300 Subject: [PATCH] Drop freeport dependency from pkg/oci tests Signed-off-by: Stefan Prodan --- oci/client/suite_test.go | 28 ++++++++++++++++++++++------ oci/go.mod | 1 - oci/go.sum | 2 -- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/oci/client/suite_test.go b/oci/client/suite_test.go index 7fec6ef1..9acdf2e5 100644 --- a/oci/client/suite_test.go +++ b/oci/client/suite_test.go @@ -21,7 +21,10 @@ import ( "fmt" "io" "math/rand" + "net" "os" + "strconv" + "strings" "testing" "time" @@ -29,7 +32,6 @@ import ( "github.com/distribution/distribution/v3/registry" _ "github.com/distribution/distribution/v3/registry/auth/htpasswd" _ "github.com/distribution/distribution/v3/registry/storage/driver/inmemory" - "github.com/phayes/freeport" "github.com/sirupsen/logrus" ) @@ -38,17 +40,31 @@ var ( ) func setupRegistryServer(ctx context.Context) error { + // Find a free port + lis, err := net.Listen("tcp", "localhost:0") + if err != nil { + return fmt.Errorf("failed to create listener: %s", err) + } + + addr := lis.Addr().String() + addrParts := strings.Split(addr, ":") + portStr := addrParts[len(addrParts)-1] + port, err := strconv.Atoi(portStr) + if err != nil { + return fmt.Errorf("failed to parse port: %s", err) + } + + err = lis.Close() + if err != nil { + return fmt.Errorf("failed to close listener: %s", err) + } + // Registry config config := &configuration.Configuration{} config.Log.AccessLog.Disabled = true config.Log.Level = "error" logrus.SetOutput(io.Discard) - port, err := freeport.GetFreePort() - if err != nil { - return fmt.Errorf("failed to get free port: %s", err) - } - dockerReg = fmt.Sprintf("localhost:%d", port) config.HTTP.Addr = fmt.Sprintf("127.0.0.1:%d", port) config.HTTP.DrainTimeout = time.Duration(10) * time.Second diff --git a/oci/go.mod b/oci/go.mod index acff1fa3..9fdd6b8a 100644 --- a/oci/go.mod +++ b/oci/go.mod @@ -25,7 +25,6 @@ require ( github.com/go-logr/logr v1.4.2 github.com/google/go-containerregistry v0.20.2 github.com/onsi/gomega v1.34.1 - github.com/phayes/freeport v0.0.0-20220201140144-74d24b5ae9f5 github.com/sirupsen/logrus v1.9.3 ) diff --git a/oci/go.sum b/oci/go.sum index 5fd457c5..344e91ef 100644 --- a/oci/go.sum +++ b/oci/go.sum @@ -263,8 +263,6 @@ github.com/opencontainers/image-spec v1.1.0 h1:8SG7/vwALn54lVB/0yZ/MMwhFrPYtpEHQ github.com/opencontainers/image-spec v1.1.0/go.mod h1:W4s4sFTMaBeK1BQLXbG4AdM2szdn85PY75RI83NrTrM= github.com/peterbourgon/diskv v2.0.1+incompatible h1:UBdAOUP5p4RWqPBg048CAvpKN+vxiaj6gdUUzhl4XmI= github.com/peterbourgon/diskv v2.0.1+incompatible/go.mod h1:uqqh8zWWbv1HBMNONnaR/tNboyR3/BZd58JJSHlUSCU= -github.com/phayes/freeport v0.0.0-20220201140144-74d24b5ae9f5 h1:Ii+DKncOVM8Cu1Hc+ETb5K+23HdAMvESYE3ZJ5b5cMI= -github.com/phayes/freeport v0.0.0-20220201140144-74d24b5ae9f5/go.mod h1:iIss55rKnNBTvrwdmkUpLnDpZoAHvWaiq5+iMmen4AE= github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c h1:+mdjkGKdHQG3305AYmdv1U2eRNDiU2ErMBj1gwrq8eQ= github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c/go.mod h1:7rwL4CYBLnjLxUqIJNnCWiEdr3bn6IUYi15bNlnbCCU= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=