rest_test.go 20 KB

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