Remove GenericController and add tests
This commit is contained in:
parent
1701bfc334
commit
86f39d9deb
39 changed files with 1131 additions and 1325 deletions
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue