From 78c20ca6595ccbbd50b8f710891af07553feadce Mon Sep 17 00:00:00 2001 From: Miguel Duarte Barroso Date: Mon, 28 Oct 2024 13:42:11 +0100 Subject: [PATCH] localnet, e2e: use a golang http server instead of a python process Signed-off-by: Miguel Duarte Barroso --- test/e2e/multihoming.go | 54 ++++++++++++++++++++++++++++++++--------- 1 file changed, 43 insertions(+), 11 deletions(-) diff --git a/test/e2e/multihoming.go b/test/e2e/multihoming.go index 45ddf1e195..9ee71b1b9c 100644 --- a/test/e2e/multihoming.go +++ b/test/e2e/multihoming.go @@ -2,10 +2,11 @@ package e2e import ( "context" + goerrors "errors" "fmt" + "net/http" "os" "os/exec" - "strconv" "strings" "time" @@ -654,7 +655,7 @@ var _ = Describe("Multi Homing", func() { var netConfig networkAttachmentConfig var nodes []v1.Pod var underlayBridgeName string - var cmdWebServer *exec.Cmd + var httpSrv *httpServer underlayIP := underlayServiceIP + "/24" Context("with a service running on the underlay", func() { @@ -694,9 +695,8 @@ var _ = Describe("Multi Homing", func() { BeforeEach(func() { By("starting a service, connected to the underlay") - cmdWebServer = exec.Command("python3", "-m", "http.server", "--bind", underlayServiceIP, strconv.Itoa(servicePort)) - cmdWebServer.Stderr = os.Stderr - Expect(cmdWebServer.Start()).NotTo(HaveOccurred(), "failed to create web server, port might be busy") + httpSrv = newHTTPServer(underlayServiceIP, servicePort) + Expect(httpSrv.listen()).To(Succeed()) }) BeforeEach(func() { @@ -710,8 +710,7 @@ var _ = Describe("Multi Homing", func() { }) AfterEach(func() { - err := cmdWebServer.Process.Kill() - Expect(err).NotTo(HaveOccurred()) + Expect(httpSrv.stop()).To(Succeed()) }) AfterEach(func() { @@ -938,13 +937,12 @@ var _ = Describe("Multi Homing", func() { ) By("starting a service, connected to the underlay") - cmdWebServer = exec.Command("python3", "-m", "http.server", "--bind", underlayServiceIP, strconv.Itoa(port)) - cmdWebServer.Stderr = os.Stderr - Expect(cmdWebServer.Start()).NotTo(HaveOccurred(), "failed to create web server, port might be busy") + httpSrv = newHTTPServer(underlayServiceIP, servicePort) + Expect(httpSrv.listen()).To(Succeed()) }) AfterEach(func() { - Expect(cmdWebServer.Process.Kill()).NotTo(HaveOccurred(), "kill the python webserver") + Expect(httpSrv.stop()).To(Succeed()) Expect(vlanIface.delete()).NotTo(HaveOccurred(), "remove the underlay physical configuration") Expect(teardownUnderlay(nodes)).To(Succeed(), "tear down the localnet underlay") }) @@ -1746,3 +1744,37 @@ func createMultiNetworkPolicy(mnpClient mnpclient.K8sCniCncfIoV1beta1Interface, ) return err } + +type httpServer struct { + srv *http.Server +} + +func newHTTPServer(ip string, port int) *httpServer { + mux := http.NewServeMux() + mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + }) + return &httpServer{ + srv: &http.Server{ + Addr: fmt.Sprintf("%s:%d", ip, port), + Handler: mux, + }, + } +} + +func (hs *httpServer) listen() error { + var err error + go func() { + err = hs.srv.ListenAndServe() + if err != nil && !goerrors.Is(err, http.ErrServerClosed) { + _, _ = fmt.Fprintf(os.Stderr, "error shutting down http server: %v\n", err) + } + }() + return err +} + +func (hs *httpServer) stop() error { + ctx, cancelFn := context.WithTimeout(context.Background(), 5*time.Second) + defer cancelFn() + return hs.srv.Shutdown(ctx) +}