rest_test.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. // Copyright 2021 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. "fmt"
  17. "net/http"
  18. "net/http/httptest"
  19. "reflect"
  20. "strings"
  21. "testing"
  22. )
  23. func TestParseHtml(t1 *testing.T) {
  24. var tests = []struct {
  25. html string
  26. plugins []string
  27. arch string
  28. error string
  29. }{
  30. {
  31. html: `<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><html>
  32. <title>Directory listing for enterprise: /4.1.1/</title>
  33. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  34. <meta name="robots" content="noindex,nofollow">
  35. <body>
  36. <h2>Directory listing for enterprise: /4.1.1/</h2>
  37. <hr>
  38. <ul>
  39. <li><a href="file_386.zip">file_386.zip</a>
  40. <li><a href="file_amd64.zip">file_amd64.zip</a>
  41. <li><a href="file_arm.zip">file_arm.zip</a>
  42. <li><a href="file_arm64.zip">file_arm64.zip</a>
  43. <li><a href="file_ppc64le.zip">file_ppc64le.zip</a>
  44. <li><a href="influx_386.zip">influx_386.zip</a>
  45. <li><a href="influx_amd64.zip">influx_amd64.zip</a>
  46. <li><a href="influx_arm.zip">influx_arm.zip</a>
  47. <li><a href="influx_arm64.zip">influx_arm64.zip</a>
  48. <li><a href="influx_ppc64le.zip">influx_ppc64le.zip</a>
  49. </ul>
  50. <hr>
  51. </body>
  52. </html>
  53. `,
  54. arch: "arm64",
  55. plugins: []string{"file", "influx"},
  56. error: "",
  57. },
  58. {
  59. html: `<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><html>
  60. <title>Directory listing for enterprise: /4.1.1/</title>
  61. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  62. <meta name="robots" content="noindex,nofollow">
  63. <body>
  64. <h2>Directory listing for enterprise: /4.1.1/</h2>
  65. <hr>
  66. <ul>
  67. <li><a href="file_386.zip">file_386.zip</a>
  68. <li><a href="file_amd64.zip">file_amd64.zip</a>
  69. <li><a href="file_arm.zip">file_arm.zip</a>
  70. <li><a href="file_arm64.zip">file_arm64.zip</a>
  71. <li><a href="file_ppc64le.zip">file_ppc64le.zip</a>
  72. <li><a href="influx_386.zip">influx_386.zip</a>
  73. <li><a href="influx_amd64.zip">influx_amd64.zip</a>
  74. <li><a href="influx_arm.zip">influx_arm.zip</a>
  75. <li><a href="influx_arm64.zip">influx_arm64.zip</a>
  76. <li><a href="influx_ppc64le.zip">influx_ppc64le.zip</a>
  77. </ul>
  78. <hr>
  79. </body>
  80. </html>
  81. `,
  82. arch: "arm7",
  83. plugins: []string{},
  84. error: "",
  85. },
  86. {
  87. html: `<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><html>
  88. <title>Directory listing for enterprise: /4.1.1/</title>
  89. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  90. <meta name="robots" content="noindex,nofollow">
  91. <body>
  92. <h2>Directory listing for enterprise: /4.1.1/</h2>
  93. <hr>
  94. <ul>
  95. <li><a href="file_386.zip">file_386.zip</a>
  96. <li><a href="file_amd64.zip">file_amd64.zip</a>
  97. <li><a href="file_arm.zip">file_arm.zip</a>
  98. <li><a href="file_arm64.zip">file_arm64.zip</a>
  99. <li><a href="file_ppc64le.zip">file_ppc64le.zip</a>
  100. <li><a href="influx_arm.zip">influx_arm.zip</a>
  101. <li><a href="influx_arm64.zip">influx_arm64.zip</a>
  102. <li><a href="influx_ppc64le.zip">influx_ppc64le.zip</a>
  103. </ul>
  104. <hr>
  105. </body>
  106. </html>
  107. `,
  108. arch: "amd64",
  109. plugins: []string{"file"},
  110. error: "",
  111. },
  112. {
  113. html: `<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><html>
  114. <title>Directory listing for enterprise: /4.1.1/</title>
  115. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  116. <meta name="robots" content="noindex,nofollow">
  117. <body>
  118. <h2>Directory listing for enterprise: /4.1.1/</h2>
  119. <hr>
  120. <ul>
  121. </ul>
  122. <hr>
  123. </body>
  124. </html>
  125. `,
  126. arch: "amd64",
  127. plugins: []string{},
  128. error: "",
  129. },
  130. {
  131. html: ``,
  132. arch: "amd64",
  133. plugins: []string{},
  134. error: "",
  135. },
  136. }
  137. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  138. for i, t := range tests {
  139. result := extractFromHtml(t.html, t.arch)
  140. if t.error == "" && !reflect.DeepEqual(t.plugins, result) {
  141. t1.Errorf("%d. %q\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, t.html, t.plugins, result)
  142. }
  143. }
  144. }
  145. func TestFetchPluginList(t1 *testing.T) {
  146. version = "0.9.1"
  147. // Start a local HTTP server
  148. server1 := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
  149. // Send response to be tested
  150. if _, err := rw.Write([]byte(`<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><html>
  151. <title>Directory listing for enterprise: /4.1.1/</title>
  152. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  153. <meta name="robots" content="noindex,nofollow">
  154. <body>
  155. <h2>Directory listing for enterprise: /4.1.1/</h2>
  156. <hr>
  157. <ul>
  158. <li><a href="file_386.zip">file_386.zip</a>
  159. <li><a href="file_amd64.zip">file_amd64.zip</a>
  160. <li><a href="file_arm.zip">file_arm.zip</a>
  161. <li><a href="file_arm64.zip">file_arm64.zip</a>
  162. <li><a href="file_ppc64le.zip">file_ppc64le.zip</a>
  163. <li><a href="influx_386.zip">influx_386.zip</a>
  164. <li><a href="influx_amd64.zip">influx_amd64.zip</a>
  165. <li><a href="influx_arm.zip">influx_arm.zip</a>
  166. <li><a href="influx_arm64.zip">influx_arm64.zip</a>
  167. <li><a href="influx_ppc64le.zip">influx_ppc64le.zip</a>
  168. </ul>
  169. <hr>
  170. </body>
  171. </html>
  172. `)); err != nil {
  173. fmt.Printf("%s", err)
  174. }
  175. }))
  176. server2 := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
  177. // Send response to be tested
  178. if _, err := rw.Write([]byte(`<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><html>
  179. <title>Directory listing for enterprise: /4.1.1/</title>
  180. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  181. <meta name="robots" content="noindex,nofollow">
  182. <body>
  183. <h2>Directory listing for enterprise: /4.1.1/</h2>
  184. <hr>
  185. <ul>
  186. <li><a href="file_arm64.zip">file_arm64.zip</a>
  187. <li><a href="zmq_386.zip">influx_386.zip</a>
  188. <li><a href="zmq_amd64.zip">influx_amd64.zip</a>
  189. <li><a href="zmq_arm.zip">influx_arm.zip</a>
  190. <li><a href="zmq_arm64.zip">influx_arm64.zip</a>
  191. <li><a href="zmq_ppc64le.zip">influx_ppc64le.zip</a>
  192. </ul>
  193. <hr>
  194. </body>
  195. </html>
  196. `)); err != nil {
  197. fmt.Printf("%s", err)
  198. }
  199. }))
  200. // Close the server when test finishes
  201. defer server2.Close()
  202. if e, r := fetchPluginList(strings.Join([]string{server1.URL, server2.URL}, ","), "sinks", "alpine", "arm64"); e != nil {
  203. t1.Errorf("Error: %v", e)
  204. } else {
  205. exp := map[string]string{
  206. "file": server1.URL + "/kuiper-plugins/" + version + "/alpine/sinks/file_arm64.zip",
  207. "influx": server1.URL + "/kuiper-plugins/" + version + "/alpine/sinks/influx_arm64.zip",
  208. "zmq": server2.URL + "/kuiper-plugins/" + version + "/alpine/sinks/zmq_arm64.zip",
  209. }
  210. if !reflect.DeepEqual(exp, r) {
  211. t1.Errorf("result mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", exp, r)
  212. }
  213. }
  214. if e, r := fetchPluginList(strings.Join([]string{server2.URL}, ","), "sinks", "alpine", "arm64"); e != nil {
  215. t1.Errorf("Error: %v", e)
  216. } else {
  217. exp := map[string]string{
  218. "zmq": server2.URL + "/kuiper-plugins/" + version + "/alpine/sinks/zmq_arm64.zip",
  219. "file": server2.URL + "/kuiper-plugins/" + version + "/alpine/sinks/file_arm64.zip",
  220. }
  221. if !reflect.DeepEqual(exp, r) {
  222. t1.Errorf("result mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", exp, r)
  223. }
  224. }
  225. if e, r := fetchPluginList(strings.Join([]string{server1.URL, server2.URL}, ","), "sinks", "alpine", "armv7"); e != nil {
  226. t1.Errorf("Error: %v", e)
  227. } else {
  228. exp := map[string]string{}
  229. if !reflect.DeepEqual(exp, r) {
  230. t1.Errorf("result mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", exp, r)
  231. }
  232. }
  233. }