Skip to content

Commit 92b5744

Browse files
committed
refactor: replace adminhelper with local addHostsEntry in proxy tests
552bd4b added adminhelper implementation for integration test but it fails because admin helper path is depend on crc binary path and integration test is also standalone built and run in testing machine so it is not able to get right admin helper binary. This PR remove current implementation and add cross-platform hosts file modification directly in the test, supporting Windows, Linux, and macOS. Note: It is assume that test on windows have admin rights, for mac/linux we are using sudo but in windows it might be problem to have UAC prompt.
1 parent 1830173 commit 92b5744

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

test/integration/hosts_unix.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//go:build !windows
2+
3+
package test_test
4+
5+
import (
6+
crcos "github.com/crc-org/crc/v2/pkg/os"
7+
)
8+
9+
// addHostsEntry adds an entry to /etc/hosts using sudo tee.
10+
func addHostsEntry(ip, hostname string) error {
11+
cmd := `printf '%s %s\n' "$1" "$2" | tee -a /etc/hosts > /dev/null`
12+
_, _, err := crcos.RunPrivileged("adding hosts entry for proxy test", "sh", "-c", cmd, "addHostsEntry", ip, hostname)
13+
return err
14+
}

test/integration/hosts_windows.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//go:build windows
2+
3+
package test_test
4+
5+
import (
6+
"fmt"
7+
"os"
8+
"path/filepath"
9+
10+
"github.com/crc-org/crc/v2/pkg/os/windows/powershell"
11+
)
12+
13+
// addHostsEntry adds an entry to the Windows hosts file using ExecuteAsAdmin.
14+
func addHostsEntry(ip, hostname string) error {
15+
systemRoot := os.Getenv("SystemRoot")
16+
if systemRoot == "" {
17+
systemRoot = `C:\Windows`
18+
}
19+
hostsPath := filepath.Join(systemRoot, "System32", "drivers", "etc", "hosts")
20+
21+
entry := fmt.Sprintf("%s %s", ip, hostname)
22+
cmd := fmt.Sprintf(`Add-Content -Path "%s" -Value "%s"`, hostsPath, entry)
23+
24+
_, _, err := powershell.ExecuteAsAdmin("adding hosts entry for proxy test", cmd)
25+
return err
26+
}

test/integration/proxy_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"os/exec"
1010
"time"
1111

12-
"github.com/crc-org/crc/v2/pkg/crc/adminhelper"
1312
crc "github.com/crc-org/crc/v2/test/extended/crc/cmd"
1413
"github.com/crc-org/crc/v2/test/extended/util"
1514
. "github.com/onsi/ginkgo/v2"
@@ -141,9 +140,8 @@ var _ = Describe("", Serial, Ordered, Label("openshift-preset", "goproxy"), func
141140
})
142141

143142
It("add host.crc.testing to host's /etc/hosts", func() {
144-
// After setup, adminhelper is available
145143
// Add host.crc.testing -> 127.0.0.1 so host can resolve it to localhost proxy
146-
err := adminhelper.AddToHostsFile("127.0.0.1", "host.crc.testing")
144+
err := addHostsEntry("127.0.0.1", "host.crc.testing")
147145
Expect(err).NotTo(HaveOccurred())
148146
})
149147

0 commit comments

Comments
 (0)