plugin_init_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. package server
  16. import (
  17. "bytes"
  18. "net/http"
  19. "net/http/httptest"
  20. "testing"
  21. "github.com/gorilla/mux"
  22. "github.com/stretchr/testify/assert"
  23. "github.com/stretchr/testify/suite"
  24. "github.com/lf-edge/ekuiper/internal/plugin"
  25. )
  26. type PluginTestSuite struct {
  27. suite.Suite
  28. m pluginComp
  29. r *mux.Router
  30. }
  31. func (suite *PluginTestSuite) SetupTest() {
  32. suite.m = pluginComp{}
  33. suite.r = mux.NewRouter()
  34. suite.m.rest(suite.r)
  35. suite.m.register()
  36. }
  37. func (suite *PluginTestSuite) Test_fetchPluginList() {
  38. version = "1.4.0"
  39. type args struct {
  40. t plugin.PluginType
  41. hosts string
  42. os string
  43. arch string
  44. }
  45. tests := []struct {
  46. name string
  47. args args
  48. wantErr error
  49. }{
  50. {
  51. "source",
  52. args{
  53. t: plugin.SOURCE,
  54. hosts: "http://127.0.0.1:8080",
  55. os: "debian",
  56. arch: "amd64",
  57. },
  58. nil,
  59. },
  60. {
  61. "sink",
  62. args{
  63. t: plugin.SINK,
  64. hosts: "http://127.0.0.1:8080",
  65. os: "debian",
  66. arch: "amd64",
  67. },
  68. nil,
  69. },
  70. {
  71. "function",
  72. args{
  73. t: plugin.FUNCTION,
  74. hosts: "http://127.0.0.1:8080",
  75. os: "debian",
  76. arch: "amd64",
  77. },
  78. nil,
  79. },
  80. }
  81. for _, tt := range tests {
  82. _, gotErr := fetchPluginList(tt.args.t, tt.args.hosts, tt.args.os, tt.args.arch)
  83. assert.Equal(suite.T(), tt.wantErr, gotErr)
  84. }
  85. }
  86. func (suite *PluginTestSuite) TestSourcesHandler() {
  87. req, _ := http.NewRequest(http.MethodGet, "/plugins/sources", bytes.NewBufferString("any"))
  88. w := httptest.NewRecorder()
  89. suite.r.ServeHTTP(w, req)
  90. assert.Equal(suite.T(), http.StatusOK, w.Code)
  91. }
  92. func (suite *PluginTestSuite) TestSinksHandler() {
  93. req, _ := http.NewRequest(http.MethodGet, "/plugins/sinks", bytes.NewBufferString("any"))
  94. w := httptest.NewRecorder()
  95. suite.r.ServeHTTP(w, req)
  96. assert.Equal(suite.T(), http.StatusOK, w.Code)
  97. }
  98. func (suite *PluginTestSuite) TestFunctionsHandler() {
  99. req, _ := http.NewRequest(http.MethodGet, "/plugins/functions", bytes.NewBufferString("any"))
  100. w := httptest.NewRecorder()
  101. suite.r.ServeHTTP(w, req)
  102. assert.Equal(suite.T(), http.StatusOK, w.Code)
  103. }
  104. func (suite *PluginTestSuite) TestFunctionsUpdateHandler() {
  105. s := httptest.NewServer(
  106. http.FileServer(http.Dir("../plugin/testzips")),
  107. )
  108. defer s.Close()
  109. endpoint := s.URL
  110. // Test decoding problem
  111. req, _ := http.NewRequest(http.MethodPut, "/plugins/functions/echo2", bytes.NewBufferString("\"name\":\"echo2\", \"file\": \""+endpoint+"/functions/echo2.zip\"}"))
  112. w := httptest.NewRecorder()
  113. suite.r.ServeHTTP(w, req)
  114. assert.Equal(suite.T(), http.StatusBadRequest, w.Code)
  115. // Test plugin not exist problem
  116. req, _ = http.NewRequest(http.MethodPut, "/plugins/functions/echo2", bytes.NewBufferString("{\"name\":\"echo2\", \"file\": \""+endpoint+"/functions/echo2.zip\"}"))
  117. w = httptest.NewRecorder()
  118. suite.r.ServeHTTP(w, req)
  119. assert.Equal(suite.T(), http.StatusNotFound, w.Code)
  120. req, _ = http.NewRequest(http.MethodPost, "/plugins/functions", bytes.NewBufferString("{\"name\":\"echo2\", \"file\": \""+endpoint+"/functions/echo2.zip\"}"))
  121. w = httptest.NewRecorder()
  122. suite.r.ServeHTTP(w, req)
  123. assert.Equal(suite.T(), http.StatusCreated, w.Code)
  124. // Test plugin create problem
  125. req, _ = http.NewRequest(http.MethodPut, "/plugins/functions/echo2", bytes.NewBufferString("{\"name\":\"echo2\", \"file\": \""+endpoint+"/functions/echo22.zip\"}"))
  126. w = httptest.NewRecorder()
  127. suite.r.ServeHTTP(w, req)
  128. assert.Equal(suite.T(), http.StatusBadRequest, w.Code)
  129. req, _ = http.NewRequest(http.MethodPut, "/plugins/functions/echo2", bytes.NewBufferString("{\"name\":\"echo2\", \"file\": \""+endpoint+"/functions/echo2.zip\"}"))
  130. w = httptest.NewRecorder()
  131. suite.r.ServeHTTP(w, req)
  132. assert.Equal(suite.T(), http.StatusOK, w.Code)
  133. req, _ = http.NewRequest(http.MethodDelete, "/plugins/functions/echo2", bytes.NewBufferString(""))
  134. w = httptest.NewRecorder()
  135. suite.r.ServeHTTP(w, req)
  136. assert.Equal(suite.T(), http.StatusOK, w.Code)
  137. }
  138. func (suite *PluginTestSuite) TestUdfsHandler() {
  139. req, _ := http.NewRequest(http.MethodGet, "/plugins/udfs", bytes.NewBufferString("any"))
  140. w := httptest.NewRecorder()
  141. suite.r.ServeHTTP(w, req)
  142. assert.Equal(suite.T(), http.StatusOK, w.Code)
  143. }
  144. func TestPluginTestSuite(t *testing.T) {
  145. suite.Run(t, new(PluginTestSuite))
  146. }