test: make suite browser detection more robust and extensible (#4807)

pull/4806/merge
James Elliott 2023-01-21 16:02:27 +11:00 committed by GitHub
parent 309c355026
commit 8cbd9cb30a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 4 deletions

View File

@ -18,6 +18,38 @@ import (
"github.com/google/uuid" "github.com/google/uuid"
) )
var browserPaths = []string{"/usr/bin/chromium-browser", "/usr/bin/chromium"}
// ValidateBrowserPath validates the appropriate chromium browser path.
func ValidateBrowserPath(path string) (browserPath string, err error) {
var info os.FileInfo
if info, err = os.Stat(path); err != nil {
return "", err
} else if info.IsDir() {
return "", fmt.Errorf("browser cannot be a directory")
}
return path, nil
}
// GetBrowserPath retrieves the appropriate chromium browser path.
func GetBrowserPath() (path string, err error) {
browserPath := os.Getenv("BROWSER_PATH")
if browserPath != "" {
return ValidateBrowserPath(browserPath)
}
for _, browserPath = range browserPaths {
if browserPath, err = ValidateBrowserPath(browserPath); err == nil {
return browserPath, nil
}
}
return "", fmt.Errorf("no chromium browser was detected in the known paths, set the BROWSER_PATH environment variable to override the path")
}
// GetLoginBaseURL returns the URL of the login portal and the path prefix if specified. // GetLoginBaseURL returns the URL of the login portal and the path prefix if specified.
func GetLoginBaseURL(baseDomain string) string { func GetLoginBaseURL(baseDomain string) string {
if PathPrefix != "" { if PathPrefix != "" {

View File

@ -19,10 +19,11 @@ type RodSession struct {
} }
// StartRodWithProxy create a rod/chromedp session. // StartRodWithProxy create a rod/chromedp session.
func StartRodWithProxy(proxy string) (*RodSession, error) { func StartRodWithProxy(proxy string) (session *RodSession, err error) {
browserPath := os.Getenv("BROWSER_PATH") var browserPath string
if browserPath == "" {
browserPath = "/usr/bin/chromium-browser" if browserPath, err = GetBrowserPath(); err != nil {
return nil, err
} }
headless := false headless := false