-
Notifications
You must be signed in to change notification settings - Fork 12
/
helper_test.go
157 lines (146 loc) · 3.35 KB
/
helper_test.go
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main
import (
"encoding/json"
"errors"
"net"
"reflect"
"strings"
"testing"
"github.com/digitalocean/go-metadata"
)
func TestFindInterfaceName(t *testing.T) {
ifaces, _ := net.Interfaces()
addrs, _ := ifaces[0].Addrs()
localAddr := addrs[0]
ip := localAddr.String()
switch v := localAddr.(type) {
case *net.IPAddr:
ip = v.IP.String()
case *net.IPNet:
ip = v.IP.String()
}
tests := []struct {
name string
ifaces []net.Interface
local string
exp string
expErr error
}{
{
name: "valid ip",
ifaces: ifaces,
local: ip,
exp: ifaces[0].Name,
expErr: nil,
},
{
name: "invalid ip",
ifaces: ifaces,
local: "somethingbad",
exp: "",
expErr: errors.New("local interface could not be found"),
},
}
for _, test := range tests {
out, err := FindInterfaceName(test.ifaces, test.local)
if !reflect.DeepEqual(err, test.expErr) {
t.Logf("want:%v", test.expErr)
t.Logf("got:%v", err)
t.Fatalf("test case failed: %s", test.name)
}
if out != test.exp {
t.Logf("want:%v", test.exp)
t.Logf("got:%v", out)
t.Fatalf("test case failed: %s", test.name)
}
}
}
func TestPrivateAddress(t *testing.T) {
tests := []struct {
name string
data *metadata.Metadata
exp string
expErr error
}{
{
name: "private ipv4 address",
data: decodeMetadata(`{"interfaces": {"private": [{"ipv4": {"ip_address": "privateIP"}}]}}`),
exp: "privateIP",
expErr: nil,
},
{
name: "private ipv6 address",
data: decodeMetadata(`{"interfaces": {"private": [{"ipv6": {"ip_address": "privateIP"}}]}}`),
exp: "",
expErr: errors.New("no ipv4 private iface"),
},
{
name: "no private addresses",
data: &metadata.Metadata{},
exp: "",
expErr: errors.New("no private interfaces"),
},
}
for _, test := range tests {
out, err := PrivateAddress(test.data)
if !reflect.DeepEqual(err, test.expErr) {
t.Logf("want:%v", test.expErr)
t.Logf("got:%v", err)
t.Fatalf("test case failed: %s", test.name)
}
if out != test.exp {
t.Logf("want:%v", test.exp)
t.Logf("got:%v", out)
t.Fatalf("test case failed: %s", test.name)
}
}
}
func TestPublicAddress(t *testing.T) {
tests := []struct {
name string
data *metadata.Metadata
exp string
expErr error
}{
{
name: "public ipv4 address",
data: decodeMetadata(`{"interfaces": {"public": [{"ipv4": {"ip_address": "publicIP"}}]}}`),
exp: "publicIP",
expErr: nil,
},
{
name: "public ipv6 address",
data: decodeMetadata(`{"interfaces": {"public": [{"ipv6": {"ip_address": "publicIP"}}]}}`),
exp: "",
expErr: errors.New("no ipv4 public iface"),
},
{
name: "no public addresses",
data: &metadata.Metadata{},
exp: "",
expErr: errors.New("no public interfaces"),
},
}
for _, test := range tests {
out, err := PublicAddress(test.data)
if !reflect.DeepEqual(err, test.expErr) {
t.Logf("want:%v", test.expErr)
t.Logf("got:%v", err)
t.Fatalf("test case failed: %s", test.name)
}
if out != test.exp {
t.Logf("want:%v", test.exp)
t.Logf("got:%v", out)
t.Fatalf("test case failed: %s", test.name)
}
}
}
func decodeMetadata(data string) *metadata.Metadata {
var output metadata.Metadata
var err error
err = json.NewDecoder(strings.NewReader(data)).Decode(&output)
if err != nil {
panic(err)
}
return &output
}