expose_test.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. /*
  2. Copyright 2015 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 cmd
  14. import (
  15. "bytes"
  16. "net/http"
  17. "strings"
  18. "testing"
  19. "k8s.io/kubernetes/pkg/api"
  20. "k8s.io/kubernetes/pkg/client/unversioned/fake"
  21. "k8s.io/kubernetes/pkg/kubectl"
  22. "k8s.io/kubernetes/pkg/runtime"
  23. "k8s.io/kubernetes/pkg/util/intstr"
  24. )
  25. func TestRunExposeService(t *testing.T) {
  26. tests := []struct {
  27. name string
  28. args []string
  29. ns string
  30. calls map[string]string
  31. input runtime.Object
  32. flags map[string]string
  33. output runtime.Object
  34. expected string
  35. status int
  36. }{
  37. {
  38. name: "expose-service-from-service-no-selector-defined",
  39. args: []string{"service", "baz"},
  40. ns: "test",
  41. calls: map[string]string{
  42. "GET": "/namespaces/test/services/baz",
  43. "POST": "/namespaces/test/services",
  44. },
  45. input: &api.Service{
  46. ObjectMeta: api.ObjectMeta{Name: "baz", Namespace: "test", ResourceVersion: "12"},
  47. Spec: api.ServiceSpec{
  48. Selector: map[string]string{"app": "go"},
  49. },
  50. },
  51. flags: map[string]string{"protocol": "UDP", "port": "14", "name": "foo", "labels": "svc=test"},
  52. output: &api.Service{
  53. ObjectMeta: api.ObjectMeta{Name: "foo", Namespace: "", Labels: map[string]string{"svc": "test"}},
  54. Spec: api.ServiceSpec{
  55. Ports: []api.ServicePort{
  56. {
  57. Protocol: api.ProtocolUDP,
  58. Port: 14,
  59. TargetPort: intstr.FromInt(14),
  60. },
  61. },
  62. Selector: map[string]string{"app": "go"},
  63. },
  64. },
  65. expected: "service \"foo\" exposed",
  66. status: 200,
  67. },
  68. {
  69. name: "expose-service-from-service",
  70. args: []string{"service", "baz"},
  71. ns: "test",
  72. calls: map[string]string{
  73. "GET": "/namespaces/test/services/baz",
  74. "POST": "/namespaces/test/services",
  75. },
  76. input: &api.Service{
  77. ObjectMeta: api.ObjectMeta{Name: "baz", Namespace: "test", ResourceVersion: "12"},
  78. Spec: api.ServiceSpec{
  79. Selector: map[string]string{"app": "go"},
  80. },
  81. },
  82. flags: map[string]string{"selector": "func=stream", "protocol": "UDP", "port": "14", "name": "foo", "labels": "svc=test"},
  83. output: &api.Service{
  84. ObjectMeta: api.ObjectMeta{Name: "foo", Namespace: "", Labels: map[string]string{"svc": "test"}},
  85. Spec: api.ServiceSpec{
  86. Ports: []api.ServicePort{
  87. {
  88. Protocol: api.ProtocolUDP,
  89. Port: 14,
  90. TargetPort: intstr.FromInt(14),
  91. },
  92. },
  93. Selector: map[string]string{"func": "stream"},
  94. },
  95. },
  96. expected: "service \"foo\" exposed",
  97. status: 200,
  98. },
  99. {
  100. name: "no-name-passed-from-the-cli",
  101. args: []string{"service", "mayor"},
  102. ns: "default",
  103. calls: map[string]string{
  104. "GET": "/namespaces/default/services/mayor",
  105. "POST": "/namespaces/default/services",
  106. },
  107. input: &api.Service{
  108. ObjectMeta: api.ObjectMeta{Name: "mayor", Namespace: "default", ResourceVersion: "12"},
  109. Spec: api.ServiceSpec{
  110. Selector: map[string]string{"run": "this"},
  111. },
  112. },
  113. // No --name flag specified below. Service will use the rc's name passed via the 'default-name' parameter
  114. flags: map[string]string{"selector": "run=this", "port": "80", "labels": "runas=amayor"},
  115. output: &api.Service{
  116. ObjectMeta: api.ObjectMeta{Name: "mayor", Namespace: "", Labels: map[string]string{"runas": "amayor"}},
  117. Spec: api.ServiceSpec{
  118. Ports: []api.ServicePort{
  119. {
  120. Protocol: api.ProtocolTCP,
  121. Port: 80,
  122. TargetPort: intstr.FromInt(80),
  123. },
  124. },
  125. Selector: map[string]string{"run": "this"},
  126. },
  127. },
  128. expected: "service \"mayor\" exposed",
  129. status: 200,
  130. },
  131. {
  132. name: "expose-service",
  133. args: []string{"service", "baz"},
  134. ns: "test",
  135. calls: map[string]string{
  136. "GET": "/namespaces/test/services/baz",
  137. "POST": "/namespaces/test/services",
  138. },
  139. input: &api.Service{
  140. ObjectMeta: api.ObjectMeta{Name: "baz", Namespace: "test", ResourceVersion: "12"},
  141. Spec: api.ServiceSpec{
  142. Selector: map[string]string{"app": "go"},
  143. },
  144. },
  145. flags: map[string]string{"selector": "func=stream", "protocol": "UDP", "port": "14", "name": "foo", "labels": "svc=test", "type": "LoadBalancer", "dry-run": "true"},
  146. output: &api.Service{
  147. ObjectMeta: api.ObjectMeta{Name: "foo", Namespace: "", Labels: map[string]string{"svc": "test"}},
  148. Spec: api.ServiceSpec{
  149. Ports: []api.ServicePort{
  150. {
  151. Protocol: api.ProtocolUDP,
  152. Port: 14,
  153. TargetPort: intstr.FromInt(14),
  154. },
  155. },
  156. Selector: map[string]string{"func": "stream"},
  157. Type: api.ServiceTypeLoadBalancer,
  158. },
  159. },
  160. status: 200,
  161. },
  162. {
  163. name: "expose-affinity-service",
  164. args: []string{"service", "baz"},
  165. ns: "test",
  166. calls: map[string]string{
  167. "GET": "/namespaces/test/services/baz",
  168. "POST": "/namespaces/test/services",
  169. },
  170. input: &api.Service{
  171. ObjectMeta: api.ObjectMeta{Name: "baz", Namespace: "test", ResourceVersion: "12"},
  172. Spec: api.ServiceSpec{
  173. Selector: map[string]string{"app": "go"},
  174. },
  175. },
  176. flags: map[string]string{"selector": "func=stream", "protocol": "UDP", "port": "14", "name": "foo", "labels": "svc=test", "type": "LoadBalancer", "session-affinity": "ClientIP", "dry-run": "true"},
  177. output: &api.Service{
  178. ObjectMeta: api.ObjectMeta{Name: "foo", Namespace: "", Labels: map[string]string{"svc": "test"}},
  179. Spec: api.ServiceSpec{
  180. Ports: []api.ServicePort{
  181. {
  182. Protocol: api.ProtocolUDP,
  183. Port: 14,
  184. TargetPort: intstr.FromInt(14),
  185. },
  186. },
  187. Selector: map[string]string{"func": "stream"},
  188. Type: api.ServiceTypeLoadBalancer,
  189. SessionAffinity: api.ServiceAffinityClientIP,
  190. },
  191. },
  192. status: 200,
  193. },
  194. {
  195. name: "expose-service-cluster-ip",
  196. args: []string{"service", "baz"},
  197. ns: "test",
  198. calls: map[string]string{
  199. "GET": "/namespaces/test/services/baz",
  200. "POST": "/namespaces/test/services",
  201. },
  202. input: &api.Service{
  203. ObjectMeta: api.ObjectMeta{Name: "baz", Namespace: "test", ResourceVersion: "12"},
  204. Spec: api.ServiceSpec{
  205. Selector: map[string]string{"app": "go"},
  206. },
  207. },
  208. flags: map[string]string{"selector": "func=stream", "protocol": "UDP", "port": "14", "name": "foo", "labels": "svc=test", "cluster-ip": "10.10.10.10", "dry-run": "true"},
  209. output: &api.Service{
  210. ObjectMeta: api.ObjectMeta{Name: "foo", Namespace: "", Labels: map[string]string{"svc": "test"}},
  211. Spec: api.ServiceSpec{
  212. Ports: []api.ServicePort{
  213. {
  214. Protocol: api.ProtocolUDP,
  215. Port: 14,
  216. TargetPort: intstr.FromInt(14),
  217. },
  218. },
  219. Selector: map[string]string{"func": "stream"},
  220. ClusterIP: "10.10.10.10",
  221. },
  222. },
  223. expected: "service \"foo\" exposed",
  224. status: 200,
  225. },
  226. {
  227. name: "expose-headless-service",
  228. args: []string{"service", "baz"},
  229. ns: "test",
  230. calls: map[string]string{
  231. "GET": "/namespaces/test/services/baz",
  232. "POST": "/namespaces/test/services",
  233. },
  234. input: &api.Service{
  235. ObjectMeta: api.ObjectMeta{Name: "baz", Namespace: "test", ResourceVersion: "12"},
  236. Spec: api.ServiceSpec{
  237. Selector: map[string]string{"app": "go"},
  238. },
  239. },
  240. flags: map[string]string{"selector": "func=stream", "protocol": "UDP", "port": "14", "name": "foo", "labels": "svc=test", "cluster-ip": "None", "dry-run": "true"},
  241. output: &api.Service{
  242. ObjectMeta: api.ObjectMeta{Name: "foo", Namespace: "", Labels: map[string]string{"svc": "test"}},
  243. Spec: api.ServiceSpec{
  244. Ports: []api.ServicePort{
  245. {
  246. Protocol: api.ProtocolUDP,
  247. Port: 14,
  248. TargetPort: intstr.FromInt(14),
  249. },
  250. },
  251. Selector: map[string]string{"func": "stream"},
  252. ClusterIP: api.ClusterIPNone,
  253. },
  254. },
  255. expected: "service \"foo\" exposed",
  256. status: 200,
  257. },
  258. {
  259. name: "expose-from-file",
  260. args: []string{},
  261. ns: "test",
  262. calls: map[string]string{
  263. "GET": "/namespaces/test/services/redis-master",
  264. "POST": "/namespaces/test/services",
  265. },
  266. input: &api.Service{
  267. ObjectMeta: api.ObjectMeta{Name: "redis-master", Namespace: "test", ResourceVersion: "12"},
  268. Spec: api.ServiceSpec{
  269. Selector: map[string]string{"app": "go"},
  270. },
  271. },
  272. flags: map[string]string{"filename": "../../../examples/guestbook/redis-master-service.yaml", "selector": "func=stream", "protocol": "UDP", "port": "14", "name": "foo", "labels": "svc=test", "dry-run": "true"},
  273. output: &api.Service{
  274. ObjectMeta: api.ObjectMeta{Name: "foo", Labels: map[string]string{"svc": "test"}},
  275. Spec: api.ServiceSpec{
  276. Ports: []api.ServicePort{
  277. {
  278. Protocol: api.ProtocolUDP,
  279. Port: 14,
  280. TargetPort: intstr.FromInt(14),
  281. },
  282. },
  283. Selector: map[string]string{"func": "stream"},
  284. },
  285. },
  286. status: 200,
  287. },
  288. {
  289. name: "truncate-name",
  290. args: []string{"pod", "a-name-that-is-toooo-big-for-a-service-because-it-can-only-handle-63-characters"},
  291. ns: "test",
  292. calls: map[string]string{
  293. "GET": "/namespaces/test/pods/a-name-that-is-toooo-big-for-a-service-because-it-can-only-handle-63-characters",
  294. "POST": "/namespaces/test/services",
  295. },
  296. input: &api.Pod{
  297. ObjectMeta: api.ObjectMeta{Name: "baz", Namespace: "test", ResourceVersion: "12"},
  298. },
  299. flags: map[string]string{"selector": "svc=frompod", "port": "90", "labels": "svc=frompod", "generator": "service/v2"},
  300. output: &api.Service{
  301. ObjectMeta: api.ObjectMeta{Name: "a-name-that-is-toooo-big-for-a-service-because-it-can-only-handle-63-characters", Namespace: "", Labels: map[string]string{"svc": "frompod"}},
  302. Spec: api.ServiceSpec{
  303. Ports: []api.ServicePort{
  304. {
  305. Protocol: api.ProtocolTCP,
  306. Port: 90,
  307. TargetPort: intstr.FromInt(90),
  308. },
  309. },
  310. Selector: map[string]string{"svc": "frompod"},
  311. },
  312. },
  313. expected: "service \"a-name-that-is-toooo-big-for-a-service-because-it-can-only-hand\" exposed",
  314. status: 200,
  315. },
  316. {
  317. name: "expose-multiport-object",
  318. args: []string{"service", "foo"},
  319. ns: "test",
  320. calls: map[string]string{
  321. "GET": "/namespaces/test/services/foo",
  322. "POST": "/namespaces/test/services",
  323. },
  324. input: &api.Service{
  325. ObjectMeta: api.ObjectMeta{Name: "foo", Namespace: "", Labels: map[string]string{"svc": "multiport"}},
  326. Spec: api.ServiceSpec{
  327. Ports: []api.ServicePort{
  328. {
  329. Protocol: api.ProtocolTCP,
  330. Port: 80,
  331. TargetPort: intstr.FromInt(80),
  332. },
  333. {
  334. Protocol: api.ProtocolTCP,
  335. Port: 443,
  336. TargetPort: intstr.FromInt(443),
  337. },
  338. },
  339. },
  340. },
  341. flags: map[string]string{"selector": "svc=fromfoo", "generator": "service/v2", "name": "fromfoo", "dry-run": "true"},
  342. output: &api.Service{
  343. ObjectMeta: api.ObjectMeta{Name: "fromfoo", Namespace: "", Labels: map[string]string{"svc": "multiport"}},
  344. Spec: api.ServiceSpec{
  345. Ports: []api.ServicePort{
  346. {
  347. Name: "port-1",
  348. Protocol: api.ProtocolTCP,
  349. Port: 80,
  350. TargetPort: intstr.FromInt(80),
  351. },
  352. {
  353. Name: "port-2",
  354. Protocol: api.ProtocolTCP,
  355. Port: 443,
  356. TargetPort: intstr.FromInt(443),
  357. },
  358. },
  359. Selector: map[string]string{"svc": "fromfoo"},
  360. },
  361. },
  362. status: 200,
  363. },
  364. {
  365. name: "expose-multiprotocol-object",
  366. args: []string{"service", "foo"},
  367. ns: "test",
  368. calls: map[string]string{
  369. "GET": "/namespaces/test/services/foo",
  370. "POST": "/namespaces/test/services",
  371. },
  372. input: &api.Service{
  373. ObjectMeta: api.ObjectMeta{Name: "foo", Namespace: "", Labels: map[string]string{"svc": "multiport"}},
  374. Spec: api.ServiceSpec{
  375. Ports: []api.ServicePort{
  376. {
  377. Protocol: api.ProtocolTCP,
  378. Port: 80,
  379. TargetPort: intstr.FromInt(80),
  380. },
  381. {
  382. Protocol: api.ProtocolUDP,
  383. Port: 8080,
  384. TargetPort: intstr.FromInt(8080),
  385. },
  386. {
  387. Protocol: api.ProtocolUDP,
  388. Port: 8081,
  389. TargetPort: intstr.FromInt(8081),
  390. },
  391. },
  392. },
  393. },
  394. flags: map[string]string{"selector": "svc=fromfoo", "generator": "service/v2", "name": "fromfoo", "dry-run": "true"},
  395. output: &api.Service{
  396. ObjectMeta: api.ObjectMeta{Name: "fromfoo", Namespace: "", Labels: map[string]string{"svc": "multiport"}},
  397. Spec: api.ServiceSpec{
  398. Ports: []api.ServicePort{
  399. {
  400. Name: "port-1",
  401. Protocol: api.ProtocolTCP,
  402. Port: 80,
  403. TargetPort: intstr.FromInt(80),
  404. },
  405. {
  406. Name: "port-2",
  407. Protocol: api.ProtocolUDP,
  408. Port: 8080,
  409. TargetPort: intstr.FromInt(8080),
  410. },
  411. {
  412. Name: "port-3",
  413. Protocol: api.ProtocolUDP,
  414. Port: 8081,
  415. TargetPort: intstr.FromInt(8081),
  416. },
  417. },
  418. Selector: map[string]string{"svc": "fromfoo"},
  419. },
  420. },
  421. status: 200,
  422. },
  423. }
  424. for _, test := range tests {
  425. f, tf, codec, ns := NewAPIFactory()
  426. tf.Printer = &kubectl.JSONPrinter{}
  427. tf.Client = &fake.RESTClient{
  428. NegotiatedSerializer: ns,
  429. Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
  430. switch p, m := req.URL.Path, req.Method; {
  431. case p == test.calls[m] && m == "GET":
  432. return &http.Response{StatusCode: test.status, Header: defaultHeader(), Body: objBody(codec, test.input)}, nil
  433. case p == test.calls[m] && m == "POST":
  434. return &http.Response{StatusCode: test.status, Header: defaultHeader(), Body: objBody(codec, test.output)}, nil
  435. default:
  436. t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
  437. return nil, nil
  438. }
  439. }),
  440. }
  441. tf.Namespace = test.ns
  442. buf := bytes.NewBuffer([]byte{})
  443. cmd := NewCmdExposeService(f, buf)
  444. cmd.SetOutput(buf)
  445. for flag, value := range test.flags {
  446. cmd.Flags().Set(flag, value)
  447. }
  448. cmd.Run(cmd, test.args)
  449. out := buf.String()
  450. if _, ok := test.flags["dry-run"]; ok {
  451. buf.Reset()
  452. if err := tf.Printer.PrintObj(test.output, buf); err != nil {
  453. t.Errorf("%s: Unexpected error: %v", test.name, err)
  454. continue
  455. }
  456. test.expected = buf.String()
  457. }
  458. if !strings.Contains(out, test.expected) {
  459. t.Errorf("%s: Unexpected output! Expected\n%s\ngot\n%s", test.name, test.expected, out)
  460. }
  461. }
  462. }