diff --git a/end-to-end/main_test.go b/end-to-end/main_test.go index 8b629033e..dd9ae166a 100644 --- a/end-to-end/main_test.go +++ b/end-to-end/main_test.go @@ -66,7 +66,7 @@ func TestMain(m *testing.M) { log.Fatalf("Setup: %v\n", err) } - if err := assertCleanLocalAppData(); err != nil { + if err := assertCleanFilesystem(); err != nil { log.Fatalf("Setup: %v\n", err) } @@ -228,45 +228,68 @@ func powershellf(ctx context.Context, command string, args ...any) *exec.Cmd { "-Command", fmt.Sprintf(`$env:PsModulePath="" ; `+command, args...)) } -// assertCleanLocalAppData returns error if directory '%LocalAppData%/Ubuntu Pro' exists. +var filesToCleanUp = []struct { + prefixEnv string + path string +}{ + {prefixEnv: "LocalAppData", path: "Ubuntu Pro"}, + {prefixEnv: "UserProfile", path: ".ubuntupro"}, + {prefixEnv: "UserProfile", path: ".ubuntupro.logs"}, +} + +// assertCleanFilesystem returns error if directory '%LocalAppData%/Ubuntu Pro' exists. // If safety checks are overridden, then the directory is removed and no error is returned. -func assertCleanLocalAppData() error { - path := os.Getenv("LocalAppData") - if path == "" { - return errors.New("variable $env:LocalAppData should not be empty") +func assertCleanFilesystem() error { + if os.Getenv(overrideSafety) != "" { + return cleanupFilesystem() } - path = filepath.Join(path, "Ubuntu Pro") + var errs error + for _, f := range filesToCleanUp { + errs = errors.Join(errs, func() error { + path := os.Getenv(f.prefixEnv) + if path == "" { + return fmt.Errorf("variable $env:%s should not be empty", f.prefixEnv) + } - _, err := os.Stat(path) - if errors.Is(err, fs.ErrNotExist) { - return nil - } - if err != nil { - return fmt.Errorf("could not stat %q: %v", path, err) - } + path = filepath.Join(path, f.path) - if os.Getenv(overrideSafety) != "" { - return cleanupLocalAppData() + _, err := os.Stat(path) + if errors.Is(err, fs.ErrNotExist) { + return nil + } + if err != nil { + return fmt.Errorf("could not stat %q: %v", path, err) + } + + return fmt.Errorf("Path %q should not exist. Remove it from your machine "+ + "to agree to run this potentially destructive test.", path) + }()) } - return fmt.Errorf("Directory %q should not exist. Remove it from your machine "+ - "to agree to run this potentially destructive test.", path) + return nil } -// cleanupLocalAppData removes directory '%LocalAppData%/Ubuntu Pro' and all its contents. -func cleanupLocalAppData() error { - path := os.Getenv("LocalAppData") - if path == "" { - return errors.New("variable $env:LocalAppData should not be empty") - } +func cleanupFilesystem() error { + var errs error + for _, f := range filesToCleanUp { + errs = errors.Join(errs, func() error { + path := os.Getenv(f.prefixEnv) + if path == "" { + return fmt.Errorf("variable $env:%s should not be empty", f.prefixEnv) + } - path = filepath.Join(path, "Ubuntu Pro") - if err := os.RemoveAll(path); err != nil { - return fmt.Errorf("could not clean up LocalAppData: %v", err) + path = filepath.Join(path, f.path) + + if err := os.RemoveAll(path); err != nil { + return fmt.Errorf("could not clean up %s: %v", path, err) + } + + return nil + }()) } - return nil + return errs } // assertCleanRegistry returns error if registry key 'UbuntuPro' exists. diff --git a/end-to-end/utils_test.go b/end-to-end/utils_test.go index 342c4dc86..3eeb17276 100644 --- a/end-to-end/utils_test.go +++ b/end-to-end/utils_test.go @@ -34,14 +34,14 @@ func testSetup(t *testing.T) { err = assertCleanRegistry() require.NoError(t, err, "Setup: registry is polluted, potentially by a previous test") - err = assertCleanLocalAppData() + err = assertCleanFilesystem() require.NoError(t, err, "Setup: local app data is polluted, potentially by a previous test") t.Cleanup(func() { err := errors.Join( stopAgent(ctx), cleanupRegistry(), - cleanupLocalAppData(), + cleanupFilesystem(), ) // Cannot assert: the test is finished already if err != nil { @@ -113,19 +113,16 @@ func startAgent(t *testing.T, ctx context.Context, arg string, environ ...string } }() - require.Eventually(t, func() bool { - localAppData := os.Getenv("LocalAppData") - if localAppData == "" { - t.Logf("Agent setup: $env:LocalAppData should not be empty") - return false - } + home := os.Getenv("UserProfile") + require.NotEmptyf(t, home, "Agent setup: $env:UserProfile should not be empty") - _, err := os.Stat(filepath.Join(localAppData, "Ubuntu Pro", "addr")) + require.Eventually(t, func() bool { + _, err := os.Stat(filepath.Join(home, ".ubuntupro")) if errors.Is(err, fs.ErrNotExist) { return false } if err != nil { - t.Logf("Agent setup: could not read addr file: %v", err) + t.Logf("Agent setup: could not read address file: %v", err) return false } return true