-
Notifications
You must be signed in to change notification settings - Fork 6
/
ping_test.go
210 lines (168 loc) · 5.77 KB
/
ping_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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// SPDX-FileCopyrightText: 2023 Steffen Vogel <[email protected]>
// SPDX-License-Identifier: Apache-2.0
package gont_test
import (
"testing"
g "cunicu.li/gont/v2/pkg"
o "cunicu.li/gont/v2/pkg/options"
"github.com/stretchr/testify/require"
)
// TestPingDualStack performs and end-to-end ping test
// between two hosts on a switched topology
// using both IPv4 and IPv6 addresses
//
// h1 <-> sw <-> h2
func TestPingDualStack(t *testing.T) {
n, err := g.NewNetwork(*nname, globalNetworkOptions...)
require.NoError(t, err, "Failed to create network")
defer n.Close()
sw, err := n.AddSwitch("sw")
require.NoError(t, err, "Failed to create switch")
h1, err := n.AddHost("h1",
g.NewInterface("veth0", sw,
o.AddressIP("10.0.0.1/24"),
o.AddressIP("fc::1/64")))
require.NoError(t, err, "Failed to create host")
h2, err := n.AddHost("h2",
g.NewInterface("veth0", sw,
o.AddressIP("10.0.0.2/24"),
o.AddressIP("fc::2/64")))
require.NoError(t, err, "Failed to create host")
for _, net := range []string{"ip4", "ip6"} {
_, err = h1.PingWithNetwork(h2, net)
require.NoError(t, err, "Failed to test connectivity")
}
}
// TestPingIPv4 performs and end-to-end ping test
// between two hosts on a switched topology
// using IPv4 addressing
//
// h1 <-> sw <-> h2
func TestPingIPv4(t *testing.T) {
n, err := g.NewNetwork(*nname, globalNetworkOptions...)
require.NoError(t, err, "Failed to create network")
defer n.Close()
sw, err := n.AddSwitch("sw")
require.NoError(t, err, "Failed to create switch")
h1, err := n.AddHost("h1",
g.NewInterface("veth0", sw,
o.AddressIP("10.0.0.1/24")))
require.NoError(t, err, "Failed to create host")
h2, err := n.AddHost("h2",
g.NewInterface("veth0", sw,
o.AddressIP("10.0.0.2/24")))
require.NoError(t, err, "Failed to create host")
err = g.TestConnectivity(h1, h2)
require.NoError(t, err, "Failed to test connectivity between hosts")
}
// TestPingIPv6 performs and end-to-end ping test
// between two hosts on a switched topology
// using IPv6 addressing
//
// h1 <-> sw <-> h2
func TestPingIPv6(t *testing.T) {
n, err := g.NewNetwork(*nname, globalNetworkOptions...)
require.NoError(t, err, "Failed to create network")
defer n.Close()
sw, err := n.AddSwitch("sw")
require.NoError(t, err, "Failed to create switch")
h1, err := n.AddHost("h1",
g.NewInterface("veth0", sw,
o.AddressIP("fc::1/64")))
require.NoError(t, err, "Failed to create host")
h2, err := n.AddHost("h2",
g.NewInterface("veth0", sw,
o.AddressIP("fc::2/64")))
require.NoError(t, err, "Failed to create host")
err = g.TestConnectivity(h1, h2)
require.NoError(t, err, "Failed to test connectivity between hosts")
}
// TestPingDirect performs and end-to-end ping test between two
// directly connected hosts
//
// h1 <-> h2
func TestPingDirect(t *testing.T) {
n, err := g.NewNetwork(*nname, globalNetworkOptions...)
require.NoError(t, err, "Failed to create network")
defer n.Close()
h1, err := n.AddHost("h1")
require.NoError(t, err, "Failed to create host")
h2, err := n.AddHost("h2")
require.NoError(t, err, "Failed to create host")
err = n.AddLink(
g.NewInterface("veth0", h1,
o.AddressIP("10.0.0.1/24")),
g.NewInterface("veth0", h2,
o.AddressIP("10.0.0.2/24")))
require.NoError(t, err, "Failed to connect hosts")
_, err = h1.Run("cat", "/etc/hosts")
require.NoError(t, err, "Failed to show /etc/hosts file")
err = g.TestConnectivity(h1, h2)
require.NoError(t, err, "Failed to test connectivity between hosts")
}
// TestPingMultiHop performs and end-to-end ping test
// through a routed multi-hop topology
//
// h1 <-> sw1 <-> r1 <-> sw2 <-> h2
func TestPingMultiHop(t *testing.T) {
n, err := g.NewNetwork(*nname, globalNetworkOptions...)
require.NoError(t, err, "Failed to create network")
defer n.Close()
sw1, err := n.AddSwitch("sw1")
require.NoError(t, err, "Failed to create switch")
sw2, err := n.AddSwitch("sw2")
require.NoError(t, err, "Failed to create switch")
h1, err := n.AddHost("h1",
o.DefaultGatewayIP("10.0.1.1"),
g.NewInterface("veth0", sw1,
o.AddressIP("10.0.1.2/24"),
))
require.NoError(t, err, "Failed to add host")
h2, err := n.AddHost("h2",
o.DefaultGatewayIP("10.0.2.1"),
g.NewInterface("veth0", sw2,
o.AddressIP("10.0.2.2/24")))
require.NoError(t, err, "Failed to add host")
_, err = n.AddRouter("r1",
g.NewInterface("veth0", sw1,
o.AddressIP("10.0.1.1/24")),
g.NewInterface("veth1", sw2,
o.AddressIP("10.0.2.1/24")))
require.NoError(t, err, "Failed to add router")
err = g.TestConnectivity(h1, h2)
require.NoError(t, err, "Failed to test connectivity between hosts")
err = h1.Traceroute(h2)
require.NoError(t, err, "Failed to traceroute from h1 to h2")
}
func TestPingLoopback(t *testing.T) {
n, err := g.NewNetwork(*nname, globalNetworkOptions...)
require.NoError(t, err, "Failed to create network")
defer n.Close()
h, err := n.AddHost("h")
require.NoError(t, err, "Failed to add host")
_, err = h.Run("ping", "-4", "-c", 1, "localhost")
require.NoError(t, err, "Failed to ping")
_, err = h.Run("ping", "-6", "-c", 1, "localhost")
require.NoError(t, err, "Failed to ping")
}
func TestPingSelf(t *testing.T) {
n, err := g.NewNetwork(*nname, globalNetworkOptions...)
require.NoError(t, err, "Failed to create network")
defer n.Close()
h1, err := n.AddHost("h1")
require.NoError(t, err, "Failed to add host")
h2, err := n.AddHost("h2")
require.NoError(t, err, "Failed to add host")
err = n.AddLink(
g.NewInterface("veth0", h1,
o.AddressIP("10.0.0.1/24"),
o.AddressIP("fc::1/64")),
g.NewInterface("veth0", h2,
o.AddressIP("10.0.0.2/24"),
o.AddressIP("fc::2/64")))
require.NoError(t, err, "Failed to add link")
_, err = h1.PingWithNetwork(h1, "ip")
require.NoError(t, err, "Failed to ping")
_, err = h1.PingWithNetwork(h1, "ip6")
require.NoError(t, err, "Failed to ping")
}