load_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // INTECH Process Automation 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 conf
  15. import (
  16. "os"
  17. "reflect"
  18. "testing"
  19. )
  20. func TestEnv(t *testing.T) {
  21. key := "KUIPER__BASIC__CONSOLELOG"
  22. value := "true"
  23. err := os.Setenv(key, value)
  24. if err != nil {
  25. t.Error(err)
  26. }
  27. c := KuiperConf{}
  28. err = LoadConfig(&c)
  29. if err != nil {
  30. t.Error(err)
  31. }
  32. if c.Basic.ConsoleLog != true {
  33. t.Errorf("env variable should set it to true")
  34. }
  35. }
  36. func TestJsonCamelCase(t *testing.T) {
  37. key := "HTTPPULL__DEFAULT__BODYTYPE"
  38. value := "event"
  39. err := os.Setenv(key, value)
  40. if err != nil {
  41. t.Error(err)
  42. }
  43. const ConfigName = "sources/httppull.yaml"
  44. c := make(map[string]interface{})
  45. err = LoadConfigByName(ConfigName, &c)
  46. if err != nil {
  47. t.Error(err)
  48. }
  49. if casted, success := c["default"].(map[string]interface{}); success {
  50. if casted["bodyType"] != "event" {
  51. t.Errorf("env variable should set it to event")
  52. }
  53. } else {
  54. t.Errorf("returned value does not contains map under 'Basic' key")
  55. }
  56. }
  57. func TestKeysReplacement(t *testing.T) {
  58. input := createRandomConfigMap()
  59. expected := createExpectedRandomConfigMap()
  60. list := []string{"interval", "Seed", "deduplicate", "pattern"}
  61. applyKeys(input, list)
  62. if !reflect.DeepEqual(input, expected) {
  63. t.Errorf("key names within list should be applied \nexpected - %s\n input - %s", expected, input)
  64. }
  65. }
  66. func TestKeyReplacement(t *testing.T) {
  67. m := createRandomConfigMap()
  68. expected := createExpectedRandomConfigMap()
  69. applyKey(m, "Seed")
  70. applyKey(m, "interval")
  71. if !reflect.DeepEqual(m, expected) {
  72. t.Errorf("key names within list should be applied \nexpected - %s\nmap - %s", expected, m)
  73. }
  74. }
  75. func createRandomConfigMap() map[string]interface{} {
  76. pattern := make(map[string]interface{})
  77. pattern["count"] = 50
  78. defaultM := make(map[string]interface{})
  79. defaultM["interval"] = 1000
  80. defaultM["seed"] = 1
  81. defaultM["pattern"] = pattern
  82. defaultM["deduplicate"] = 0
  83. ext := make(map[string]interface{})
  84. ext["interval"] = 100
  85. dedup := make(map[string]interface{})
  86. dedup["interval"] = 100
  87. dedup["deduplicated"] = 50
  88. input := make(map[string]interface{})
  89. input["default"] = defaultM
  90. input["ext"] = ext
  91. input["dedup"] = dedup
  92. return input
  93. }
  94. func createExpectedRandomConfigMap() map[string]interface{} {
  95. input := createRandomConfigMap()
  96. def := input["default"]
  97. if defMap, ok := def.(map[string]interface{}); ok {
  98. tmp := defMap["seed"]
  99. delete(defMap, "seed")
  100. defMap["Seed"] = tmp
  101. }
  102. return input
  103. }