Debian Codegraph @

tests/agent_disconnect_test.go

raw
1 /*
2 Copyright 2022 The Kubernetes Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package tests
18
19 import (
20 "bufio"
21 "context"
22 "fmt"
23 "io"
24 "net"
25 "net/http"
26 "net/http/httptest"
27 "net/url"
28 "os"
29 "testing"
30 "time"
31
32 "sigs.k8s.io/apiserver-network-proxy/tests/framework"
33 )
34
35 func TestProxy_Agent_Disconnect_Persistent_Connection(t *testing.T) {
36 testcases := []struct {
37 name string
38 proxyServerFunction func(t testing.TB) framework.ProxyServer
39 clientFunction func(context.Context, string, string) (*http.Client, error)
40 }{
41 {
42 name: "grpc",
43 proxyServerFunction: runGRPCProxyServer,
44 clientFunction: createGrpcTunnelClient,
45 },
46 {
47 name: "http-connect",
48 proxyServerFunction: runHTTPConnProxyServer,
49 clientFunction: createHTTPConnectClient,
50 },
51 }
52
53 for _, tc := range testcases {
54 t.Run(tc.name, func(t *testing.T) {
55 ctx := context.Background()
56 server := httptest.NewServer(newEchoServer("hello"))
57 defer server.Close()
58
59 ps := tc.proxyServerFunction(t)
60 defer ps.Stop()
61
62 a := runAgent(t, ps.AgentAddr())
63 waitForConnectedAgentCount(t, 1, ps)
64
65 // run test client
66
67 c, err := tc.clientFunction(ctx, ps.FrontAddr(), server.URL)
68 if err != nil {
69 t.Fatalf("error obtaining client: %v", err)
70 }
71
72 _, err = clientRequest(c, server.URL)
73
74 if err != nil {
75 t.Errorf("expected no error on proxy request, got %v", err)
76 }
77 a.Stop()
78
79 // Wait for the agent to disconnect
80 waitForConnectedAgentCount(t, 0, ps)
81
82 // Reuse same client to make the request
83 _, err = clientRequest(c, server.URL)
84 if err == nil {
85 t.Errorf("expect request using http persistent connections to fail after dialing on a broken connection")
86 } else if os.IsTimeout(err) {
87 t.Errorf("expect request using http persistent connections to fail with error use of closed network connection. Got timeout")
88 }
89 })
90 }
91 }
92
93 func TestAgentRestartReconnect(t *testing.T) {
94 testcases := []struct {
95 name string
96 proxyServerFunction func(testing.TB) framework.ProxyServer
97 clientFunction func(context.Context, string, string) (*http.Client, error)
98 }{
99 {
100 name: "grpc",
101 proxyServerFunction: runGRPCProxyServer,
102 clientFunction: createGrpcTunnelClient,
103 },
104 {
105 name: "http-connect",
106 proxyServerFunction: runHTTPConnProxyServer,
107 clientFunction: createHTTPConnectClient,
108 },
109 }
110
111 for _, tc := range testcases {
112 t.Run(tc.name, func(t *testing.T) {
113 ctx := context.Background()
114
115 server := httptest.NewServer(newEchoServer("hello"))
116 defer server.Close()
117
118 ps := tc.proxyServerFunction(t)
119 defer ps.Stop()
120
121 ai1 := runAgent(t, ps.AgentAddr())
122 waitForConnectedServerCount(t, 1, ai1)
123
124 // run test client
125
126 c, err := tc.clientFunction(ctx, ps.FrontAddr(), server.URL)
127 if err != nil {
128 t.Fatalf("error obtaining client: %v", err)
129 }
130
131 _, err = clientRequest(c, server.URL)
132 if err != nil {
133 t.Errorf("expected no error on proxy request, got %v", err)
134 }
135 ai1.Stop()
136
137 // Wait for the agent to disconnect
138 waitForConnectedAgentCount(t, 0, ps)
139
140 // Reconnect agent
141 ai2 := runAgent(t, ps.AgentAddr())
142 defer ai2.Stop()
143 waitForConnectedServerCount(t, 1, ai2)
144
145 // Proxy requests should work again after agent reconnects
146 c2, err := tc.clientFunction(ctx, ps.FrontAddr(), server.URL)
147 if err != nil {
148 t.Fatalf("error obtaining client: %v", err)
149 }
150
151 _, err = clientRequest(c2, server.URL)
152
153 if err != nil {
154 t.Errorf("expected no error on proxy request, got %v", err)
155 }
156 })
157 }
158 }
159
160 func clientRequest(c *http.Client, addr string) ([]byte, error) {
161 r, err := c.Get(addr)
162 if err != nil {
163 return nil, fmt.Errorf("http GET %q: %w", addr, err)
164 }
165
166 data, err := io.ReadAll(r.Body)
167 if err != nil {
168 return nil, fmt.Errorf("error reading response body: %w", err)
169 }
170
171 r.Body.Close()
172
173 return data, nil
174 }
175
176 func createGrpcTunnelClient(ctx context.Context, proxyAddr, _ string) (*http.Client, error) {
177 tunnel, err := createSingleUseGrpcTunnel(ctx, proxyAddr)
178 if err != nil {
179 return nil, err
180 }
181
182 c := &http.Client{
183 Timeout: 10 * time.Second,
184 Transport: &http.Transport{
185 DialContext: tunnel.DialContext,
186 },
187 }
188
189 return c, nil
190 }
191
192 func createHTTPConnectClient(_ context.Context, proxyAddr, addr string) (*http.Client, error) {
193 conn, err := net.Dial("unix", proxyAddr)
194 if err != nil {
195 return nil, err
196 }
197
198 serverURL, _ := url.Parse(addr)
199
200 // Send HTTP-Connect request
201 _, err = fmt.Fprintf(conn, "CONNECT %s HTTP/1.1\r\nHost: %s\r\n\r\n", serverURL.Host, "127.0.0.1")
202 if err != nil {
203 return nil, err
204 }
205
206 // Parse the HTTP response for Connect
207 br := bufio.NewReader(conn)
208 res, err := http.ReadResponse(br, nil)
209 if err != nil {
210 return nil, fmt.Errorf("reading HTTP response from CONNECT: %v", err)
211 }
212 if res.StatusCode != 200 {
213 return nil, fmt.Errorf("expect 200; got %d", res.StatusCode)
214 }
215 if br.Buffered() > 0 {
216 return nil, fmt.Errorf("unexpected extra buffer")
217 }
218
219 dialer := func(_, _ string) (net.Conn, error) {
220 return conn, nil
221 }
222
223 c := &http.Client{
224 Timeout: 30 * time.Second,
225 Transport: &http.Transport{
226 Dial: dialer,
227 },
228 }
229
230 return c, nil
231 }