conf_util_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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: "5000",
  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": "5000",
  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{"application_conf", "test"},
  91. val: "ssl",
  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. },
  99. },
  100. "application_conf": map[interface{}]interface{} {
  101. "test": "ssl",
  102. },
  103. },
  104. },
  105. }
  106. for i, tt := range tests {
  107. Handle("edgex", tt.config, tt.skeys, tt.val)
  108. if !reflect.DeepEqual(tt.exp, tt.config) {
  109. t.Errorf("%d \tresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.exp, tt.config)
  110. }
  111. }
  112. }
  113. func TestProcessEnv(t *testing.T) {
  114. fileMap["edgex"] = "test/edgex.yaml"
  115. var tests = []struct {
  116. vars []string
  117. file string
  118. expt map[interface{}]interface{}
  119. }{
  120. {
  121. vars: []string{
  122. "EDGEX__DEFAULT__TYPE=zmq",
  123. "EDGEX__DEFAULT__OPTIONAL__CLIENTID=clientid_0000",
  124. "EDGEX__APPLICATION_CONF__PROTOCOL=ssl",
  125. },
  126. file: "edgex",
  127. expt: map[interface{}]interface{}{
  128. "default": map[interface {}]interface{} {
  129. "protocol": "tcp",
  130. "type": "zmq",
  131. "optional": map[interface{}] interface{} {
  132. "ClientId": "clientid_0000",
  133. },
  134. },
  135. "application_conf": map[interface{}]interface{} {
  136. "protocol":"ssl",
  137. },
  138. },
  139. },
  140. }
  141. files := make(map[string]map[interface{}]interface{})
  142. for i, tt := range tests {
  143. ProcessEnv(files, tt.vars)
  144. if !reflect.DeepEqual(tt.expt, files[tt.file]) {
  145. t.Errorf("%d \tresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.expt, files[tt.file])
  146. }
  147. }
  148. }