diff --git a/end-to-end/main_test.go b/end-to-end/main_test.go index 8b629033e..52b9c7ca3 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,64 @@ 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 filesToClean = map[string]string{ + "LocalAppData": "Ubuntu Pro", + "UserProfile": ".ubuntupro", +} + +// 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 env, subPath := range filesToClean { + errs = errors.Join(errs, func() error { + path := os.Getenv(env) + if path == "" { + return fmt.Errorf("variable $env:%s should not be empty", env) + } - _, 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, subPath) - 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 env, subPath := range filesToClean { + errs = errors.Join(errs, func() error { + path := os.Getenv(env) + if path == "" { + return fmt.Errorf("variable $env:%s should not be empty", env) + } - 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, subPath) + + if err := os.RemoveAll(path); err != nil { + return fmt.Errorf("could not clean up %s: %v", env, err) + } + + return nil + }()) } - return nil + return errs } // assertCleanRegistry returns error if registry key 'UbuntuPro' exists. diff --git a/end-to-end/organization_token_test.go b/end-to-end/organization_token_test.go index 8572ee483..c18c603af 100644 --- a/end-to-end/organization_token_test.go +++ b/end-to-end/organization_token_test.go @@ -100,6 +100,6 @@ func activateOrgSubscription(t *testing.T) { require.NoErrorf(t, err, "Setup: could not open UbuntuPro registry key") defer key.Close() - err = key.SetStringValue("ProTokenOrg", token) + err = key.SetStringValue("UbuntuProToken", token) require.NoError(t, err, "could not write token in registry") } diff --git a/end-to-end/utils_test.go b/end-to-end/utils_test.go index c493711db..f783fb70b 100644 --- a/end-to-end/utils_test.go +++ b/end-to-end/utils_test.go @@ -33,14 +33,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 { @@ -112,19 +112,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