From e8fcdae778c6ee53609291b53e585e2258453822 Mon Sep 17 00:00:00 2001 From: ansgarz Date: Sun, 30 Jul 2023 19:45:35 +0200 Subject: [PATCH] improve installPpaFirefox to set priority for ppa above default (snap) installation and allow unattended upgrades --- .../provs/desktop/infrastructure/Firefox.kt | 65 +++++++++++-------- .../desktop/infrastructure/FirefoxKtTest.kt | 38 +++++++++-- 2 files changed, 71 insertions(+), 32 deletions(-) diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/Firefox.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/Firefox.kt index 7056fe5..c785b7c 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/Firefox.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/Firefox.kt @@ -1,42 +1,53 @@ package org.domaindrivenarchitecture.provs.desktop.infrastructure import org.domaindrivenarchitecture.provs.framework.core.Prov -import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.addTextToFile -import org.domaindrivenarchitecture.provs.framework.ubuntu.install.base.aptInstall -import java.io.File +import org.domaindrivenarchitecture.provs.framework.core.ProvResult +import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.checkFile +import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.createFile +import org.domaindrivenarchitecture.provs.framework.ubuntu.install.base.aptInstallFromPpa /** - * Installs non-snap firefox, removing a firefox snap-installation if existing + * Installs ppa firefox (i.e. non-snap), removing snap-firefox if existing. */ -fun Prov.installPpaFirefox() = task { +fun Prov.installPpaFirefox() = taskWithResult { - // inspired by: https://www.omgubuntu.co.uk/2022/04/how-to-install-firefox-deb-apt-ubuntu-22-04 + // inspired by: https://wiki.ubuntuusers.de/Firefox/Installation/PPA/ - task("remove snap firefox") { - if (chk("snap list | grep firefox")) { - cmd("snap remove firefox", sudo = true) - } + val unattendeUpgradesForPpaFirefox = "/etc/apt/apt.conf.d/51unattended-upgrades-firefox" + + val preCondition = checkFile(unattendeUpgradesForPpaFirefox) + if (preCondition) { + return@taskWithResult ProvResult(true, out = "Firefox already installed with ppa") } - aptInstall("software-properties-common") - cmd("add-apt-repository -y ppa:mozillateam/ppa", sudo = true) + cmd("sudo apt-get -qy remove firefox", sudo = true) + optional { + cmd("snap remove firefox", sudo = true) + } - // set prio in order to use ppa-firefox above snap - addTextToFile( - "\nPackage: *\n" + - "Pin: release o=LP-PPA-mozillateam\n" + - "Pin-Priority: 1001\n", - File("/etc/apt/preferences.d/mozilla-firefox"), + createFile("/etc/apt/preferences.d/mozillateam", mozillaTeamFileContent, sudo = true) + + aptInstallFromPpa("mozillateam", "ppa", "firefox") + + createFile( + unattendeUpgradesForPpaFirefox, + "Unattended-Upgrade::Allowed-Origins:: \"LP-PPA-mozillateam:\${distro_codename}\";\n", sudo = true ) - - addTextToFile( - """Unattended-Upgrade::Allowed-Origins:: "LP-PPA-mozillateam:${'$'}{distro_codename}";""", - File("/etc/apt/preferences.d/mozilla-firefox"), - sudo = true - ) - - aptInstall("firefox") - cmd("apt-get upgrade -y --allow-downgrades firefox", sudo = true) } + + +private val mozillaTeamFileContent = """ + Package: * + Pin: release o=LP-PPA-mozillateam + Pin-Priority: 100 + + Package: firefox* + Pin: release o=LP-PPA-mozillateam + Pin-Priority: 1001 + + Package: firefox* + Pin: release o=Ubuntu + Pin-Priority: -1 +""".trimIndent() \ No newline at end of file diff --git a/src/test/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/FirefoxKtTest.kt b/src/test/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/FirefoxKtTest.kt index 8a30473..88b7290 100644 --- a/src/test/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/FirefoxKtTest.kt +++ b/src/test/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/FirefoxKtTest.kt @@ -1,32 +1,60 @@ package org.domaindrivenarchitecture.provs.desktop.infrastructure import org.domaindrivenarchitecture.provs.framework.core.remote +import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.deleteFile +import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.fileContainsText import org.domaindrivenarchitecture.provs.framework.ubuntu.install.base.checkPackageInstalled +import org.domaindrivenarchitecture.provs.framework.ubuntu.install.base.isPackageInstalled import org.domaindrivenarchitecture.provs.framework.ubuntu.secret.secretSources.PromptSecretSource import org.domaindrivenarchitecture.provs.test.defaultTestContainer import org.domaindrivenarchitecture.provs.test.tags.ExtensiveContainerTest +import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Assertions.assertTrue import org.junit.jupiter.api.Disabled import org.junit.jupiter.api.Test internal class FirefoxKtTest { - // Attention: this test does not test full functionality of installFirefox, e.g. does not test - // remove snap, as this test runs against a container which does not have snap-firefox installed + // Attention: this test does not test full functionality of installPpaFirefox, e.g. does not test + // remove snap-firefox, as this test runs against a container, which does have neither snap nor snap-firefox installed @ExtensiveContainerTest fun installFirefox() { + // given + val prov = defaultTestContainer() + // when - val result = defaultTestContainer().session { + val result = prov.session { + deleteFile("/etc/apt/apt.conf.d/51unattended-upgrades-firefox", sudo = true) + deleteFile("/etc/apt/preferences.d/mozillateam", sudo = true) installPpaFirefox() - checkPackageInstalled("firefox") } + val result2 = prov.installPpaFirefox() // then assertTrue(result.success) + assertEquals("Firefox already installed with ppa", result2.out) + + assertTrue(prov.isPackageInstalled("firefox")) + assertTrue( + prov.fileContainsText( + "/etc/apt/apt.conf.d/51unattended-upgrades-firefox", + "Unattended-Upgrade::Allowed-Origins:: \"LP-PPA-mozillateam:\${distro_codename}\";\n", + sudo = true + ) + ) + val expectedPolicyLine = Regex("1001? https?://ppa.launchpad(?:content)?.net/mozillateam/ppa/ubuntu") + val policy = prov.cmd("apt policy firefox").out + assertTrue( + policy?.contains(expectedPolicyLine) ?: false, + "$expectedPolicyLine was not found in $policy" + ) } + /** + * Tests installing firefox on a remote machine, e.g. a virtual machine + */ @Test - @Disabled("Update connection details,then enable and run manually") + @Disabled("Update connection details, then enable the test and run manually") fun installFirefox_remotely() { val host = "192.168.56.123" val user = "user"