| 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 | "context" |
| 21 | "io" |
| 22 | "net/http" |
| 23 | "net/http/httptest" |
| 24 | "sync" |
| 25 | "testing" |
| 26 | |
| 27 | "sigs.k8s.io/apiserver-network-proxy/tests/framework" |
| 28 | ) |
| 29 | |
| 30 | func TestProxy_ConcurrencyGRPC(t *testing.T) { |
| 31 | ctx := context.Background() |
| 32 | length := 1 << 20 |
| 33 | chunks := 10 |
| 34 | server := httptest.NewServer(newSizedServer(length, chunks)) |
| 35 | defer server.Close() |
| 36 | |
| 37 | ps := runGRPCProxyServer(t) |
| 38 | defer ps.Stop() |
| 39 | |
| 40 | a := runAgent(t, ps.AgentAddr()) |
| 41 | defer a.Stop() |
| 42 | waitForConnectedServerCount(t, 1, a) |
| 43 | |
| 44 | var wg sync.WaitGroup |
| 45 | verify := func() { |
| 46 | defer wg.Done() |
| 47 | |
| 48 | // run test client |
| 49 | tunnel, err := createSingleUseGrpcTunnel(ctx, ps.FrontAddr()) |
| 50 | if err != nil { |
| 51 | t.Error(err) |
| 52 | return |
| 53 | } |
| 54 | |
| 55 | c := &http.Client{ |
| 56 | Transport: &http.Transport{ |
| 57 | DialContext: tunnel.DialContext, |
| 58 | }, |
| 59 | } |
| 60 | |
| 61 | r, err := c.Get(server.URL) |
| 62 | if err != nil { |
| 63 | t.Error(err) |
| 64 | return |
| 65 | } |
| 66 | |
| 67 | data, err := io.ReadAll(r.Body) |
| 68 | if err != nil { |
| 69 | t.Error(err) |
| 70 | return |
| 71 | } |
| 72 | defer r.Body.Close() |
| 73 | |
| 74 | if len(data) != length*chunks { |
| 75 | t.Errorf("expect data length %d; got %d", length*chunks, len(data)) |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | concurrency := 10 |
| 80 | wg.Add(concurrency) |
| 81 | for i := 0; i < concurrency; i++ { |
| 82 | go verify() |
| 83 | } |
| 84 | wg.Wait() |
| 85 | } |
| 86 | |
| 87 | func TestProxy_ConcurrencyHTTP(t *testing.T) { |
| 88 | ctx := context.Background() |
| 89 | length := 1 << 20 |
| 90 | chunks := 10 |
| 91 | server := httptest.NewServer(newSizedServer(length, chunks)) |
| 92 | defer server.Close() |
| 93 | |
| 94 | ps := runHTTPConnProxyServer(t) |
| 95 | defer ps.Stop() |
| 96 | |
| 97 | a := runAgent(t, ps.AgentAddr()) |
| 98 | defer a.Stop() |
| 99 | waitForConnectedServerCount(t, 1, a) |
| 100 | |
| 101 | // run test clients |
| 102 | var wg sync.WaitGroup |
| 103 | verify := func() { |
| 104 | defer wg.Done() |
| 105 | tunnel, err := createHTTPConnectClient(ctx, ps.FrontAddr(), server.URL) |
| 106 | if err != nil { |
| 107 | t.Error(err) |
| 108 | } |
| 109 | data, err := clientRequest(tunnel, server.URL) |
| 110 | if err != nil { |
| 111 | t.Error(err) |
| 112 | } else if len(data) != length*chunks { |
| 113 | t.Errorf("expect data length %d; got %d", length*chunks, len(data)) |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | concurrency := 10 |
| 118 | wg.Add(concurrency) |
| 119 | for i := 0; i < concurrency; i++ { |
| 120 | go verify() |
| 121 | } |
| 122 | wg.Wait() |
| 123 | } |
| 124 | |
| 125 | // This test verifies that when one stream between a proxy agent and the proxy |
| 126 | // server terminates, the proxy server does not terminate other frontends |
| 127 | // supported by the same proxy agent but on different streams. |
| 128 | func TestAgent_MultipleConn(t *testing.T) { |
| 129 | testcases := []struct { |
| 130 | name string |
| 131 | proxyServerFunction func(testing.TB) framework.ProxyServer |
| 132 | clientFunction func(context.Context, string, string) (*http.Client, error) |
| 133 | }{ |
| 134 | { |
| 135 | name: "grpc", |
| 136 | proxyServerFunction: runGRPCProxyServer, |
| 137 | clientFunction: createGrpcTunnelClient, |
| 138 | }, |
| 139 | { |
| 140 | name: "http-connect", |
| 141 | proxyServerFunction: runHTTPConnProxyServer, |
| 142 | clientFunction: createHTTPConnectClient, |
| 143 | }, |
| 144 | } |
| 145 | |
| 146 | for _, tc := range testcases { |
| 147 | t.Run(tc.name, func(t *testing.T) { |
| 148 | ctx := context.Background() |
| 149 | waitServer := newWaitingServer() |
| 150 | server := httptest.NewServer(waitServer) |
| 151 | defer server.Close() |
| 152 | |
| 153 | ps := tc.proxyServerFunction(t) |
| 154 | defer ps.Stop() |
| 155 | |
| 156 | ai1 := runAgentWithID(t, "multipleAgentConn", ps.AgentAddr()) |
| 157 | defer ai1.Stop() |
| 158 | waitForConnectedServerCount(t, 1, ai1) |
| 159 | |
| 160 | // run test client |
| 161 | c, err := tc.clientFunction(ctx, ps.FrontAddr(), server.URL) |
| 162 | if err != nil { |
| 163 | t.Fatal(err) |
| 164 | } |
| 165 | |
| 166 | fcnStopCh := make(chan struct{}) |
| 167 | |
| 168 | go func() { |
| 169 | _, err := clientRequest(c, server.URL) |
| 170 | if err != nil { |
| 171 | t.Errorf("expected no error on proxy request, got %v", err) |
| 172 | } |
| 173 | close(fcnStopCh) |
| 174 | }() |
| 175 | <-waitServer.requestReceivedCh |
| 176 | |
| 177 | // Running an agent with the same ID simulates a second connection from the same agent. |
| 178 | // This simulates the scenario where a proxy agent established connections with HA proxy server |
| 179 | // and creates multiple connections with the same proxy server |
| 180 | ai2 := runAgentWithID(t, "multipleAgentConn", ps.AgentAddr()) |
| 181 | defer ai2.Stop() |
| 182 | waitForConnectedServerCount(t, 1, ai2) |
| 183 | // Wait for the server to run cleanup routine |
| 184 | waitForConnectedAgentCount(t, 1, ps) |
| 185 | close(waitServer.respondCh) |
| 186 | |
| 187 | <-fcnStopCh |
| 188 | }) |
| 189 | } |
| 190 | } |