Skip to content

Commit 3e9185a

Browse files
committed
test/proxy: Make sure proxy goroutine run as part of proxy test
Looks like proxy go routine also run for other test and since we are not closing it properly it fails the complete test so in this PR, proxy server have stop/start functionailty and test is also updated to start the proxy server for specific test and stop it once test finish.
1 parent 943f457 commit 3e9185a

2 files changed

Lines changed: 79 additions & 21 deletions

File tree

test/extended/util/proxy.go

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"os"
1010
"path/filepath"
1111
"regexp"
12+
"time"
1213

1314
"github.com/elazarl/goproxy"
1415
log "github.com/sirupsen/logrus"
@@ -45,26 +46,38 @@ func setCA() error {
4546
return nil
4647
}
4748

48-
func RunProxy() {
49-
50-
err := setCA()
51-
if err != nil {
52-
log.Fatalf("error setting up the CA: %s", err)
49+
// NewProxy creates and configures a new proxy server.
50+
// Returns the server, a cleanup function to close the log file, and any error.
51+
// The caller is responsible for:
52+
// - Starting the server with go server.ListenAndServe()
53+
// - Shutting it down with server.Shutdown()
54+
// - Calling the cleanup function after shutdown to close the log file
55+
func NewProxy() (*http.Server, func(), error) {
56+
if err := setCA(); err != nil {
57+
return nil, nil, fmt.Errorf("error setting up the CA: %w", err)
5358
}
5459

5560
// Create a DEDICATED logger for the proxy instead of using the global one
5661
proxyLogger := log.New()
5762

58-
logfile := filepath.Join("out", "goproxylogfile.log")
59-
f, err := os.OpenFile(logfile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
63+
var logFile *os.File
64+
logfilePath := filepath.Join("out", "goproxylogfile.log")
65+
f, err := os.OpenFile(logfilePath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
6066
if err != nil {
6167
log.Printf("error opening file: %v", err)
6268
proxyLogger.SetOutput(os.Stderr)
6369
} else {
64-
defer f.Close()
70+
logFile = f
6571
proxyLogger.SetOutput(f)
6672
}
6773

74+
// Cleanup function to close log file
75+
cleanup := func() {
76+
if logFile != nil {
77+
logFile.Close()
78+
}
79+
}
80+
6881
proxy := goproxy.NewProxyHttpServer()
6982

7083
// Log and handle all CONNECT requests (HTTPS tunnels)
@@ -94,10 +107,13 @@ func RunProxy() {
94107
proxy.Verbose = true
95108
proxy.Logger = proxyLogger
96109

97-
proxyLogger.Infof("Starting goproxy on %s", addr)
98-
99-
err = http.ListenAndServe(addr, proxy) // #nosec G114
100-
if err != nil {
101-
proxyLogger.Errorf("error running proxy: %s", err)
110+
server := &http.Server{
111+
Addr: addr,
112+
Handler: proxy,
113+
ReadHeaderTimeout: 10 * time.Second,
102114
}
115+
116+
proxyLogger.Infof("Proxy server configured on %s", addr)
117+
118+
return server, cleanup, nil
103119
}

test/integration/proxy_test.go

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package test_test
22

33
import (
4+
"context"
45
"net"
6+
"net/http"
57
"os"
68
"os/exec"
9+
"time"
710

811
"github.com/crc-org/crc/v2/pkg/crc/adminhelper"
912
crc "github.com/crc-org/crc/v2/test/extended/crc/cmd"
@@ -14,6 +17,42 @@ import (
1417

1518
var _ = Describe("", Serial, Ordered, Label("openshift-preset", "goproxy"), func() {
1619

20+
var proxyServer *http.Server
21+
var proxyCleanup func()
22+
23+
// Start proxy server before any tests run
24+
BeforeAll(func() {
25+
// Create proxy server - errors are caught here in the main goroutine
26+
var err error
27+
proxyServer, proxyCleanup, err = util.NewProxy()
28+
Expect(err).NotTo(HaveOccurred())
29+
30+
ln, err := net.Listen("tcp", "127.0.0.1:8888")
31+
Expect(err).NotTo(HaveOccurred())
32+
33+
errChan := make(chan error, 1)
34+
35+
go func() {
36+
if err := proxyServer.Serve(ln); err != nil && err != http.ErrServerClosed {
37+
errChan <- err
38+
}
39+
}()
40+
41+
// Wait for proxy to be ready
42+
Eventually(func() error {
43+
select {
44+
case err := <-errChan:
45+
return err
46+
default:
47+
}
48+
conn, err := net.Dial("tcp", "127.0.0.1:8888")
49+
if err == nil {
50+
conn.Close()
51+
}
52+
return err
53+
}).Should(Succeed())
54+
})
55+
1756
// runs 1x after all the It blocks (specs) inside this Describe node
1857
AfterAll(func() {
1958

@@ -32,16 +71,19 @@ var _ = Describe("", Serial, Ordered, Label("openshift-preset", "goproxy"), func
3271
err = os.Unsetenv("HTTP_PROXY")
3372
Expect(err).NotTo(HaveOccurred())
3473

35-
})
74+
// Stop the proxy server
75+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
76+
defer cancel()
77+
if proxyServer != nil {
78+
err = proxyServer.Shutdown(ctx)
79+
Expect(err).NotTo(HaveOccurred())
80+
}
3681

37-
go util.RunProxy()
38-
Eventually(func() error {
39-
conn, err := net.Dial("tcp", "127.0.0.1:8888")
40-
if err == nil {
41-
conn.Close()
82+
// Close the log file
83+
if proxyCleanup != nil {
84+
proxyCleanup()
4285
}
43-
return err
44-
}).Should(Succeed())
86+
})
4587

4688
Describe("Behind proxy", Serial, Ordered, func() {
4789

0 commit comments

Comments
 (0)