config_test.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package zrpc
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. "github.com/wuntsong-org/go-zero-plus/core/discov"
  6. "github.com/wuntsong-org/go-zero-plus/core/service"
  7. "github.com/wuntsong-org/go-zero-plus/core/stores/redis"
  8. )
  9. func TestRpcClientConf(t *testing.T) {
  10. t.Run("direct", func(t *testing.T) {
  11. conf := NewDirectClientConf([]string{"localhost:1234"}, "foo", "bar")
  12. assert.True(t, conf.HasCredential())
  13. })
  14. t.Run("etcd", func(t *testing.T) {
  15. conf := NewEtcdClientConf([]string{"localhost:1234", "localhost:5678"},
  16. "key", "foo", "bar")
  17. assert.True(t, conf.HasCredential())
  18. })
  19. t.Run("etcd with account", func(t *testing.T) {
  20. conf := NewEtcdClientConf([]string{"localhost:1234", "localhost:5678"},
  21. "key", "foo", "bar")
  22. conf.Etcd.User = "user"
  23. conf.Etcd.Pass = "pass"
  24. _, err := conf.BuildTarget()
  25. assert.NoError(t, err)
  26. })
  27. t.Run("etcd with tls", func(t *testing.T) {
  28. conf := NewEtcdClientConf([]string{"localhost:1234", "localhost:5678"},
  29. "key", "foo", "bar")
  30. conf.Etcd.CertFile = "cert"
  31. conf.Etcd.CertKeyFile = "key"
  32. conf.Etcd.CACertFile = "ca"
  33. _, err := conf.BuildTarget()
  34. assert.Error(t, err)
  35. })
  36. }
  37. func TestRpcServerConf(t *testing.T) {
  38. conf := RpcServerConf{
  39. ServiceConf: service.ServiceConf{},
  40. ListenOn: "",
  41. Etcd: discov.EtcdConf{
  42. Hosts: []string{"localhost:1234"},
  43. Key: "key",
  44. },
  45. Auth: true,
  46. Redis: redis.RedisKeyConf{
  47. RedisConf: redis.RedisConf{
  48. Type: redis.NodeType,
  49. },
  50. Key: "foo",
  51. },
  52. StrictControl: false,
  53. Timeout: 0,
  54. CpuThreshold: 0,
  55. }
  56. assert.True(t, conf.HasEtcd())
  57. assert.NotNil(t, conf.Validate())
  58. conf.Redis.Host = "localhost:5678"
  59. assert.Nil(t, conf.Validate())
  60. }