-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathdualstack_test.go
More file actions
180 lines (159 loc) · 5.37 KB
/
dualstack_test.go
File metadata and controls
180 lines (159 loc) · 5.37 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
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
package dualstack
import (
"context"
"encoding/json"
"fmt"
"io"
"os"
"strings"
"testing"
"github.com/linkerd/linkerd2/testutil"
)
type IP struct {
IP string `json:"ip"`
}
var TestHelper *testutil.TestHelper
func TestMain(m *testing.M) {
TestHelper = testutil.NewTestHelper()
// Block test execution until control plane is running
TestHelper.WaitUntilDeployReady(testutil.LinkerdDeployReplicasEdge)
os.Exit(m.Run())
}
// TestDualStack creates an injected pod that starts two servers, one listening
// on the IPv4 wildcard address and serving the string "IPv4", and another
// listening on the IPv6 wildcard address and serving the string "IPv6". They
// are fronted by a DualStack Service. We test that we can reach those two IPs
// directly, and that making a request to the service's FQDN always hits the
// IPv6 endpoint.
func TestDualStack(t *testing.T) {
if !TestHelper.DualStack() {
t.Skip("Skipping DualStack test")
}
TestHelper.WithDataPlaneNamespace(context.Background(), "dualstack-test", map[string]string{}, t, func(t *testing.T, ns string) {
out, err := TestHelper.Kubectl("",
"create", "configmap", "go-app",
"--from-file=main.go=testdata/ipfamilies-server.go",
"-n", ns,
)
if err != nil {
testutil.AnnotatedFatalf(t, "unexpected error", "unexpected error: %v\noutput:\n%s", err, out)
}
out, err = TestHelper.Kubectl("",
"apply", "-f", "testdata/ipfamilies-server-client.yml",
"-n", ns,
)
if err != nil {
testutil.AnnotatedFatalf(t, "unexpected error", "unexpected error: %v\noutput:\n%s", err, out)
}
checkPods(t, ns, "ipfamilies-server")
checkPods(t, ns, "client")
var clientIPv6, serverIPv4, serverIPv6 string
t.Run("Retrieve pod IPs", func(t *testing.T) {
cmd := []string{
"get", "po",
"-o", "jsonpath='{.items[*].status.podIPs}'",
"-n", ns,
}
out, err = TestHelper.Kubectl("", append(cmd, "-l", "app=server")...)
if err != nil {
testutil.AnnotatedFatalf(t, "unexpected error", "unexpected error: %v\noutput:\n%s", err, out)
}
var IPs []IP
out = strings.Trim(out, "'")
if err = json.Unmarshal([]byte(out), &IPs); err != nil {
testutil.AnnotatedFatalf(t, "error unmarshaling JSON", "error unmarshaling JSON '%s': %s", out, err)
}
if len(IPs) != 2 {
testutil.AnnotatedFatalf(t, "unexpected number of IPs", "expected 2 IPs, got %s", fmt.Sprint(len(IPs)))
}
serverIPv4 = IPs[0].IP
serverIPv6 = IPs[1].IP
out, err = TestHelper.Kubectl("", append(cmd, "-l", "app=client")...)
if err != nil {
testutil.AnnotatedFatalf(t, "unexpected error", "unexpected error: %v\noutput:\n%s", err, out)
}
out = strings.Trim(out, "'")
if err = json.Unmarshal([]byte(out), &IPs); err != nil {
testutil.AnnotatedFatalf(t, "error unmarshaling JSON", "error unmarshaling JSON '%s': %s", out, err)
}
if len(IPs) != 2 {
testutil.AnnotatedFatalf(t, "unexpected number of IPs", "expected 2 IPs, got %s", fmt.Sprint(len(IPs)))
}
clientIPv6 = IPs[1].IP
})
t.Run("Apply policy", func(t *testing.T) {
file, err := os.Open("testdata/ipfamilies-policy.yml")
if err != nil {
testutil.AnnotatedFatalf(t, "unexpected error", "unexpected error: %v", err)
}
defer file.Close()
manifest, err := io.ReadAll(file)
if err != nil {
testutil.AnnotatedFatalf(t, "unexpected error", "unexpected error: %v", err)
}
in := strings.ReplaceAll(string(manifest), "{IPv6}", clientIPv6)
out, err = TestHelper.KubectlApply(in, ns)
if err != nil {
testutil.AnnotatedFatalf(t, "unexpected error", "unexpected error: %v\noutput:\n%s", err, out)
}
})
t.Run("Hit IPv4 addr directly", func(t *testing.T) {
out, err = TestHelper.Kubectl("",
"exec", "deploy/client",
"-c", "curl",
"-n", ns,
"--",
"curl", "-s", "-S", "--stderr", "-", "http://"+serverIPv4+":8080",
)
if err != nil {
testutil.AnnotatedFatalf(t, "unexpected error", "unexpected error: %v\noutput:\n%s", err, out)
}
if out != "IPv4\n" {
testutil.AnnotatedFatalf(t, "unexpected output", "expected 'IPv4', received '%s'", out)
}
})
t.Run("Hit IPv6 addr directly", func(t *testing.T) {
out, err = TestHelper.Kubectl("",
"exec", "deploy/client",
"-c", "curl",
"-n", ns,
"--",
"curl", "-s", "-S", "--stderr", "-", "http://["+serverIPv6+"]:8080",
)
if err != nil {
testutil.AnnotatedFatalf(t, "unexpected error", "unexpected error: %v\noutput:\n%s", err, out)
}
if out != "IPv6\n" {
testutil.AnnotatedFatalf(t, "expected 'IPv6', received '%s'", out)
}
})
t.Run("Hit FQDN directly (should always resolve to IPv6)", func(t *testing.T) {
for i := 0; i < 10; i++ {
out, err = TestHelper.Kubectl("",
"exec", "deploy/client",
"-c", "curl",
"-n", ns,
"--",
"curl", "-s", "-S", "--stderr", "-", "http://ipfamilies-server:8080",
)
if err != nil {
testutil.AnnotatedFatalf(t, "unexpected error", "unexpected error: %v\noutput:\n%s", err, out)
}
if out != "IPv6\n" {
testutil.AnnotatedFatalf(t, "expected 'IPv6', received '%s'", out)
}
}
})
})
}
func checkPods(t *testing.T, ns, pod string) {
t.Helper()
if err := TestHelper.CheckPods(context.Background(), ns, pod, 1); err != nil {
//nolint:errorlint
if rce, ok := err.(*testutil.RestartCountError); ok {
testutil.AnnotatedWarn(t, "CheckPods timed-out", rce)
} else {
testutil.AnnotatedError(t, "CheckPods timed-out", err)
}
}
}