-
Notifications
You must be signed in to change notification settings - Fork 669
fix: preserve multi-host HA DSN addresses (Go 1.26) #1946
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -563,6 +563,57 @@ func TestParseDSN(t *testing.T) { | |
| }, | ||
| "", | ||
| }, | ||
| // Regression for https://github.com/ClickHouse/clickhouse-go/issues/1784 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we do not name tests after issues - tests should match functionality |
||
| // (Go 1.26 net/url multi-host / HA DSN). Also covers auth + query. | ||
| { | ||
| "HA multi-host with auth and secure (issue 1784)", | ||
| "clickhouse://user:pass@host1:9440,host2:9440/database?secure=true", | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what user and password would be used for second host? |
||
| &Options{ | ||
| Protocol: Native, | ||
| TLS: &tls.Config{ | ||
| InsecureSkipVerify: false, | ||
| }, | ||
| Addr: []string{"host1:9440", "host2:9440"}, | ||
| Settings: Settings{}, | ||
| Auth: Auth{ | ||
| Username: "user", | ||
| Password: "pass", | ||
| Database: "database", | ||
| }, | ||
| scheme: "clickhouse", | ||
| }, | ||
| "", | ||
| }, | ||
| { | ||
| "HA multi-host HTTP scheme", | ||
| "http://host1:8123,host2:8123/db", | ||
| &Options{ | ||
| Protocol: HTTP, | ||
| TLS: nil, | ||
| Addr: []string{"host1:8123", "host2:8123"}, | ||
| Settings: Settings{}, | ||
| Auth: Auth{ | ||
| Database: "db", | ||
| }, | ||
| scheme: "http", | ||
| }, | ||
| "", | ||
| }, | ||
| { | ||
| "HA multi-host IPv6", | ||
| "clickhouse://[::1]:9440,[2001:db8::1]:9440/test_database", | ||
| &Options{ | ||
| Protocol: Native, | ||
| TLS: nil, | ||
| Addr: []string{"[::1]:9440", "[2001:db8::1]:9440"}, | ||
| Settings: Settings{}, | ||
| Auth: Auth{ | ||
| Database: "test_database", | ||
| }, | ||
| scheme: "clickhouse", | ||
| }, | ||
| "", | ||
| }, | ||
| } | ||
|
|
||
| for _, testCase := range testCases { | ||
|
|
@@ -587,6 +638,53 @@ func parseURL(t *testing.T, v string) *url.URL { | |
| return u | ||
| } | ||
|
|
||
| func TestDSNAddrList(t *testing.T) { | ||
| t.Parallel() | ||
| cases := []struct { | ||
| name string | ||
| raw string | ||
| parsedHost string | ||
| want []string | ||
| }{ | ||
| { | ||
| name: "issue 1784 multi-host", | ||
| raw: "clickhouse://host1:9440,host2:9440/database", | ||
| parsedHost: "host1:9440,host2:9440", | ||
| want: []string{"host1:9440", "host2:9440"}, | ||
| }, | ||
| { | ||
| name: "multi-host with userinfo", | ||
| raw: "clickhouse://user:pass@host1:9440,host2:9440/db?secure=true", | ||
| parsedHost: "host1:9440,host2:9440", | ||
| want: []string{"host1:9440", "host2:9440"}, | ||
| }, | ||
| { | ||
| name: "bracketed IPv6 multi-host", | ||
| raw: "clickhouse://[::1]:9440,[2001:db8::1]:9440/db", | ||
| parsedHost: "[2001:db8::1]:9440", // parsers may keep only the last host | ||
| want: []string{"[::1]:9440", "[2001:db8::1]:9440"}, | ||
| }, | ||
| { | ||
| name: "fallback to parsed host", | ||
| raw: "not-a-url", | ||
| parsedHost: "only-host:9000", | ||
| want: []string{"only-host:9000"}, | ||
| }, | ||
| } | ||
| for _, tc := range cases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| assert.Equal(t, tc.want, dsnAddrList(tc.raw, tc.parsedHost)) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestSplitHostList(t *testing.T) { | ||
| t.Parallel() | ||
| assert.Equal(t, []string{"a:1", "b:2"}, splitHostList("a:1,b:2")) | ||
| assert.Equal(t, []string{"[::1]:1", "[::2]:2"}, splitHostList("[::1]:1,[::2]:2")) | ||
| assert.Equal(t, []string{"single"}, splitHostList("single")) | ||
| } | ||
|
|
||
| func TestLogger(t *testing.T) { | ||
| t.Run("debug=1 via DSN produces non-noop logger", func(t *testing.T) { | ||
| opts, err := ParseDSN("clickhouse://127.0.0.1/test?debug=1") | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| package churl | ||
|
|
||
| import ( | ||
| "testing" | ||
| ) | ||
|
|
||
| // Multi-host (HA) DSNs must parse under churl so clickhouse-go remains compatible | ||
| // when net/url rejects or normalizes multi-host authorities (see Go 1.26 / #1784). | ||
| func TestParseMultiHost(t *testing.T) { | ||
| t.Parallel() | ||
| cases := []struct { | ||
| raw string | ||
| host string | ||
| path string | ||
| }{ | ||
| { | ||
| raw: "clickhouse://host1:9440,host2:9440/database", | ||
| host: "host1:9440,host2:9440", | ||
| path: "/database", | ||
| }, | ||
| { | ||
| raw: "clickhouse://user:pass@host1:9440,host2:9440/db?secure=true", | ||
| host: "host1:9440,host2:9440", | ||
| path: "/db", | ||
| }, | ||
| { | ||
| raw: "http://host1:8123,host2:8123/db", | ||
| host: "host1:8123,host2:8123", | ||
| path: "/db", | ||
| }, | ||
| } | ||
| for _, tc := range cases { | ||
| t.Run(tc.raw, func(t *testing.T) { | ||
| u, err := Parse(tc.raw) | ||
| if err != nil { | ||
| t.Fatalf("Parse(%q): %v", tc.raw, err) | ||
| } | ||
| if u.Host != tc.host { | ||
| t.Fatalf("Host: got %q want %q", u.Host, tc.host) | ||
| } | ||
| if u.Path != tc.path { | ||
| t.Fatalf("Path: got %q want %q", u.Path, tc.path) | ||
| } | ||
| }) | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are already tools that can parse such form of url: