diff --git a/go.mod b/go.mod index 88f1cc963..a5a22891c 100644 --- a/go.mod +++ b/go.mod @@ -24,7 +24,6 @@ require ( github.com/onsi/gomega v1.19.0 github.com/opencontainers/go-digest v1.0.0 github.com/opencontainers/image-spec v1.0.3-0.20211202183452-c5a74bcca799 - github.com/phayes/freeport v0.0.0-20220201140144-74d24b5ae9f5 github.com/pkg/sftp v1.13.5 github.com/sirupsen/logrus v1.8.1 github.com/spf13/viper v1.4.0 diff --git a/go.sum b/go.sum index 35cb045ad..bf5a95769 100644 --- a/go.sum +++ b/go.sum @@ -454,8 +454,6 @@ github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFSt github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= github.com/pelletier/go-toml v1.9.4 h1:tjENF6MfZAg8e4ZmZTeWaWiT2vXtsoO6+iuOjFhECwM= github.com/pelletier/go-toml v1.9.4/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c= -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/pingcap/errors v0.11.4 h1:lFuQV/oaUMGcD2tqt+01ROSmJs75VG1ToEOkZIZ4nE4= github.com/pingcap/errors v0.11.4/go.mod h1:Oi8TUi2kEtXXLMJk9l1cGmz20kV3TaQ0usTwv5KuLY8= github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= diff --git a/pkg/util/netutil/netutil.go b/pkg/util/netutil/netutil.go index ea67fbd7d..a1b1da48a 100644 --- a/pkg/util/netutil/netutil.go +++ b/pkg/util/netutil/netutil.go @@ -14,14 +14,13 @@ package netutil -import ( - "github.com/phayes/freeport" -) +import "net" func GetFreePort() (int, error) { - port, err := freeport.GetFreePort() + l, err := net.Listen("tcp", ":0") if err != nil { return 0, err } - return port, nil + defer l.Close() + return l.Addr().(*net.TCPAddr).Port, nil } diff --git a/pkg/util/netutil/netutil_test.go b/pkg/util/netutil/netutil_test.go new file mode 100644 index 000000000..e6f93d9be --- /dev/null +++ b/pkg/util/netutil/netutil_test.go @@ -0,0 +1,27 @@ +// Copyright 2022 The envd Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package netutil + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestGetFreePort(t *testing.T) { + port, err := GetFreePort() + assert.NoError(t, err) + assert.NotEqual(t, port, 0) +}