plugin_init_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright 2022-2023 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. //go:build plugin || !core
  15. // +build plugin !core
  16. package server
  17. import (
  18. "bytes"
  19. "net/http"
  20. "net/http/httptest"
  21. "testing"
  22. "github.com/gorilla/mux"
  23. "github.com/stretchr/testify/assert"
  24. "github.com/stretchr/testify/suite"
  25. "github.com/lf-edge/ekuiper/internal/plugin"
  26. )
  27. type PluginTestSuite struct {
  28. suite.Suite
  29. m pluginComp
  30. r *mux.Router
  31. }
  32. func (suite *PluginTestSuite) SetupTest() {
  33. suite.m = pluginComp{}
  34. suite.r = mux.NewRouter()
  35. suite.m.rest(suite.r)
  36. suite.m.register()
  37. }
  38. func (suite *PluginTestSuite) Test_fetchPluginList() {
  39. version = "1.4.0"
  40. type args struct {
  41. t plugin.PluginType
  42. hosts string
  43. os string
  44. arch string
  45. }
  46. tests := []struct {
  47. name string
  48. args args
  49. wantErr error
  50. }{
  51. {
  52. "source",
  53. args{
  54. t: plugin.SOURCE,
  55. hosts: "http://127.0.0.1:8080",
  56. os: "debian",
  57. arch: "amd64",
  58. },
  59. nil,
  60. },
  61. {
  62. "sink",
  63. args{
  64. t: plugin.SINK,
  65. hosts: "http://127.0.0.1:8080",
  66. os: "debian",
  67. arch: "amd64",
  68. },
  69. nil,
  70. },
  71. {
  72. "function",
  73. args{
  74. t: plugin.FUNCTION,
  75. hosts: "http://127.0.0.1:8080",
  76. os: "debian",
  77. arch: "amd64",
  78. },
  79. nil,
  80. },
  81. }
  82. for _, tt := range tests {
  83. _, gotErr := fetchPluginList(tt.args.t, tt.args.hosts, tt.args.os, tt.args.arch)
  84. assert.Equal(suite.T(), tt.wantErr, gotErr)
  85. }
  86. }
  87. func (suite *PluginTestSuite) TestSourcesHandler() {
  88. req, _ := http.NewRequest(http.MethodGet, "/plugins/sources", bytes.NewBufferString("any"))
  89. w := httptest.NewRecorder()
  90. suite.r.ServeHTTP(w, req)
  91. assert.Equal(suite.T(), http.StatusOK, w.Code)
  92. }
  93. func (suite *PluginTestSuite) TestSinksHandler() {
  94. req, _ := http.NewRequest(http.MethodGet, "/plugins/sinks", bytes.NewBufferString("any"))
  95. w := httptest.NewRecorder()
  96. suite.r.ServeHTTP(w, req)
  97. assert.Equal(suite.T(), http.StatusOK, w.Code)
  98. }
  99. func (suite *PluginTestSuite) TestFunctionsHandler() {
  100. req, _ := http.NewRequest(http.MethodGet, "/plugins/functions", bytes.NewBufferString("any"))
  101. w := httptest.NewRecorder()
  102. suite.r.ServeHTTP(w, req)
  103. assert.Equal(suite.T(), http.StatusOK, w.Code)
  104. }
  105. func (suite *PluginTestSuite) TestUdfsHandler() {
  106. req, _ := http.NewRequest(http.MethodGet, "/plugins/udfs", bytes.NewBufferString("any"))
  107. w := httptest.NewRecorder()
  108. suite.r.ServeHTTP(w, req)
  109. assert.Equal(suite.T(), http.StatusOK, w.Code)
  110. }
  111. func TestPluginTestSuite(t *testing.T) {
  112. suite.Run(t, new(PluginTestSuite))
  113. }