yamlConfigMeta_test.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 meta
  15. import (
  16. "github.com/lf-edge/ekuiper/internal/conf"
  17. "os"
  18. "path/filepath"
  19. "testing"
  20. )
  21. func createPaths() {
  22. dataDir, err := conf.GetDataLoc()
  23. if err != nil {
  24. panic(err)
  25. }
  26. dirs := []string{"sources", "sinks", "functions", "services", "services/schemas", "connections"}
  27. for _, v := range dirs {
  28. // Create dir if not exist
  29. realDir := filepath.Join(dataDir, v)
  30. if _, err := os.Stat(realDir); os.IsNotExist(err) {
  31. if err := os.MkdirAll(realDir, os.ModePerm); err != nil {
  32. panic(err)
  33. }
  34. }
  35. }
  36. files := []string{"connections/connection.yaml"}
  37. for _, v := range files {
  38. // Create dir if not exist
  39. realFile := filepath.Join(dataDir, v)
  40. if _, err := os.Stat(realFile); os.IsNotExist(err) {
  41. if _, err := os.Create(realFile); err != nil {
  42. panic(err)
  43. }
  44. }
  45. }
  46. }
  47. func TestYamlConfigMeta_Ops(t *testing.T) {
  48. createPaths()
  49. plgName := "mocksource"
  50. addData := `{"url":"127.0.0.1","method":"post","headers":{"Accept":"json"}}`
  51. // init new ConfigOperator, success
  52. err := AddSourceConfKey(plgName, "new", "en_US", []byte(addData))
  53. if err != nil {
  54. t.Error(err)
  55. }
  56. //Exist ConfigKey , fail
  57. err = AddSourceConfKey(plgName, "new", "en_US", []byte(addData))
  58. if err != nil {
  59. t.Error("should overwrite exist config key")
  60. }
  61. }