Skip to content

Commit ce816b2

Browse files
committed
endpoints.Interpret returns Host:port as ServerName
Signed-off-by: Chao Chen <[email protected]>
1 parent e59e3d7 commit ce816b2

File tree

4 files changed

+49
-46
lines changed

4 files changed

+49
-46
lines changed

client/v3/internal/endpoint/endpoint.go

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,6 @@ func extractHostFromHostPort(ep string) string {
4141
return host
4242
}
4343

44-
func extractHostFromPath(pathStr string) string {
45-
return extractHostFromHostPort(path.Base(pathStr))
46-
}
47-
4844
// mustSplit2 returns the values from strings.SplitN(s, sep, 2).
4945
// If sep is not found, it returns ("", "", false) instead.
5046
func mustSplit2(s, sep string) (string, string) {
@@ -96,29 +92,29 @@ func translateEndpoint(ep string) (addr string, serverName string, requireCreds
9692
if strings.HasPrefix(ep, "unix:///") || strings.HasPrefix(ep, "unixs:///") {
9793
// absolute path case
9894
schema, absolutePath := mustSplit2(ep, "://")
99-
return "unix://" + absolutePath, extractHostFromPath(absolutePath), schemeToCredsRequirement(schema)
95+
return "unix://" + absolutePath, path.Base(absolutePath), schemeToCredsRequirement(schema)
10096
}
10197
if strings.HasPrefix(ep, "unix://") || strings.HasPrefix(ep, "unixs://") {
10298
// legacy etcd local path
10399
schema, localPath := mustSplit2(ep, "://")
104-
return "unix:" + localPath, extractHostFromPath(localPath), schemeToCredsRequirement(schema)
100+
return "unix:" + localPath, path.Base(localPath), schemeToCredsRequirement(schema)
105101
}
106102
schema, localPath := mustSplit2(ep, ":")
107-
return "unix:" + localPath, extractHostFromPath(localPath), schemeToCredsRequirement(schema)
103+
return "unix:" + localPath, path.Base(localPath), schemeToCredsRequirement(schema)
108104
}
109105

110106
if strings.Contains(ep, "://") {
111107
url, err := url.Parse(ep)
112108
if err != nil {
113-
return ep, extractHostFromHostPort(ep), CREDS_OPTIONAL
109+
return ep, ep, CREDS_OPTIONAL
114110
}
115111
if url.Scheme == "http" || url.Scheme == "https" {
116-
return url.Host, url.Hostname(), schemeToCredsRequirement(url.Scheme)
112+
return url.Host, url.Host, schemeToCredsRequirement(url.Scheme)
117113
}
118-
return ep, url.Hostname(), schemeToCredsRequirement(url.Scheme)
114+
return ep, url.Host, schemeToCredsRequirement(url.Scheme)
119115
}
120116
// Handles plain addresses like 10.0.0.44:437.
121-
return ep, extractHostFromHostPort(ep), CREDS_OPTIONAL
117+
return ep, ep, CREDS_OPTIONAL
122118
}
123119

124120
// RequiresCredentials returns whether given endpoint requires

client/v3/internal/endpoint/endpoint_test.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,35 +27,35 @@ func Test_interpret(t *testing.T) {
2727
}{
2828
{"127.0.0.1", "127.0.0.1", "127.0.0.1", CREDS_OPTIONAL},
2929
{"localhost", "localhost", "localhost", CREDS_OPTIONAL},
30-
{"localhost:8080", "localhost:8080", "localhost", CREDS_OPTIONAL},
30+
{"localhost:8080", "localhost:8080", "localhost:8080", CREDS_OPTIONAL},
3131

3232
{"unix:127.0.0.1", "unix:127.0.0.1", "127.0.0.1", CREDS_OPTIONAL},
33-
{"unix:127.0.0.1:8080", "unix:127.0.0.1:8080", "127.0.0.1", CREDS_OPTIONAL},
33+
{"unix:127.0.0.1:8080", "unix:127.0.0.1:8080", "127.0.0.1:8080", CREDS_OPTIONAL},
3434

3535
{"unix://127.0.0.1", "unix:127.0.0.1", "127.0.0.1", CREDS_OPTIONAL},
36-
{"unix://127.0.0.1:8080", "unix:127.0.0.1:8080", "127.0.0.1", CREDS_OPTIONAL},
36+
{"unix://127.0.0.1:8080", "unix:127.0.0.1:8080", "127.0.0.1:8080", CREDS_OPTIONAL},
3737

3838
{"unixs:127.0.0.1", "unix:127.0.0.1", "127.0.0.1", CREDS_REQUIRE},
39-
{"unixs:127.0.0.1:8080", "unix:127.0.0.1:8080", "127.0.0.1", CREDS_REQUIRE},
39+
{"unixs:127.0.0.1:8080", "unix:127.0.0.1:8080", "127.0.0.1:8080", CREDS_REQUIRE},
4040
{"unixs://127.0.0.1", "unix:127.0.0.1", "127.0.0.1", CREDS_REQUIRE},
41-
{"unixs://127.0.0.1:8080", "unix:127.0.0.1:8080", "127.0.0.1", CREDS_REQUIRE},
41+
{"unixs://127.0.0.1:8080", "unix:127.0.0.1:8080", "127.0.0.1:8080", CREDS_REQUIRE},
4242

4343
{"http://127.0.0.1", "127.0.0.1", "127.0.0.1", CREDS_DROP},
44-
{"http://127.0.0.1:8080", "127.0.0.1:8080", "127.0.0.1", CREDS_DROP},
44+
{"http://127.0.0.1:8080", "127.0.0.1:8080", "127.0.0.1:8080", CREDS_DROP},
4545
{"https://127.0.0.1", "127.0.0.1", "127.0.0.1", CREDS_REQUIRE},
46-
{"https://127.0.0.1:8080", "127.0.0.1:8080", "127.0.0.1", CREDS_REQUIRE},
47-
{"https://localhost:20000", "localhost:20000", "localhost", CREDS_REQUIRE},
46+
{"https://127.0.0.1:8080", "127.0.0.1:8080", "127.0.0.1:8080", CREDS_REQUIRE},
47+
{"https://localhost:20000", "localhost:20000", "localhost:20000", CREDS_REQUIRE},
4848

4949
{"unix:///tmp/abc", "unix:///tmp/abc", "abc", CREDS_OPTIONAL},
5050
{"unixs:///tmp/abc", "unix:///tmp/abc", "abc", CREDS_REQUIRE},
51-
{"unix:///tmp/abc:1234", "unix:///tmp/abc:1234", "abc", CREDS_OPTIONAL},
52-
{"unixs:///tmp/abc:1234", "unix:///tmp/abc:1234", "abc", CREDS_REQUIRE},
51+
{"unix:///tmp/abc:1234", "unix:///tmp/abc:1234", "abc:1234", CREDS_OPTIONAL},
52+
{"unixs:///tmp/abc:1234", "unix:///tmp/abc:1234", "abc:1234", CREDS_REQUIRE},
5353
{"etcd.io", "etcd.io", "etcd.io", CREDS_OPTIONAL},
5454
{"http://etcd.io/abc", "etcd.io", "etcd.io", CREDS_DROP},
5555
{"dns://something-other", "dns://something-other", "something-other", CREDS_OPTIONAL},
5656

57-
{"http://[2001:db8:1f70::999:de8:7648:6e8]:100/", "[2001:db8:1f70::999:de8:7648:6e8]:100", "2001:db8:1f70::999:de8:7648:6e8", CREDS_DROP},
58-
{"[2001:db8:1f70::999:de8:7648:6e8]:100", "[2001:db8:1f70::999:de8:7648:6e8]:100", "2001:db8:1f70::999:de8:7648:6e8", CREDS_OPTIONAL},
57+
{"http://[2001:db8:1f70::999:de8:7648:6e8]:100/", "[2001:db8:1f70::999:de8:7648:6e8]:100", "[2001:db8:1f70::999:de8:7648:6e8]:100", CREDS_DROP},
58+
{"[2001:db8:1f70::999:de8:7648:6e8]:100", "[2001:db8:1f70::999:de8:7648:6e8]:100", "[2001:db8:1f70::999:de8:7648:6e8]:100", CREDS_OPTIONAL},
5959
{"unix:unexpected-file_name#123$456", "unix:unexpected-file_name#123$456", "unexpected-file_name#123$456", CREDS_OPTIONAL},
6060
}
6161
for _, tt := range tests {

tests/e2e/ctl_v3_grpc_test.go

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ package e2e
1919
import (
2020
"context"
2121
"fmt"
22+
"net/url"
2223
"strings"
2324
"testing"
2425
"time"
2526

2627
"github.com/stretchr/testify/assert"
2728

28-
"go.etcd.io/etcd/tests/v3/framework/config"
2929
"go.etcd.io/etcd/tests/v3/framework/e2e"
3030
"go.etcd.io/etcd/tests/v3/framework/testutils"
3131
)
@@ -127,20 +127,20 @@ func TestAuthority(t *testing.T) {
127127
t.Fatalf("could not start etcd process cluster (%v)", err)
128128
}
129129
defer epc.Close()
130-
endpoints := templateEndpoints(t, tc.clientURLPattern, epc)
131130

132-
client, err := e2e.NewEtcdctl(cfg.Client, endpoints)
133-
assert.NoError(t, err)
134-
err = client.Put(ctx, "foo", "bar", config.PutOptions{})
135-
if err != nil {
136-
t.Fatal(err)
131+
endpoints := templateEndpoints(t, tc.clientURLPattern, epc)
132+
client := newClient(t, endpoints, cfg.Client)
133+
for i := 0; i < 100; i++ {
134+
_, err = client.Put(ctx, "foo", "bar")
135+
if err != nil {
136+
t.Fatal(err)
137+
}
137138
}
138139

139140
testutils.ExecuteWithTimeout(t, 5*time.Second, func() {
140-
assertAuthority(t, strings.ReplaceAll(tc.expectAuthorityPattern, "${MEMBER_PORT}", "20000"), epc)
141+
assertAuthority(t, tc.expectAuthorityPattern, epc)
141142
})
142143
})
143-
144144
}
145145
}
146146
}
@@ -156,16 +156,20 @@ func templateEndpoints(t *testing.T, pattern string, clus *e2e.EtcdProcessCluste
156156
return endpoints
157157
}
158158

159-
func assertAuthority(t *testing.T, expectAurhority string, clus *e2e.EtcdProcessCluster) {
160-
var logs []e2e.LogsExpect
161-
for _, proc := range clus.Procs {
162-
logs = append(logs, proc.Logs())
159+
func assertAuthority(t *testing.T, expectAuthorityPattern string, clus *e2e.EtcdProcessCluster) {
160+
for i := range clus.Procs {
161+
line := firstMatch(t, `http2: decoded hpack field header field ":authority"`, clus.Procs[i].Logs())
162+
line = strings.TrimSuffix(line, "\n")
163+
line = strings.TrimSuffix(line, "\r")
164+
165+
u, err := url.Parse(clus.Procs[i].EndpointsGRPC()[0])
166+
if err != nil {
167+
t.Fatal(err)
168+
}
169+
expectAuthority := strings.ReplaceAll(expectAuthorityPattern, "${MEMBER_PORT}", u.Port())
170+
expectLine := fmt.Sprintf(`http2: decoded hpack field header field ":authority" = %q`, expectAuthority)
171+
assert.True(t, strings.HasSuffix(line, expectLine), fmt.Sprintf("Got %q expected suffix %q", line, expectLine))
163172
}
164-
line := firstMatch(t, `http2: decoded hpack field header field ":authority"`, logs...)
165-
line = strings.TrimSuffix(line, "\n")
166-
line = strings.TrimSuffix(line, "\r")
167-
expectLine := fmt.Sprintf(`http2: decoded hpack field header field ":authority" = %q`, expectAurhority)
168-
assert.True(t, strings.HasSuffix(line, expectLine), fmt.Sprintf("Got %q expected suffix %q", line, expectLine))
169173
}
170174

171175
func firstMatch(t *testing.T, expectLine string, logs ...e2e.LogsExpect) string {

tests/integration/grpc_test.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,14 @@ func TestAuthority(t *testing.T) {
103103
defer kv.Close()
104104

105105
putRequestMethod := "/etcdserverpb.KV/Put"
106-
_, err := kv.Put(context.TODO(), "foo", "bar")
107-
if err != nil {
108-
t.Fatal(err)
106+
for i := 0; i < 100; i++ {
107+
_, err := kv.Put(context.TODO(), "foo", "bar")
108+
if err != nil {
109+
t.Fatal(err)
110+
}
109111
}
110112

111-
assertAuthority(t, templateAuthority(t, tc.expectAuthorityPattern, clus.Members[0]), clus, putRequestMethod)
113+
assertAuthority(t, tc.expectAuthorityPattern, clus, putRequestMethod)
112114
})
113115
}
114116
}
@@ -162,10 +164,11 @@ func templateAuthority(t *testing.T, pattern string, m *integration.Member) stri
162164
return authority
163165
}
164166

165-
func assertAuthority(t *testing.T, expectedAuthority string, clus *integration.Cluster, filterMethod string) {
167+
func assertAuthority(t *testing.T, expectedAuthorityPattern string, clus *integration.Cluster, filterMethod string) {
166168
t.Helper()
167169
requestsFound := 0
168170
for _, m := range clus.Members {
171+
expectedAuthority := templateAuthority(t, expectedAuthorityPattern, m)
169172
for _, r := range m.RecordedRequests() {
170173
if filterMethod != "" && r.FullMethod != filterMethod {
171174
continue

0 commit comments

Comments
 (0)