@@ -3,6 +3,7 @@ package aboutme
33import (
44 "net"
55 "os"
6+ "strings"
67 "testing"
78
89 "k8s.io/kubernetes/pkg/client/unversioned"
@@ -43,6 +44,57 @@ func TestShuntEnv(t *testing.T) {
4344 }
4445}
4546
47+ func TestMyIPLocal (t * testing.T ) {
48+ // This version does not require running inside of k8s.
49+ ip , _ := MyIP ()
50+ if len (ip ) == 0 {
51+ t .Error ("Expected a string, got empty" )
52+ }
53+ octets := strings .Split (ip , "." )
54+ if len (octets ) != 4 {
55+ t .Errorf ("Expected 4 octets, got %d" , len (octets ))
56+ }
57+ }
58+
59+ func TestByInterfaceEth0 (t * testing.T ) {
60+ if _ , err := net .InterfaceByName ("eth0" ); err != nil {
61+ t .Skip ("Host operating system does not have an eth0 device to test." )
62+ }
63+
64+ ip , err := IPByInterface ("eth0" )
65+ if err != nil {
66+ t .Errorf ("Failed to get eth0: %s" , err )
67+ }
68+ if len (ip ) == 0 {
69+ t .Error ("IP address is empty." )
70+ }
71+
72+ if ip == "0.0.0.0" {
73+ t .Error ("Got generic IP instead of real one." )
74+ }
75+
76+ }
77+
78+ func TestByInterfaceEn0 (t * testing.T ) {
79+ // This works on most Macs.
80+ if _ , err := net .InterfaceByName ("en0" ); err != nil {
81+ t .Skip ("Host operating system does not have an en0 device to test." )
82+ }
83+
84+ ip , err := IPByInterface ("en0" )
85+ if err != nil {
86+ t .Errorf ("Failed to get en0: %s" , err )
87+ }
88+ if len (ip ) == 0 {
89+ t .Error ("IP address is empty." )
90+ }
91+
92+ if ip == "0.0.0.0" {
93+ t .Error ("Got generic IP instead of real one." )
94+ }
95+
96+ }
97+
4698func TestMyIP (t * testing.T ) {
4799 if _ , err := net .InterfaceByName ("eth0" ); err != nil {
48100 t .Skip ("Host operating system does not have an eth0 device to test." )
0 commit comments