conf_util_test.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. package main
  2. import (
  3. "fmt"
  4. "reflect"
  5. "testing"
  6. )
  7. func TestHandle(t *testing.T) {
  8. var tests = []struct {
  9. config map[interface{}]interface{}
  10. skeys []string
  11. val string
  12. exp map[interface{}]interface{}
  13. }{
  14. {
  15. config: map[interface{}]interface{}{
  16. "default": map[interface{}]interface{}{
  17. "protocol": "tcp",
  18. "port": 5563,
  19. "optional": map[interface{}]interface{}{
  20. "ClientId": "client1",
  21. },
  22. },
  23. },
  24. skeys: []string{"default", "protocol"},
  25. val: "ssl",
  26. exp: map[interface{}]interface{}{
  27. "default": map[interface{}]interface{}{
  28. "protocol": "ssl",
  29. "port": 5563,
  30. "optional": map[interface{}]interface{}{
  31. "ClientId": "client1",
  32. },
  33. },
  34. },
  35. },
  36. {
  37. config: map[interface{}]interface{}{
  38. "default": map[interface{}]interface{}{
  39. "protocol": "tcp",
  40. "port": 5563,
  41. "optional": map[interface{}]interface{}{
  42. "ClientId": "client1",
  43. },
  44. },
  45. },
  46. skeys: []string{"default", "optional", "CLIENTID"},
  47. val: "client2",
  48. exp: map[interface{}]interface{}{
  49. "default": map[interface{}]interface{}{
  50. "protocol": "tcp",
  51. "port": 5563,
  52. "optional": map[interface{}]interface{}{
  53. "ClientId": "client2",
  54. },
  55. },
  56. },
  57. },
  58. {
  59. config: map[interface{}]interface{}{
  60. "default": map[interface{}]interface{}{
  61. "protocol": "tcp",
  62. "port": 5563,
  63. "optional": map[interface{}]interface{}{
  64. "ClientId": "client1",
  65. },
  66. },
  67. },
  68. skeys: []string{"default", "optional", "KEEPALIVE"},
  69. val: "6000",
  70. exp: map[interface{}]interface{}{
  71. "default": map[interface{}]interface{}{
  72. "protocol": "tcp",
  73. "port": 5563,
  74. "optional": map[interface{}]interface{}{
  75. "ClientId": "client1",
  76. "KeepAlive": int64(6000),
  77. },
  78. },
  79. },
  80. },
  81. {
  82. config: map[interface{}]interface{}{
  83. "default": map[interface{}]interface{}{
  84. "protocol": "tcp",
  85. "port": 5563,
  86. "optional": map[interface{}]interface{}{
  87. "ClientId": "client1",
  88. },
  89. },
  90. },
  91. skeys: []string{"default", "optional", "RETAINED"},
  92. val: "true",
  93. exp: map[interface{}]interface{}{
  94. "default": map[interface{}]interface{}{
  95. "protocol": "tcp",
  96. "port": 5563,
  97. "optional": map[interface{}]interface{}{
  98. "ClientId": "client1",
  99. "Retained": true,
  100. },
  101. },
  102. },
  103. },
  104. {
  105. config: map[interface{}]interface{}{
  106. "default": map[interface{}]interface{}{
  107. "protocol": "tcp",
  108. "port": 5563,
  109. "optional": map[interface{}]interface{}{
  110. "ClientId": "client1",
  111. },
  112. },
  113. },
  114. skeys: []string{"default", "optional", "test"},
  115. val: "3.14",
  116. exp: map[interface{}]interface{}{
  117. "default": map[interface{}]interface{}{
  118. "protocol": "tcp",
  119. "port": 5563,
  120. "optional": map[interface{}]interface{}{
  121. "ClientId": "client1",
  122. "test": 3.14,
  123. },
  124. },
  125. },
  126. },
  127. {
  128. config: map[interface{}]interface{}{
  129. "default": map[interface{}]interface{}{
  130. "protocol": "tcp",
  131. "port": 5563,
  132. "optional": map[interface{}]interface{}{
  133. "ClientId": "client1",
  134. },
  135. },
  136. },
  137. skeys: []string{"application_conf", "test"},
  138. val: "ssl",
  139. exp: map[interface{}]interface{}{
  140. "default": map[interface{}]interface{}{
  141. "protocol": "tcp",
  142. "port": 5563,
  143. "optional": map[interface{}]interface{}{
  144. "ClientId": "client1",
  145. },
  146. },
  147. "application_conf": map[interface{}]interface{}{
  148. "test": "ssl",
  149. },
  150. },
  151. },
  152. }
  153. for i, tt := range tests {
  154. Handle("edgex", tt.config, tt.skeys, tt.val)
  155. if !reflect.DeepEqual(tt.exp, tt.config) {
  156. t.Errorf("%d \tresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.exp, tt.config)
  157. }
  158. }
  159. }
  160. func TestProcessEnv(t *testing.T) {
  161. fileMap["edgex"] = "test/edgex.yaml"
  162. var tests = []struct {
  163. vars []string
  164. file string
  165. expt map[interface{}]interface{}
  166. out string
  167. }{
  168. {
  169. vars: []string{
  170. "EDGEX__DEFAULT__TYPE=zmq",
  171. "EDGEX__DEFAULT__OPTIONAL__CLIENTID=clientid_0000",
  172. "EDGEX__DEFAULT__OPTIONAL__PASSWORD=should_not_print",
  173. "EDGEX__APPLICATION_CONF__PROTOCOL=ssl",
  174. },
  175. file: "edgex",
  176. expt: map[interface{}]interface{}{
  177. "default": map[interface{}]interface{}{
  178. "protocol": "tcp",
  179. "type": "zmq",
  180. "optional": map[interface{}]interface{}{
  181. "ClientId": "clientid_0000",
  182. "Password": "should_not_print",
  183. },
  184. },
  185. "application_conf": map[interface{}]interface{}{
  186. "protocol": "ssl",
  187. },
  188. },
  189. out: "application_conf:\n protocol: ssl\ndefault:\n optional:\n ClientId: clientid_0000\n Password: '*'\n protocol: tcp\n type: zmq\n",
  190. },
  191. }
  192. files := make(map[string]map[interface{}]interface{})
  193. for i, tt := range tests {
  194. ProcessEnv(files, tt.vars)
  195. if !reflect.DeepEqual(tt.expt, files[tt.file]) {
  196. t.Errorf("%d \tresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.expt, files[tt.file])
  197. }
  198. for f, v := range files {
  199. p := toPrintableString(v)
  200. if !reflect.DeepEqual(tt.out, p) {
  201. t.Errorf("%d \tresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.out, p)
  202. }
  203. message := fmt.Sprintf("-------------------\nConf file %s: \n %s", f, p)
  204. fmt.Println(message)
  205. }
  206. }
  207. }
  208. func TestProcessEnvArrayValue(t *testing.T) {
  209. fileMap["mqtt_source"] = "test/mqtt_source.yaml"
  210. var tests = []struct {
  211. vars []string
  212. file string
  213. expt map[interface{}]interface{}
  214. }{
  215. {
  216. vars: []string{
  217. "MQTT_SOURCE__DEFAULT__SERVERS=[tcp://10.211.55.12:1883,tcp://10.211.55.13:1883]",
  218. "MQTT_SOURCE__DEFAULT__TEST=[1,2]",
  219. },
  220. file: "mqtt_source",
  221. expt: map[interface{}]interface{}{
  222. "default": map[interface{}]interface{}{
  223. "servers": []interface{}{"tcp://10.211.55.12:1883", "tcp://10.211.55.13:1883"},
  224. "test": []interface{}{int64(1), int64(2)},
  225. },
  226. },
  227. },
  228. }
  229. files := make(map[string]map[interface{}]interface{})
  230. for i, tt := range tests {
  231. ProcessEnv(files, tt.vars)
  232. if !reflect.DeepEqual(tt.expt, files[tt.file]) {
  233. t.Errorf("%d \tresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.expt, files[tt.file])
  234. }
  235. }
  236. }