Skip to content

Commit 1f02013

Browse files
kola: iso.*: don't leak http server
1 parent 4212945 commit 1f02013

File tree

2 files changed

+45
-23
lines changed

2 files changed

+45
-23
lines changed

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
}
@@ -324,7 +326,7 @@ type PXE struct {
324326
bootfile string
325327
}
326328

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

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

411422
return pxe, nil

0 commit comments

Comments
 (0)