From b1abe9c0574d5d2b11223185ca325fa672df4e57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edu=20G=C3=B3mez=20Escandell?= Date: Wed, 22 Nov 2023 08:55:32 +0100 Subject: [PATCH] Update windows-agent log finder too look at the virtualized directory --- end-to-end/manual_token_input_test.go | 2 +- end-to-end/organization_token_test.go | 2 +- end-to-end/purchase_test.go | 5 +++-- end-to-end/utils_test.go | 27 +++++++++++++++++++++++---- 4 files changed, 28 insertions(+), 8 deletions(-) diff --git a/end-to-end/manual_token_input_test.go b/end-to-end/manual_token_input_test.go index acd0a5c56..15c3337f2 100644 --- a/end-to-end/manual_token_input_test.go +++ b/end-to-end/manual_token_input_test.go @@ -39,7 +39,7 @@ func TestManualTokenInput(t *testing.T) { ctx := context.Background() testSetup(t) - defer logWindowsAgentOnError(t) + defer logWindowsAgentOnError(t, ctx) // Either runs the ubuntupro app before... if tc.whenToken == beforeDistroRegistration { diff --git a/end-to-end/organization_token_test.go b/end-to-end/organization_token_test.go index a98339309..cbf594ef1 100644 --- a/end-to-end/organization_token_test.go +++ b/end-to-end/organization_token_test.go @@ -39,7 +39,7 @@ func TestOrganizationProvidedToken(t *testing.T) { ctx := context.Background() testSetup(t) - defer logWindowsAgentOnError(t) + defer logWindowsAgentOnError(t, ctx) if tc.whenToken == beforeDistroRegistration { activateOrgSubscription(t) diff --git a/end-to-end/purchase_test.go b/end-to-end/purchase_test.go index 66b700a27..79520dcb6 100644 --- a/end-to-end/purchase_test.go +++ b/end-to-end/purchase_test.go @@ -51,8 +51,10 @@ func TestPurchase(t *testing.T) { for name, tc := range testCases { tc := tc t.Run(name, func(t *testing.T) { + ctx := context.Background() + testSetup(t) - defer logWindowsAgentOnError(t) + defer logWindowsAgentOnError(t, ctx) settings := contractsmockserver.DefaultSettings() @@ -67,7 +69,6 @@ func TestPurchase(t *testing.T) { //nolint:errcheck // Nothing we can do about it defer cs.Stop() - ctx := context.Background() contractsCtx, contractsCancel := context.WithCancel(ctx) defer contractsCancel() diff --git a/end-to-end/utils_test.go b/end-to-end/utils_test.go index 3eeb17276..f341b2354 100644 --- a/end-to-end/utils_test.go +++ b/end-to-end/utils_test.go @@ -195,7 +195,8 @@ func logWslProServiceOnError(t *testing.T, ctx context.Context, d gowsl.Distro) t.Logf("WSL Pro Service logs:\n%s\n", out) } -func logWindowsAgentOnError(t *testing.T) { +//nolint:revive // testing.T must precede the context +func logWindowsAgentOnError(t *testing.T, ctx context.Context) { t.Helper() if !t.Failed() { @@ -204,13 +205,31 @@ func logWindowsAgentOnError(t *testing.T) { localAppData := os.Getenv("LocalAppData") if localAppData == "" { - t.Log("could not access Windows Agent's logs: $env:LocalAppData is not assigned") + t.Log("could not find Windows Agent's logs: $env:LocalAppData is not assigned") return } - out, err := os.ReadFile(filepath.Join(localAppData, common.LocalAppDataDir, "log")) + // The virtualized LocalAppData is located under a path similar to this one: + // + // %LocalAppData%/Packages/CanonicalGroupLimited.UbuntuProForWindows_hhj52ngek5ykr/LocalCache/Local + // ^~~~~~~~~~~~~ + // This part changes from version to version + // + packageDir := filepath.Join(localAppData, "Packages", "CanonicalGroupLimited.UbuntuProForWindows_*") + + out, err := powershellf(ctx, "(Get-Item %q).FullName", packageDir).CombinedOutput() + if err != nil { + t.Logf("could not find Windows Agent's logs: could not find virtualized LocalAppData: %v", err) + return + } + + // Remove trailing endline + packageDir = strings.TrimSpace(string(out)) + + logsPath := filepath.Join(packageDir, "LocalCache", "Local", common.LocalAppDataDir, ".ubuntupro.log") + out, err = os.ReadFile(logsPath) if err != nil { - t.Logf("could not read Windows Agent's logs: %v", err) + t.Logf("could not read Windows Agent's logs at %q: %v", logsPath, err) return }