|
| 1 | +// Copyright 2023 The etcd Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +// These tests are directly validating etcd connection multiplexing. |
| 16 | +//go:build !cluster_proxy |
| 17 | + |
| 18 | +package e2e |
| 19 | + |
| 20 | +import ( |
| 21 | + "context" |
| 22 | + "encoding/json" |
| 23 | + "fmt" |
| 24 | + "strings" |
| 25 | + "testing" |
| 26 | + |
| 27 | + "github.com/prometheus/common/expfmt" |
| 28 | + "github.com/stretchr/testify/assert" |
| 29 | + "github.com/stretchr/testify/require" |
| 30 | + |
| 31 | + pb "go.etcd.io/etcd/api/v3/etcdserverpb" |
| 32 | + "go.etcd.io/etcd/api/v3/mvccpb" |
| 33 | + "go.etcd.io/etcd/api/v3/version" |
| 34 | + "go.etcd.io/etcd/server/v3/etcdserver/api/etcdhttp" |
| 35 | + "go.etcd.io/etcd/tests/v3/framework/config" |
| 36 | + "go.etcd.io/etcd/tests/v3/framework/e2e" |
| 37 | +) |
| 38 | + |
| 39 | +func TestConnectionMultiplexing(t *testing.T) { |
| 40 | + e2e.BeforeTest(t) |
| 41 | + for _, tc := range []struct { |
| 42 | + name string |
| 43 | + serverTLS e2e.ClientConnType |
| 44 | + }{ |
| 45 | + { |
| 46 | + name: "ServerTLS", |
| 47 | + serverTLS: e2e.ClientTLS, |
| 48 | + }, |
| 49 | + { |
| 50 | + name: "ServerNonTLS", |
| 51 | + serverTLS: e2e.ClientNonTLS, |
| 52 | + }, |
| 53 | + { |
| 54 | + name: "ServerTLSAndNonTLS", |
| 55 | + serverTLS: e2e.ClientTLSAndNonTLS, |
| 56 | + }, |
| 57 | + } { |
| 58 | + t.Run(tc.name, func(t *testing.T) { |
| 59 | + ctx := context.Background() |
| 60 | + cfg := e2e.EtcdProcessClusterConfig{ClusterSize: 1, Client: e2e.ClientConfig{ConnectionType: tc.serverTLS}} |
| 61 | + clus, err := e2e.NewEtcdProcessCluster(ctx, t, e2e.WithConfig(&cfg)) |
| 62 | + require.NoError(t, err) |
| 63 | + defer clus.Close() |
| 64 | + |
| 65 | + var clientScenarios []e2e.ClientConnType |
| 66 | + switch tc.serverTLS { |
| 67 | + case e2e.ClientTLS: |
| 68 | + clientScenarios = []e2e.ClientConnType{e2e.ClientTLS} |
| 69 | + case e2e.ClientNonTLS: |
| 70 | + clientScenarios = []e2e.ClientConnType{e2e.ClientNonTLS} |
| 71 | + case e2e.ClientTLSAndNonTLS: |
| 72 | + clientScenarios = []e2e.ClientConnType{e2e.ClientTLS, e2e.ClientNonTLS} |
| 73 | + } |
| 74 | + |
| 75 | + for _, clientTLS := range clientScenarios { |
| 76 | + name := "ClientNonTLS" |
| 77 | + if clientTLS == e2e.ClientTLS { |
| 78 | + name = "ClientTLS" |
| 79 | + } |
| 80 | + t.Run(name, func(t *testing.T) { |
| 81 | + testConnectionMultiplexing(t, ctx, clus.EndpointsV3()[0], clientTLS) |
| 82 | + }) |
| 83 | + } |
| 84 | + }) |
| 85 | + } |
| 86 | + |
| 87 | +} |
| 88 | + |
| 89 | +func testConnectionMultiplexing(t *testing.T, ctx context.Context, endpoint string, connType e2e.ClientConnType) { |
| 90 | + switch connType { |
| 91 | + case e2e.ClientTLS: |
| 92 | + endpoint = e2e.ToTLS(endpoint) |
| 93 | + case e2e.ClientNonTLS: |
| 94 | + default: |
| 95 | + panic(fmt.Sprintf("Unsupported conn type %v", connType)) |
| 96 | + } |
| 97 | + t.Run("etcdctl", func(t *testing.T) { |
| 98 | + etcdctl, err := e2e.NewEtcdctl(e2e.ClientConfig{ConnectionType: connType}, []string{endpoint}) |
| 99 | + require.NoError(t, err) |
| 100 | + _, err = etcdctl.Get(ctx, "a", config.GetOptions{}) |
| 101 | + assert.NoError(t, err) |
| 102 | + }) |
| 103 | + t.Run("clientv3", func(t *testing.T) { |
| 104 | + c := newClient(t, []string{endpoint}, e2e.ClientConfig{ConnectionType: connType}) |
| 105 | + _, err := c.Get(ctx, "a") |
| 106 | + assert.NoError(t, err) |
| 107 | + }) |
| 108 | + t.Run("curl", func(t *testing.T) { |
| 109 | + for _, httpVersion := range []string{"2", "1.1", "1.0", ""} { |
| 110 | + tname := "http" + httpVersion |
| 111 | + if httpVersion == "" { |
| 112 | + tname = "default" |
| 113 | + } |
| 114 | + t.Run(tname, func(t *testing.T) { |
| 115 | + assert.NoError(t, fetchGrpcGateway(endpoint, httpVersion, connType)) |
| 116 | + assert.NoError(t, fetchMetrics(endpoint, httpVersion, connType)) |
| 117 | + assert.NoError(t, fetchVersion(endpoint, httpVersion, connType)) |
| 118 | + assert.NoError(t, fetchHealth(endpoint, httpVersion, connType)) |
| 119 | + assert.NoError(t, fetchDebugVars(endpoint, httpVersion, connType)) |
| 120 | + }) |
| 121 | + } |
| 122 | + }) |
| 123 | +} |
| 124 | + |
| 125 | +func fetchGrpcGateway(endpoint string, httpVersion string, connType e2e.ClientConnType) error { |
| 126 | + rangeData, err := json.Marshal(&pb.RangeRequest{ |
| 127 | + Key: []byte("a"), |
| 128 | + }) |
| 129 | + if err != nil { |
| 130 | + return err |
| 131 | + } |
| 132 | + req := e2e.CURLReq{Endpoint: "/v3/kv/range", Value: string(rangeData), Timeout: 5, HttpVersion: httpVersion} |
| 133 | + respData, err := curl(endpoint, "POST", req, connType) |
| 134 | + return validateGrpcgatewayRangeReponse([]byte(respData)) |
| 135 | +} |
| 136 | + |
| 137 | +func validateGrpcgatewayRangeReponse(respData []byte) error { |
| 138 | + // Modified json annotation so ResponseHeader fields are stored in string. |
| 139 | + type responseHeader struct { |
| 140 | + ClusterId uint64 `json:"cluster_id,string,omitempty"` |
| 141 | + MemberId uint64 `json:"member_id,string,omitempty"` |
| 142 | + Revision int64 `json:"revision,string,omitempty"` |
| 143 | + RaftTerm uint64 `json:"raft_term,string,omitempty"` |
| 144 | + } |
| 145 | + type rangeResponse struct { |
| 146 | + Header *responseHeader `json:"header,omitempty"` |
| 147 | + Kvs []*mvccpb.KeyValue `json:"kvs,omitempty"` |
| 148 | + More bool `json:"more,omitempty"` |
| 149 | + Count int64 `json:"count,omitempty"` |
| 150 | + } |
| 151 | + var resp rangeResponse |
| 152 | + return json.Unmarshal(respData, &resp) |
| 153 | +} |
| 154 | + |
| 155 | +func fetchMetrics(endpoint string, httpVersion string, connType e2e.ClientConnType) error { |
| 156 | + req := e2e.CURLReq{Endpoint: "/metrics", Timeout: 5, HttpVersion: httpVersion} |
| 157 | + respData, err := curl(endpoint, "GET", req, connType) |
| 158 | + if err != nil { |
| 159 | + return err |
| 160 | + } |
| 161 | + var parser expfmt.TextParser |
| 162 | + _, err = parser.TextToMetricFamilies(strings.NewReader(strings.ReplaceAll(respData, "\r\n", "\n"))) |
| 163 | + return err |
| 164 | +} |
| 165 | + |
| 166 | +func fetchVersion(endpoint string, httpVersion string, connType e2e.ClientConnType) error { |
| 167 | + req := e2e.CURLReq{Endpoint: "/version", Timeout: 5, HttpVersion: httpVersion} |
| 168 | + respData, err := curl(endpoint, "GET", req, connType) |
| 169 | + if err != nil { |
| 170 | + return err |
| 171 | + } |
| 172 | + var resp version.Versions |
| 173 | + return json.Unmarshal([]byte(respData), &resp) |
| 174 | +} |
| 175 | + |
| 176 | +func fetchHealth(endpoint string, httpVersion string, connType e2e.ClientConnType) error { |
| 177 | + req := e2e.CURLReq{Endpoint: "/health", Timeout: 5, HttpVersion: httpVersion} |
| 178 | + respData, err := curl(endpoint, "GET", req, connType) |
| 179 | + if err != nil { |
| 180 | + return err |
| 181 | + } |
| 182 | + var resp etcdhttp.Health |
| 183 | + return json.Unmarshal([]byte(respData), &resp) |
| 184 | +} |
| 185 | + |
| 186 | +func fetchDebugVars(endpoint string, httpVersion string, connType e2e.ClientConnType) error { |
| 187 | + req := e2e.CURLReq{Endpoint: "/debug/vars", Timeout: 5, HttpVersion: httpVersion} |
| 188 | + respData, err := curl(endpoint, "GET", req, connType) |
| 189 | + if err != nil { |
| 190 | + return err |
| 191 | + } |
| 192 | + var resp map[string]interface{} |
| 193 | + return json.Unmarshal([]byte(respData), &resp) |
| 194 | +} |
| 195 | + |
| 196 | +func curl(endpoint string, method string, curlReq e2e.CURLReq, connType e2e.ClientConnType) (string, error) { |
| 197 | + args := e2e.CURLPrefixArgs(endpoint, e2e.ClientConfig{ConnectionType: connType}, false, method, curlReq) |
| 198 | + lines, err := e2e.RunUtilCompletion(args, nil) |
| 199 | + if err != nil { |
| 200 | + return "", err |
| 201 | + } |
| 202 | + return strings.Join(lines, "\n"), nil |
| 203 | +} |
0 commit comments