connect_selector_test.go 3.5 KB

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