Compare commits

...
2 changed files with 61 additions and 6 deletions

No files matched your search

@@ -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")
@@ -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<Int, List<String>> {
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())
}
}