From e2643344d9c500916882c66b51f5abf902026e4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edu=20G=C3=B3mez=20Escandell?= Date: Tue, 28 Nov 2023 16:51:56 +0100 Subject: [PATCH 1/3] Fix debian package paths in E2E tests For some reason the path being found by locateWSLProService. I fixed it by getting rid of this function. For more context: we were doing a weird mix of keeping track of the path of the debian pkg in a local variable, and the path to the MSIX in a global one. Instead, I moved the DEB to the global namespace for consistency's sake. The code is now more readable. --- end-to-end/go.mod | 2 +- end-to-end/main_test.go | 76 ++++++++++++++--------------------------- 2 files changed, 27 insertions(+), 51 deletions(-) diff --git a/end-to-end/go.mod b/end-to-end/go.mod index 34ae22089..a8f4774f1 100644 --- a/end-to-end/go.mod +++ b/end-to-end/go.mod @@ -6,7 +6,6 @@ require ( github.com/canonical/ubuntu-pro-for-windows/common v0.0.0-20230906090052-60fb5d60ada4 github.com/canonical/ubuntu-pro-for-windows/mocks v0.0.0-20231018132816-d78b12b1d065 github.com/stretchr/testify v1.8.4 - github.com/ubuntu/decorate v0.0.0-20230905131025-e968fa48a85c github.com/ubuntu/gowsl v0.0.0-20231004124730-8fd8df02f394 golang.org/x/sys v0.15.0 gopkg.in/yaml.v3 v3.0.1 @@ -21,6 +20,7 @@ require ( github.com/sirupsen/logrus v1.9.3 // indirect github.com/spf13/cobra v1.7.0 // indirect github.com/spf13/pflag v1.0.5 // indirect + github.com/ubuntu/decorate v0.0.0-20230905131025-e968fa48a85c // indirect golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect gopkg.in/yaml.v2 v2.4.0 // indirect ) diff --git a/end-to-end/main_test.go b/end-to-end/main_test.go index 76581d6a4..ed66a6c7f 100644 --- a/end-to-end/main_test.go +++ b/end-to-end/main_test.go @@ -13,7 +13,6 @@ import ( "testing" "github.com/canonical/ubuntu-pro-for-windows/common" - "github.com/ubuntu/decorate" "github.com/ubuntu/gowsl" wsl "github.com/ubuntu/gowsl" "golang.org/x/sys/windows/registry" @@ -25,6 +24,9 @@ var ( // msixPath is the path to the Ubuntu Pro For Windows MSIX. msixPath string + + // debPkgPath is the path to the Wsl Pro Service Debian package. + debPkgPath string ) const ( @@ -76,20 +78,23 @@ func TestMain(m *testing.M) { log.Fatalf("Setup: %v\n", err) } - f := buildProject - if buildPath := os.Getenv(prebuiltPath); buildPath != "" { - f = usePrebuiltProject + buildPath := os.Getenv(prebuiltPath) + if buildPath == "" { + path, err := buildProject(ctx) + if err != nil { + log.Fatalf("Setup: %v\n", err) + } + buildPath = path } - wslProServiceDebPath, err := f(ctx) - if err != nil { + if err := usePrebuiltProject(buildPath); err != nil { log.Fatalf("Setup: %v\n", err) } log.Printf("MSIX package located at %s", msixPath) - log.Printf("Deb package located at %s", wslProServiceDebPath) + log.Printf("Deb package located at %s", debPkgPath) - path, cleanup, err := generateTestImage(ctx, referenceDistro, wslProServiceDebPath) + path, cleanup, err := generateTestImage(ctx, referenceDistro) if err != nil { log.Fatalf("Setup: %v\n", err) } @@ -108,28 +113,27 @@ func TestMain(m *testing.M) { } } -func usePrebuiltProject(ctx context.Context) (debPath string, err error) { - buildPath := os.Getenv(prebuiltPath) - +func usePrebuiltProject(buildPath string) (err error) { // Locate the Appx package and store the path in global variable so that we can // reinstall it before every test result, err := globSingleResult(filepath.Join(buildPath, "windows-agent", "UbuntuProForWindows_*.msixbundle")) if err != nil { - return "", fmt.Errorf("could not locate MSIX: %v", err) + return fmt.Errorf("could not locate MSIX: %v", err) } msixPath = result // Locate WSL-Pro-Service (it'll be installed later into the distros) - debPath = filepath.Join(buildPath, "wsl-pro-service") - _, err = locateWslProServiceDeb(debPath) + path, err := globSingleResult(filepath.Join(buildPath, "wsl-pro-service", "wsl-pro-service_*.deb")) if err != nil { - return "", fmt.Errorf("could not locate pre-built WSL-Pro-Service: %v", err) + return fmt.Errorf("could not locate WSL-Pro-Service: %v", err) } - return debPath, err + debPkgPath = path + + return nil } -func buildProject(ctx context.Context) (debPath string, err error) { +func buildProject(ctx context.Context) (string, error) { ctx, cancel := context.WithCancel(ctx) defer cancel() @@ -138,7 +142,7 @@ func buildProject(ctx context.Context) (debPath string, err error) { return "", fmt.Errorf("could not create temporary directory for build artifacts") } - debPath = filepath.Join(buildPath, "wsl-pro-service") + debPath := filepath.Join(buildPath, "wsl-pro-service") winPath := filepath.Join(buildPath, "windows-agent") if err := os.MkdirAll(debPath, 0600); err != nil { @@ -189,15 +193,9 @@ func buildProject(ctx context.Context) (debPath string, err error) { return "", fmt.Errorf("could not build project: %v", err) } - // Locate the Appx package and store the path in global variable so that we can - // reinstall it before every test - path, err := globSingleResult(filepath.Join(winPath, "UbuntuProForWindows_*.msixbundle")) - if err != nil { - return "", fmt.Errorf("could not locate Appx: %v", err) - } - log.Println("Project built") - return path, nil + + return buildPath, nil } // assertAppxInstalled returns an error if the provided Appx is not installed. @@ -214,23 +212,6 @@ func assertAppxInstalled(ctx context.Context, appx string) error { return nil } -// locateWslProServiceDeb locates the WSL pro service at the repository root and returns its absolute path. -func locateWslProServiceDeb(path string) (debPath string, err error) { - defer decorate.OnError(&err, "could not locate wsl-pro-service deb package") - - path, err = globSingleResult(filepath.Join(path, "wsl-pro-service_*.deb")) - if err != nil { - return "", err - } - - debPath, err = filepath.Abs(debPath) - if err != nil { - return "", fmt.Errorf("could not make path %q absolute: %v", path, err) - } - - return debPath, nil -} - // powershellf is syntax sugar to run powrshell commands. func powershellf(ctx context.Context, command string, args ...any) *exec.Cmd { //nolint:gosec // Tainted input is acceptable because all callers have their values hardcoded. @@ -353,7 +334,7 @@ func cleanupRegistry() error { // generateTestImage fails if the sourceDistro is registered, unless the safety checks are overridden, // in which case the sourceDistro is removed. // The source distro is then registered, exported after first boot, and unregistered. -func generateTestImage(ctx context.Context, sourceDistro, wslProServiceDebPath string) (path string, cleanup func(), err error) { +func generateTestImage(ctx context.Context, sourceDistro string) (path string, cleanup func(), err error) { log.Printf("Setup: Generating test image from %q\n", sourceDistro) defer log.Printf("Setup: Generated test image from %q\n", sourceDistro) @@ -396,12 +377,7 @@ func generateTestImage(ctx context.Context, sourceDistro, wslProServiceDebPath s // From now on, all cleanups must be deferred because the distro // must be unregistered before removing the directory it is in. - debPath, err := locateWslProServiceDeb(wslProServiceDebPath) - if err != nil { - return "", nil, err - } - - out, err = d.Command(ctx, fmt.Sprintf("DEBIAN_FRONTEND=noninteractive apt install -y $(wslpath -ua '%s')", debPath)).CombinedOutput() + out, err = d.Command(ctx, fmt.Sprintf("DEBIAN_FRONTEND=noninteractive apt install -y $(wslpath -ua '%s')", debPkgPath)).CombinedOutput() if err != nil { defer cleanup() return "", nil, fmt.Errorf("could not install wsl-pro-service: %v. %s", err, out) From 885d302e10d00385a530a68ddea1b8e3f6c52dc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edu=20G=C3=B3mez=20Escandell?= Date: Tue, 28 Nov 2023 17:03:28 +0100 Subject: [PATCH 2/3] Fix E2E agent log paths The path for the log file was incorrect. The logs are in the virtualized LocalAppData/Ubuntu Pro/log --- end-to-end/main_test.go | 1 - end-to-end/utils_test.go | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/end-to-end/main_test.go b/end-to-end/main_test.go index ed66a6c7f..5a43182c6 100644 --- a/end-to-end/main_test.go +++ b/end-to-end/main_test.go @@ -275,7 +275,6 @@ func filesToCleanUp() ([]string, error) { }{ {prefixEnv: "LocalAppData", path: "Ubuntu Pro"}, {prefixEnv: "UserProfile", path: ".ubuntupro"}, - {prefixEnv: "UserProfile", path: ".ubuntupro.logs"}, } var out []string diff --git a/end-to-end/utils_test.go b/end-to-end/utils_test.go index bb9a9b25f..7fca0eddc 100644 --- a/end-to-end/utils_test.go +++ b/end-to-end/utils_test.go @@ -224,7 +224,7 @@ func logWindowsAgentOnError(t *testing.T) { return } - logsPath := filepath.Join(packageDir, "LocalCache", "Local", common.LocalAppDataDir, ".ubuntupro.log") + logsPath := filepath.Join(packageDir, "LocalCache", "Local", common.LocalAppDataDir, "log") out, err := os.ReadFile(logsPath) if err != nil { t.Logf("could not read Windows Agent's logs at %q: %v", logsPath, err) From eeaaee696d3a73810036c17c539a45f20c8bf695 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edu=20G=C3=B3mez=20Escandell?= Date: Wed, 29 Nov 2023 17:31:08 +0100 Subject: [PATCH 3/3] Use absolute paths --- end-to-end/main_test.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/end-to-end/main_test.go b/end-to-end/main_test.go index 5a43182c6..bc818648b 100644 --- a/end-to-end/main_test.go +++ b/end-to-end/main_test.go @@ -120,7 +120,11 @@ func usePrebuiltProject(buildPath string) (err error) { if err != nil { return fmt.Errorf("could not locate MSIX: %v", err) } - msixPath = result + + msixPath, err = filepath.Abs(result) + if err != nil { + return fmt.Errorf("could not make MSIX path absolute: %v", err) + } // Locate WSL-Pro-Service (it'll be installed later into the distros) path, err := globSingleResult(filepath.Join(buildPath, "wsl-pro-service", "wsl-pro-service_*.deb")) @@ -128,7 +132,10 @@ func usePrebuiltProject(buildPath string) (err error) { return fmt.Errorf("could not locate WSL-Pro-Service: %v", err) } - debPkgPath = path + debPkgPath, err = filepath.Abs(path) + if err != nil { + return fmt.Errorf("could not make debian package path absolute: %v", err) + } return nil }