rest_test.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. // Copyright 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. package server
  15. import (
  16. "bytes"
  17. "encoding/json"
  18. "fmt"
  19. "io"
  20. "net/http"
  21. "net/http/httptest"
  22. "os"
  23. "path/filepath"
  24. "testing"
  25. "github.com/gorilla/mux"
  26. "github.com/stretchr/testify/assert"
  27. "github.com/stretchr/testify/suite"
  28. "github.com/lf-edge/ekuiper/internal/conf"
  29. "github.com/lf-edge/ekuiper/internal/pkg/model"
  30. "github.com/lf-edge/ekuiper/internal/pkg/store"
  31. "github.com/lf-edge/ekuiper/internal/processor"
  32. "github.com/lf-edge/ekuiper/internal/testx"
  33. "github.com/lf-edge/ekuiper/internal/topo/rule"
  34. )
  35. func init() {
  36. testx.InitEnv()
  37. streamProcessor = processor.NewStreamProcessor()
  38. ruleProcessor = processor.NewRuleProcessor()
  39. rulesetProcessor = processor.NewRulesetProcessor(ruleProcessor, streamProcessor)
  40. registry = &RuleRegistry{internal: make(map[string]*rule.RuleState)}
  41. uploadsDb, _ = store.GetKV("uploads")
  42. uploadsStatusDb, _ = store.GetKV("uploadsStatusDb")
  43. }
  44. type RestTestSuite struct {
  45. suite.Suite
  46. r *mux.Router
  47. }
  48. func (suite *RestTestSuite) SetupTest() {
  49. dataDir, err := conf.GetDataLoc()
  50. if err != nil {
  51. panic(err)
  52. }
  53. uploadDir = filepath.Join(dataDir, "uploads")
  54. r := mux.NewRouter()
  55. r.HandleFunc("/", rootHandler).Methods(http.MethodGet, http.MethodPost)
  56. r.HandleFunc("/ping", pingHandler).Methods(http.MethodGet)
  57. r.HandleFunc("/streams", streamsHandler).Methods(http.MethodGet, http.MethodPost)
  58. r.HandleFunc("/streams/{name}", streamHandler).Methods(http.MethodGet, http.MethodDelete, http.MethodPut)
  59. r.HandleFunc("/streams/{name}/schema", streamSchemaHandler).Methods(http.MethodGet)
  60. r.HandleFunc("/tables", tablesHandler).Methods(http.MethodGet, http.MethodPost)
  61. r.HandleFunc("/tables/{name}", tableHandler).Methods(http.MethodGet, http.MethodDelete, http.MethodPut)
  62. r.HandleFunc("/tables/{name}/schema", tableSchemaHandler).Methods(http.MethodGet)
  63. r.HandleFunc("/rules", rulesHandler).Methods(http.MethodGet, http.MethodPost)
  64. r.HandleFunc("/rules/{name}", ruleHandler).Methods(http.MethodDelete, http.MethodGet, http.MethodPut)
  65. r.HandleFunc("/rules/{name}/status", getStatusRuleHandler).Methods(http.MethodGet)
  66. r.HandleFunc("/rules/{name}/start", startRuleHandler).Methods(http.MethodPost)
  67. r.HandleFunc("/rules/{name}/stop", stopRuleHandler).Methods(http.MethodPost)
  68. r.HandleFunc("/rules/{name}/restart", restartRuleHandler).Methods(http.MethodPost)
  69. r.HandleFunc("/rules/{name}/topo", getTopoRuleHandler).Methods(http.MethodGet)
  70. r.HandleFunc("/ruleset/export", exportHandler).Methods(http.MethodPost)
  71. r.HandleFunc("/ruleset/import", importHandler).Methods(http.MethodPost)
  72. r.HandleFunc("/configs", configurationUpdateHandler).Methods(http.MethodPatch)
  73. r.HandleFunc("/config/uploads", fileUploadHandler).Methods(http.MethodPost, http.MethodGet)
  74. r.HandleFunc("/config/uploads/{name}", fileDeleteHandler).Methods(http.MethodDelete)
  75. r.HandleFunc("/data/export", configurationExportHandler).Methods(http.MethodGet, http.MethodPost)
  76. r.HandleFunc("/data/import", configurationImportHandler).Methods(http.MethodPost)
  77. r.HandleFunc("/data/import/status", configurationStatusHandler).Methods(http.MethodGet)
  78. suite.r = r
  79. }
  80. func (suite *RestTestSuite) Test_rootHandler() {
  81. req, _ := http.NewRequest(http.MethodPost, "/", bytes.NewBufferString("any"))
  82. w := httptest.NewRecorder()
  83. suite.r.ServeHTTP(w, req)
  84. assert.Equal(suite.T(), http.StatusOK, w.Code)
  85. }
  86. func (suite *RestTestSuite) Test_sourcesManageHandler() {
  87. req, _ := http.NewRequest(http.MethodGet, "/", bytes.NewBufferString("any"))
  88. w := httptest.NewRecorder()
  89. suite.r.ServeHTTP(w, req)
  90. assert.Equal(suite.T(), http.StatusOK, w.Code)
  91. // get scan table
  92. req, _ = http.NewRequest(http.MethodGet, "http://localhost:8080/streams?kind=scan", bytes.NewBufferString("any"))
  93. w = httptest.NewRecorder()
  94. suite.r.ServeHTTP(w, req)
  95. assert.Equal(suite.T(), http.StatusOK, w.Code)
  96. // get lookup table
  97. req, _ = http.NewRequest(http.MethodGet, "http://localhost:8080/streams?kind=lookup", bytes.NewBufferString("any"))
  98. w = httptest.NewRecorder()
  99. suite.r.ServeHTTP(w, req)
  100. assert.Equal(suite.T(), http.StatusOK, w.Code)
  101. // create table
  102. buf := bytes.NewBuffer([]byte(` {"sql":"CREATE TABLE alertTable() WITH (DATASOURCE=\"0\", TYPE=\"memory\", KEY=\"id\", KIND=\"lookup\")"}`))
  103. req, _ = http.NewRequest(http.MethodPost, "http://localhost:8080/streams?kind=lookup", buf)
  104. w = httptest.NewRecorder()
  105. suite.r.ServeHTTP(w, req)
  106. assert.Equal(suite.T(), http.StatusCreated, w.Code)
  107. var returnVal []byte
  108. returnVal, _ = io.ReadAll(w.Result().Body)
  109. fmt.Printf("returnVal %s\n", string(returnVal))
  110. // create stream
  111. buf = bytes.NewBuffer([]byte(`{"sql":"CREATE stream alert() WITH (DATASOURCE=\"0\", TYPE=\"mqtt\")"}`))
  112. req, _ = http.NewRequest(http.MethodPost, "http://localhost:8080/streams", buf)
  113. w = httptest.NewRecorder()
  114. suite.r.ServeHTTP(w, req)
  115. assert.Equal(suite.T(), http.StatusCreated, w.Code)
  116. returnVal, _ = io.ReadAll(w.Result().Body)
  117. fmt.Printf("returnVal %s\n", string(returnVal))
  118. // get stream
  119. req, _ = http.NewRequest(http.MethodGet, "http://localhost:8080/streams/alert", bytes.NewBufferString("any"))
  120. w = httptest.NewRecorder()
  121. suite.r.ServeHTTP(w, req)
  122. assert.Equal(suite.T(), http.StatusOK, w.Code)
  123. expect := []byte(`{"Name":"alert","Options":{"datasource":"0","type":"mqtt"},"Statement":null,"StreamFields":null,"StreamType":0}`)
  124. exp := map[string]interface{}{}
  125. _ = json.NewDecoder(bytes.NewBuffer(expect)).Decode(&exp)
  126. res := map[string]interface{}{}
  127. _ = json.NewDecoder(w.Result().Body).Decode(&res)
  128. assert.Equal(suite.T(), exp, res)
  129. req, _ = http.NewRequest(http.MethodGet, "http://localhost:8080/streams/alert/schema", bytes.NewBufferString("any"))
  130. w = httptest.NewRecorder()
  131. suite.r.ServeHTTP(w, req)
  132. assert.Equal(suite.T(), http.StatusOK, w.Code)
  133. // get table
  134. req, _ = http.NewRequest(http.MethodGet, "http://localhost:8080/tables/alertTable", bytes.NewBufferString("any"))
  135. w = httptest.NewRecorder()
  136. suite.r.ServeHTTP(w, req)
  137. assert.Equal(suite.T(), http.StatusOK, w.Code)
  138. expect = []byte(`{"Name":"alertTable","Options":{"datasource":"0","type":"memory", "key":"id","kind":"lookup"},"Statement":null,"StreamFields":null,"StreamType":1}`)
  139. exp = map[string]interface{}{}
  140. _ = json.NewDecoder(bytes.NewBuffer(expect)).Decode(&exp)
  141. res = map[string]interface{}{}
  142. _ = json.NewDecoder(w.Result().Body).Decode(&res)
  143. assert.Equal(suite.T(), exp, res)
  144. req, _ = http.NewRequest(http.MethodGet, "http://localhost:8080/tables/alertTable/schema", bytes.NewBufferString("any"))
  145. w = httptest.NewRecorder()
  146. suite.r.ServeHTTP(w, req)
  147. assert.Equal(suite.T(), http.StatusOK, w.Code)
  148. // put table
  149. buf = bytes.NewBuffer([]byte(` {"sql":"CREATE TABLE alertTable() WITH (DATASOURCE=\"0\", TYPE=\"memory\", KEY=\"id\", KIND=\"lookup\")"}`))
  150. req, _ = http.NewRequest(http.MethodPut, "http://localhost:8080/tables/alertTable", buf)
  151. w = httptest.NewRecorder()
  152. suite.r.ServeHTTP(w, req)
  153. assert.Equal(suite.T(), http.StatusOK, w.Code)
  154. // put stream
  155. buf = bytes.NewBuffer([]byte(`{"sql":"CREATE stream alert() WITH (DATASOURCE=\"0\", TYPE=\"httppull\")"}`))
  156. req, _ = http.NewRequest(http.MethodPut, "http://localhost:8080/streams/alert", buf)
  157. w = httptest.NewRecorder()
  158. suite.r.ServeHTTP(w, req)
  159. assert.Equal(suite.T(), http.StatusOK, w.Code)
  160. // drop table
  161. req, _ = http.NewRequest(http.MethodDelete, "http://localhost:8080/tables/alertTable", bytes.NewBufferString("any"))
  162. w = httptest.NewRecorder()
  163. suite.r.ServeHTTP(w, req)
  164. assert.Equal(suite.T(), http.StatusOK, w.Code)
  165. // drop stream
  166. req, _ = http.NewRequest(http.MethodDelete, "http://localhost:8080/streams/alert", bytes.NewBufferString("any"))
  167. w = httptest.NewRecorder()
  168. suite.r.ServeHTTP(w, req)
  169. assert.Equal(suite.T(), http.StatusOK, w.Code)
  170. }
  171. func (suite *RestTestSuite) Test_rulesManageHandler() {
  172. // Start rules
  173. if rules, err := ruleProcessor.GetAllRules(); err != nil {
  174. logger.Infof("Start rules error: %s", err)
  175. } else {
  176. logger.Info("Starting rules")
  177. var reply string
  178. for _, name := range rules {
  179. rule, err := ruleProcessor.GetRuleById(name)
  180. if err != nil {
  181. logger.Error(err)
  182. continue
  183. }
  184. // err = server.StartRule(rule, &reply)
  185. reply = recoverRule(rule)
  186. if 0 != len(reply) {
  187. logger.Info(reply)
  188. }
  189. }
  190. }
  191. buf1 := bytes.NewBuffer([]byte(`{"sql":"CREATE stream alert() WITH (DATASOURCE=\"0\", TYPE=\"mqtt\")"}`))
  192. req1, _ := http.NewRequest(http.MethodPost, "http://localhost:8080/streams", buf1)
  193. w1 := httptest.NewRecorder()
  194. suite.r.ServeHTTP(w1, req1)
  195. // create rule with trigger false
  196. ruleJson := `{"id": "rule1","triggered": false,"sql": "select * from alert","actions": [{"log": {}}]}`
  197. buf2 := bytes.NewBuffer([]byte(ruleJson))
  198. req2, _ := http.NewRequest(http.MethodPost, "http://localhost:8080/rules", buf2)
  199. w2 := httptest.NewRecorder()
  200. suite.r.ServeHTTP(w2, req2)
  201. // get all rules
  202. req3, _ := http.NewRequest(http.MethodGet, "http://localhost:8080/rules", bytes.NewBufferString("any"))
  203. w3 := httptest.NewRecorder()
  204. suite.r.ServeHTTP(w3, req3)
  205. _, _ = io.ReadAll(w3.Result().Body)
  206. // update rule, will set rule to triggered
  207. ruleJson = `{"id": "rule1","triggered": false,"sql": "select * from alert","actions": [{"nop": {}}]}`
  208. buf2 = bytes.NewBuffer([]byte(ruleJson))
  209. req1, _ = http.NewRequest(http.MethodPut, "http://localhost:8080/rules/rule1", buf2)
  210. w1 = httptest.NewRecorder()
  211. suite.r.ServeHTTP(w1, req1)
  212. assert.Equal(suite.T(), http.StatusOK, w1.Code)
  213. // update wron rule
  214. ruleJson = `{"id": "rule1","sql": "select * from alert1","actions": [{"nop": {}}]}`
  215. buf2 = bytes.NewBuffer([]byte(ruleJson))
  216. req1, _ = http.NewRequest(http.MethodPut, "http://localhost:8080/rules/rule1", buf2)
  217. w1 = httptest.NewRecorder()
  218. suite.r.ServeHTTP(w1, req1)
  219. assert.Equal(suite.T(), http.StatusBadRequest, w1.Code)
  220. // get rule
  221. req1, _ = http.NewRequest(http.MethodGet, "http://localhost:8080/rules/rule1", bytes.NewBufferString("any"))
  222. w1 = httptest.NewRecorder()
  223. suite.r.ServeHTTP(w1, req1)
  224. returnVal, _ := io.ReadAll(w1.Result().Body)
  225. expect := `{"id": "rule1","triggered": false,"sql": "select * from alert","actions": [{"nop": {}}]}`
  226. assert.Equal(suite.T(), expect, string(returnVal))
  227. // get rule status
  228. req1, _ = http.NewRequest(http.MethodGet, "http://localhost:8080/rules/rule1/status", bytes.NewBufferString("any"))
  229. w1 = httptest.NewRecorder()
  230. suite.r.ServeHTTP(w1, req1)
  231. returnVal, _ = io.ReadAll(w1.Result().Body) //nolint
  232. // get rule topo
  233. req1, _ = http.NewRequest(http.MethodGet, "http://localhost:8080/rules/rule1/topo", bytes.NewBufferString("any"))
  234. w1 = httptest.NewRecorder()
  235. suite.r.ServeHTTP(w1, req1)
  236. returnVal, _ = io.ReadAll(w1.Result().Body)
  237. expect = `{"sources":["source_alert"],"edges":{"op_2_project":["sink_nop_0"],"source_alert":["op_2_project"]}}`
  238. assert.Equal(suite.T(), expect, string(returnVal))
  239. // start rule
  240. req1, _ = http.NewRequest(http.MethodPost, "http://localhost:8080/rules/rule1/start", bytes.NewBufferString("any"))
  241. w1 = httptest.NewRecorder()
  242. suite.r.ServeHTTP(w1, req1)
  243. returnVal, _ = io.ReadAll(w1.Result().Body)
  244. expect = `Rule rule1 was started`
  245. assert.Equal(suite.T(), expect, string(returnVal))
  246. // stop rule
  247. req1, _ = http.NewRequest(http.MethodPost, "http://localhost:8080/rules/rule1/stop", bytes.NewBufferString("any"))
  248. w1 = httptest.NewRecorder()
  249. suite.r.ServeHTTP(w1, req1)
  250. returnVal, _ = io.ReadAll(w1.Result().Body)
  251. expect = `Rule rule1 was stopped.`
  252. assert.Equal(suite.T(), expect, string(returnVal))
  253. // restart rule
  254. req1, _ = http.NewRequest(http.MethodPost, "http://localhost:8080/rules/rule1/restart", bytes.NewBufferString("any"))
  255. w1 = httptest.NewRecorder()
  256. suite.r.ServeHTTP(w1, req1)
  257. returnVal, _ = io.ReadAll(w1.Result().Body)
  258. expect = `Rule rule1 was restarted`
  259. assert.Equal(suite.T(), expect, string(returnVal))
  260. // delete rule
  261. req1, _ = http.NewRequest(http.MethodDelete, "http://localhost:8080/rules/rule1", bytes.NewBufferString("any"))
  262. w1 = httptest.NewRecorder()
  263. suite.r.ServeHTTP(w1, req1)
  264. // drop stream
  265. req, _ := http.NewRequest(http.MethodDelete, "http://localhost:8080/streams/alert", bytes.NewBufferString("any"))
  266. w := httptest.NewRecorder()
  267. suite.r.ServeHTTP(w, req)
  268. }
  269. func (suite *RestTestSuite) Test_configUpdate() {
  270. req, _ := http.NewRequest(http.MethodPatch, "http://localhost:8080/configs", bytes.NewBufferString(""))
  271. w := httptest.NewRecorder()
  272. suite.r.ServeHTTP(w, req)
  273. assert.Equal(suite.T(), http.StatusBadRequest, w.Code)
  274. b, _ := json.Marshal(map[string]any{
  275. "debug": true,
  276. "timezone": "",
  277. })
  278. req, _ = http.NewRequest(http.MethodPatch, "http://localhost:8080/configs", bytes.NewBuffer(b))
  279. w = httptest.NewRecorder()
  280. suite.r.ServeHTTP(w, req)
  281. assert.Equal(suite.T(), http.StatusNoContent, w.Code)
  282. b, _ = json.Marshal(map[string]any{
  283. "debug": true,
  284. "timezone": "unknown",
  285. })
  286. req, _ = http.NewRequest(http.MethodPatch, "http://localhost:8080/configs", bytes.NewBuffer(b))
  287. w = httptest.NewRecorder()
  288. suite.r.ServeHTTP(w, req)
  289. assert.Equal(suite.T(), http.StatusBadRequest, w.Code)
  290. b, _ = json.Marshal(map[string]any{
  291. "debug": true,
  292. "fileLog": true,
  293. })
  294. req, _ = http.NewRequest(http.MethodPatch, "http://localhost:8080/configs", bytes.NewBuffer(b))
  295. w = httptest.NewRecorder()
  296. suite.r.ServeHTTP(w, req)
  297. assert.Equal(suite.T(), http.StatusNoContent, w.Code)
  298. b, _ = json.Marshal(map[string]any{
  299. "debug": true,
  300. "consoleLog": true,
  301. })
  302. req, _ = http.NewRequest(http.MethodPatch, "http://localhost:8080/configs", bytes.NewBuffer(b))
  303. w = httptest.NewRecorder()
  304. suite.r.ServeHTTP(w, req)
  305. assert.Equal(suite.T(), http.StatusNoContent, w.Code)
  306. }
  307. func (suite *RestTestSuite) Test_ruleSetImport() {
  308. ruleJson := `{"streams":{"plugin":"\n CREATE STREAM plugin\n ()\n WITH (FORMAT=\"json\", CONF_KEY=\"default\", TYPE=\"mqtt\", SHARED=\"false\", );\n "},"tables":{},"rules":{"rule1":"{\"id\":\"rule1\",\"name\":\"\",\"sql\":\"select name from plugin\",\"actions\":[{\"log\":{\"runAsync\":false,\"omitIfEmpty\":false,\"sendSingle\":true,\"bufferLength\":1024,\"enableCache\":false,\"format\":\"json\"}}],\"options\":{\"restartStrategy\":{}}}"}}`
  309. ruleSetJson := map[string]string{
  310. "content": ruleJson,
  311. }
  312. buf, _ := json.Marshal(ruleSetJson)
  313. buf2 := bytes.NewBuffer(buf)
  314. req1, _ := http.NewRequest(http.MethodPost, "http://localhost:8080/ruleset/import", buf2)
  315. w1 := httptest.NewRecorder()
  316. suite.r.ServeHTTP(w1, req1)
  317. assert.Equal(suite.T(), http.StatusOK, w1.Code)
  318. req1, _ = http.NewRequest(http.MethodPost, "http://localhost:8080/ruleset/export", bytes.NewBufferString("any"))
  319. w1 = httptest.NewRecorder()
  320. suite.r.ServeHTTP(w1, req1)
  321. assert.Equal(suite.T(), http.StatusOK, w1.Code)
  322. }
  323. func (suite *RestTestSuite) Test_dataImport() {
  324. file := "rpc_test_data/data/import_configuration.json"
  325. f, err := os.Open(file)
  326. if err != nil {
  327. fmt.Printf("fail to open file %s: %v", file, err)
  328. return
  329. }
  330. defer f.Close()
  331. buffer := new(bytes.Buffer)
  332. _, err = io.Copy(buffer, f)
  333. if err != nil {
  334. fmt.Printf("fail to convert file %s: %v", file, err)
  335. return
  336. }
  337. content := buffer.Bytes()
  338. ruleSetJson := map[string]string{
  339. "content": string(content),
  340. }
  341. buf, _ := json.Marshal(ruleSetJson)
  342. buf2 := bytes.NewBuffer(buf)
  343. req, _ := http.NewRequest(http.MethodPost, "http://localhost:8080/data/import", buf2)
  344. w := httptest.NewRecorder()
  345. suite.r.ServeHTTP(w, req)
  346. assert.Equal(suite.T(), http.StatusOK, w.Code)
  347. req, _ = http.NewRequest(http.MethodGet, "http://localhost:8080/data/import/status", bytes.NewBufferString("any"))
  348. w = httptest.NewRecorder()
  349. suite.r.ServeHTTP(w, req)
  350. suite.r.ServeHTTP(w, req)
  351. assert.Equal(suite.T(), http.StatusOK, w.Code)
  352. req, _ = http.NewRequest(http.MethodGet, "http://localhost:8080/data/export", bytes.NewBufferString("any"))
  353. w = httptest.NewRecorder()
  354. suite.r.ServeHTTP(w, req)
  355. assert.Equal(suite.T(), http.StatusOK, w.Code)
  356. req, _ = http.NewRequest(http.MethodPost, "http://localhost:8080/data/import?partial=1", bytes.NewBuffer(buf))
  357. w = httptest.NewRecorder()
  358. suite.r.ServeHTTP(w, req)
  359. assert.Equal(suite.T(), http.StatusOK, w.Code)
  360. }
  361. func (suite *RestTestSuite) Test_fileUpload() {
  362. fileJson := `{"Name": "test.txt", "Content": "test"}`
  363. req, _ := http.NewRequest(http.MethodPost, "http://localhost:8080/config/uploads", bytes.NewBufferString(fileJson))
  364. req.Header["Content-Type"] = []string{"application/json"}
  365. os.Mkdir(uploadDir, 0o777)
  366. w := httptest.NewRecorder()
  367. suite.r.ServeHTTP(w, req)
  368. assert.Equal(suite.T(), http.StatusCreated, w.Code)
  369. postContent := map[string]string{}
  370. postContent["Name"] = "test1.txt"
  371. postContent["file"] = "file://" + uploadDir + "/test.txt"
  372. bdy, _ := json.Marshal(postContent)
  373. req, _ = http.NewRequest(http.MethodPost, "http://localhost:8080/config/uploads", bytes.NewBuffer(bdy))
  374. req.Header["Content-Type"] = []string{"application/json"}
  375. w = httptest.NewRecorder()
  376. suite.r.ServeHTTP(w, req)
  377. assert.Equal(suite.T(), http.StatusCreated, w.Code)
  378. req, _ = http.NewRequest(http.MethodGet, "http://localhost:8080/config/uploads", bytes.NewBufferString("any"))
  379. w = httptest.NewRecorder()
  380. suite.r.ServeHTTP(w, req)
  381. assert.Equal(suite.T(), http.StatusOK, w.Code)
  382. req, _ = http.NewRequest(http.MethodDelete, "http://localhost:8080/config/uploads/test.txt", bytes.NewBufferString("any"))
  383. w = httptest.NewRecorder()
  384. suite.r.ServeHTTP(w, req)
  385. assert.Equal(suite.T(), http.StatusOK, w.Code)
  386. req, _ = http.NewRequest(http.MethodDelete, "http://localhost:8080/config/uploads/test1.txt", bytes.NewBufferString("any"))
  387. w = httptest.NewRecorder()
  388. suite.r.ServeHTTP(w, req)
  389. assert.Equal(suite.T(), http.StatusOK, w.Code)
  390. os.Remove(uploadDir)
  391. }
  392. func (suite *RestTestSuite) Test_fileUploadValidate() {
  393. fileJson := `{"Name": "test.txt", "Content": test}`
  394. req, _ := http.NewRequest(http.MethodPost, "http://localhost:8080/config/uploads", bytes.NewBufferString(fileJson))
  395. req.Header["Content-Type"] = []string{"application/json"}
  396. os.Mkdir(uploadDir, 0o777)
  397. w := httptest.NewRecorder()
  398. suite.r.ServeHTTP(w, req)
  399. assert.Equal(suite.T(), http.StatusBadRequest, w.Code)
  400. fileJson = `{"Name": "test.txt", "Contents": "test"}`
  401. req, _ = http.NewRequest(http.MethodPost, "http://localhost:8080/config/uploads", bytes.NewBufferString(fileJson))
  402. req.Header["Content-Type"] = []string{"application/json"}
  403. os.Mkdir(uploadDir, 0o777)
  404. w = httptest.NewRecorder()
  405. suite.r.ServeHTTP(w, req)
  406. assert.Equal(suite.T(), http.StatusBadRequest, w.Code)
  407. fileJson = `{"Content": "test"}`
  408. req, _ = http.NewRequest(http.MethodPost, "http://localhost:8080/config/uploads", bytes.NewBufferString(fileJson))
  409. req.Header["Content-Type"] = []string{"application/json"}
  410. os.Mkdir(uploadDir, 0o777)
  411. w = httptest.NewRecorder()
  412. suite.r.ServeHTTP(w, req)
  413. assert.Equal(suite.T(), http.StatusBadRequest, w.Code)
  414. postContent := map[string]string{}
  415. postContent["Name"] = "test1.txt"
  416. postContent["file"] = "file://" + uploadDir + "/test.txt"
  417. bdy, _ := json.Marshal(postContent)
  418. req, _ = http.NewRequest(http.MethodPost, "http://localhost:8080/config/uploads", bytes.NewBuffer(bdy))
  419. req.Header["Content-Type"] = []string{"application/json"}
  420. w = httptest.NewRecorder()
  421. suite.r.ServeHTTP(w, req)
  422. assert.Equal(suite.T(), http.StatusBadRequest, w.Code)
  423. os.Remove(uploadDir)
  424. }
  425. func TestRestTestSuite(t *testing.T) {
  426. suite.Run(t, new(RestTestSuite))
  427. }
  428. func (suite *ServerTestSuite) TestStartRuleAfterSchemaChange() {
  429. sql := `Create Stream test (a bigint) WITH (DATASOURCE="../internal/server/rpc_test_data/test.json", FORMAT="JSON", type="file");`
  430. var reply string
  431. err := suite.s.Stream(sql, &reply)
  432. assert.Nil(suite.T(), err)
  433. assert.Equal(suite.T(), "Stream test is created.\n", reply)
  434. reply = ""
  435. rule := `{
  436. "sql": "SELECT a from test;",
  437. "actions": [{
  438. "file": {
  439. "path": "../internal/server/rpc_test_data/data/result.txt",
  440. "interval": 5000,
  441. "fileType": "lines",
  442. "format": "json"
  443. }
  444. }]
  445. }`
  446. ruleId := "myRule"
  447. args := &model.RPCArgDesc{Name: ruleId, Json: rule}
  448. err = suite.s.CreateRule(args, &reply)
  449. assert.Nil(suite.T(), err)
  450. assert.Equal(suite.T(), "Rule myRule was created successfully, please use 'bin/kuiper getstatus rule myRule' command to get rule status.", reply)
  451. reply = ""
  452. err = suite.s.GetStatusRule(ruleId, &reply)
  453. assert.Nil(suite.T(), err)
  454. reply = ""
  455. err = suite.s.StopRule(ruleId, &reply)
  456. assert.Nil(suite.T(), err)
  457. assert.Equal(suite.T(), "Rule myRule was stopped.", reply)
  458. reply = ""
  459. sql = `drop stream test`
  460. err = suite.s.Stream(sql, &reply)
  461. assert.Nil(suite.T(), err)
  462. assert.Equal(suite.T(), "Stream test is dropped.\n", reply)
  463. reply = ""
  464. sql = `Create Stream test (b bigint) WITH (DATASOURCE="../internal/server/rpc_test_data/test.json", FORMAT="JSON", type="file");`
  465. err = suite.s.Stream(sql, &reply)
  466. assert.Nil(suite.T(), err)
  467. assert.Equal(suite.T(), "Stream test is created.\n", reply)
  468. reply = ""
  469. err = suite.s.StartRule(ruleId, &reply)
  470. assert.Error(suite.T(), err)
  471. assert.Equal(suite.T(), err.Error(), "unknown field a")
  472. }