rest_test.go 22 KB

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