You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-zero/zrpc/resolver/internal/kube/targetparser_test.go

76 lines
1.3 KiB
Go

package kube
import (
"net/url"
"testing"
"github.com/stretchr/testify/assert"
"google.golang.org/grpc/resolver"
)
func TestParseTarget(t *testing.T) {
tests := []struct {
name string
input string
expect Service
hasErr bool
}{
{
name: "normal case",
input: "k8s://ns1/my-svc:8080",
expect: Service{
Namespace: "ns1",
Name: "my-svc",
Port: 8080,
},
},
{
name: "normal case",
input: "k8s:///my-svc:8080",
expect: Service{
Namespace: defaultNamespace,
Name: "my-svc",
Port: 8080,
},
},
{
name: "no port",
input: "k8s://ns1/my-svc:",
hasErr: true,
},
{
name: "no port, no colon",
input: "k8s://ns1/my-svc",
expect: Service{
Namespace: "ns1",
Name: "my-svc",
},
},
{
name: "bad port",
input: "k8s://ns1/my-svc:800a",
hasErr: true,
},
{
name: "bad endpoint",
input: "k8s://ns1:800/:",
hasErr: true,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
uri, err := url.Parse(test.input)
if assert.NoError(t, err) {
svc, err := ParseTarget(resolver.Target{URL: *uri})
if test.hasErr {
assert.NotNil(t, err)
} else {
assert.Nil(t, err)
assert.Equal(t, test.expect, svc)
}
}
})
}
}