Skip to content

Commit 70dbe91

Browse files
kola: iso.*: don't leak http server
1 parent 5f29d33 commit 70dbe91

2 files changed

Lines changed: 45 additions & 23 deletions

File tree

mantle/kola/tests/iso/live-iso.go

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package iso
22

33
import (
4+
"context"
45
_ "embed"
56
"fmt"
7+
"log"
68
"net"
79
"net/http"
810
"os"
@@ -203,17 +205,6 @@ func isoLiveInstall(c cluster.TestCluster, opts IsoTestOpts) {
203205
c.Fatal("Cannot use `--add-nm-keyfile` with offline mode")
204206
}
205207

206-
qc, ok := c.Cluster.(*qemu.Cluster)
207-
if !ok {
208-
c.Fatalf("Unsupported cluster type")
209-
}
210-
if opts.enable4k {
211-
qc.EnforceNative4k()
212-
}
213-
if opts.enableMultipath {
214-
qc.EnforceMultipath()
215-
}
216-
217208
tempdir, err := os.MkdirTemp("/var/tmp", "iso")
218209
if err != nil {
219210
c.Fatal(err)
@@ -222,12 +213,22 @@ func isoLiveInstall(c cluster.TestCluster, opts IsoTestOpts) {
222213
os.RemoveAll(tempdir)
223214
}()
224215

225-
if err := isoRunTest(qc, opts, tempdir); err != nil {
216+
if err := isoRunTest(c, opts, tempdir); err != nil {
226217
c.Fatal(err)
227218
}
228219
}
229220

230-
func isoRunTest(qc *qemu.Cluster, opts IsoTestOpts, tempdir string) error {
221+
func isoRunTest(c cluster.TestCluster, opts IsoTestOpts, tempdir string) error {
222+
qc, ok := c.Cluster.(*qemu.Cluster)
223+
if !ok {
224+
return errors.Errorf("Unsupported cluster type")
225+
}
226+
if opts.enable4k {
227+
qc.EnforceNative4k()
228+
}
229+
if opts.enableMultipath {
230+
qc.EnforceMultipath()
231+
}
231232
targetConfig, err := conf.EmptyIgnition().Render(conf.FailWarnings)
232233
if err != nil {
233234
return err
@@ -315,11 +316,21 @@ func isoRunTest(qc *qemu.Cluster, opts IsoTestOpts, tempdir string) error {
315316
targetConfig.AddConfigSource(baseurl + "/target.ign")
316317
serializedTargetConfig = targetConfig.String()
317318

318-
//nolint // Yeah this leaks
319+
ctx := c.Context()
320+
mux := http.NewServeMux()
321+
mux.Handle("/", http.FileServer(http.Dir(tempdir)))
322+
srv := &http.Server{Handler: mux}
323+
324+
go func() {
325+
if err := srv.Serve(listener); err != nil && !errors.Is(err, http.ErrServerClosed) {
326+
log.Printf("http serve: %v", err)
327+
}
328+
}()
329+
330+
// stop server when ctx is canceled
319331
go func() {
320-
mux := http.NewServeMux()
321-
mux.Handle("/", http.FileServer(http.Dir(tempdir)))
322-
http.Serve(listener, mux)
332+
<-ctx.Done()
333+
_ = srv.Shutdown(context.Background())
323334
}()
324335
}
325336

mantle/kola/tests/iso/live-pxe.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package iso
22

33
import (
4+
"context"
45
"fmt"
6+
"log"
57
"net"
68
"net/http"
79
"os"
@@ -197,7 +199,7 @@ func testPXE(c cluster.TestCluster, opts IsoTestOpts) {
197199
os.RemoveAll(tempdir)
198200
}()
199201

200-
pxe, err := createPXE(tempdir, opts)
202+
pxe, err := createPXE(c.Context(), tempdir, opts)
201203
if err != nil {
202204
c.Fatal(errors.Wrapf(err, "setting up install"))
203205
}
@@ -326,7 +328,7 @@ type PXE struct {
326328
bootfile string
327329
}
328330

329-
func createPXE(tempdir string, opts IsoTestOpts) (*PXE, error) {
331+
func createPXE(ctx context.Context, tempdir string, opts IsoTestOpts) (*PXE, error) {
330332
kernel := kola.CosaBuild.Meta.BuildArtifacts.LiveKernel.Path
331333
initramfs := kola.CosaBuild.Meta.BuildArtifacts.LiveInitramfs.Path
332334
rootfs := kola.CosaBuild.Meta.BuildArtifacts.LiveRootfs.Path
@@ -403,11 +405,20 @@ func createPXE(tempdir string, opts IsoTestOpts) (*PXE, error) {
403405
return nil, errors.Errorf("Unhandled boottype %s", pxe.boottype)
404406
}
405407

406-
//nolint // Yeah this leaks
408+
mux := http.NewServeMux()
409+
mux.Handle("/", http.FileServer(http.Dir(tftpdir)))
410+
srv := &http.Server{Handler: mux}
411+
412+
go func() {
413+
if err := srv.Serve(listener); err != nil && !errors.Is(err, http.ErrServerClosed) {
414+
log.Printf("http serve: %v", err)
415+
}
416+
}()
417+
418+
// stop server when ctx is canceled
407419
go func() {
408-
mux := http.NewServeMux()
409-
mux.Handle("/", http.FileServer(http.Dir(tftpdir)))
410-
http.Serve(listener, mux)
420+
<-ctx.Done()
421+
_ = srv.Shutdown(context.Background())
411422
}()
412423

413424
return pxe, nil

0 commit comments

Comments
 (0)