Remove GenericController and add tests

This commit is contained in:
Manuel de Brito Fontes 2017-11-04 22:18:28 -03:00
parent 1701bfc334
commit 86f39d9deb
39 changed files with 1131 additions and 1325 deletions

View file

@ -16,9 +16,29 @@ limitations under the License.
package net
import _net "net"
import (
"fmt"
_net "net"
"os/exec"
)
// IsIPV6 checks if the input contains a valid IPV6 address
func IsIPV6(ip _net.IP) bool {
return ip.To4() == nil
}
// IsPortAvailable checks if a TCP port is available or not
func IsPortAvailable(p int) bool {
ln, err := _net.Listen("tcp", fmt.Sprintf(":%v", p))
if err != nil {
return false
}
ln.Close()
return true
}
// IsIPv6Enabled checks if IPV6 is enabled or not
func IsIPv6Enabled() bool {
cmd := exec.Command("test", "-f", "/proc/net/if_inet6")
return cmd.Run() == nil
}

View file

@ -41,3 +41,20 @@ func TestIsIPV6(t *testing.T) {
}
}
}
func TestIsPortAvailable(t *testing.T) {
if !IsPortAvailable(0) {
t.Fatal("expected port 0 to be avilable (random port) but returned false")
}
ln, err := net.Listen("tcp", ":0")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
defer ln.Close()
p := ln.Addr().(*net.TCPAddr).Port
if IsPortAvailable(p) {
t.Fatalf("expected port %v to not be available", p)
}
}