Skip to content

Commit

Permalink
test(server): Decompose base URL to URI to helper and add test (#3839)
Browse files Browse the repository at this point in the history
Decompose the logic for parsing base URL into the URI for client dial.
Add a unit test for the new helper.
  • Loading branch information
redgoat650 committed May 2, 2024
1 parent 0fc8b1b commit 4cf9582
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 11 deletions.
31 changes: 20 additions & 11 deletions repo/grpc_repository_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -829,18 +829,9 @@ func openGRPCAPIRepository(ctx context.Context, si *APIServerInfo, password stri
transportCreds = credentials.NewClientTLSFromCert(nil, "")
}

u, err := url.Parse(si.BaseURL)
uri, err := baseURLToURI(si.BaseURL)
if err != nil {
return nil, errors.Wrap(err, "unable to parse server URL")
}

if u.Scheme != "kopia" && u.Scheme != "https" && u.Scheme != "unix+https" {
return nil, errors.Errorf("invalid server address, must be 'https://host:port' or 'unix+https://<path>")
}

uri := net.JoinHostPort(u.Hostname(), u.Port())
if u.Scheme == "unix+https" {
uri = "unix:" + u.Path
return nil, errors.Wrap(err, "parsing base URL")
}

conn, err := grpc.NewClient(
Expand Down Expand Up @@ -869,6 +860,24 @@ func openGRPCAPIRepository(ctx context.Context, si *APIServerInfo, password stri
return rep, nil
}

func baseURLToURI(baseURL string) (uri string, err error) {
u, err := url.Parse(baseURL)
if err != nil {
return "", errors.Wrap(err, "unable to parse server URL")
}

if u.Scheme != "kopia" && u.Scheme != "https" && u.Scheme != "unix+https" {
return "", errors.Errorf("invalid server address, must be 'https://host:port' or 'unix+https://<path>")
}

uri = net.JoinHostPort(u.Hostname(), u.Port())
if u.Scheme == "unix+https" {
uri = "unix:" + u.Path
}

return uri, nil
}

func (r *grpcRepositoryClient) getOrEstablishInnerSession(ctx context.Context) (*grpcInnerSession, error) {
r.innerSessionMutex.Lock()
defer r.innerSessionMutex.Unlock()
Expand Down
64 changes: 64 additions & 0 deletions repo/grpc_repository_client_unit_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package repo

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestBaseURLToURI(t *testing.T) {
for _, tc := range []struct {
name string
baseURL string
expURI string
expErrMsg string
}{
{
name: "ipv4",
baseURL: "https://1.2.3.4:5678",
expURI: "1.2.3.4:5678",
expErrMsg: "",
},
{
name: "ipv6",
baseURL: "https://[2600:1f14:253f:ef00:87b9::10]:51515",
expURI: "[2600:1f14:253f:ef00:87b9::10]:51515",
expErrMsg: "",
},
{
name: "unix https scheme",
baseURL: "unix+https:///tmp/kopia-test606141450/sock",
expURI: "unix:/tmp/kopia-test606141450/sock",
expErrMsg: "",
},
{
name: "kopia scheme",
baseURL: "kopia://a:0",
expURI: "a:0",
expErrMsg: "",
},
{
name: "unix http scheme is invalid",
baseURL: "unix+http:///tmp/kopia-test606141450/sock",
expURI: "",
expErrMsg: "invalid server address",
},
{
name: "invalid address",
baseURL: "a",
expURI: "",
expErrMsg: "invalid server address",
},
} {
t.Run(tc.name, func(t *testing.T) {
gotURI, err := baseURLToURI(tc.baseURL)
if tc.expErrMsg != "" {
require.ErrorContains(t, err, tc.expErrMsg)
return
}

require.NoError(t, err)
require.Equal(t, tc.expURI, gotURI)
})
}
}

0 comments on commit 4cf9582

Please sign in to comment.