Skip to content

Commit

Permalink
localnet, e2e: use a golang http server instead of a python process
Browse files Browse the repository at this point in the history
Signed-off-by: Miguel Duarte Barroso <mdbarroso@redhat.com>
  • Loading branch information
maiqueb committed Oct 29, 2024
1 parent 2eb0107 commit 78c20ca
Showing 1 changed file with 43 additions and 11 deletions.
54 changes: 43 additions & 11 deletions test/e2e/multihoming.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package e2e

import (
"context"
goerrors "errors"
"fmt"
"net/http"
"os"
"os/exec"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand All @@ -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() {
Expand Down Expand Up @@ -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")
})
Expand Down Expand Up @@ -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)
}

0 comments on commit 78c20ca

Please sign in to comment.