Skip to content

Commit

Permalink
Implement a basic download test
Browse files Browse the repository at this point in the history
  • Loading branch information
radeusgd committed Jul 20, 2021
1 parent bd3e519 commit 322a9f6
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ object DefaultManagers {

/** Default [[TemporaryDirectoryManager]]. */
lazy val temporaryDirectoryManager =
new TemporaryDirectoryManager(distributionManager, defaultResourceManager)
TemporaryDirectoryManager(distributionManager, defaultResourceManager)

/** Default [[RuntimeComponentConfiguration]]. */
lazy val componentConfig: RuntimeComponentConfiguration =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import scala.util.Random
* so that they can be moved all at once at the last step of the installation.
*/
class TemporaryDirectoryManager(
distribution: DistributionManager,
unsafeRoot: Path,
resourceManager: ResourceManager
) {
private val logger = Logger[TemporaryDirectoryManager]
Expand Down Expand Up @@ -54,11 +54,10 @@ class TemporaryDirectoryManager(
* should ensure that). If that fails, it is also cleaned before any future
* accesses.
*/
private lazy val safeTemporaryDirectory = {
private lazy val safeTemporaryDirectory: Path = {
resourceManager.startUsingTemporaryDirectory()
val path = distribution.paths.unsafeTemporaryDirectory
Files.createDirectories(path)
path
Files.createDirectories(unsafeRoot)
unsafeRoot
}

/** Tries to clean the temporary files directory.
Expand All @@ -70,18 +69,27 @@ class TemporaryDirectoryManager(
* marked as in-use and will not be cleaned.
*/
def tryCleaningTemporaryDirectory(): Unit = {
val tmp = distribution.paths.unsafeTemporaryDirectory
if (Files.exists(tmp)) {
if (Files.exists(unsafeRoot)) {
resourceManager.tryWithExclusiveTemporaryDirectory {
if (!FileSystem.isDirectoryEmpty(tmp)) {
if (!FileSystem.isDirectoryEmpty(unsafeRoot)) {
logger.info(
"Cleaning up temporary files from a previous installation."
)
}
FileSystem.removeDirectory(tmp)
Files.createDirectories(tmp)
FileSystem.removeEmptyDirectoryOnExit(tmp)
FileSystem.removeDirectory(unsafeRoot)
Files.createDirectories(unsafeRoot)
FileSystem.removeEmptyDirectoryOnExit(unsafeRoot)
}
}
}
}

object TemporaryDirectoryManager {
def apply(
distribution: DistributionManager,
resourceManager: ResourceManager
): TemporaryDirectoryManager = new TemporaryDirectoryManager(
distribution.paths.unsafeTemporaryDirectory,
resourceManager
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,7 @@ class DownloadingLibraryCache(
if (shouldDownloadArchive(archiveName)) {
val tmpArchivePath = globalTmpDir / archiveName

val download = access.downloadArchive(
archiveName,
tmpArchivePath / archiveName
)
val download = access.downloadArchive(archiveName, tmpArchivePath)
progressReporter.trackProgress(
s"Downloading [$archiveName] of [$libraryName].",
download
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.enso.librarymanager.published.repository

import org.apache.commons.compress.archivers.tar.{
TarArchiveEntry,
TarArchiveOutputStream
}
import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream

import java.io.{BufferedOutputStream, FileOutputStream}
import java.nio.file.Path
import scala.util.Using

object ArchiveWriter {
sealed trait FileToWrite {
def relativePath: String
}

case class TextFile(relativePath: String, content: String) extends FileToWrite

def writeTarArchive(path: Path, files: Seq[FileToWrite]): Unit = {
Using(new FileOutputStream(path.toFile)) { outputStream =>
Using(new BufferedOutputStream(outputStream)) { bufferedStream =>
Using(new GzipCompressorOutputStream(bufferedStream)) { gzipStream =>
Using(new TarArchiveOutputStream(gzipStream)) { archive =>
for (file <- files) {
file match {
case TextFile(relativePath, content) =>
val entry = new TarArchiveEntry(relativePath)
archive.putArchiveEntry(entry)
archive.write(content.getBytes)
archive.closeArchiveEntry()
}
}
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ abstract class DummyRepository {
.resolve(lib.libraryName.name)
.resolve(lib.version.toString)
Files.createDirectories(libraryRoot)
val pkg = createLibraryProject(libraryRoot, lib)
FileSystem.writeTextFile(
pkg.sourceDir.toPath.resolve("Main.enso"),
lib.mainContent
createLibraryProject(libraryRoot, lib)
val files = Seq(
ArchiveWriter.TextFile("src/Main.enso", lib.mainContent)
)
ArchiveWriter.writeTarArchive(libraryRoot.resolve("main.tgz"), files)
createManifest(libraryRoot)
}
}

Expand All @@ -51,6 +52,15 @@ abstract class DummyRepository {
pkg
}

private def createManifest(path: Path): Unit = {
FileSystem.writeTextFile(
path.resolve("manifest.yaml"),
s"""archives:
| - main.tgz
|""".stripMargin
)
}

def createEdition(repoUrl: String): RawEdition = {
Editions.Raw.Edition(
parent = Some(buildinfo.Info.currentEdition),
Expand All @@ -62,8 +72,9 @@ abstract class DummyRepository {
)
}

def runServer(port: Int, root: Path): Process = {
val serverDirectory = Path.of("../../../tools/simple-library-server")
def startServer(port: Int, root: Path): Process = {
val serverDirectory =
Path.of("tools/simple-library-server").toAbsolutePath.normalize
(new ProcessBuilder)
.command(
"node",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
package org.enso.librarymanager.published.repository

import org.enso.distribution.FileSystem
import org.enso.cli.task.{ProgressReporter, TaskProgress}
import org.enso.distribution.locking.{
LockUserInterface,
Resource,
ResourceManager,
ThreadSafeFileLockManager
}
import org.enso.distribution.{FileSystem, TemporaryDirectoryManager}
import org.enso.editions.Editions
import org.enso.librarymanager.published.cache.DownloadingLibraryCache
import org.enso.pkg.PackageManager
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpec

Expand All @@ -13,29 +21,55 @@ class LibraryDownloadTest extends AnyWordSpec with Matchers {
"DownloadingLibraryCache" should {
"be able to download and install libraries from a repository" in {
val repo = new ExampleRepository
FileSystem.withTemporaryDirectory("enso-test-repo") { repoRoot =>
FileSystem.withTemporaryDirectory("enso-test-lib") { tmp =>
val repoRoot = tmp.resolve("repo")
repo.createRepository(repoRoot)
val server = repo.runServer(port, repoRoot)

// TODO
val cache = new DownloadingLibraryCache(???, ???, ???, ???, ???)
val lockManager = new ThreadSafeFileLockManager(tmp.resolve("locks"))
val resourceManager = new ResourceManager(lockManager)
val cache = new DownloadingLibraryCache(
cacheRoot = tmp.resolve("cache"),
temporaryDirectoryManager =
new TemporaryDirectoryManager(tmp.resolve("tmp"), resourceManager),
resourceManager = resourceManager,
lockUserInterface = new LockUserInterface {
override def startWaitingForResource(resource: Resource): Unit =
println(s"Waiting for ${resource.name}")

cache.findCachedLibrary(
repo.testLib.libraryName,
repo.testLib.version
) shouldBe empty
override def finishWaitingForResource(resource: Resource): Unit =
println(s"${resource.name} is ready")
},
progressReporter = new ProgressReporter {
override def trackProgress(
message: String,
task: TaskProgress[_]
): Unit = {}
}
)

cache
.findOrInstallLibrary(
val server = repo.startServer(port, repoRoot)
try {
cache.findCachedLibrary(
repo.testLib.libraryName,
repo.testLib.version,
Editions
.Repository("test_repo", s"http://localhost:$port/libraries")
)
.get
repo.testLib.version
) shouldBe empty

server.destroy()
server.waitFor()
val libPath = cache
.findOrInstallLibrary(
repo.testLib.libraryName,
repo.testLib.version,
Editions
.Repository("test_repo", s"http://localhost:$port/libraries")
)
.get
val pkg = PackageManager.Default.loadPackage(libPath.toFile).get
pkg.name shouldEqual "Bar"
val sources = pkg.listSources
sources should have size 1
sources.head.file.getName shouldEqual "Main.enso"
} finally {
server.destroy()
server.waitFor()
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ object DefaultDistributionConfiguration

/** @inheritdoc */
lazy val temporaryDirectoryManager =
new TemporaryDirectoryManager(distributionManager, resourceManager)
TemporaryDirectoryManager(distributionManager, resourceManager)

lazy val componentConfiguration: RuntimeComponentConfiguration =
new GraalVMComponentConfiguration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class TestDistributionConfiguration(
lazy val editionManager: EditionManager = EditionManager(distributionManager)

lazy val temporaryDirectoryManager =
new TemporaryDirectoryManager(distributionManager, resourceManager)
TemporaryDirectoryManager(distributionManager, resourceManager)

lazy val componentConfig = new GraalVMComponentConfiguration

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class RuntimeVersionManagerTest

val resourceManager = TestLocalResourceManager.create()
val temporaryDirectoryManager =
new TemporaryDirectoryManager(distributionManager, resourceManager)
TemporaryDirectoryManager(distributionManager, resourceManager)
val componentConfig = new GraalVMComponentConfiguration

val runtimeVersionManager = new RuntimeVersionManager(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ class ConcurrencyTest
}

val temporaryDirectoryManager =
new TemporaryDirectoryManager(distributionManager, resourceManager)
TemporaryDirectoryManager(distributionManager, resourceManager)
val componentConfig = new GraalVMComponentConfiguration
val componentsManager = new RuntimeVersionManager(
TestRuntimeVersionManagementUserInterface.default,
Expand Down

0 comments on commit 322a9f6

Please sign in to comment.