connect_selector_test.go 3.2 KB

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