plugin_test.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Copyright 2021 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 plugin
  15. import (
  16. "encoding/json"
  17. "fmt"
  18. "reflect"
  19. "testing"
  20. )
  21. func TestDecode(t *testing.T) {
  22. tests := []struct {
  23. t PluginType
  24. j string
  25. name string
  26. file string
  27. shellParas []string
  28. symbols []string
  29. }{
  30. {
  31. t: PORTABLE,
  32. j: "{\"name\":\"mirror\",\"file\":\"mirror_win.zip\"}",
  33. name: "mirror",
  34. file: "mirror_win.zip",
  35. }, {
  36. t: SINK,
  37. j: `{"name":"tdengine","file":"https://packages.emqx.io/kuiper-plugins/1.3.1/debian/sinks/tdengine_amd64.zip","shellParas": ["2.0.3.1"]}`,
  38. name: "tdengine",
  39. file: "https://packages.emqx.io/kuiper-plugins/1.3.1/debian/sinks/tdengine_amd64.zip",
  40. shellParas: []string{"2.0.3.1"},
  41. }, {
  42. t: FUNCTION,
  43. j: `{"name":"image","file":"https://packages.emqx.io/kuiper-plugins/1.3.1/debian/functions/image_amd64.zip","functions": ["resize","thumbnail"]}`,
  44. name: "image",
  45. file: "https://packages.emqx.io/kuiper-plugins/1.3.1/debian/functions/image_amd64.zip",
  46. symbols: []string{"resize", "thumbnail"},
  47. }, {
  48. t: FUNCTION,
  49. j: "{\"name1\":\"image\",\"file1\":\"abc\"}",
  50. },
  51. }
  52. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  53. for i, tt := range tests {
  54. p := NewPluginByType(tt.t)
  55. err := json.Unmarshal([]byte(tt.j), p)
  56. if err != nil {
  57. t.Error(err)
  58. return
  59. }
  60. if tt.name != p.GetName() {
  61. t.Errorf("%d name mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.name, p.GetName())
  62. }
  63. if tt.file != p.GetFile() {
  64. t.Errorf("%d file mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.file, p.GetFile())
  65. }
  66. if !reflect.DeepEqual(tt.shellParas, p.GetShellParas()) {
  67. t.Errorf("%d shellParas mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.shellParas, p.GetShellParas())
  68. }
  69. if !reflect.DeepEqual(tt.symbols, p.GetSymbols()) {
  70. t.Errorf("%d symbols mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.symbols, p.GetSymbols())
  71. }
  72. }
  73. }