connect_selector_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package connection
  2. import (
  3. "github.com/lf-edge/ekuiper/internal/conf"
  4. "os"
  5. "reflect"
  6. "testing"
  7. )
  8. func Test_getConnectionConf(t *testing.T) {
  9. type args struct {
  10. connectionSelector string
  11. }
  12. tests := []struct {
  13. name string
  14. args args
  15. want map[string]interface{}
  16. wantErr bool
  17. }{
  18. {
  19. name: "mqtt:localConnection",
  20. args: args{
  21. connectionSelector: "mqtt.localConnection",
  22. },
  23. want: map[string]interface{}{
  24. "servers": []interface{}{"tcp://127.0.0.1:1883"},
  25. "username": "ekuiper",
  26. "password": "password",
  27. "clientid": "ekuiper",
  28. },
  29. wantErr: false,
  30. },
  31. {
  32. name: "mqtt:cloudConnection",
  33. args: args{
  34. connectionSelector: "mqtt.cloudConnection",
  35. },
  36. want: map[string]interface{}{
  37. "servers": []interface{}{"tcp://broker.emqx.io:1883"},
  38. "username": "user1",
  39. "password": "password",
  40. },
  41. wantErr: false,
  42. },
  43. {
  44. name: "mqtt:mqtt_conf3 not exist",
  45. args: args{
  46. connectionSelector: "mqtt.mqtt_conf3",
  47. },
  48. wantErr: true,
  49. },
  50. {
  51. name: "edgex:redisMsgBus",
  52. args: args{
  53. connectionSelector: "edgex.redisMsgBus",
  54. },
  55. want: map[string]interface{}{
  56. "protocol": "redis",
  57. "server": "127.0.0.1",
  58. "port": 6379,
  59. "type": "redis",
  60. },
  61. wantErr: false,
  62. },
  63. }
  64. for _, tt := range tests {
  65. t.Run(tt.name, func(t *testing.T) {
  66. c := conf.ConSelector{
  67. ConnSelectorStr: tt.args.connectionSelector,
  68. }
  69. _ = c.Init()
  70. got, err := c.ReadCfgFromYaml()
  71. if (err != nil) != tt.wantErr {
  72. t.Errorf("getConnectionConf() error = %v, wantErr %v", err, tt.wantErr)
  73. return
  74. }
  75. if !reflect.DeepEqual(got, tt.want) {
  76. t.Errorf("getConnectionConf() got = %v, want %v", got, tt.want)
  77. }
  78. })
  79. }
  80. }
  81. func Test_getConnectionConfWithEnv(t *testing.T) {
  82. mqttServerKey := "CONNECTION__MQTT__LOCALCONNECTION__SERVERS"
  83. mqttServerValue := "[tcp://broker.emqx.io:1883]"
  84. edgexPortKey := "CONNECTION__EDGEX__REDISMSGBUS__PORT"
  85. edgexPortValue := "6666"
  86. err := os.Setenv(mqttServerKey, mqttServerValue)
  87. if err != nil {
  88. t.Error(err)
  89. }
  90. err = os.Setenv(edgexPortKey, edgexPortValue)
  91. if err != nil {
  92. t.Error(err)
  93. }
  94. type args struct {
  95. connectionSelector string
  96. }
  97. tests := []struct {
  98. name string
  99. args args
  100. want map[string]interface{}
  101. wantErr bool
  102. }{
  103. {
  104. name: "mqtt:localConnection",
  105. args: args{
  106. connectionSelector: "mqtt.localConnection",
  107. },
  108. want: map[string]interface{}{
  109. "servers": []interface{}{"tcp://broker.emqx.io:1883"},
  110. "username": "ekuiper",
  111. "password": "password",
  112. "clientid": "ekuiper",
  113. },
  114. wantErr: false,
  115. },
  116. {
  117. name: "edgex:redisMsgBus",
  118. args: args{
  119. connectionSelector: "edgex.redisMsgBus",
  120. },
  121. want: map[string]interface{}{
  122. "protocol": "redis",
  123. "server": "127.0.0.1",
  124. "port": int64(6666),
  125. "type": "redis",
  126. },
  127. wantErr: false,
  128. },
  129. }
  130. for _, tt := range tests {
  131. t.Run(tt.name, func(t *testing.T) {
  132. c := conf.ConSelector{
  133. ConnSelectorStr: tt.args.connectionSelector,
  134. }
  135. _ = c.Init()
  136. got, err := c.ReadCfgFromYaml()
  137. if (err != nil) != tt.wantErr {
  138. t.Errorf("getConnectionConf() error = %v, wantErr %v", err, tt.wantErr)
  139. return
  140. }
  141. if !reflect.DeepEqual(got, tt.want) {
  142. t.Errorf("getConnectionConf() got = %v, want %v", got, tt.want)
  143. }
  144. })
  145. }
  146. }