package platform import ( "os" "os/exec" "path/filepath" "testing" ) func TestExportFileToTargetUnmountsExistingMountpoint(t *testing.T) { t.Parallel() tmp := t.TempDir() src := filepath.Join(tmp, "bundle.tar.gz") mountpoint := filepath.Join(tmp, "mnt") if err := os.MkdirAll(mountpoint, 0755); err != nil { t.Fatalf("mkdir mountpoint: %v", err) } if err := os.WriteFile(src, []byte("bundle"), 0644); err != nil { t.Fatalf("write src: %v", err) } var calls [][]string oldExec := exportExecCommand exportExecCommand = func(name string, args ...string) *exec.Cmd { calls = append(calls, append([]string{name}, args...)) return exec.Command("sh", "-c", "exit 0") } t.Cleanup(func() { exportExecCommand = oldExec }) s := &System{} dst, err := s.ExportFileToTarget(src, RemovableTarget{ Device: "/dev/sdb1", Mountpoint: mountpoint, }) if err != nil { t.Fatalf("ExportFileToTarget error: %v", err) } if got, want := dst, filepath.Join(mountpoint, "bundle.tar.gz"); got != want { t.Fatalf("dst=%q want %q", got, want) } if _, err := os.Stat(filepath.Join(mountpoint, "bundle.tar.gz")); err != nil { t.Fatalf("exported file missing: %v", err) } foundUmount := false for _, call := range calls { if len(call) == 2 && call[0] == "umount" && call[1] == mountpoint { foundUmount = true break } } if !foundUmount { t.Fatalf("expected umount %q call, got %#v", mountpoint, calls) } }