manager_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright erfenjiao, 630166475@qq.com.
  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 wasm
  15. import (
  16. "fmt"
  17. "net/http"
  18. "net/http/httptest"
  19. "os"
  20. "path"
  21. "reflect"
  22. "testing"
  23. "github.com/lf-edge/ekuiper/internal/plugin"
  24. "github.com/lf-edge/ekuiper/internal/plugin/wasm/runtime"
  25. "github.com/lf-edge/ekuiper/internal/testx"
  26. )
  27. func init() {
  28. InitManager()
  29. }
  30. func TestManager_Install(t *testing.T) {
  31. s := httptest.NewServer(
  32. http.FileServer(http.Dir("../testzips")),
  33. )
  34. defer s.Close()
  35. endpoint := s.URL
  36. data := []struct {
  37. n string
  38. u string
  39. v string
  40. err error
  41. }{
  42. { // 1
  43. n: "fibonacci",
  44. u: endpoint + "/wasm/fibonacci.zip",
  45. },
  46. }
  47. fmt.Printf("The test bucket size is %d.\n\n", len(data))
  48. for i, tt := range data {
  49. p := &plugin.IOPlugin{
  50. Name: tt.n,
  51. File: tt.u,
  52. }
  53. err := manager.Register(p)
  54. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) { // not same
  55. fmt.Println("err: ", err)
  56. // t.Errorf("%d: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.err, err)
  57. } else { // same
  58. err := checkFileForMirror(manager.pluginDir, true)
  59. if err != nil {
  60. t.Errorf("%d: error : %s\n\n", i, err)
  61. }
  62. }
  63. }
  64. }
  65. func TestManager_Read(t *testing.T) {
  66. requiredFiles := []string{
  67. path.Join(manager.pluginDir, "fibonacci", "fibonacci.wasm"),
  68. path.Join(manager.pluginDir, "fibonacci", "fibonacci.json"),
  69. }
  70. expPlugins := []*PluginInfo{
  71. {
  72. PluginMeta: runtime.PluginMeta{
  73. Name: "fibonacci",
  74. Version: "v1.0.0",
  75. // WasmFile: "/home/erfenjiao/ekuiper/plugins/wasm/fibonacci/fibonacci.wasm",
  76. WasmFile: requiredFiles[0],
  77. WasmEngine: "wasmedge",
  78. },
  79. Functions: []string{"fib"},
  80. },
  81. }
  82. // fmt.Println("[TestManager_Read] List: ")
  83. // result := manager.List()
  84. // fmt.Println("[TestManager_Read] result: ", result)
  85. pi, ok := manager.GetPluginInfo("fibonacci")
  86. if !ok {
  87. t.Error("can't find plugin fibonacci")
  88. }
  89. fmt.Println("[TestManager_Read] pi: ", pi)
  90. fmt.Println("[TestManager_Read] expPlugins[0]: ", expPlugins[0])
  91. if !reflect.DeepEqual(expPlugins[0], pi) {
  92. t.Errorf("Get plugin fibonacci mismatch:\n exp=%v\n got=%v", expPlugins[0], pi)
  93. }
  94. }
  95. func TestDelete(t *testing.T) {
  96. err := manager.Delete("fibonacci")
  97. if err != nil {
  98. t.Errorf("delete plugin error: %v", err)
  99. }
  100. }
  101. func checkFileForMirror(pluginDir string, exist bool) error {
  102. requiredFiles := []string{
  103. path.Join(pluginDir, "fibonacci", "fibonacci.wasm"),
  104. path.Join(pluginDir, "fibonacci", "fibonacci.json"),
  105. }
  106. for _, file := range requiredFiles {
  107. _, err := os.Stat(file)
  108. if exist && err != nil {
  109. return err
  110. } else if !exist && err == nil {
  111. return fmt.Errorf("file still exists: %s", file)
  112. }
  113. }
  114. return nil
  115. }