conf_util_test.go 4.6 KB

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