Skip to content

Commit

Permalink
lightning: fix lightning TLS.WithHost to access the correct host (pin…
Browse files Browse the repository at this point in the history
  • Loading branch information
ti-chi-bot committed Oct 18, 2023
1 parent 7a83625 commit e5004d8
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
2 changes: 1 addition & 1 deletion br/pkg/lightning/common/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ go_test(
],
embed = [":common"],
flaky = True,
shard_count = 19,
shard_count = 20,
deps = [
"//br/pkg/errors",
"//br/pkg/lightning/log",
Expand Down
8 changes: 8 additions & 0 deletions br/pkg/lightning/common/security.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"net"
"net/http"
"net/http/httptest"
"strings"

"github.com/pingcap/errors"
"github.com/pingcap/tidb/br/pkg/httputil"
Expand Down Expand Up @@ -88,8 +89,15 @@ func NewTLSFromMockServer(server *httptest.Server) *TLS {
}
}

// GetMockTLSUrl returns tls's host for mock test
func GetMockTLSUrl(tls *TLS) string {
return tls.url
}

// WithHost creates a new TLS instance with the host replaced.
func (tc *TLS) WithHost(host string) *TLS {
host = strings.TrimPrefix(host, "http://")
host = strings.TrimPrefix(host, "https://")
var url string
if tc.inner != nil {
url = "https://" + host
Expand Down
43 changes: 43 additions & 0 deletions br/pkg/lightning/common/security_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,49 @@ func TestGetJSONSecure(t *testing.T) {
require.Equal(t, "/dddd", result.Path)
}

func TestWithHost(t *testing.T) {
mockTLSServer := httptest.NewTLSServer(http.HandlerFunc(respondPathHandler))
defer mockTLSServer.Close()
mockServer := httptest.NewServer(http.HandlerFunc(respondPathHandler))
defer mockServer.Close()

testCases := []struct {
expected string
host string
secure bool
}{
{
"https://127.0.0.1:2379",
"http://127.0.0.1:2379",
true,
},
{
"http://127.0.0.1:2379",
"https://127.0.0.1:2379",
false,
},
{
"http://127.0.0.1:2379/pd/api/v1/stores",
"127.0.0.1:2379/pd/api/v1/stores",
false,
},
{
"https://127.0.0.1:2379",
"127.0.0.1:2379",
true,
},
}

for _, testCase := range testCases {
server := mockServer
if testCase.secure {
server = mockTLSServer
}
tls := common.NewTLSFromMockServer(server)
require.Equal(t, testCase.expected, common.GetMockTLSUrl(tls.WithHost(testCase.host)))
}
}

func TestInvalidTLS(t *testing.T) {
tempDir := t.TempDir()
caPath := filepath.Join(tempDir, "ca.pem")
Expand Down
1 change: 1 addition & 0 deletions br/tests/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
set -eu
export PATH="tests/_utils:bin:$PATH"
export TEST_DIR=/tmp/backup_restore_test
export COV_DIR="/tmp/group_cover"

# Create COV_DIR if not exists
if [ -d "$COV_DIR" ]; then
Expand Down

0 comments on commit e5004d8

Please sign in to comment.