proxy_server_test.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package kubectl
  14. import (
  15. "fmt"
  16. "io/ioutil"
  17. "net/http"
  18. "net/http/httptest"
  19. "net/url"
  20. "path/filepath"
  21. "strings"
  22. "testing"
  23. "k8s.io/kubernetes/pkg/client/restclient"
  24. )
  25. func TestAccept(t *testing.T) {
  26. tests := []struct {
  27. acceptPaths string
  28. rejectPaths string
  29. acceptHosts string
  30. path string
  31. host string
  32. method string
  33. expectAccept bool
  34. }{
  35. {
  36. acceptPaths: DefaultPathAcceptRE,
  37. rejectPaths: DefaultPathRejectRE,
  38. acceptHosts: DefaultHostAcceptRE,
  39. path: "/api/v1/pods",
  40. host: "127.0.0.1",
  41. method: "GET",
  42. expectAccept: true,
  43. },
  44. {
  45. acceptPaths: DefaultPathAcceptRE,
  46. rejectPaths: DefaultPathRejectRE,
  47. acceptHosts: DefaultHostAcceptRE,
  48. path: "/api/v1/pods",
  49. host: "localhost",
  50. method: "GET",
  51. expectAccept: true,
  52. },
  53. {
  54. acceptPaths: DefaultPathAcceptRE,
  55. rejectPaths: DefaultPathRejectRE,
  56. acceptHosts: DefaultHostAcceptRE,
  57. path: "/api/v1/namespaces/default/pods/foo",
  58. host: "localhost",
  59. method: "GET",
  60. expectAccept: true,
  61. },
  62. {
  63. acceptPaths: DefaultPathAcceptRE,
  64. rejectPaths: DefaultPathRejectRE,
  65. acceptHosts: DefaultHostAcceptRE,
  66. path: "/api/v1/namespaces/default/pods/attachfoo",
  67. host: "localhost",
  68. method: "GET",
  69. expectAccept: true,
  70. },
  71. {
  72. acceptPaths: DefaultPathAcceptRE,
  73. rejectPaths: DefaultPathRejectRE,
  74. acceptHosts: DefaultHostAcceptRE,
  75. path: "/api/v1/namespaces/default/pods/execfoo",
  76. host: "localhost",
  77. method: "GET",
  78. expectAccept: true,
  79. },
  80. {
  81. acceptPaths: DefaultPathAcceptRE,
  82. rejectPaths: DefaultPathRejectRE,
  83. acceptHosts: DefaultHostAcceptRE,
  84. path: "/api/v1/namespaces/default/pods/foo/exec",
  85. host: "127.0.0.1",
  86. method: "GET",
  87. expectAccept: false,
  88. },
  89. {
  90. acceptPaths: DefaultPathAcceptRE,
  91. rejectPaths: DefaultPathRejectRE,
  92. acceptHosts: DefaultHostAcceptRE,
  93. path: "/api/v1/namespaces/default/pods/foo/attach",
  94. host: "127.0.0.1",
  95. method: "GET",
  96. expectAccept: false,
  97. },
  98. {
  99. acceptPaths: DefaultPathAcceptRE,
  100. rejectPaths: DefaultPathRejectRE,
  101. acceptHosts: DefaultHostAcceptRE,
  102. path: "/api/v1/pods",
  103. host: "evil.com",
  104. method: "GET",
  105. expectAccept: false,
  106. },
  107. {
  108. acceptPaths: DefaultPathAcceptRE,
  109. rejectPaths: DefaultPathRejectRE,
  110. acceptHosts: DefaultHostAcceptRE,
  111. path: "/api/v1/pods",
  112. host: "localhost.evil.com",
  113. method: "GET",
  114. expectAccept: false,
  115. },
  116. {
  117. acceptPaths: DefaultPathAcceptRE,
  118. rejectPaths: DefaultPathRejectRE,
  119. acceptHosts: DefaultHostAcceptRE,
  120. path: "/api/v1/pods",
  121. host: "127a0b0c1",
  122. method: "GET",
  123. expectAccept: false,
  124. },
  125. {
  126. acceptPaths: DefaultPathAcceptRE,
  127. rejectPaths: DefaultPathRejectRE,
  128. acceptHosts: DefaultHostAcceptRE,
  129. path: "/ui",
  130. host: "localhost",
  131. method: "GET",
  132. expectAccept: true,
  133. },
  134. {
  135. acceptPaths: DefaultPathAcceptRE,
  136. rejectPaths: DefaultPathRejectRE,
  137. acceptHosts: DefaultHostAcceptRE,
  138. path: "/api/v1/pods",
  139. host: "localhost",
  140. method: "POST",
  141. expectAccept: false,
  142. },
  143. {
  144. acceptPaths: DefaultPathAcceptRE,
  145. rejectPaths: DefaultPathRejectRE,
  146. acceptHosts: DefaultHostAcceptRE,
  147. path: "/api/v1/namespaces/default/pods/somepod",
  148. host: "localhost",
  149. method: "PUT",
  150. expectAccept: false,
  151. },
  152. {
  153. acceptPaths: DefaultPathAcceptRE,
  154. rejectPaths: DefaultPathRejectRE,
  155. acceptHosts: DefaultHostAcceptRE,
  156. path: "/api/v1/namespaces/default/pods/somepod",
  157. host: "localhost",
  158. method: "PATCH",
  159. expectAccept: false,
  160. },
  161. }
  162. for _, test := range tests {
  163. filter := &FilterServer{
  164. AcceptPaths: MakeRegexpArrayOrDie(test.acceptPaths),
  165. RejectPaths: MakeRegexpArrayOrDie(test.rejectPaths),
  166. AcceptHosts: MakeRegexpArrayOrDie(test.acceptHosts),
  167. RejectMethods: MakeRegexpArrayOrDie(DefaultMethodRejectRE),
  168. }
  169. accept := filter.accept(test.method, test.path, test.host)
  170. if accept != test.expectAccept {
  171. t.Errorf("expected: %v, got %v for %#v", test.expectAccept, accept, test)
  172. }
  173. }
  174. }
  175. func TestRegexpMatch(t *testing.T) {
  176. tests := []struct {
  177. str string
  178. regexps string
  179. expectMatch bool
  180. }{
  181. {
  182. str: "foo",
  183. regexps: "bar,.*",
  184. expectMatch: true,
  185. },
  186. {
  187. str: "foo",
  188. regexps: "bar,fo.*",
  189. expectMatch: true,
  190. },
  191. {
  192. str: "bar",
  193. regexps: "bar,fo.*",
  194. expectMatch: true,
  195. },
  196. {
  197. str: "baz",
  198. regexps: "bar,fo.*",
  199. expectMatch: false,
  200. },
  201. }
  202. for _, test := range tests {
  203. match := matchesRegexp(test.str, MakeRegexpArrayOrDie(test.regexps))
  204. if test.expectMatch != match {
  205. t.Errorf("expected: %v, found: %v, for %s and %v", test.expectMatch, match, test.str, test.regexps)
  206. }
  207. }
  208. }
  209. func TestFileServing(t *testing.T) {
  210. const (
  211. fname = "test.txt"
  212. data = "This is test data"
  213. )
  214. dir, err := ioutil.TempDir("", "data")
  215. if err != nil {
  216. t.Fatalf("error creating tmp dir: %v", err)
  217. }
  218. if err := ioutil.WriteFile(filepath.Join(dir, fname), []byte(data), 0755); err != nil {
  219. t.Fatalf("error writing tmp file: %v", err)
  220. }
  221. const prefix = "/foo/"
  222. handler := newFileHandler(prefix, dir)
  223. server := httptest.NewServer(handler)
  224. defer server.Close()
  225. url := server.URL + prefix + fname
  226. res, err := http.Get(url)
  227. if err != nil {
  228. t.Fatalf("http.Get(%q) error: %v", url, err)
  229. }
  230. defer res.Body.Close()
  231. if res.StatusCode != http.StatusOK {
  232. t.Errorf("res.StatusCode = %d; want %d", res.StatusCode, http.StatusOK)
  233. }
  234. b, err := ioutil.ReadAll(res.Body)
  235. if err != nil {
  236. t.Fatalf("error reading resp body: %v", err)
  237. }
  238. if string(b) != data {
  239. t.Errorf("have %q; want %q", string(b), data)
  240. }
  241. }
  242. func TestAPIRequests(t *testing.T) {
  243. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  244. b, err := ioutil.ReadAll(r.Body)
  245. if err != nil {
  246. http.Error(w, err.Error(), http.StatusInternalServerError)
  247. return
  248. }
  249. fmt.Fprintf(w, "%s %s %s", r.Method, r.RequestURI, string(b))
  250. }))
  251. defer ts.Close()
  252. // httptest.NewServer should always generate a valid URL.
  253. target, _ := url.Parse(ts.URL)
  254. proxy := newProxy(target)
  255. tests := []struct{ method, body string }{
  256. {"GET", ""},
  257. {"DELETE", ""},
  258. {"POST", "test payload"},
  259. {"PUT", "test payload"},
  260. }
  261. const path = "/api/test?fields=ID%3Dfoo&labels=key%3Dvalue"
  262. for i, tt := range tests {
  263. r, err := http.NewRequest(tt.method, path, strings.NewReader(tt.body))
  264. if err != nil {
  265. t.Errorf("error creating request: %v", err)
  266. continue
  267. }
  268. w := httptest.NewRecorder()
  269. proxy.ServeHTTP(w, r)
  270. if w.Code != http.StatusOK {
  271. t.Errorf("%d: proxy.ServeHTTP w.Code = %d; want %d", i, w.Code, http.StatusOK)
  272. }
  273. want := strings.Join([]string{tt.method, path, tt.body}, " ")
  274. if w.Body.String() != want {
  275. t.Errorf("%d: response body = %q; want %q", i, w.Body.String(), want)
  276. }
  277. }
  278. }
  279. func TestPathHandling(t *testing.T) {
  280. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  281. fmt.Fprint(w, r.URL.Path)
  282. }))
  283. defer ts.Close()
  284. table := []struct {
  285. prefix string
  286. reqPath string
  287. expectPath string
  288. }{
  289. {"/api/", "/metrics", "404 page not found\n"},
  290. {"/api/", "/api/metrics", "/api/metrics"},
  291. {"/api/", "/api/v1/pods/", "/api/v1/pods/"},
  292. {"/", "/metrics", "/metrics"},
  293. {"/", "/api/v1/pods/", "/api/v1/pods/"},
  294. {"/custom/", "/metrics", "404 page not found\n"},
  295. {"/custom/", "/api/metrics", "404 page not found\n"},
  296. {"/custom/", "/api/v1/pods/", "404 page not found\n"},
  297. {"/custom/", "/custom/api/metrics", "/api/metrics"},
  298. {"/custom/", "/custom/api/v1/pods/", "/api/v1/pods/"},
  299. }
  300. cc := &restclient.Config{
  301. Host: ts.URL,
  302. }
  303. for _, item := range table {
  304. func() {
  305. p, err := NewProxyServer("", item.prefix, "/not/used/for/this/test", nil, cc)
  306. if err != nil {
  307. t.Fatalf("%#v: %v", item, err)
  308. }
  309. pts := httptest.NewServer(p.handler)
  310. defer pts.Close()
  311. r, err := http.Get(pts.URL + item.reqPath)
  312. if err != nil {
  313. t.Fatalf("%#v: %v", item, err)
  314. }
  315. body, err := ioutil.ReadAll(r.Body)
  316. r.Body.Close()
  317. if err != nil {
  318. t.Fatalf("%#v: %v", item, err)
  319. }
  320. if e, a := item.expectPath, string(body); e != a {
  321. t.Errorf("%#v: Wanted %q, got %q", item, e, a)
  322. }
  323. }()
  324. }
  325. }
  326. func TestExtractHost(t *testing.T) {
  327. fixtures := map[string]string{
  328. "localhost:8085": "localhost",
  329. "marmalade": "marmalade",
  330. }
  331. for header, expected := range fixtures {
  332. host := extractHost(header)
  333. if host != expected {
  334. t.Fatalf("%s != %s", host, expected)
  335. }
  336. }
  337. }