diff --git a/app/src/main/java/com/droiddeck/launcher/agent/AgentSetup.kt b/app/src/main/java/com/droiddeck/launcher/agent/AgentSetup.kt index 573814f00c..4a2e07b45b 100644 --- a/app/src/main/java/com/droiddeck/launcher/agent/AgentSetup.kt +++ b/app/src/main/java/com/droiddeck/launcher/agent/AgentSetup.kt @@ -126,6 +126,16 @@ object AgentSetup { Thread({ for ((step, command) in probes) probe(app, harness, step, command) }, "agent-checks").start() } + /** The last failed refresh ends the script: the installs after it would run from stale indexes and could report success. */ + internal fun installScript(harnessInstall: String) = "set -e; export DEBIAN_FRONTEND=noninteractive; " + + "apt() { apt-get -o Acquire::Retries=5 -o Acquire::http::Timeout=30 \"\$@\"; }; " + + "dpkg --configure -a || echo 'Some packages are not configured yet; repairing dependencies'; " + + "for i in 1 2 3; do apt -o APT::Update::Error-Mode=any update -q && break; " + + "[ \$i = 3 ] && { echo 'Some package sources could not be refreshed'; exit 1; }; " + + "echo 'Some package sources could not be refreshed; retrying'; sleep 3; done; " + + "apt -f install -y --fix-missing; " + + "apt install -y --no-install-recommends git gh adb curl ca-certificates python3; $harnessInstall" + fun runsAsRoot(step: Step) = step == Step.INSTALL /** Runs [step]'s action. [CONNECTION] is the app's own pairing flow, so the caller opens it. */ @@ -140,12 +150,7 @@ object AgentSetup { val command = override ?: when (step) { Step.CONNECTION -> return Step.INSTALL -> { - listOf("sh", "-c", "set -e; export DEBIAN_FRONTEND=noninteractive; " + - "apt() { apt-get -o Acquire::Retries=5 -o Acquire::http::Timeout=30 \"\$@\"; }; " + - "dpkg --configure -a || echo 'Some packages are not configured yet; repairing dependencies'; " + - "for i in 1 2 3; do apt -o APT::Update::Error-Mode=any update -q && break; echo 'Some package sources could not be refreshed; retrying'; sleep 3; done; " + - "apt -f install -y --fix-missing; " + - "apt install -y --no-install-recommends git gh adb curl ca-certificates python3; ${harness.install}") + listOf("sh", "-c", installScript(harness.install)) } Step.SIGN_IN -> harness.signIn Step.GITHUB -> listOf("dd-gh-login") diff --git a/app/src/test/java/com/droiddeck/launcher/agent/AgentSetupInstallScriptTest.kt b/app/src/test/java/com/droiddeck/launcher/agent/AgentSetupInstallScriptTest.kt new file mode 100644 index 0000000000..bc36c84fe6 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/agent/AgentSetupInstallScriptTest.kt @@ -0,0 +1,50 @@ +package com.droiddeck.launcher.agent + +import java.io.File +import org.junit.Assert.assertEquals +import org.junit.Assert.assertTrue +import org.junit.Assume.assumeTrue +import org.junit.Rule +import org.junit.Test +import org.junit.rules.TemporaryFolder + +class AgentSetupInstallScriptTest { + @get:Rule val tmp = TemporaryFolder() + + private fun run(updateSucceedsOnCall: Int): Pair> { + val sh = listOf("/bin/sh", "/usr/bin/sh").firstOrNull { File(it).canExecute() } + assumeTrue(sh != null) + val bin = tmp.newFolder("bin") + val calls = File(tmp.root, "calls") + fun tool(name: String, body: String) = File(bin, name).apply { writeText("#!/bin/sh\n$body\n"); setExecutable(true) } + tool("dpkg", "exit 0") + tool("sleep", "exit 0") + tool("apt-get", """ + echo "${'$'}*" >> '${calls.path}' + case "${'$'}*" in + *" update "*) n=${'$'}(grep -c ' update ' '${calls.path}'); [ "${'$'}n" -ge $updateSucceedsOnCall ] ;; + *) exit 0 ;; + esac + """.trimIndent()) + val child = ProcessBuilder(sh, "-c", AgentSetup.installScript("echo harness-installed >> '${calls.path}'")) + .apply { environment()["PATH"] = bin.path + File.pathSeparator + System.getenv("PATH") } + .redirectErrorStream(true).start() + child.inputStream.readBytes() + val exit = child.waitFor() + return exit to (if (calls.exists()) calls.readLines() else emptyList()) + } + + @Test fun theInstallStopsWhenEveryUpdateAttemptFails() { + val (exit, calls) = run(updateSucceedsOnCall = 99) + assertTrue(exit != 0) + assertEquals(3, calls.count { " update " in "$it " }) + assertTrue(calls.none { "install" in it }) + } + + @Test fun theInstallContinuesWhenALaterUpdateAttemptSucceeds() { + val (exit, calls) = run(updateSucceedsOnCall = 3) + assertEquals(0, exit) + assertTrue(calls.any { "install -y --no-install-recommends" in it }) + assertTrue("harness-installed" in calls.last()) + } +}