services/device/internal/impl: shard device unit tests cleanup

Expose device maanger cleanup functions to be replaced in tests that
have been sharded across multiple packages.

Change-Id: Id1f064d04ccdb99bda8fe94ce24c953f85e36c53
diff --git a/services/device/internal/impl/app_service.go b/services/device/internal/impl/app_service.go
index 066b667..9d6ab22 100644
--- a/services/device/internal/impl/app_service.go
+++ b/services/device/internal/impl/app_service.go
@@ -414,7 +414,7 @@
 	installationID := generateID()
 	installationDir := filepath.Join(i.config.Root, applicationDirName(envelope.Title), installationDirName(installationID))
 	deferrer := func() {
-		cleanupDir(installationDir, "")
+		CleanupDir(installationDir, "")
 	}
 	if err := mkdirPerm(installationDir, 0711); err != nil {
 		return "", verror.New(ErrOperationFailed, nil)
@@ -917,12 +917,12 @@
 	helper := i.config.Helper
 	instanceDir, instanceID, err := i.newInstance(ctx, call)
 	if err != nil {
-		cleanupDir(instanceDir, helper)
+		CleanupDir(instanceDir, helper)
 		return "", err
 	}
 	systemName := suidHelper.usernameForPrincipal(ctx, call.Security(), i.uat)
 	if err := saveSystemNameForInstance(instanceDir, systemName); err != nil {
-		cleanupDir(instanceDir, helper)
+		CleanupDir(instanceDir, helper)
 		return "", err
 	}
 	return instanceID, nil
@@ -1101,7 +1101,7 @@
 	}
 	versionDir, err := newVersion(ctx, installationDir, newEnvelope, oldVersionDir)
 	if err != nil {
-		cleanupDir(versionDir, "")
+		CleanupDir(versionDir, "")
 		return err
 	}
 	return nil
diff --git a/services/device/internal/impl/device_service.go b/services/device/internal/impl/device_service.go
index 1678fb8..5b732f3 100644
--- a/services/device/internal/impl/device_service.go
+++ b/services/device/internal/impl/device_service.go
@@ -504,7 +504,7 @@
 	}
 
 	deferrer := func() {
-		cleanupDir(workspace, "")
+		CleanupDir(workspace, "")
 	}
 	defer func() {
 		if deferrer != nil {
diff --git a/services/device/internal/impl/impl_helper_test.go b/services/device/internal/impl/impl_helper_test.go
index 4eed0cb..1ee97bf 100644
--- a/services/device/internal/impl/impl_helper_test.go
+++ b/services/device/internal/impl/impl_helper_test.go
@@ -38,12 +38,12 @@
 	// Setup a helper.
 	helper := utiltest.GenerateSuidHelperScript(t, dir)
 
-	impl.WrapBaseCleanupDir(helperTarget, helper)
+	impl.BaseCleanupDir(helperTarget, helper)
 	if _, err := os.Stat(helperTarget); err == nil || os.IsExist(err) {
 		t.Fatalf("%s should be missing but isn't", helperTarget)
 	}
 
-	impl.WrapBaseCleanupDir(nohelperTarget, "")
+	impl.BaseCleanupDir(nohelperTarget, "")
 	if _, err := os.Stat(nohelperTarget); err == nil || os.IsExist(err) {
 		t.Fatalf("%s should be missing but isn't", nohelperTarget)
 	}
diff --git a/services/device/internal/impl/only_for_test.go b/services/device/internal/impl/only_for_test.go
index d257860..9b02bb7 100644
--- a/services/device/internal/impl/only_for_test.go
+++ b/services/device/internal/impl/only_for_test.go
@@ -6,11 +6,8 @@
 
 import (
 	"fmt"
-	"os"
-	"path/filepath"
 
 	"v.io/v23/rpc"
-	"v.io/x/lib/vlog"
 )
 
 // This file contains code in the impl package that we only want built for tests
@@ -32,25 +29,3 @@
 		panic(fmt.Sprintf("unexpected type: %T", d))
 	}
 }
-
-func init() {
-	cleanupDir = func(dir, helper string) {
-		if dir == "" {
-			return
-		}
-		parentDir, base := filepath.Dir(dir), filepath.Base(dir)
-		var renamed string
-		if helper != "" {
-			renamed = filepath.Join(parentDir, "helper_deleted_"+base)
-		} else {
-			renamed = filepath.Join(parentDir, "deleted_"+base)
-		}
-		if err := os.Rename(dir, renamed); err != nil {
-			vlog.Errorf("Rename(%v, %v) failed: %v", dir, renamed, err)
-		}
-	}
-}
-
-func WrapBaseCleanupDir(path, helper string) {
-	baseCleanupDir(path, helper)
-}
diff --git a/services/device/internal/impl/util.go b/services/device/internal/impl/util.go
index dd22307..d7aa4f2 100644
--- a/services/device/internal/impl/util.go
+++ b/services/device/internal/impl/util.go
@@ -129,7 +129,7 @@
 	return nil
 }
 
-func baseCleanupDir(path, helper string) {
+func BaseCleanupDir(path, helper string) {
 	if helper != "" {
 		out, err := exec.Command(helper, "--rm", path).CombinedOutput()
 		if err != nil {
@@ -150,7 +150,7 @@
 	return filepath.Join(c.Root, "device-manager", "device-data", "acls")
 }
 
-// cleanupDir is defined like this so we can override its implementation for
-// tests. cleanupDir will use the helper to delete application state possibly
+// CleanupDir is defined like this so we can override its implementation for
+// tests. CleanupDir will use the helper to delete application state possibly
 // owned by different accounts if helper is provided.
-var cleanupDir = baseCleanupDir
+var CleanupDir = BaseCleanupDir
diff --git a/services/device/internal/impl/utiltest/helpers.go b/services/device/internal/impl/utiltest/helpers.go
index 35d1e06..6469493 100644
--- a/services/device/internal/impl/utiltest/helpers.go
+++ b/services/device/internal/impl/utiltest/helpers.go
@@ -52,6 +52,22 @@
 	impl.Describe = func() (descr device.Description, err error) {
 		return device.Description{Profiles: map[string]struct{}{"test-profile": struct{}{}}}, nil
 	}
+
+	impl.CleanupDir = func(dir, helper string) {
+		if dir == "" {
+			return
+		}
+		parentDir, base := filepath.Dir(dir), filepath.Base(dir)
+		var renamed string
+		if helper != "" {
+			renamed = filepath.Join(parentDir, "helper_deleted_"+base)
+		} else {
+			renamed = filepath.Join(parentDir, "deleted_"+base)
+		}
+		if err := os.Rename(dir, renamed); err != nil {
+			vlog.Errorf("Rename(%v, %v) failed: %v", dir, renamed, err)
+		}
+	}
 }
 
 func EnvelopeFromShell(sh *modules.Shell, env []string, cmd, title string, args ...string) application.Envelope {