-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathaboutme_test.go
More file actions
111 lines (91 loc) · 2.31 KB
/
Copy pathaboutme_test.go
File metadata and controls
111 lines (91 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package aboutme
import (
"net"
"os"
"strings"
"testing"
"k8s.io/kubernetes/pkg/client/unversioned"
)
func TestFromEnv(t *testing.T) {
if _, err := unversioned.InClusterConfig(); err != nil {
t.Skip("This can only be run inside Kubernetes. Skipping.")
}
me, err := FromEnv()
if err != nil {
t.Errorf("Could not get an environment: %s", err)
}
if len(me.Name) == 0 {
t.Error("Could not get a pod name.")
}
}
func TestShuntEnv(t *testing.T) {
e := &Me{
Annotations: map[string]string{"a": "a"},
Labels: map[string]string{"b": "b"},
Name: "c",
}
e.ShuntEnv()
if "a" != os.Getenv("MY_ANNOTATION_A") {
t.Errorf("Expected 'a', got '%s'", os.Getenv("MY_ANNOTATION_A"))
}
if "b" != os.Getenv("MY_LABEL_B") {
t.Errorf("Expected 'b', got '%s'", os.Getenv("MY_LABEL_B"))
}
if "c" != os.Getenv("MY_NAME") {
t.Errorf("Expected 'c', got '%s'", os.Getenv("MY_NAME"))
}
}
func TestMyIPLocal(t *testing.T) {
// This version does not require running inside of k8s.
ip, _ := MyIP()
if len(ip) == 0 {
t.Error("Expected a string, got empty")
}
octets := strings.Split(ip, ".")
if len(octets) != 4 {
t.Errorf("Expected 4 octets, got %d", len(octets))
}
}
func TestByInterfaceEth0(t *testing.T) {
if _, err := net.InterfaceByName("eth0"); err != nil {
t.Skip("Host operating system does not have an eth0 device to test.")
}
ip, err := IPByInterface("eth0")
if err != nil {
t.Errorf("Failed to get eth0: %s", err)
}
if len(ip) == 0 {
t.Error("IP address is empty.")
}
if ip == "0.0.0.0" {
t.Error("Got generic IP instead of real one.")
}
}
func TestByInterfaceEn0(t *testing.T) {
// This works on most Macs.
if _, err := net.InterfaceByName("en0"); err != nil {
t.Skip("Host operating system does not have an en0 device to test.")
}
ip, err := IPByInterface("en0")
if err != nil {
t.Errorf("Failed to get en0: %s", err)
}
if len(ip) == 0 {
t.Error("IP address is empty.")
}
if ip == "0.0.0.0" {
t.Error("Got generic IP instead of real one.")
}
}
func TestMyIP(t *testing.T) {
if _, err := net.InterfaceByName("eth0"); err != nil {
t.Skip("Host operating system does not have an eth0 device to test.")
}
ip, err := MyIP()
if err != nil {
t.Errorf("Could not get IP address: %s", err)
}
if len(ip) == 0 {
t.Errorf("Expected a valid IP address. Got nuthin.")
}
}