From 12e4eb56c89babefefd9933b879325d5dacc4f2d Mon Sep 17 00:00:00 2001 From: Andrew Cholakian Date: Tue, 7 Aug 2018 01:17:39 -0500 Subject: [PATCH] Replace freeport with independent impl. (#7853) This removes a trivial dep I recently added and substitutes it for a small function. --- NOTICE.txt | 22 ------- heartbeat/monitors/active/http/http_test.go | 7 ++- heartbeat/monitors/active/tcp/tcp_test.go | 7 ++- libbeat/testing/available_port.go | 38 ++++++++++++ vendor/github.com/phayes/freeport/LICENSE.md | 15 ----- vendor/github.com/phayes/freeport/README.md | 58 ------------------- vendor/github.com/phayes/freeport/freeport.go | 30 ---------- vendor/vendor.json | 6 -- 8 files changed, 46 insertions(+), 137 deletions(-) create mode 100644 libbeat/testing/available_port.go delete mode 100644 vendor/github.com/phayes/freeport/LICENSE.md delete mode 100644 vendor/github.com/phayes/freeport/README.md delete mode 100644 vendor/github.com/phayes/freeport/freeport.go diff --git a/NOTICE.txt b/NOTICE.txt index 8236a49e12f..6813934b54e 100644 --- a/NOTICE.txt +++ b/NOTICE.txt @@ -1718,28 +1718,6 @@ For more information, please see http://www.bis.doc.gov See also http://www.apache.org/dev/crypto.html and/or seek legal counsel. --------------------------------------------------------------------- -Dependency: github.com/phayes/freeport -Revision: e27662a4a9d6b2083dfd7e7b5d0e30985daca925 -License type (autodetected): BSD-3-Clause -./vendor/github.com/phayes/freeport/LICENSE.md: --------------------------------------------------------------------- -Open Source License (BSD 3-Clause) ----------------------------------- - -Copyright (c) 2014, Patrick Hayes / HighWire Press -All rights reserved. - -Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - -1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -------------------------------------------------------------------- Dependency: github.com/pierrec/lz4 Revision: 90290f74b1b4d9c097f0a3b3c7eba2ef3875c699 diff --git a/heartbeat/monitors/active/http/http_test.go b/heartbeat/monitors/active/http/http_test.go index 82cac063ad7..b814cb49ecd 100644 --- a/heartbeat/monitors/active/http/http_test.go +++ b/heartbeat/monitors/active/http/http_test.go @@ -25,13 +25,12 @@ import ( "github.com/stretchr/testify/require" - "github.com/phayes/freeport" - "github.com/elastic/beats/heartbeat/hbtest" "github.com/elastic/beats/heartbeat/monitors" "github.com/elastic/beats/libbeat/beat" "github.com/elastic/beats/libbeat/common" "github.com/elastic/beats/libbeat/common/mapval" + btesting "github.com/elastic/beats/libbeat/testing" "github.com/elastic/beats/libbeat/testing/mapvaltest" ) @@ -198,7 +197,9 @@ func TestDownStatuses(t *testing.T) { func TestConnRefusedJob(t *testing.T) { ip := "127.0.0.1" - port := uint16(freeport.GetPort()) + port, err := btesting.AvailableTCP4Port() + require.NoError(t, err) + url := fmt.Sprintf("http://%s:%d", ip, port) event := testRequest(t, url) diff --git a/heartbeat/monitors/active/tcp/tcp_test.go b/heartbeat/monitors/active/tcp/tcp_test.go index a671709fd65..73dcefb3a87 100644 --- a/heartbeat/monitors/active/tcp/tcp_test.go +++ b/heartbeat/monitors/active/tcp/tcp_test.go @@ -24,8 +24,6 @@ import ( "github.com/stretchr/testify/require" - "github.com/phayes/freeport" - "net/http" "github.com/elastic/beats/heartbeat/hbtest" @@ -33,6 +31,7 @@ import ( "github.com/elastic/beats/libbeat/beat" "github.com/elastic/beats/libbeat/common" "github.com/elastic/beats/libbeat/common/mapval" + btesting "github.com/elastic/beats/libbeat/testing" "github.com/elastic/beats/libbeat/testing/mapvaltest" ) @@ -91,7 +90,9 @@ func TestUpEndpointJob(t *testing.T) { func TestConnectionRefusedEndpointJob(t *testing.T) { ip := "127.0.0.1" - port := uint16(freeport.GetPort()) + port, err := btesting.AvailableTCP4Port() + require.NoError(t, err) + event := testTCPCheck(t, ip, port) dialErr := fmt.Sprintf("dial tcp %s:%d", ip, port) diff --git a/libbeat/testing/available_port.go b/libbeat/testing/available_port.go new file mode 100644 index 00000000000..9087db589a0 --- /dev/null +++ b/libbeat/testing/available_port.go @@ -0,0 +1,38 @@ +// Licensed to Elasticsearch B.V. under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. Elasticsearch B.V. licenses this file to you 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 testing + +import "net" + +// AvailableTCP4Port returns an unused TCP port for 127.0.0.1. +func AvailableTCP4Port() (uint16, error) { + resolved, err := net.ResolveTCPAddr("tcp4", "127.0.0.1:0") + if err != nil { + return 0, err + } + + listener, err := net.ListenTCP("tcp4", resolved) + if err != nil { + return 0, err + } + defer listener.Close() + + tcpAddr := uint16(listener.Addr().(*net.TCPAddr).Port) + + return tcpAddr, nil +} diff --git a/vendor/github.com/phayes/freeport/LICENSE.md b/vendor/github.com/phayes/freeport/LICENSE.md deleted file mode 100644 index d9882e595e4..00000000000 --- a/vendor/github.com/phayes/freeport/LICENSE.md +++ /dev/null @@ -1,15 +0,0 @@ -Open Source License (BSD 3-Clause) ----------------------------------- - -Copyright (c) 2014, Patrick Hayes / HighWire Press -All rights reserved. - -Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - -1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/vendor/github.com/phayes/freeport/README.md b/vendor/github.com/phayes/freeport/README.md deleted file mode 100644 index 1665ccf4d4f..00000000000 --- a/vendor/github.com/phayes/freeport/README.md +++ /dev/null @@ -1,58 +0,0 @@ -FreePort -======== - -Get a free open TCP port that is ready to use. - -## Command Line Example: -```bash -# Ask the kernel to give us an open port. -export port=$(freeport) - -# Start standalone httpd server for testing -httpd -X -c "Listen $port" & - -# Curl local server on the selected port -curl localhost:$port -``` - -## Golang example: -```go -package main - -import "github.com/phayes/freeport" - -func main() { - port, err := freeport.GetFreePort() - if err != nil { - log.Fatal(err) - } - // port is ready to listen on -} - -``` - -## Installation - -#### Mac OSX -```bash -brew install phayes/repo/freeport -``` - - -#### CentOS and other RPM based systems -```bash -wget https://github.com/phayes/freeport/releases/download/1.0.2/freeport_1.0.2_linux_386.rpm -rpm -Uvh freeport_1.0.2_linux_386.rpm -``` - -#### Ubuntu and other DEB based systems -```bash -wget wget https://github.com/phayes/freeport/releases/download/1.0.2/freeport_1.0.2_linux_amd64.deb -dpkg -i freeport_1.0.2_linux_amd64.deb -``` - -#### Building From Source -```bash -sudo apt-get install golang # Download go. Alternativly build from source: https://golang.org/doc/install/source -go get github.com/phayes/freeport -``` diff --git a/vendor/github.com/phayes/freeport/freeport.go b/vendor/github.com/phayes/freeport/freeport.go deleted file mode 100644 index c44b60df278..00000000000 --- a/vendor/github.com/phayes/freeport/freeport.go +++ /dev/null @@ -1,30 +0,0 @@ -package freeport - -import ( - "net" -) - -// GetFreePort asks the kernel for a free open port that is ready to use. -func GetFreePort() (int, error) { - addr, err := net.ResolveTCPAddr("tcp", "localhost:0") - if err != nil { - return 0, err - } - - l, err := net.ListenTCP("tcp", addr) - if err != nil { - return 0, err - } - defer l.Close() - return l.Addr().(*net.TCPAddr).Port, nil -} - -// GetPort is deprecated, use GetFreePort instead -// Ask the kernel for a free open port that is ready to use -func GetPort() int { - port, err := GetFreePort() - if err != nil { - panic(err) - } - return port -} diff --git a/vendor/vendor.json b/vendor/vendor.json index d08891f5e71..7f525c9319a 100644 --- a/vendor/vendor.json +++ b/vendor/vendor.json @@ -1252,12 +1252,6 @@ "revision": "653207bc29a6d2d62b5d4f55b596467cb715a128", "revisionTime": "2017-03-27T18:58:03Z" }, - { - "checksumSHA1": "kZRhErakejBG0U2e8D+Ap/Djje8=", - "path": "github.com/phayes/freeport", - "revision": "e27662a4a9d6b2083dfd7e7b5d0e30985daca925", - "revisionTime": "2017-10-02T18:52:19Z" - }, { "checksumSHA1": "WmrPO1ovmQ7t7hs9yZGbr2SAoM4=", "path": "github.com/pierrec/lz4",