Repository: zeromicro/go-zero
Pull request #2812 (2023-01-23 15:02:43)

chore: add more tests

codecov-commenter: # [Codecov](https://codecov.io/gh/zeromicro/go-zero/pull/2812?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) Report
> Merging [#2812](https://codecov.io/gh/zeromicro/go-zero/pull/2812?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) (be506b7) into [master](https://codecov.io/gh/zeromicro/go-zero/commit/f3369f8e816d7bdeef970069b409ca3a017818bf?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) (f3369f8) will **increase** coverage by `0.09%`.
> The diff coverage is `n/a`.


| [Impacted Files](https://codecov.io/gh/zeromicro/go-zero/pull/2812?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) | Coverage Δ | |
|---|---|---|
| [zrpc/resolver/internal/kube/targetparser.go](https://codecov.io/gh/zeromicro/go-zero/pull/2812?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-enJwYy9yZXNvbHZlci9pbnRlcm5hbC9rdWJlL3RhcmdldHBhcnNlci5nbw==) | `86.36% <ø> (ø)` | |
| [core/stores/sqlx/sqlconn.go](https://codecov.io/gh/zeromicro/go-zero/pull/2812?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-Y29yZS9zdG9yZXMvc3FseC9zcWxjb25uLmdv) | `56.37% <0.00%> (-1.48%)` | :arrow_down: |
| [core/logx/rotatelogger.go](https://codecov.io/gh/zeromicro/go-zero/pull/2812?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-Y29yZS9sb2d4L3JvdGF0ZWxvZ2dlci5nbw==) | `72.32% <0.00%> (-0.37%)` | :arrow_down: |
| [core/stores/sqlx/tx.go](https://codecov.io/gh/zeromicro/go-zero/pull/2812?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-Y29yZS9zdG9yZXMvc3FseC90eC5nbw==) | `23.30% <0.00%> (+3.88%)` | :arrow_up: |
| [zrpc/resolver/internal/kubebuilder.go](https://codecov.io/gh/zeromicro/go-zero/pull/2812?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-enJwYy9yZXNvbHZlci9pbnRlcm5hbC9rdWJlYnVpbGRlci5nbw==) | `22.00% <0.00%> (+6.00%)` | :arrow_up: |
| [zrpc/config.go](https://codecov.io/gh/zeromicro/go-zero/pull/2812?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-enJwYy9jb25maWcuZ28=) | `100.00% <0.00%> (+19.04%)` | :arrow_up: |
| [zrpc/resolver/internal/resolver.go](https://codecov.io/gh/zeromicro/go-zero/pull/2812?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-enJwYy9yZXNvbHZlci9pbnRlcm5hbC9yZXNvbHZlci5nbw==) | `100.00% <0.00%> (+71.42%)` | :arrow_up: |


Patch:
diff --git a/zrpc/resolver/internal/kube/targetparser.go b/zrpc/resolver/internal/kube/targetparser.go
index d74cfba3..811cae2e 100644
--- a/zrpc/resolver/internal/kube/targetparser.go
+++ b/zrpc/resolver/internal/kube/targetparser.go
@@ -48,5 +48,6 @@ func ParseTarget(target resolver.Target) (Service, error) {
 	} else {
 		service.Name = endpoints
 	}
+
 	return service, nil
 }


Test patch:
diff --git a/zrpc/config_test.go b/zrpc/config_test.go
index 02a3a10d..0f7a637e 100644
--- a/zrpc/config_test.go
+++ b/zrpc/config_test.go
@@ -10,10 +10,35 @@ import (
 )
 
 func TestRpcClientConf(t *testing.T) {
-	conf := NewDirectClientConf([]string{"localhost:1234"}, "foo", "bar")
-	assert.True(t, conf.HasCredential())
-	conf = NewEtcdClientConf([]string{"localhost:1234", "localhost:5678"}, "key", "foo", "bar")
-	assert.True(t, conf.HasCredential())
+	t.Run("direct", func(t *testing.T) {
+		conf := NewDirectClientConf([]string{"localhost:1234"}, "foo", "bar")
+		assert.True(t, conf.HasCredential())
+	})
+
+	t.Run("etcd", func(t *testing.T) {
+		conf := NewEtcdClientConf([]string{"localhost:1234", "localhost:5678"},
+			"key", "foo", "bar")
+		assert.True(t, conf.HasCredential())
+	})
+
+	t.Run("etcd with account", func(t *testing.T) {
+		conf := NewEtcdClientConf([]string{"localhost:1234", "localhost:5678"},
+			"key", "foo", "bar")
+		conf.Etcd.User = "user"
+		conf.Etcd.Pass = "pass"
+		_, err := conf.BuildTarget()
+		assert.NoError(t, err)
+	})
+
+	t.Run("etcd with tls", func(t *testing.T) {
+		conf := NewEtcdClientConf([]string{"localhost:1234", "localhost:5678"},
+			"key", "foo", "bar")
+		conf.Etcd.CertFile = "cert"
+		conf.Etcd.CertKeyFile = "key"
+		conf.Etcd.CACertFile = "ca"
+		_, err := conf.BuildTarget()
+		assert.Error(t, err)
+	})
 }
 
 func TestRpcServerConf(t *testing.T) {
diff --git a/zrpc/resolver/internal/kubebuilder_test.go b/zrpc/resolver/internal/kubebuilder_test.go
index 38a663d9..bc9750dd 100644
--- a/zrpc/resolver/internal/kubebuilder_test.go
+++ b/zrpc/resolver/internal/kubebuilder_test.go
@@ -18,6 +18,15 @@ func TestKubeBuilder_Build(t *testing.T) {
 	var b kubeBuilder
 	u, err := url.Parse(fmt.Sprintf("%s://%s", KubernetesScheme, "a,b"))
 	assert.NoError(t, err)
+
+	_, err = b.Build(resolver.Target{
+		URL: *u,
+	}, nil, resolver.BuildOptions{})
+	assert.Error(t, err)
+
+	u, err = url.Parse(fmt.Sprintf("%s://%s:9100/a:b:c", KubernetesScheme, "a,b,c,d"))
+	assert.NoError(t, err)
+
 	_, err = b.Build(resolver.Target{
 		URL: *u,
 	}, nil, resolver.BuildOptions{})
diff --git a/zrpc/resolver/internal/resolver_test.go b/zrpc/resolver/internal/resolver_test.go
index 2f4a4690..62c82387 100644
--- a/zrpc/resolver/internal/resolver_test.go
+++ b/zrpc/resolver/internal/resolver_test.go
@@ -3,15 +3,19 @@ package internal
 import (
 	"testing"
 
+	"github.com/stretchr/testify/assert"
 	"google.golang.org/grpc/resolver"
 	"google.golang.org/grpc/serviceconfig"
 )
 
 func TestNopResolver(t *testing.T) {
-	// make sure ResolveNow & Close don't panic
-	var r nopResolver
-	r.ResolveNow(resolver.ResolveNowOptions{})
-	r.Close()
+	assert.NotPanics(t, func() {
+		RegisterResolver()
+		// make sure ResolveNow & Close don't panic
+		var r nopResolver
+		r.ResolveNow(resolver.ResolveNowOptions{})
+		r.Close()
+	})
 }
 
 type mockedClientConn struct {