From 8cbd9cb30a22f105cc09ad6d5246a9f92639549a Mon Sep 17 00:00:00 2001 From: James Elliott Date: Sat, 21 Jan 2023 16:02:27 +1100 Subject: [PATCH] test: make suite browser detection more robust and extensible (#4807) --- internal/suites/utils.go | 32 ++++++++++++++++++++++++++++++++ internal/suites/webdriver.go | 9 +++++---- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/internal/suites/utils.go b/internal/suites/utils.go index 6d96d05ec..df4fb4f18 100644 --- a/internal/suites/utils.go +++ b/internal/suites/utils.go @@ -18,6 +18,38 @@ import ( "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. func GetLoginBaseURL(baseDomain string) string { if PathPrefix != "" { diff --git a/internal/suites/webdriver.go b/internal/suites/webdriver.go index 79d0d94ad..da14b607b 100644 --- a/internal/suites/webdriver.go +++ b/internal/suites/webdriver.go @@ -19,10 +19,11 @@ type RodSession struct { } // StartRodWithProxy create a rod/chromedp session. -func StartRodWithProxy(proxy string) (*RodSession, error) { - browserPath := os.Getenv("BROWSER_PATH") - if browserPath == "" { - browserPath = "/usr/bin/chromium-browser" +func StartRodWithProxy(proxy string) (session *RodSession, err error) { + var browserPath string + + if browserPath, err = GetBrowserPath(); err != nil { + return nil, err } headless := false