proxier_test.go 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155
  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 userspace
  14. import (
  15. "fmt"
  16. "io/ioutil"
  17. "net"
  18. "net/http"
  19. "net/http/httptest"
  20. "net/url"
  21. "os"
  22. "reflect"
  23. "strconv"
  24. "sync/atomic"
  25. "testing"
  26. "time"
  27. "k8s.io/api/core/v1"
  28. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  29. "k8s.io/apimachinery/pkg/types"
  30. "k8s.io/apimachinery/pkg/util/runtime"
  31. "k8s.io/apimachinery/pkg/util/wait"
  32. "k8s.io/kubernetes/pkg/proxy"
  33. ipttest "k8s.io/kubernetes/pkg/util/iptables/testing"
  34. "k8s.io/utils/exec"
  35. fakeexec "k8s.io/utils/exec/testing"
  36. )
  37. const (
  38. udpIdleTimeoutForTest = 250 * time.Millisecond
  39. )
  40. func joinHostPort(host string, port int) string {
  41. return net.JoinHostPort(host, fmt.Sprintf("%d", port))
  42. }
  43. func waitForClosedPortTCP(p *Proxier, proxyPort int) error {
  44. for i := 0; i < 50; i++ {
  45. conn, err := net.Dial("tcp", joinHostPort("", proxyPort))
  46. if err != nil {
  47. return nil
  48. }
  49. conn.Close()
  50. time.Sleep(1 * time.Millisecond)
  51. }
  52. return fmt.Errorf("port %d still open", proxyPort)
  53. }
  54. func waitForClosedPortUDP(p *Proxier, proxyPort int) error {
  55. for i := 0; i < 50; i++ {
  56. conn, err := net.Dial("udp", joinHostPort("", proxyPort))
  57. if err != nil {
  58. return nil
  59. }
  60. conn.SetReadDeadline(time.Now().Add(10 * time.Millisecond))
  61. // To detect a closed UDP port write, then read.
  62. _, err = conn.Write([]byte("x"))
  63. if err != nil {
  64. if e, ok := err.(net.Error); ok && !e.Timeout() {
  65. return nil
  66. }
  67. }
  68. var buf [4]byte
  69. _, err = conn.Read(buf[0:])
  70. if err != nil {
  71. if e, ok := err.(net.Error); ok && !e.Timeout() {
  72. return nil
  73. }
  74. }
  75. conn.Close()
  76. time.Sleep(1 * time.Millisecond)
  77. }
  78. return fmt.Errorf("port %d still open", proxyPort)
  79. }
  80. func waitForServiceInfo(p *Proxier, service proxy.ServicePortName) (*ServiceInfo, bool) {
  81. var svcInfo *ServiceInfo
  82. var exists bool
  83. wait.PollImmediate(50*time.Millisecond, 3*time.Second, func() (bool, error) {
  84. svcInfo, exists = p.getServiceInfo(service)
  85. return exists, nil
  86. })
  87. return svcInfo, exists
  88. }
  89. // udpEchoServer is a simple echo server in UDP, intended for testing the proxy.
  90. type udpEchoServer struct {
  91. net.PacketConn
  92. }
  93. func newUDPEchoServer() (*udpEchoServer, error) {
  94. packetconn, err := net.ListenPacket("udp", ":0")
  95. if err != nil {
  96. return nil, err
  97. }
  98. return &udpEchoServer{packetconn}, nil
  99. }
  100. func (r *udpEchoServer) Loop() {
  101. var buffer [4096]byte
  102. for {
  103. n, cliAddr, err := r.ReadFrom(buffer[0:])
  104. if err != nil {
  105. fmt.Printf("ReadFrom failed: %v\n", err)
  106. continue
  107. }
  108. r.WriteTo(buffer[0:n], cliAddr)
  109. }
  110. }
  111. var tcpServerPort int32
  112. var udpServerPort int32
  113. func TestMain(m *testing.M) {
  114. // Don't handle panics
  115. runtime.ReallyCrash = true
  116. // TCP setup.
  117. tcp := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  118. w.WriteHeader(http.StatusOK)
  119. w.Write([]byte(r.URL.Path[1:]))
  120. }))
  121. defer tcp.Close()
  122. u, err := url.Parse(tcp.URL)
  123. if err != nil {
  124. panic(fmt.Sprintf("failed to parse: %v", err))
  125. }
  126. _, port, err := net.SplitHostPort(u.Host)
  127. if err != nil {
  128. panic(fmt.Sprintf("failed to parse: %v", err))
  129. }
  130. tcpServerPortValue, err := strconv.Atoi(port)
  131. if err != nil {
  132. panic(fmt.Sprintf("failed to atoi(%s): %v", port, err))
  133. }
  134. tcpServerPort = int32(tcpServerPortValue)
  135. // UDP setup.
  136. udp, err := newUDPEchoServer()
  137. if err != nil {
  138. panic(fmt.Sprintf("failed to make a UDP server: %v", err))
  139. }
  140. _, port, err = net.SplitHostPort(udp.LocalAddr().String())
  141. if err != nil {
  142. panic(fmt.Sprintf("failed to parse: %v", err))
  143. }
  144. udpServerPortValue, err := strconv.Atoi(port)
  145. if err != nil {
  146. panic(fmt.Sprintf("failed to atoi(%s): %v", port, err))
  147. }
  148. udpServerPort = int32(udpServerPortValue)
  149. go udp.Loop()
  150. ret := m.Run()
  151. // it should be safe to call Close() multiple times.
  152. tcp.Close()
  153. os.Exit(ret)
  154. }
  155. func testEchoTCP(t *testing.T, address string, port int) {
  156. path := "aaaaa"
  157. res, err := http.Get("http://" + address + ":" + fmt.Sprintf("%d", port) + "/" + path)
  158. if err != nil {
  159. t.Fatalf("error connecting to server: %v", err)
  160. }
  161. defer res.Body.Close()
  162. data, err := ioutil.ReadAll(res.Body)
  163. if err != nil {
  164. t.Errorf("error reading data: %v %v", err, string(data))
  165. }
  166. if string(data) != path {
  167. t.Errorf("expected: %s, got %s", path, string(data))
  168. }
  169. }
  170. func testEchoUDP(t *testing.T, address string, port int) {
  171. data := "abc123"
  172. conn, err := net.Dial("udp", joinHostPort(address, port))
  173. if err != nil {
  174. t.Fatalf("error connecting to server: %v", err)
  175. }
  176. if _, err := conn.Write([]byte(data)); err != nil {
  177. t.Fatalf("error sending to server: %v", err)
  178. }
  179. var resp [1024]byte
  180. n, err := conn.Read(resp[0:])
  181. if err != nil {
  182. t.Errorf("error receiving data: %v", err)
  183. }
  184. if string(resp[0:n]) != data {
  185. t.Errorf("expected: %s, got %s", data, string(resp[0:n]))
  186. }
  187. }
  188. func waitForNumProxyLoops(t *testing.T, p *Proxier, want int32) {
  189. var got int32
  190. for i := 0; i < 600; i++ {
  191. got = atomic.LoadInt32(&p.numProxyLoops)
  192. if got == want {
  193. return
  194. }
  195. time.Sleep(100 * time.Millisecond)
  196. }
  197. t.Errorf("expected %d ProxyLoops running, got %d", want, got)
  198. }
  199. func waitForNumProxyClients(t *testing.T, s *ServiceInfo, want int, timeout time.Duration) {
  200. var got int
  201. now := time.Now()
  202. deadline := now.Add(timeout)
  203. for time.Now().Before(deadline) {
  204. s.ActiveClients.Mu.Lock()
  205. got = len(s.ActiveClients.Clients)
  206. s.ActiveClients.Mu.Unlock()
  207. if got == want {
  208. return
  209. }
  210. time.Sleep(500 * time.Millisecond)
  211. }
  212. t.Errorf("expected %d ProxyClients live, got %d", want, got)
  213. }
  214. func startProxier(p *Proxier, t *testing.T) {
  215. go func() {
  216. p.SyncLoop()
  217. }()
  218. waitForNumProxyLoops(t, p, 0)
  219. p.OnServiceSynced()
  220. p.OnEndpointsSynced()
  221. }
  222. func TestTCPProxy(t *testing.T) {
  223. lb := NewLoadBalancerRR()
  224. service := proxy.ServicePortName{NamespacedName: types.NamespacedName{Namespace: "testnamespace", Name: "echo"}, Port: "p"}
  225. lb.OnEndpointsAdd(&v1.Endpoints{
  226. ObjectMeta: metav1.ObjectMeta{Name: service.Name, Namespace: service.Namespace},
  227. Subsets: []v1.EndpointSubset{{
  228. Addresses: []v1.EndpointAddress{{IP: "127.0.0.1"}},
  229. Ports: []v1.EndpointPort{{Name: "p", Port: tcpServerPort}},
  230. }},
  231. })
  232. fexec := makeFakeExec()
  233. p, err := createProxier(lb, net.ParseIP("0.0.0.0"), ipttest.NewFake(), fexec, net.ParseIP("127.0.0.1"), nil, time.Minute, time.Second, udpIdleTimeoutForTest, newProxySocket)
  234. if err != nil {
  235. t.Fatal(err)
  236. }
  237. startProxier(p, t)
  238. defer p.shutdown()
  239. svcInfo, err := p.addServiceOnPort(service, "TCP", 0, time.Second)
  240. if err != nil {
  241. t.Fatalf("error adding new service: %#v", err)
  242. }
  243. testEchoTCP(t, "127.0.0.1", svcInfo.proxyPort)
  244. waitForNumProxyLoops(t, p, 1)
  245. }
  246. func TestUDPProxy(t *testing.T) {
  247. lb := NewLoadBalancerRR()
  248. service := proxy.ServicePortName{NamespacedName: types.NamespacedName{Namespace: "testnamespace", Name: "echo"}, Port: "p"}
  249. lb.OnEndpointsAdd(&v1.Endpoints{
  250. ObjectMeta: metav1.ObjectMeta{Name: service.Name, Namespace: service.Namespace},
  251. Subsets: []v1.EndpointSubset{{
  252. Addresses: []v1.EndpointAddress{{IP: "127.0.0.1"}},
  253. Ports: []v1.EndpointPort{{Name: "p", Port: udpServerPort}},
  254. }},
  255. })
  256. fexec := makeFakeExec()
  257. p, err := createProxier(lb, net.ParseIP("0.0.0.0"), ipttest.NewFake(), fexec, net.ParseIP("127.0.0.1"), nil, time.Minute, time.Second, udpIdleTimeoutForTest, newProxySocket)
  258. if err != nil {
  259. t.Fatal(err)
  260. }
  261. startProxier(p, t)
  262. defer p.shutdown()
  263. svcInfo, err := p.addServiceOnPort(service, "UDP", 0, time.Second)
  264. if err != nil {
  265. t.Fatalf("error adding new service: %#v", err)
  266. }
  267. testEchoUDP(t, "127.0.0.1", svcInfo.proxyPort)
  268. waitForNumProxyLoops(t, p, 1)
  269. }
  270. func TestUDPProxyTimeout(t *testing.T) {
  271. lb := NewLoadBalancerRR()
  272. service := proxy.ServicePortName{NamespacedName: types.NamespacedName{Namespace: "testnamespace", Name: "echo"}, Port: "p"}
  273. lb.OnEndpointsAdd(&v1.Endpoints{
  274. ObjectMeta: metav1.ObjectMeta{Name: service.Name, Namespace: service.Namespace},
  275. Subsets: []v1.EndpointSubset{{
  276. Addresses: []v1.EndpointAddress{{IP: "127.0.0.1"}},
  277. Ports: []v1.EndpointPort{{Name: "p", Port: udpServerPort}},
  278. }},
  279. })
  280. fexec := makeFakeExec()
  281. p, err := createProxier(lb, net.ParseIP("0.0.0.0"), ipttest.NewFake(), fexec, net.ParseIP("127.0.0.1"), nil, time.Minute, time.Second, udpIdleTimeoutForTest, newProxySocket)
  282. if err != nil {
  283. t.Fatal(err)
  284. }
  285. startProxier(p, t)
  286. defer p.shutdown()
  287. svcInfo, err := p.addServiceOnPort(service, "UDP", 0, time.Second)
  288. if err != nil {
  289. t.Fatalf("error adding new service: %#v", err)
  290. }
  291. waitForNumProxyLoops(t, p, 1)
  292. testEchoUDP(t, "127.0.0.1", svcInfo.proxyPort)
  293. // When connecting to a UDP service endpoint, there should be a Conn for proxy.
  294. waitForNumProxyClients(t, svcInfo, 1, time.Second)
  295. // If conn has no activity for serviceInfo.timeout since last Read/Write, it should be closed because of timeout.
  296. waitForNumProxyClients(t, svcInfo, 0, 2*time.Second)
  297. }
  298. func TestMultiPortProxy(t *testing.T) {
  299. lb := NewLoadBalancerRR()
  300. serviceP := proxy.ServicePortName{NamespacedName: types.NamespacedName{Namespace: "testnamespace", Name: "echo-p"}, Port: "p"}
  301. serviceQ := proxy.ServicePortName{NamespacedName: types.NamespacedName{Namespace: "testnamespace", Name: "echo-q"}, Port: "q"}
  302. lb.OnEndpointsAdd(&v1.Endpoints{
  303. ObjectMeta: metav1.ObjectMeta{Name: serviceP.Name, Namespace: serviceP.Namespace},
  304. Subsets: []v1.EndpointSubset{{
  305. Addresses: []v1.EndpointAddress{{IP: "127.0.0.1"}},
  306. Ports: []v1.EndpointPort{{Name: "p", Protocol: "TCP", Port: tcpServerPort}},
  307. }},
  308. })
  309. lb.OnEndpointsAdd(&v1.Endpoints{
  310. ObjectMeta: metav1.ObjectMeta{Name: serviceQ.Name, Namespace: serviceQ.Namespace},
  311. Subsets: []v1.EndpointSubset{{
  312. Addresses: []v1.EndpointAddress{{IP: "127.0.0.1"}},
  313. Ports: []v1.EndpointPort{{Name: "q", Protocol: "UDP", Port: udpServerPort}},
  314. }},
  315. })
  316. fexec := makeFakeExec()
  317. p, err := createProxier(lb, net.ParseIP("0.0.0.0"), ipttest.NewFake(), fexec, net.ParseIP("127.0.0.1"), nil, time.Minute, time.Second, udpIdleTimeoutForTest, newProxySocket)
  318. if err != nil {
  319. t.Fatal(err)
  320. }
  321. startProxier(p, t)
  322. defer p.shutdown()
  323. svcInfoP, err := p.addServiceOnPort(serviceP, "TCP", 0, time.Second)
  324. if err != nil {
  325. t.Fatalf("error adding new service: %#v", err)
  326. }
  327. testEchoTCP(t, "127.0.0.1", svcInfoP.proxyPort)
  328. waitForNumProxyLoops(t, p, 1)
  329. svcInfoQ, err := p.addServiceOnPort(serviceQ, "UDP", 0, time.Second)
  330. if err != nil {
  331. t.Fatalf("error adding new service: %#v", err)
  332. }
  333. testEchoUDP(t, "127.0.0.1", svcInfoQ.proxyPort)
  334. waitForNumProxyLoops(t, p, 2)
  335. }
  336. func TestMultiPortOnServiceAdd(t *testing.T) {
  337. lb := NewLoadBalancerRR()
  338. serviceP := proxy.ServicePortName{NamespacedName: types.NamespacedName{Namespace: "testnamespace", Name: "echo"}, Port: "p"}
  339. serviceQ := proxy.ServicePortName{NamespacedName: types.NamespacedName{Namespace: "testnamespace", Name: "echo"}, Port: "q"}
  340. serviceX := proxy.ServicePortName{NamespacedName: types.NamespacedName{Namespace: "testnamespace", Name: "echo"}, Port: "x"}
  341. fexec := makeFakeExec()
  342. p, err := createProxier(lb, net.ParseIP("0.0.0.0"), ipttest.NewFake(), fexec, net.ParseIP("127.0.0.1"), nil, time.Minute, time.Second, udpIdleTimeoutForTest, newProxySocket)
  343. if err != nil {
  344. t.Fatal(err)
  345. }
  346. startProxier(p, t)
  347. defer p.shutdown()
  348. p.OnServiceAdd(&v1.Service{
  349. ObjectMeta: metav1.ObjectMeta{Name: serviceP.Name, Namespace: serviceP.Namespace},
  350. Spec: v1.ServiceSpec{ClusterIP: "1.2.3.4", Ports: []v1.ServicePort{{
  351. Name: "p",
  352. Port: 80,
  353. Protocol: "TCP",
  354. }, {
  355. Name: "q",
  356. Port: 81,
  357. Protocol: "UDP",
  358. }}},
  359. })
  360. waitForNumProxyLoops(t, p, 2)
  361. svcInfo, exists := waitForServiceInfo(p, serviceP)
  362. if !exists {
  363. t.Fatalf("can't find serviceInfo for %s", serviceP)
  364. }
  365. if svcInfo.portal.ip.String() != "1.2.3.4" || svcInfo.portal.port != 80 || svcInfo.protocol != "TCP" {
  366. t.Errorf("unexpected serviceInfo for %s: %#v", serviceP, svcInfo)
  367. }
  368. svcInfo, exists = waitForServiceInfo(p, serviceQ)
  369. if !exists {
  370. t.Fatalf("can't find serviceInfo for %s", serviceQ)
  371. }
  372. if svcInfo.portal.ip.String() != "1.2.3.4" || svcInfo.portal.port != 81 || svcInfo.protocol != "UDP" {
  373. t.Errorf("unexpected serviceInfo for %s: %#v", serviceQ, svcInfo)
  374. }
  375. svcInfo, exists = p.getServiceInfo(serviceX)
  376. if exists {
  377. t.Fatalf("found unwanted serviceInfo for %s: %#v", serviceX, svcInfo)
  378. }
  379. }
  380. // Helper: Stops the proxy for the named service.
  381. func stopProxyByName(proxier *Proxier, service proxy.ServicePortName) error {
  382. proxier.mu.Lock()
  383. defer proxier.mu.Unlock()
  384. info, found := proxier.serviceMap[service]
  385. if !found {
  386. return fmt.Errorf("unknown service: %s", service)
  387. }
  388. return proxier.stopProxy(service, info)
  389. }
  390. func TestTCPProxyStop(t *testing.T) {
  391. lb := NewLoadBalancerRR()
  392. service := proxy.ServicePortName{NamespacedName: types.NamespacedName{Namespace: "testnamespace", Name: "echo"}, Port: "p"}
  393. lb.OnEndpointsAdd(&v1.Endpoints{
  394. ObjectMeta: metav1.ObjectMeta{Namespace: service.Namespace, Name: service.Name},
  395. Subsets: []v1.EndpointSubset{{
  396. Addresses: []v1.EndpointAddress{{IP: "127.0.0.1"}},
  397. Ports: []v1.EndpointPort{{Name: "p", Port: tcpServerPort}},
  398. }},
  399. })
  400. fexec := makeFakeExec()
  401. p, err := createProxier(lb, net.ParseIP("0.0.0.0"), ipttest.NewFake(), fexec, net.ParseIP("127.0.0.1"), nil, time.Minute, time.Second, udpIdleTimeoutForTest, newProxySocket)
  402. if err != nil {
  403. t.Fatal(err)
  404. }
  405. startProxier(p, t)
  406. defer p.shutdown()
  407. svcInfo, err := p.addServiceOnPort(service, "TCP", 0, time.Second)
  408. if err != nil {
  409. t.Fatalf("error adding new service: %#v", err)
  410. }
  411. if !svcInfo.IsAlive() {
  412. t.Fatalf("wrong value for IsAlive(): expected true")
  413. }
  414. conn, err := net.Dial("tcp", joinHostPort("", svcInfo.proxyPort))
  415. if err != nil {
  416. t.Fatalf("error connecting to proxy: %v", err)
  417. }
  418. conn.Close()
  419. waitForNumProxyLoops(t, p, 1)
  420. stopProxyByName(p, service)
  421. if svcInfo.IsAlive() {
  422. t.Fatalf("wrong value for IsAlive(): expected false")
  423. }
  424. // Wait for the port to really close.
  425. if err := waitForClosedPortTCP(p, svcInfo.proxyPort); err != nil {
  426. t.Fatalf(err.Error())
  427. }
  428. waitForNumProxyLoops(t, p, 0)
  429. }
  430. func TestUDPProxyStop(t *testing.T) {
  431. lb := NewLoadBalancerRR()
  432. service := proxy.ServicePortName{NamespacedName: types.NamespacedName{Namespace: "testnamespace", Name: "echo"}, Port: "p"}
  433. lb.OnEndpointsAdd(&v1.Endpoints{
  434. ObjectMeta: metav1.ObjectMeta{Namespace: service.Namespace, Name: service.Name},
  435. Subsets: []v1.EndpointSubset{{
  436. Addresses: []v1.EndpointAddress{{IP: "127.0.0.1"}},
  437. Ports: []v1.EndpointPort{{Name: "p", Port: udpServerPort}},
  438. }},
  439. })
  440. fexec := makeFakeExec()
  441. p, err := createProxier(lb, net.ParseIP("0.0.0.0"), ipttest.NewFake(), fexec, net.ParseIP("127.0.0.1"), nil, time.Minute, time.Second, udpIdleTimeoutForTest, newProxySocket)
  442. if err != nil {
  443. t.Fatal(err)
  444. }
  445. startProxier(p, t)
  446. defer p.shutdown()
  447. svcInfo, err := p.addServiceOnPort(service, "UDP", 0, time.Second)
  448. if err != nil {
  449. t.Fatalf("error adding new service: %#v", err)
  450. }
  451. conn, err := net.Dial("udp", joinHostPort("", svcInfo.proxyPort))
  452. if err != nil {
  453. t.Fatalf("error connecting to proxy: %v", err)
  454. }
  455. conn.Close()
  456. waitForNumProxyLoops(t, p, 1)
  457. stopProxyByName(p, service)
  458. // Wait for the port to really close.
  459. if err := waitForClosedPortUDP(p, svcInfo.proxyPort); err != nil {
  460. t.Fatalf(err.Error())
  461. }
  462. waitForNumProxyLoops(t, p, 0)
  463. }
  464. func TestTCPProxyUpdateDelete(t *testing.T) {
  465. lb := NewLoadBalancerRR()
  466. servicePortName := proxy.ServicePortName{NamespacedName: types.NamespacedName{Namespace: "testnamespace", Name: "echo"}, Port: "p"}
  467. lb.OnEndpointsAdd(&v1.Endpoints{
  468. ObjectMeta: metav1.ObjectMeta{Namespace: servicePortName.Namespace, Name: servicePortName.Name},
  469. Subsets: []v1.EndpointSubset{{
  470. Addresses: []v1.EndpointAddress{{IP: "127.0.0.1"}},
  471. Ports: []v1.EndpointPort{{Name: "p", Port: tcpServerPort}},
  472. }},
  473. })
  474. fexec := makeFakeExec()
  475. p, err := createProxier(lb, net.ParseIP("0.0.0.0"), ipttest.NewFake(), fexec, net.ParseIP("127.0.0.1"), nil, time.Minute, time.Second, udpIdleTimeoutForTest, newProxySocket)
  476. if err != nil {
  477. t.Fatal(err)
  478. }
  479. startProxier(p, t)
  480. defer p.shutdown()
  481. service := &v1.Service{
  482. ObjectMeta: metav1.ObjectMeta{Name: servicePortName.Name, Namespace: servicePortName.Namespace},
  483. Spec: v1.ServiceSpec{ClusterIP: "1.2.3.4", Ports: []v1.ServicePort{{
  484. Name: "p",
  485. Port: 9997,
  486. Protocol: "TCP",
  487. }}},
  488. }
  489. p.OnServiceAdd(service)
  490. waitForNumProxyLoops(t, p, 1)
  491. p.OnServiceDelete(service)
  492. if err := waitForClosedPortTCP(p, int(service.Spec.Ports[0].Port)); err != nil {
  493. t.Fatalf(err.Error())
  494. }
  495. waitForNumProxyLoops(t, p, 0)
  496. }
  497. func TestUDPProxyUpdateDelete(t *testing.T) {
  498. lb := NewLoadBalancerRR()
  499. service := proxy.ServicePortName{NamespacedName: types.NamespacedName{Namespace: "testnamespace", Name: "echo"}, Port: "p"}
  500. lb.OnEndpointsAdd(&v1.Endpoints{
  501. ObjectMeta: metav1.ObjectMeta{Namespace: service.Namespace, Name: service.Name},
  502. Subsets: []v1.EndpointSubset{{
  503. Addresses: []v1.EndpointAddress{{IP: "127.0.0.1"}},
  504. Ports: []v1.EndpointPort{{Name: "p", Port: udpServerPort}},
  505. }},
  506. })
  507. fexec := makeFakeExec()
  508. p, err := createProxier(lb, net.ParseIP("0.0.0.0"), ipttest.NewFake(), fexec, net.ParseIP("127.0.0.1"), nil, time.Minute, time.Second, udpIdleTimeoutForTest, newProxySocket)
  509. if err != nil {
  510. t.Fatal(err)
  511. }
  512. startProxier(p, t)
  513. defer p.shutdown()
  514. svcInfo, err := p.addServiceOnPort(service, "UDP", 0, time.Second)
  515. if err != nil {
  516. t.Fatalf("error adding new service: %#v", err)
  517. }
  518. conn, err := net.Dial("udp", joinHostPort("", svcInfo.proxyPort))
  519. if err != nil {
  520. t.Fatalf("error connecting to proxy: %v", err)
  521. }
  522. conn.Close()
  523. waitForNumProxyLoops(t, p, 1)
  524. p.OnServiceDelete(&v1.Service{
  525. ObjectMeta: metav1.ObjectMeta{Name: service.Name, Namespace: service.Namespace},
  526. Spec: v1.ServiceSpec{ClusterIP: "1.2.3.4", Ports: []v1.ServicePort{{
  527. Name: "p",
  528. Port: int32(svcInfo.proxyPort),
  529. Protocol: "UDP",
  530. }}},
  531. })
  532. if err := waitForClosedPortUDP(p, svcInfo.proxyPort); err != nil {
  533. t.Fatalf(err.Error())
  534. }
  535. waitForNumProxyLoops(t, p, 0)
  536. }
  537. func TestTCPProxyUpdateDeleteUpdate(t *testing.T) {
  538. lb := NewLoadBalancerRR()
  539. service := proxy.ServicePortName{NamespacedName: types.NamespacedName{Namespace: "testnamespace", Name: "echo"}, Port: "p"}
  540. endpoint := &v1.Endpoints{
  541. ObjectMeta: metav1.ObjectMeta{Name: service.Name, Namespace: service.Namespace},
  542. Subsets: []v1.EndpointSubset{{
  543. Addresses: []v1.EndpointAddress{{IP: "127.0.0.1"}},
  544. Ports: []v1.EndpointPort{{Name: "p", Port: tcpServerPort}},
  545. }},
  546. }
  547. lb.OnEndpointsAdd(endpoint)
  548. fexec := makeFakeExec()
  549. p, err := createProxier(lb, net.ParseIP("0.0.0.0"), ipttest.NewFake(), fexec, net.ParseIP("127.0.0.1"), nil, time.Minute, time.Second, udpIdleTimeoutForTest, newProxySocket)
  550. if err != nil {
  551. t.Fatal(err)
  552. }
  553. startProxier(p, t)
  554. defer p.shutdown()
  555. svcInfo, err := p.addServiceOnPort(service, "TCP", 0, time.Second)
  556. if err != nil {
  557. t.Fatalf("error adding new service: %#v", err)
  558. }
  559. conn, err := net.Dial("tcp", joinHostPort("", svcInfo.proxyPort))
  560. if err != nil {
  561. t.Fatalf("error connecting to proxy: %v", err)
  562. }
  563. conn.Close()
  564. waitForNumProxyLoops(t, p, 1)
  565. p.OnServiceDelete(&v1.Service{
  566. ObjectMeta: metav1.ObjectMeta{Name: service.Name, Namespace: service.Namespace},
  567. Spec: v1.ServiceSpec{ClusterIP: "1.2.3.4", Ports: []v1.ServicePort{{
  568. Name: "p",
  569. Port: int32(svcInfo.proxyPort),
  570. Protocol: "TCP",
  571. }}},
  572. })
  573. if err := waitForClosedPortTCP(p, svcInfo.proxyPort); err != nil {
  574. t.Fatalf(err.Error())
  575. }
  576. waitForNumProxyLoops(t, p, 0)
  577. // need to add endpoint here because it got clean up during service delete
  578. lb.OnEndpointsAdd(endpoint)
  579. p.OnServiceAdd(&v1.Service{
  580. ObjectMeta: metav1.ObjectMeta{Name: service.Name, Namespace: service.Namespace},
  581. Spec: v1.ServiceSpec{ClusterIP: "1.2.3.4", Ports: []v1.ServicePort{{
  582. Name: "p",
  583. Port: int32(svcInfo.proxyPort),
  584. Protocol: "TCP",
  585. }}},
  586. })
  587. svcInfo, exists := waitForServiceInfo(p, service)
  588. if !exists {
  589. t.Fatalf("can't find serviceInfo for %s", service)
  590. }
  591. testEchoTCP(t, "127.0.0.1", svcInfo.proxyPort)
  592. waitForNumProxyLoops(t, p, 1)
  593. }
  594. func TestUDPProxyUpdateDeleteUpdate(t *testing.T) {
  595. lb := NewLoadBalancerRR()
  596. service := proxy.ServicePortName{NamespacedName: types.NamespacedName{Namespace: "testnamespace", Name: "echo"}, Port: "p"}
  597. endpoint := &v1.Endpoints{
  598. ObjectMeta: metav1.ObjectMeta{Name: service.Name, Namespace: service.Namespace},
  599. Subsets: []v1.EndpointSubset{{
  600. Addresses: []v1.EndpointAddress{{IP: "127.0.0.1"}},
  601. Ports: []v1.EndpointPort{{Name: "p", Port: udpServerPort}},
  602. }},
  603. }
  604. lb.OnEndpointsAdd(endpoint)
  605. fexec := makeFakeExec()
  606. p, err := createProxier(lb, net.ParseIP("0.0.0.0"), ipttest.NewFake(), fexec, net.ParseIP("127.0.0.1"), nil, time.Minute, time.Second, udpIdleTimeoutForTest, newProxySocket)
  607. if err != nil {
  608. t.Fatal(err)
  609. }
  610. startProxier(p, t)
  611. defer p.shutdown()
  612. svcInfo, err := p.addServiceOnPort(service, "UDP", 0, time.Second)
  613. if err != nil {
  614. t.Fatalf("error adding new service: %#v", err)
  615. }
  616. conn, err := net.Dial("udp", joinHostPort("", svcInfo.proxyPort))
  617. if err != nil {
  618. t.Fatalf("error connecting to proxy: %v", err)
  619. }
  620. conn.Close()
  621. waitForNumProxyLoops(t, p, 1)
  622. p.OnServiceDelete(&v1.Service{
  623. ObjectMeta: metav1.ObjectMeta{Name: service.Name, Namespace: service.Namespace},
  624. Spec: v1.ServiceSpec{ClusterIP: "1.2.3.4", Ports: []v1.ServicePort{{
  625. Name: "p",
  626. Port: int32(svcInfo.proxyPort),
  627. Protocol: "UDP",
  628. }}},
  629. })
  630. if err := waitForClosedPortUDP(p, svcInfo.proxyPort); err != nil {
  631. t.Fatalf(err.Error())
  632. }
  633. waitForNumProxyLoops(t, p, 0)
  634. // need to add endpoint here because it got clean up during service delete
  635. lb.OnEndpointsAdd(endpoint)
  636. p.OnServiceAdd(&v1.Service{
  637. ObjectMeta: metav1.ObjectMeta{Name: service.Name, Namespace: service.Namespace},
  638. Spec: v1.ServiceSpec{ClusterIP: "1.2.3.4", Ports: []v1.ServicePort{{
  639. Name: "p",
  640. Port: int32(svcInfo.proxyPort),
  641. Protocol: "UDP",
  642. }}},
  643. })
  644. svcInfo, exists := waitForServiceInfo(p, service)
  645. if !exists {
  646. t.Fatalf("can't find serviceInfo")
  647. }
  648. testEchoUDP(t, "127.0.0.1", svcInfo.proxyPort)
  649. waitForNumProxyLoops(t, p, 1)
  650. }
  651. func TestTCPProxyUpdatePort(t *testing.T) {
  652. lb := NewLoadBalancerRR()
  653. service := proxy.ServicePortName{NamespacedName: types.NamespacedName{Namespace: "testnamespace", Name: "echo"}, Port: "p"}
  654. lb.OnEndpointsAdd(&v1.Endpoints{
  655. ObjectMeta: metav1.ObjectMeta{Name: service.Name, Namespace: service.Namespace},
  656. Subsets: []v1.EndpointSubset{{
  657. Addresses: []v1.EndpointAddress{{IP: "127.0.0.1"}},
  658. Ports: []v1.EndpointPort{{Name: "p", Port: tcpServerPort}},
  659. }},
  660. })
  661. fexec := makeFakeExec()
  662. p, err := createProxier(lb, net.ParseIP("0.0.0.0"), ipttest.NewFake(), fexec, net.ParseIP("127.0.0.1"), nil, time.Minute, time.Second, udpIdleTimeoutForTest, newProxySocket)
  663. if err != nil {
  664. t.Fatal(err)
  665. }
  666. startProxier(p, t)
  667. defer p.shutdown()
  668. svcInfo, err := p.addServiceOnPort(service, "TCP", 0, time.Second)
  669. if err != nil {
  670. t.Fatalf("error adding new service: %#v", err)
  671. }
  672. testEchoTCP(t, "127.0.0.1", svcInfo.proxyPort)
  673. waitForNumProxyLoops(t, p, 1)
  674. p.OnServiceAdd(&v1.Service{
  675. ObjectMeta: metav1.ObjectMeta{Name: service.Name, Namespace: service.Namespace},
  676. Spec: v1.ServiceSpec{ClusterIP: "1.2.3.4", Ports: []v1.ServicePort{{
  677. Name: "p",
  678. Port: 99,
  679. Protocol: "TCP",
  680. }}},
  681. })
  682. // Wait for the socket to actually get free.
  683. if err := waitForClosedPortTCP(p, svcInfo.proxyPort); err != nil {
  684. t.Fatalf(err.Error())
  685. }
  686. svcInfo, exists := waitForServiceInfo(p, service)
  687. if !exists {
  688. t.Fatalf("can't find serviceInfo")
  689. }
  690. testEchoTCP(t, "127.0.0.1", svcInfo.proxyPort)
  691. // This is a bit async, but this should be sufficient.
  692. time.Sleep(500 * time.Millisecond)
  693. waitForNumProxyLoops(t, p, 1)
  694. }
  695. func TestUDPProxyUpdatePort(t *testing.T) {
  696. lb := NewLoadBalancerRR()
  697. service := proxy.ServicePortName{NamespacedName: types.NamespacedName{Namespace: "testnamespace", Name: "echo"}, Port: "p"}
  698. lb.OnEndpointsAdd(&v1.Endpoints{
  699. ObjectMeta: metav1.ObjectMeta{Name: service.Name, Namespace: service.Namespace},
  700. Subsets: []v1.EndpointSubset{{
  701. Addresses: []v1.EndpointAddress{{IP: "127.0.0.1"}},
  702. Ports: []v1.EndpointPort{{Name: "p", Port: udpServerPort}},
  703. }},
  704. })
  705. fexec := makeFakeExec()
  706. p, err := createProxier(lb, net.ParseIP("0.0.0.0"), ipttest.NewFake(), fexec, net.ParseIP("127.0.0.1"), nil, time.Minute, time.Second, udpIdleTimeoutForTest, newProxySocket)
  707. if err != nil {
  708. t.Fatal(err)
  709. }
  710. startProxier(p, t)
  711. defer p.shutdown()
  712. svcInfo, err := p.addServiceOnPort(service, "UDP", 0, time.Second)
  713. if err != nil {
  714. t.Fatalf("error adding new service: %#v", err)
  715. }
  716. waitForNumProxyLoops(t, p, 1)
  717. p.OnServiceAdd(&v1.Service{
  718. ObjectMeta: metav1.ObjectMeta{Name: service.Name, Namespace: service.Namespace},
  719. Spec: v1.ServiceSpec{ClusterIP: "1.2.3.4", Ports: []v1.ServicePort{{
  720. Name: "p",
  721. Port: 99,
  722. Protocol: "UDP",
  723. }}},
  724. })
  725. // Wait for the socket to actually get free.
  726. if err := waitForClosedPortUDP(p, svcInfo.proxyPort); err != nil {
  727. t.Fatalf(err.Error())
  728. }
  729. svcInfo, exists := waitForServiceInfo(p, service)
  730. if !exists {
  731. t.Fatalf("can't find serviceInfo")
  732. }
  733. testEchoUDP(t, "127.0.0.1", svcInfo.proxyPort)
  734. waitForNumProxyLoops(t, p, 1)
  735. }
  736. func TestProxyUpdatePublicIPs(t *testing.T) {
  737. lb := NewLoadBalancerRR()
  738. service := proxy.ServicePortName{NamespacedName: types.NamespacedName{Namespace: "testnamespace", Name: "echo"}, Port: "p"}
  739. lb.OnEndpointsAdd(&v1.Endpoints{
  740. ObjectMeta: metav1.ObjectMeta{Name: service.Name, Namespace: service.Namespace},
  741. Subsets: []v1.EndpointSubset{{
  742. Addresses: []v1.EndpointAddress{{IP: "127.0.0.1"}},
  743. Ports: []v1.EndpointPort{{Name: "p", Port: tcpServerPort}},
  744. }},
  745. })
  746. fexec := makeFakeExec()
  747. p, err := createProxier(lb, net.ParseIP("0.0.0.0"), ipttest.NewFake(), fexec, net.ParseIP("127.0.0.1"), nil, time.Minute, time.Second, udpIdleTimeoutForTest, newProxySocket)
  748. if err != nil {
  749. t.Fatal(err)
  750. }
  751. startProxier(p, t)
  752. defer p.shutdown()
  753. svcInfo, err := p.addServiceOnPort(service, "TCP", 0, time.Second)
  754. if err != nil {
  755. t.Fatalf("error adding new service: %#v", err)
  756. }
  757. testEchoTCP(t, "127.0.0.1", svcInfo.proxyPort)
  758. waitForNumProxyLoops(t, p, 1)
  759. p.OnServiceAdd(&v1.Service{
  760. ObjectMeta: metav1.ObjectMeta{Name: service.Name, Namespace: service.Namespace},
  761. Spec: v1.ServiceSpec{
  762. Ports: []v1.ServicePort{{
  763. Name: "p",
  764. Port: int32(svcInfo.portal.port),
  765. Protocol: "TCP",
  766. }},
  767. ClusterIP: svcInfo.portal.ip.String(),
  768. ExternalIPs: []string{"4.3.2.1"},
  769. },
  770. })
  771. // Wait for the socket to actually get free.
  772. if err := waitForClosedPortTCP(p, svcInfo.proxyPort); err != nil {
  773. t.Fatalf(err.Error())
  774. }
  775. svcInfo, exists := waitForServiceInfo(p, service)
  776. if !exists {
  777. t.Fatalf("can't find serviceInfo")
  778. }
  779. testEchoTCP(t, "127.0.0.1", svcInfo.proxyPort)
  780. // This is a bit async, but this should be sufficient.
  781. time.Sleep(500 * time.Millisecond)
  782. waitForNumProxyLoops(t, p, 1)
  783. }
  784. func TestProxyUpdatePortal(t *testing.T) {
  785. lb := NewLoadBalancerRR()
  786. service := proxy.ServicePortName{NamespacedName: types.NamespacedName{Namespace: "testnamespace", Name: "echo"}, Port: "p"}
  787. endpoint := &v1.Endpoints{
  788. ObjectMeta: metav1.ObjectMeta{Name: service.Name, Namespace: service.Namespace},
  789. Subsets: []v1.EndpointSubset{{
  790. Addresses: []v1.EndpointAddress{{IP: "127.0.0.1"}},
  791. Ports: []v1.EndpointPort{{Name: "p", Port: tcpServerPort}},
  792. }},
  793. }
  794. lb.OnEndpointsAdd(endpoint)
  795. fexec := makeFakeExec()
  796. p, err := createProxier(lb, net.ParseIP("0.0.0.0"), ipttest.NewFake(), fexec, net.ParseIP("127.0.0.1"), nil, time.Minute, time.Second, udpIdleTimeoutForTest, newProxySocket)
  797. if err != nil {
  798. t.Fatal(err)
  799. }
  800. startProxier(p, t)
  801. defer p.shutdown()
  802. svcInfo, err := p.addServiceOnPort(service, "TCP", 0, time.Second)
  803. if err != nil {
  804. t.Fatalf("error adding new service: %#v", err)
  805. }
  806. testEchoTCP(t, "127.0.0.1", svcInfo.proxyPort)
  807. waitForNumProxyLoops(t, p, 1)
  808. svcv0 := &v1.Service{
  809. ObjectMeta: metav1.ObjectMeta{Name: service.Name, Namespace: service.Namespace},
  810. Spec: v1.ServiceSpec{ClusterIP: "1.2.3.4", Ports: []v1.ServicePort{{
  811. Name: "p",
  812. Port: int32(svcInfo.proxyPort),
  813. Protocol: "TCP",
  814. }}},
  815. }
  816. svcv1 := &v1.Service{
  817. ObjectMeta: metav1.ObjectMeta{Name: service.Name, Namespace: service.Namespace},
  818. Spec: v1.ServiceSpec{ClusterIP: "", Ports: []v1.ServicePort{{
  819. Name: "p",
  820. Port: int32(svcInfo.proxyPort),
  821. Protocol: "TCP",
  822. }}},
  823. }
  824. p.OnServiceUpdate(svcv0, svcv1)
  825. // Wait for the service to be removed because it had an empty ClusterIP
  826. var exists bool
  827. for i := 0; i < 50; i++ {
  828. _, exists = p.getServiceInfo(service)
  829. if !exists {
  830. break
  831. }
  832. time.Sleep(50 * time.Millisecond)
  833. }
  834. if exists {
  835. t.Fatalf("service with empty ClusterIP should not be included in the proxy")
  836. }
  837. svcv2 := &v1.Service{
  838. ObjectMeta: metav1.ObjectMeta{Name: service.Name, Namespace: service.Namespace},
  839. Spec: v1.ServiceSpec{ClusterIP: "None", Ports: []v1.ServicePort{{
  840. Name: "p",
  841. Port: int32(svcInfo.proxyPort),
  842. Protocol: "TCP",
  843. }}},
  844. }
  845. p.OnServiceUpdate(svcv1, svcv2)
  846. _, exists = p.getServiceInfo(service)
  847. if exists {
  848. t.Fatalf("service with 'None' as ClusterIP should not be included in the proxy")
  849. }
  850. svcv3 := &v1.Service{
  851. ObjectMeta: metav1.ObjectMeta{Name: service.Name, Namespace: service.Namespace},
  852. Spec: v1.ServiceSpec{ClusterIP: "1.2.3.4", Ports: []v1.ServicePort{{
  853. Name: "p",
  854. Port: int32(svcInfo.proxyPort),
  855. Protocol: "TCP",
  856. }}},
  857. }
  858. p.OnServiceUpdate(svcv2, svcv3)
  859. lb.OnEndpointsAdd(endpoint)
  860. svcInfo, exists = waitForServiceInfo(p, service)
  861. if !exists {
  862. t.Fatalf("service with ClusterIP set not found in the proxy")
  863. }
  864. testEchoTCP(t, "127.0.0.1", svcInfo.proxyPort)
  865. waitForNumProxyLoops(t, p, 1)
  866. }
  867. type fakeRunner struct{}
  868. // assert fakeAsyncRunner is a ProxyProvider
  869. var _ asyncRunnerInterface = &fakeRunner{}
  870. func (f fakeRunner) Run() {
  871. }
  872. func (f fakeRunner) Loop(stop <-chan struct{}) {
  873. }
  874. func TestOnServiceAddChangeMap(t *testing.T) {
  875. fexec := makeFakeExec()
  876. // Use long minSyncPeriod so we can test that immediate syncs work
  877. p, err := createProxier(NewLoadBalancerRR(), net.ParseIP("0.0.0.0"), ipttest.NewFake(), fexec, net.ParseIP("127.0.0.1"), nil, time.Minute, time.Minute, udpIdleTimeoutForTest, newProxySocket)
  878. if err != nil {
  879. t.Fatal(err)
  880. }
  881. // Fake out sync runner
  882. p.syncRunner = fakeRunner{}
  883. serviceMeta := metav1.ObjectMeta{Namespace: "testnamespace", Name: "testname"}
  884. service := &v1.Service{
  885. ObjectMeta: serviceMeta,
  886. Spec: v1.ServiceSpec{ClusterIP: "1.2.3.4", Ports: []v1.ServicePort{{
  887. Name: "p",
  888. Port: 99,
  889. Protocol: "TCP",
  890. }}},
  891. }
  892. serviceUpdate := &v1.Service{
  893. ObjectMeta: serviceMeta,
  894. Spec: v1.ServiceSpec{ClusterIP: "1.2.3.5", Ports: []v1.ServicePort{{
  895. Name: "p",
  896. Port: 100,
  897. Protocol: "TCP",
  898. }}},
  899. }
  900. serviceUpdate2 := &v1.Service{
  901. ObjectMeta: serviceMeta,
  902. Spec: v1.ServiceSpec{ClusterIP: "1.2.3.6", Ports: []v1.ServicePort{{
  903. Name: "p",
  904. Port: 101,
  905. Protocol: "TCP",
  906. }}},
  907. }
  908. type onServiceTest struct {
  909. detail string
  910. changes []serviceChange
  911. expectedChange *serviceChange
  912. }
  913. tests := []onServiceTest{
  914. {
  915. detail: "add",
  916. changes: []serviceChange{
  917. {current: service},
  918. },
  919. expectedChange: &serviceChange{
  920. current: service,
  921. },
  922. },
  923. {
  924. detail: "add+update=add",
  925. changes: []serviceChange{
  926. {current: service},
  927. {
  928. previous: service,
  929. current: serviceUpdate,
  930. },
  931. },
  932. expectedChange: &serviceChange{
  933. current: serviceUpdate,
  934. },
  935. },
  936. {
  937. detail: "add+del=none",
  938. changes: []serviceChange{
  939. {current: service},
  940. {previous: service},
  941. },
  942. },
  943. {
  944. detail: "update+update=update",
  945. changes: []serviceChange{
  946. {
  947. previous: service,
  948. current: serviceUpdate,
  949. },
  950. {
  951. previous: serviceUpdate,
  952. current: serviceUpdate2,
  953. },
  954. },
  955. expectedChange: &serviceChange{
  956. previous: service,
  957. current: serviceUpdate2,
  958. },
  959. },
  960. {
  961. detail: "update+del=del",
  962. changes: []serviceChange{
  963. {
  964. previous: service,
  965. current: serviceUpdate,
  966. },
  967. {previous: serviceUpdate},
  968. },
  969. // change collapsing always keeps the oldest service
  970. // info since correct unmerging depends on the least
  971. // recent update, not the most current.
  972. expectedChange: &serviceChange{
  973. previous: service,
  974. },
  975. },
  976. {
  977. detail: "del+add=update",
  978. changes: []serviceChange{
  979. {previous: service},
  980. {current: serviceUpdate},
  981. },
  982. expectedChange: &serviceChange{
  983. previous: service,
  984. current: serviceUpdate,
  985. },
  986. },
  987. }
  988. for _, test := range tests {
  989. for _, change := range test.changes {
  990. p.serviceChange(change.previous, change.current, test.detail)
  991. }
  992. if test.expectedChange != nil {
  993. if len(p.serviceChanges) != 1 {
  994. t.Fatalf("[%s] expected 1 service change but found %d", test.detail, len(p.serviceChanges))
  995. }
  996. expectedService := test.expectedChange.current
  997. if expectedService == nil {
  998. expectedService = test.expectedChange.previous
  999. }
  1000. svcName := types.NamespacedName{Namespace: expectedService.Namespace, Name: expectedService.Name}
  1001. change, ok := p.serviceChanges[svcName]
  1002. if !ok {
  1003. t.Fatalf("[%s] did not find service change for %v", test.detail, svcName)
  1004. }
  1005. if !reflect.DeepEqual(change.previous, test.expectedChange.previous) {
  1006. t.Fatalf("[%s] change previous service and expected previous service don't match\nchange: %+v\nexp: %+v", test.detail, change.previous, test.expectedChange.previous)
  1007. }
  1008. if !reflect.DeepEqual(change.current, test.expectedChange.current) {
  1009. t.Fatalf("[%s] change current service and expected current service don't match\nchange: %+v\nexp: %+v", test.detail, change.current, test.expectedChange.current)
  1010. }
  1011. } else {
  1012. if len(p.serviceChanges) != 0 {
  1013. t.Fatalf("[%s] expected no service changes but found %d", test.detail, len(p.serviceChanges))
  1014. }
  1015. }
  1016. }
  1017. }
  1018. func makeFakeExec() *fakeexec.FakeExec {
  1019. fcmd := fakeexec.FakeCmd{
  1020. CombinedOutputScript: []fakeexec.FakeCombinedOutputAction{
  1021. func() ([]byte, error) { return []byte("1 flow entries have been deleted"), nil },
  1022. },
  1023. }
  1024. return &fakeexec.FakeExec{
  1025. CommandScript: []fakeexec.FakeCommandAction{
  1026. func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
  1027. },
  1028. LookPathFunc: func(cmd string) (string, error) { return cmd, nil },
  1029. }
  1030. }
  1031. // TODO(justinsb): Add test for nodePort conflict detection, once we have nodePort wired in