Skip to content

Commit d3fd256

Browse files
kola: iso.*: don't leak http server
1 parent 4c171d3 commit d3fd256

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
keys, err := qc.Keys()
232233
if err != nil {
233234
return err
@@ -320,11 +321,21 @@ func isoRunTest(qc *qemu.Cluster, opts IsoTestOpts, tempdir string) error {
320321
targetConfig.AddConfigSource(baseurl + "/target.ign")
321322
serializedTargetConfig = targetConfig.String()
322323

323-
//nolint // Yeah this leaks
324+
ctx := c.Context()
325+
mux := http.NewServeMux()
326+
mux.Handle("/", http.FileServer(http.Dir(tempdir)))
327+
srv := &http.Server{Handler: mux}
328+
329+
go func() {
330+
if err := srv.Serve(listener); err != nil && !errors.Is(err, http.ErrServerClosed) {
331+
log.Printf("http serve: %v", err)
332+
}
333+
}()
334+
335+
// stop server when ctx is canceled
324336
go func() {
325-
mux := http.NewServeMux()
326-
mux.Handle("/", http.FileServer(http.Dir(tempdir)))
327-
http.Serve(listener, mux)
337+
<-ctx.Done()
338+
_ = srv.Shutdown(context.Background())
328339
}()
329340
}
330341

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"
@@ -203,7 +205,7 @@ func testPXE(c cluster.TestCluster, opts IsoTestOpts) {
203205
os.RemoveAll(tempdir)
204206
}()
205207

206-
pxe, err := createPXE(tempdir, opts)
208+
pxe, err := createPXE(c.Context(), tempdir, opts)
207209
if err != nil {
208210
c.Fatal(errors.Wrapf(err, "setting up install"))
209211
}
@@ -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)