conf_util_test.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. // Copyright 2021 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package main
  15. import (
  16. "fmt"
  17. "reflect"
  18. "testing"
  19. )
  20. func TestHandle(t *testing.T) {
  21. var tests = []struct {
  22. config map[string]interface{}
  23. skeys []string
  24. val string
  25. exp map[string]interface{}
  26. }{
  27. {
  28. config: map[string]interface{}{
  29. "default": map[string]interface{}{
  30. "protocol": "tcp",
  31. "port": 5563,
  32. "optional": map[string]interface{}{
  33. "ClientId": "client1",
  34. },
  35. },
  36. },
  37. skeys: []string{"default", "protocol"},
  38. val: "ssl",
  39. exp: map[string]interface{}{
  40. "default": map[string]interface{}{
  41. "protocol": "ssl",
  42. "port": 5563,
  43. "optional": map[string]interface{}{
  44. "ClientId": "client1",
  45. },
  46. },
  47. },
  48. },
  49. {
  50. config: map[string]interface{}{
  51. "default": map[string]interface{}{
  52. "protocol": "tcp",
  53. "port": 5563,
  54. "optional": map[string]interface{}{
  55. "ClientId": "client1",
  56. },
  57. },
  58. },
  59. skeys: []string{"default", "optional", "CLIENTID"},
  60. val: "client2",
  61. exp: map[string]interface{}{
  62. "default": map[string]interface{}{
  63. "protocol": "tcp",
  64. "port": 5563,
  65. "optional": map[string]interface{}{
  66. "ClientId": "client2",
  67. },
  68. },
  69. },
  70. },
  71. {
  72. config: map[string]interface{}{
  73. "default": map[string]interface{}{
  74. "protocol": "tcp",
  75. "port": 5563,
  76. "optional": map[string]interface{}{
  77. "ClientId": "client1",
  78. },
  79. },
  80. },
  81. skeys: []string{"default", "optional", "KEEPALIVE"},
  82. val: "6000",
  83. exp: map[string]interface{}{
  84. "default": map[string]interface{}{
  85. "protocol": "tcp",
  86. "port": 5563,
  87. "optional": map[string]interface{}{
  88. "ClientId": "client1",
  89. "KeepAlive": int64(6000),
  90. },
  91. },
  92. },
  93. },
  94. {
  95. config: map[string]interface{}{
  96. "default": map[string]interface{}{
  97. "protocol": "tcp",
  98. "port": 5563,
  99. "optional": map[string]interface{}{
  100. "ClientId": "client1",
  101. },
  102. },
  103. },
  104. skeys: []string{"default", "optional", "RETAINED"},
  105. val: "true",
  106. exp: map[string]interface{}{
  107. "default": map[string]interface{}{
  108. "protocol": "tcp",
  109. "port": 5563,
  110. "optional": map[string]interface{}{
  111. "ClientId": "client1",
  112. "Retained": true,
  113. },
  114. },
  115. },
  116. },
  117. {
  118. config: map[string]interface{}{
  119. "default": map[string]interface{}{
  120. "protocol": "tcp",
  121. "port": 5563,
  122. "optional": map[string]interface{}{
  123. "ClientId": "client1",
  124. },
  125. },
  126. },
  127. skeys: []string{"default", "optional", "test"},
  128. val: "3.14",
  129. exp: map[string]interface{}{
  130. "default": map[string]interface{}{
  131. "protocol": "tcp",
  132. "port": 5563,
  133. "optional": map[string]interface{}{
  134. "ClientId": "client1",
  135. "test": 3.14,
  136. },
  137. },
  138. },
  139. },
  140. {
  141. config: map[string]interface{}{
  142. "default": map[string]interface{}{
  143. "protocol": "tcp",
  144. "port": 5563,
  145. "optional": map[string]interface{}{
  146. "ClientId": "client1",
  147. },
  148. },
  149. },
  150. skeys: []string{"application_conf", "test"},
  151. val: "ssl",
  152. exp: map[string]interface{}{
  153. "default": map[string]interface{}{
  154. "protocol": "tcp",
  155. "port": 5563,
  156. "optional": map[string]interface{}{
  157. "ClientId": "client1",
  158. },
  159. },
  160. "application_conf": map[string]interface{}{
  161. "test": "ssl",
  162. },
  163. },
  164. },
  165. }
  166. for i, tt := range tests {
  167. Handle("edgex", tt.config, tt.skeys, tt.val)
  168. if !reflect.DeepEqual(tt.exp, tt.config) {
  169. t.Errorf("%d \tresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.exp, tt.config)
  170. }
  171. }
  172. }
  173. func TestProcessEnv(t *testing.T) {
  174. fileMap["edgex"] = "test/edgex.yaml"
  175. var tests = []struct {
  176. vars []string
  177. file string
  178. expt map[string]interface{}
  179. out string
  180. }{
  181. {
  182. vars: []string{
  183. "EDGEX__DEFAULT__TYPE=zmq",
  184. "EDGEX__DEFAULT__MESSAGETYPE=event",
  185. "EDGEX__DEFAULT__OPTIONAL__CLIENTID=clientid_0000",
  186. "EDGEX__DEFAULT__OPTIONAL__PASSWORD=should_not_print",
  187. "EDGEX__APPLICATION_CONF__PROTOCOL=ssl",
  188. },
  189. file: "edgex",
  190. expt: map[string]interface{}{
  191. "default": map[string]interface{}{
  192. "messageType": "event",
  193. "protocol": "tcp",
  194. "type": "zmq",
  195. "optional": map[string]interface{}{
  196. "ClientId": "clientid_0000",
  197. "Password": "should_not_print",
  198. },
  199. },
  200. "application_conf": map[string]interface{}{
  201. "protocol": "ssl",
  202. },
  203. },
  204. out: "application_conf:\n protocol: ssl\ndefault:\n messageType: event\n optional:\n ClientId: clientid_0000\n Password: '*'\n protocol: tcp\n type: zmq\n",
  205. },
  206. }
  207. files := make(map[string]map[string]interface{})
  208. for i, tt := range tests {
  209. ProcessEnv(files, tt.vars)
  210. if !reflect.DeepEqual(tt.expt, files[tt.file]) {
  211. t.Errorf("%d \tresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.expt, files[tt.file])
  212. }
  213. for f, v := range files {
  214. p := toPrintableString(v)
  215. if !reflect.DeepEqual(tt.out, p) {
  216. t.Errorf("%d \tresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.out, p)
  217. }
  218. message := fmt.Sprintf("-------------------\nConf file %s: \n %s", f, p)
  219. fmt.Println(message)
  220. }
  221. }
  222. }
  223. func TestProcessEnvArrayValue(t *testing.T) {
  224. fileMap["mqtt_source"] = "test/mqtt_source.yaml"
  225. var tests = []struct {
  226. vars []string
  227. file string
  228. expt map[string]interface{}
  229. }{
  230. {
  231. vars: []string{
  232. "MQTT_SOURCE__DEFAULT__SERVERS=[tcp://10.211.55.12:1883,tcp://10.211.55.13:1883]",
  233. "MQTT_SOURCE__DEFAULT__TEST=[1,2]",
  234. },
  235. file: "mqtt_source",
  236. expt: map[string]interface{}{
  237. "default": map[string]interface{}{
  238. "servers": []interface{}{"tcp://10.211.55.12:1883", "tcp://10.211.55.13:1883"},
  239. "test": []interface{}{int64(1), int64(2)},
  240. },
  241. },
  242. },
  243. }
  244. files := make(map[string]map[string]interface{})
  245. for i, tt := range tests {
  246. ProcessEnv(files, tt.vars)
  247. if !reflect.DeepEqual(tt.expt, files[tt.file]) {
  248. t.Errorf("%d \tresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.expt, files[tt.file])
  249. }
  250. }
  251. }