-
Notifications
You must be signed in to change notification settings - Fork 260
Expand file tree
/
Copy pathproxy_test.go
More file actions
204 lines (166 loc) · 6.34 KB
/
Copy pathproxy_test.go
File metadata and controls
204 lines (166 loc) · 6.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package test_test
import (
"context"
"fmt"
"net"
"net/http"
"os"
"os/exec"
"time"
crc "github.com/crc-org/crc/v2/test/extended/crc/cmd"
"github.com/crc-org/crc/v2/test/extended/util"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
// runCRCWithLiveOutput runs a CRC command and streams output to os.Stdout/Stderr in real-time.
// This helps diagnose hangs by showing progress as it happens.
// Note: We use os.Stdout/Stderr directly instead of GinkgoWriter because GinkgoWriter
// buffers output until the test completes, which defeats the purpose of real-time output.
func runCRCWithLiveOutput(args ...string) error {
cmd := exec.Command("crc", args...)
cmd.Env = append(os.Environ(),
"CRC_DISABLE_UPDATE_CHECK=true",
"CRC_LOG_LEVEL=debug",
)
// Direct stdout and stderr to terminal for real-time output
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
_, _ = fmt.Fprintf(os.Stdout, "[%s] Starting: crc %v\n", time.Now().Format(time.RFC3339), args)
err := cmd.Run()
_, _ = fmt.Fprintf(os.Stdout, "[%s] Finished: crc %v (err=%v)\n", time.Now().Format(time.RFC3339), args, err)
return err
}
var _ = Describe("", Serial, Ordered, Label("openshift-preset", "goproxy"), func() {
var proxyServer *http.Server
var proxyCleanup func()
// Start proxy server before any tests run
BeforeAll(func() {
// Create proxy server - errors are caught here in the main goroutine
var err error
proxyServer, proxyCleanup, err = util.NewProxy()
Expect(err).NotTo(HaveOccurred())
ln, err := net.Listen("tcp", "127.0.0.1:8888")
Expect(err).NotTo(HaveOccurred())
errChan := make(chan error, 1)
go func() {
if err := proxyServer.Serve(ln); err != nil && err != http.ErrServerClosed {
errChan <- err
}
}()
// Wait for proxy to be ready
Eventually(func() error {
select {
case err := <-errChan:
return err
default:
}
conn, err := net.Dial("tcp", "127.0.0.1:8888")
if err == nil {
conn.Close()
}
return err
}).Should(Succeed())
})
// runs 1x after all the It blocks (specs) inside this Describe node
AfterAll(func() {
// cleanup CRC
Expect(RunCRCExpectSuccess("cleanup")).To(MatchRegexp("Cleanup finished"))
// remove config file crc.json
err := util.RemoveCRCConfig()
Expect(err).NotTo(HaveOccurred())
// HTTP_PROXY and HTTPS_PROXY vars were set implicitly
// unset them at the end
err = os.Unsetenv("HTTPS_PROXY")
Expect(err).NotTo(HaveOccurred())
err = os.Unsetenv("HTTP_PROXY")
Expect(err).NotTo(HaveOccurred())
// Stop the proxy server
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if proxyServer != nil {
err = proxyServer.Shutdown(ctx)
Expect(err).NotTo(HaveOccurred())
}
// Close the log file
if proxyCleanup != nil {
proxyCleanup()
}
})
Describe("Behind proxy", Serial, Ordered, func() {
// Two-phase proxy configuration:
// Phase 1 (setup): Use 127.0.0.1 - directly accessible on host
// Phase 2 (start): Use host.crc.testing - resolves to 127.0.0.1 on host, 192.168.127.1 in VM
localhostProxy := "http://127.0.0.1:8888"
hostProxy := "http://host.crc.testing:8888"
noProxy := ".testing"
// Start goproxy
It("configure CRC for setup with localhost proxy", func() {
// Use localhost proxy for setup phase - host can reach 127.0.0.1 directly
Expect(RunCRCExpectSuccess("config", "set", "http-proxy", localhostProxy)).To(ContainSubstring("Successfully configured http-proxy"))
Expect(RunCRCExpectSuccess("config", "set", "https-proxy", localhostProxy)).To(ContainSubstring("Successfully configured https-proxy"))
Expect(RunCRCExpectSuccess("config", "set", "no-proxy", noProxy)).To(ContainSubstring("Successfully configured no-proxy"))
Expect(RunCRCExpectSuccess("config", "set", "proxy-ca-file", util.CACertTempLocation)).To(ContainSubstring("Successfully configured proxy-ca-file"))
Expect(RunCRCExpectSuccess("config", "set", "host-network-access", "true")).To(ContainSubstring("Changes to configuration property 'host-network-access' are only applied during 'crc setup'"))
})
It("setup CRC", func() {
// Setup uses 127.0.0.1 proxy - works on host
// Use live output so we can see progress and diagnose hangs
args := []string{"setup"}
if len(bundlePath) > 0 {
args = append(args, "-b", bundlePath)
}
err := runCRCWithLiveOutput(args...)
Expect(err).NotTo(HaveOccurred())
})
It("add host.crc.testing to host's /etc/hosts", func() {
// Add host.crc.testing -> 127.0.0.1 so host can resolve it to localhost proxy
err := addHostsEntry("127.0.0.1", "host.crc.testing")
Expect(err).NotTo(HaveOccurred())
})
It("reconfigure CRC proxy for start with host.crc.testing", func() {
// Switch to host.crc.testing for start phase
// Host resolves to 127.0.0.1 (from /etc/hosts we just added)
// VM will resolve to 192.168.127.1 (we'll add to VM's /etc/hosts after start)
Expect(RunCRCExpectSuccess("config", "set", "http-proxy", hostProxy)).To(ContainSubstring("Successfully configured http-proxy"))
Expect(RunCRCExpectSuccess("config", "set", "https-proxy", hostProxy)).To(ContainSubstring("Successfully configured https-proxy"))
})
It("start CRC", func() {
// default values: "--memory", "10752", "--cpus", "4", "disk-size", "31"
// Use live output so we can see progress and diagnose hangs
args := []string{"start", "-p", pullSecretPath}
if len(bundlePath) > 0 {
args = append(args, "-b", bundlePath)
}
err := runCRCWithLiveOutput(args...)
Expect(err).NotTo(HaveOccurred())
})
It("wait for cluster in Running state", func() {
err := crc.WaitForClusterInState("running")
Expect(err).NotTo(HaveOccurred())
})
It("login to cluster using crc-admin context", func() {
err := util.AddOCToPath()
Expect(err).NotTo(HaveOccurred())
cmd := exec.Command("oc", "config", "use-context", "crc-admin")
err = cmd.Run()
Expect(err).NotTo(HaveOccurred())
cmd = exec.Command("oc", "whoami")
out, err := cmd.Output()
Expect(err).NotTo(HaveOccurred())
Expect(string(out)).To(ContainSubstring("kubeadmin"))
cmd = exec.Command("oc", "get", "co")
err = cmd.Run()
Expect(err).NotTo(HaveOccurred())
})
It("stop CRC", func() {
Expect(
crcSuccess("stop", "-f")).
To(MatchRegexp("[Ss]topped the instance"))
})
It("cleanup CRC", func() {
Expect(
crcSuccess("cleanup")).
To(MatchRegexp("Cleanup finished"))
})
})
})