yaml_config_ops_test.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. // Copyright 2022 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 conf
  15. import (
  16. "encoding/json"
  17. "fmt"
  18. "gopkg.in/yaml.v3"
  19. "os"
  20. "reflect"
  21. "sort"
  22. "testing"
  23. )
  24. func TestConfigKeys_LoadSourceFile(t *testing.T) {
  25. _, err := NewConfigOperatorFromSourceYaml("mqtt")
  26. if err != nil {
  27. t.Error(err)
  28. }
  29. }
  30. func TestConfigKeys_LoadConnectionMqtt(t *testing.T) {
  31. _, err := NewConfigOperatorFromConnectionYaml("mqtt")
  32. if err != nil {
  33. t.Error(err)
  34. }
  35. }
  36. func TestConfigKeys_LoadConnectionEdgex(t *testing.T) {
  37. _, err := NewConfigOperatorFromConnectionYaml("edgex")
  38. if err != nil {
  39. t.Error(err)
  40. }
  41. }
  42. func TestConfigKeys_Ops(t *testing.T) {
  43. httpCfg, err := NewConfigOperatorFromSourceYaml("httppull")
  44. if err != nil {
  45. t.Error(err)
  46. }
  47. addData := `{"url":"127.0.0.1","method":"post","headers":{"Accept":"json"}}`
  48. delData := `{"method":"","headers":{"Accept":""}}`
  49. reqField := make(map[string]interface{})
  50. _ = json.Unmarshal([]byte(addData), &reqField)
  51. err = httpCfg.AddConfKey("new", reqField)
  52. if err != nil {
  53. t.Error(err)
  54. }
  55. if err := isAddData(addData, httpCfg.CopyConfContent()[`new`]); nil != err {
  56. t.Error(err)
  57. }
  58. delField := make(map[string]interface{})
  59. _ = json.Unmarshal([]byte(delData), &delField)
  60. err = httpCfg.DeleteConfKeyField("new", delField)
  61. if err != nil {
  62. t.Error(err)
  63. }
  64. if err := isDelData(delData, httpCfg.CopyConfContent()[`new`]); nil != err {
  65. t.Error(err)
  66. }
  67. }
  68. func TestConfigKeys_GetPluginName(t *testing.T) {
  69. pluginName := "mqtt"
  70. mqttCfg, err := NewConfigOperatorFromSourceYaml(pluginName)
  71. if err != nil {
  72. t.Error(err)
  73. }
  74. if mqttCfg.GetPluginName() != pluginName {
  75. t.Errorf("GetPluginName() gotName = %v, wantName = %v", mqttCfg.GetPluginName(), pluginName)
  76. }
  77. }
  78. func TestConfigKeys_GetConfContentByte(t *testing.T) {
  79. mqttCfg, err := NewConfigOperatorFromSourceYaml("mqtt")
  80. if err != nil {
  81. t.Error(err)
  82. }
  83. _, err = mqttCfg.GetConfContentByte()
  84. if err != nil {
  85. t.Error(err)
  86. }
  87. }
  88. func TestConfigKeys_LoadConfContent(t *testing.T) {
  89. mqttCfg := NewConfigOperatorForSource("mqtt")
  90. cf := make(map[string]map[string]interface{})
  91. source := `{"test": {"qos": 1, "server": "tcp://127.0.0.1:1883"}}`
  92. _ = json.Unmarshal([]byte(source), &cf)
  93. mqttCfg.LoadConfContent(cf)
  94. if !reflect.DeepEqual(cf, mqttCfg.CopyUpdatableConfContent()) {
  95. t.Errorf("LoadConfContent() fail")
  96. }
  97. }
  98. func TestConfigKeys_CopyReadOnlyConfContent(t *testing.T) {
  99. mqttCfg, err := NewConfigOperatorFromSourceYaml("mqtt")
  100. if err != nil {
  101. t.Error(err)
  102. }
  103. cf := make(map[string]map[string]interface{})
  104. source := `{"default": {"qos": 1, "server": "tcp://127.0.0.1:1883"}, "demo_conf": {"qos": 0, "server": "tcp://10.211.55.6:1883"}}`
  105. _ = yaml.Unmarshal([]byte(source), &cf)
  106. if !reflect.DeepEqual(cf, mqttCfg.CopyReadOnlyConfContent()) {
  107. t.Errorf("CopyReadOnlyConfContent() fail")
  108. }
  109. }
  110. func TestConfigKeys_GetConfKeys(t *testing.T) {
  111. mqttCfg, err := NewConfigOperatorFromSourceYaml("mqtt")
  112. if err != nil {
  113. t.Error(err)
  114. }
  115. keys := mqttCfg.GetConfKeys()
  116. //currently only etcCfg, no dataCfg
  117. source := []string{"default", "demo_conf"}
  118. if keys == nil {
  119. t.Errorf("Not Equal")
  120. }
  121. if len(keys) != len(source) {
  122. t.Errorf("Length not equal, got %v, want %v", len(keys), len(source))
  123. }
  124. sort.Strings(keys)
  125. sort.Strings(source)
  126. for i, key := range keys {
  127. if key != source[i] {
  128. t.Errorf("Not equal, got %v, want %v", key, source[i])
  129. }
  130. }
  131. }
  132. func TestConfigKeys_GetReadOnlyConfKeys(t *testing.T) {
  133. mqttCfg, err := NewConfigOperatorFromSourceYaml("mqtt")
  134. if err != nil {
  135. t.Error(err)
  136. }
  137. keys := mqttCfg.GetReadOnlyConfKeys()
  138. source := []string{"default", "demo_conf"}
  139. if keys == nil {
  140. t.Errorf("Not Equal")
  141. }
  142. if len(keys) != len(source) {
  143. t.Errorf("Length not equal, got %v, want %v", len(keys), len(source))
  144. }
  145. sort.Strings(keys)
  146. sort.Strings(source)
  147. for i, key := range keys {
  148. if key != source[i] {
  149. t.Errorf("Not equal, got %v, want %v", key, source[i])
  150. }
  151. }
  152. }
  153. func TestConfigKeys_GetUpdatableConfKeys(t *testing.T) {
  154. mqttCfg := NewConfigOperatorForSource("mqtt")
  155. cf := make(map[string]map[string]interface{})
  156. source := `{"test": {"qos": 1, "server": "tcp://127.0.0.1:18883"}}`
  157. _ = json.Unmarshal([]byte(source), &cf)
  158. mqttCfg.LoadConfContent(cf)
  159. keys := mqttCfg.GetUpdatableConfKeys()
  160. srcKeys := []string{"test"}
  161. if keys == nil {
  162. t.Errorf("Not Equal")
  163. }
  164. if len(keys) != len(srcKeys) {
  165. t.Errorf("Length not equal, got %v, want %v", len(keys), len(srcKeys))
  166. }
  167. sort.Strings(keys)
  168. sort.Strings(srcKeys)
  169. for i, key := range keys {
  170. if key != srcKeys[i] {
  171. t.Errorf("Not equal, got %v, want %v", key, source[i])
  172. }
  173. }
  174. }
  175. func TestConfigKeys_DeleteConfKey(t *testing.T) {
  176. mqttCfg := NewConfigOperatorForSource("mqtt")
  177. cf := make(map[string]map[string]interface{})
  178. source := `{"test": {"qos": 1, "server": "tcp://127.0.0.1:18883"}}`
  179. _ = json.Unmarshal([]byte(source), &cf)
  180. mqttCfg.LoadConfContent(cf)
  181. mqttCfg.DeleteConfKey("test")
  182. err := isDelData(`{"qos": 1, "server": "tcp://127.0.0.1:18883"}`, mqttCfg.CopyUpdatableConfContent()["test"])
  183. if err != nil {
  184. t.Error(err)
  185. }
  186. }
  187. func TestConfigKeys_ClearConfKeys(t *testing.T) {
  188. mqttCfg := NewConfigOperatorForSource("mqtt")
  189. cf := make(map[string]map[string]interface{})
  190. source := `{"test": {"qos": 1, "server": "tcp://127.0.0.1:18883"}}`
  191. _ = json.Unmarshal([]byte(source), &cf)
  192. mqttCfg.LoadConfContent(cf)
  193. mqttCfg.ClearConfKeys()
  194. if len(mqttCfg.CopyUpdatableConfContent()) > 0 {
  195. t.Errorf("ClearConfKeys() fail")
  196. }
  197. }
  198. func TestConfigKeys_AddConfKeyField(t *testing.T) {
  199. mqttCfg := NewConfigOperatorForSource("mqtt")
  200. cf := make(map[string]map[string]interface{})
  201. source := `{"test": {"qos": 1, "server": "tcp://127.0.0.1:1883"}}`
  202. _ = json.Unmarshal([]byte(source), &cf)
  203. mqttCfg.LoadConfContent(cf)
  204. ck := make(map[string]interface{})
  205. source = `{"username": "user"}`
  206. _ = json.Unmarshal([]byte(source), &ck)
  207. confKey := "test"
  208. mqttCfg.AddConfKeyField(confKey, ck)
  209. err := isAddData(source, mqttCfg.CopyUpdatableConfContent()[confKey])
  210. if err != nil {
  211. t.Error(err)
  212. }
  213. }
  214. func TestSourceConfigKeysOps_SaveCfgToFile(t *testing.T) {
  215. mqttCfg, err := NewConfigOperatorFromSourceYaml("mqtt")
  216. if err != nil {
  217. t.Error(err)
  218. }
  219. err = os.MkdirAll("../../data/test/sources", os.ModePerm)
  220. if err != nil {
  221. t.Error(err)
  222. }
  223. _, err = os.Create("../../data/test/sources/mqtt.yaml")
  224. if err != nil {
  225. t.Error(err)
  226. }
  227. err = mqttCfg.SaveCfgToFile()
  228. if err != nil {
  229. t.Error(err)
  230. }
  231. os.RemoveAll("../../data/test/sources/mqtt.yaml")
  232. }
  233. func TestSinkConfigKeysOps_SaveCfgToFile(t *testing.T) {
  234. mqttCfg, err := NewConfigOperatorFromSinkYaml("mqtt")
  235. if err != nil {
  236. t.Error(err)
  237. }
  238. err = os.MkdirAll("../../data/test/sinks", os.ModePerm)
  239. if err != nil {
  240. t.Error(err)
  241. }
  242. _, err = os.Create("../../data/test/sinks/mqtt.yaml")
  243. if err != nil {
  244. t.Error(err)
  245. }
  246. err = mqttCfg.SaveCfgToFile()
  247. if err != nil {
  248. t.Error(err)
  249. }
  250. os.RemoveAll("../../data/test/sinks/mqtt.yaml")
  251. }
  252. func TestNewConfigOperatorForSink(t *testing.T) {
  253. sink := NewConfigOperatorForSink("mqtt")
  254. if sink.GetPluginName() != "mqtt" {
  255. t.Errorf("NewConfigOperatorForSink() fail")
  256. }
  257. }
  258. func TestNewConfigOperatorForConnection(t *testing.T) {
  259. connection := NewConfigOperatorForConnection("mqtt")
  260. if connection.GetPluginName() != "mqtt" {
  261. t.Errorf("NewConfigOperatorForSink() fail")
  262. }
  263. }
  264. func marshalUn(input, output interface{}) error {
  265. jsonString, err := json.Marshal(input)
  266. if err != nil {
  267. return err
  268. }
  269. return json.Unmarshal(jsonString, output)
  270. }
  271. func isDelData(js string, cf map[string]interface{}) error {
  272. var delNode map[string]interface{}
  273. if err := json.Unmarshal([]byte(js), &delNode); nil != err {
  274. return err
  275. }
  276. for delk, delv := range delNode {
  277. if nil == delv {
  278. if _, ok := cf[delk]; ok {
  279. return fmt.Errorf("%s still exists", delk)
  280. }
  281. }
  282. switch t := delv.(type) {
  283. case string:
  284. if 0 == len(t) {
  285. if _, ok := cf[delk]; ok {
  286. return fmt.Errorf("%s still exists", delk)
  287. }
  288. }
  289. case map[string]interface{}:
  290. if b, err := json.Marshal(t); nil != err {
  291. return fmt.Errorf("request format error")
  292. } else {
  293. var auxCf map[string]interface{}
  294. if err := marshalUn(cf[delk], &auxCf); nil == err {
  295. if err := isDelData(string(b), auxCf); nil != err {
  296. return err
  297. }
  298. }
  299. }
  300. }
  301. }
  302. return nil
  303. }
  304. func isAddData(js string, cf map[string]interface{}) error {
  305. var addNode map[string]interface{}
  306. if err := json.Unmarshal([]byte(js), &addNode); nil != err {
  307. return err
  308. }
  309. for addk := range addNode {
  310. if _, ok := cf[addk]; !ok {
  311. return fmt.Errorf("not found key:%s", addk)
  312. }
  313. }
  314. return nil
  315. }