yamlConfigMeta_test.go 1.9 KB

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