Skip to content

Commit

Permalink
Update E2E to use the new file locations
Browse files Browse the repository at this point in the history
The E2E test cleans up the LocalAppData folder to avoid test coupling.
This commit updates this cleanup to the changes to the new path for the
address file.

We also watch that file to know if the agent is serving already. This
commit updates the path being watched.
  • Loading branch information
EduardGomezEscandell committed Nov 8, 2023
1 parent 57a229e commit 0adbf9c
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 39 deletions.
75 changes: 47 additions & 28 deletions end-to-end/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion end-to-end/organization_token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
17 changes: 7 additions & 10 deletions end-to-end/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 0adbf9c

Please sign in to comment.