Skip to content

Commit 9a6e80e

Browse files
committed
Add cmd/skytest, successed direct test
1 parent bd1ce74 commit 9a6e80e

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

internal/skylb/skylb_builder.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,19 @@ type skylbResolver struct {
2121
// Build
2222
func (b *skylbBuilder) Build(target resolver.Target, cc resolver.ClientConn, opts resolver.BuildOptions) (
2323
resolver.Resolver, error) {
24-
if target.Scheme != skyRs.SkyLBScheme {
24+
if target.URL.Scheme != skyRs.SkyLBScheme {
2525
return nil, ErrUnsupportSchema
2626
}
2727

28-
u, err := url.Parse(target.Endpoint)
28+
_, err := url.Parse(target.Endpoint)
2929
if err != nil {
3030
panic(ErrInvalidTarget)
3131
}
3232

33-
values := u.Query()
33+
url := target.URL
34+
values := url.Query()
3435
servSpec := &pb.ServiceSpec{
35-
ServiceName: u.Host,
36+
ServiceName: url.Host,
3637
Namespace: values.Get("ns"),
3738
PortName: values.Get("pn"),
3839
}

resolver/target_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@ package resolver
22

33
import (
44
"fmt"
5+
"net/url"
6+
"strings"
57
"testing"
68

79
pb "github.com/binchencoder/skylb-apiv2/proto"
810
"github.com/stretchr/testify/assert"
11+
"google.golang.org/grpc/resolver"
912
)
1013

1114
func TestDirectTarget(t *testing.T) {
@@ -23,3 +26,33 @@ func TestSkyLBTarget(t *testing.T) {
2326
fmt.Println(target)
2427
assert.Equal(t, "skylb://serviceName?ns=namespace&pn=portName", target)
2528
}
29+
30+
func TestParseTarget(t *testing.T) {
31+
target := SkyLBTarget(&pb.ServiceSpec{
32+
Namespace: "namespace",
33+
ServiceName: "serviceName",
34+
PortName: "portName",
35+
})
36+
37+
u, _ := url.Parse(target)
38+
// For targets of the form "[scheme]://[authority]/endpoint, the endpoint
39+
// value returned from url.Parse() contains a leading "/". Although this is
40+
// in accordance with RFC 3986, we do not want to break existing resolver
41+
// implementations which expect the endpoint without the leading "/". So, we
42+
// end up stripping the leading "/" here. But this will result in an
43+
// incorrect parsing for something like "unix:///path/to/socket". Since we
44+
// own the "unix" resolver, we can workaround in the unix resolver by using
45+
// the `URL` field instead of the `Endpoint` field.
46+
endpoint := u.Path
47+
if endpoint == "" {
48+
endpoint = u.Opaque
49+
}
50+
endpoint = strings.TrimPrefix(endpoint, "/")
51+
resTarget := resolver.Target{
52+
Scheme: u.Scheme,
53+
Authority: u.Host,
54+
Endpoint: endpoint,
55+
URL: *u,
56+
}
57+
fmt.Printf("target: %s, parsedTarget: %+v \n", target, resTarget)
58+
}

0 commit comments

Comments
 (0)