config_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package etcd
  2. import (
  3. "context"
  4. "reflect"
  5. "testing"
  6. "time"
  7. clientv3 "go.etcd.io/etcd/client/v3"
  8. "google.golang.org/grpc"
  9. )
  10. const testKey = "/kratos/test/config"
  11. func TestConfig(t *testing.T) {
  12. client, err := clientv3.New(clientv3.Config{
  13. Endpoints: []string{"127.0.0.1:2379"},
  14. DialTimeout: time.Second,
  15. DialOptions: []grpc.DialOption{grpc.WithBlock()},
  16. })
  17. if err != nil {
  18. t.Fatal(err)
  19. }
  20. defer func() {
  21. _ = client.Close()
  22. }()
  23. if _, err = client.Put(context.Background(), testKey, "test config"); err != nil {
  24. t.Fatal(err)
  25. }
  26. source, err := New(client, WithPath(testKey))
  27. if err != nil {
  28. t.Fatal(err)
  29. }
  30. kvs, err := source.Load()
  31. if err != nil {
  32. t.Fatal(err)
  33. }
  34. if len(kvs) != 1 || kvs[0].Key != testKey || string(kvs[0].Value) != "test config" {
  35. t.Fatal("config error")
  36. }
  37. w, err := source.Watch()
  38. if err != nil {
  39. t.Fatal(err)
  40. }
  41. defer func() {
  42. _ = w.Stop()
  43. }()
  44. if _, err = client.Put(context.Background(), testKey, "new config"); err != nil {
  45. t.Error(err)
  46. }
  47. if kvs, err = w.Next(); err != nil {
  48. t.Fatal(err)
  49. }
  50. if len(kvs) != 1 || kvs[0].Key != testKey || string(kvs[0].Value) != "new config" {
  51. t.Fatal("config error")
  52. }
  53. if _, err := client.Delete(context.Background(), testKey); err != nil {
  54. t.Error(err)
  55. }
  56. }
  57. func TestExtToFormat(t *testing.T) {
  58. client, err := clientv3.New(clientv3.Config{
  59. Endpoints: []string{"127.0.0.1:2379"},
  60. DialTimeout: time.Second, DialOptions: []grpc.DialOption{grpc.WithBlock()},
  61. })
  62. if err != nil {
  63. t.Fatal(err)
  64. }
  65. defer func() {
  66. _ = client.Close()
  67. }()
  68. tp := "/kratos/test/ext"
  69. tn := "a.bird.json"
  70. tk := tp + "/" + tn
  71. tc := `{"a":1}`
  72. if _, err = client.Put(context.Background(), tk, tc); err != nil {
  73. t.Fatal(err)
  74. }
  75. source, err := New(client, WithPath(tp), WithPrefix(true))
  76. if err != nil {
  77. t.Fatal(err)
  78. }
  79. kvs, err := source.Load()
  80. if err != nil {
  81. t.Fatal(err)
  82. }
  83. if !reflect.DeepEqual(len(kvs), 1) {
  84. t.Errorf("len(kvs) = %d", len(kvs))
  85. }
  86. if !reflect.DeepEqual(tk, kvs[0].Key) {
  87. t.Errorf("kvs[0].Key is %s", kvs[0].Key)
  88. }
  89. if !reflect.DeepEqual(tc, string(kvs[0].Value)) {
  90. t.Errorf("kvs[0].Value is %s", kvs[0].Value)
  91. }
  92. if !reflect.DeepEqual("json", kvs[0].Format) {
  93. t.Errorf("kvs[0].Format is %s", kvs[0].Format)
  94. }
  95. }
  96. func TestEtcdWithPath(t *testing.T) {
  97. tests := []struct {
  98. name string
  99. fields string
  100. want string
  101. }{
  102. {
  103. name: "default",
  104. fields: testKey,
  105. want: testKey,
  106. },
  107. }
  108. for _, tt := range tests {
  109. t.Run(tt.name, func(t *testing.T) {
  110. options := &options{
  111. ctx: context.Background(),
  112. }
  113. got := WithPath(tt.fields)
  114. got(options)
  115. if options.path != tt.want {
  116. t.Errorf("WithPath(tt.fields) = %v, want %v", got, tt.want)
  117. }
  118. })
  119. }
  120. }
  121. func TestEtcdWithPrefix(t *testing.T) {
  122. tests := []struct {
  123. name string
  124. fields bool
  125. want bool
  126. }{
  127. {
  128. name: "default",
  129. fields: false,
  130. want: false,
  131. },
  132. }
  133. for _, tt := range tests {
  134. t.Run(tt.name, func(t *testing.T) {
  135. options := &options{
  136. ctx: context.Background(),
  137. }
  138. got := WithPrefix(tt.fields)
  139. got(options)
  140. if options.prefix != tt.want {
  141. t.Errorf("WithPrefix(tt.fields) = %v, want %v", got, tt.want)
  142. }
  143. })
  144. }
  145. }