diff --git a/docs/_types/dnf.md b/docs/_types/dnf.md new file mode 100644 index 0000000..bb21949 --- /dev/null +++ b/docs/_types/dnf.md @@ -0,0 +1,11 @@ +--- +name: dnf +--- +asserts packages installed via dnf on CentOS or RedHat + + +### Usage + +```bash +* dnf package-name +``` diff --git a/test/fixtures/dnf-list-updates.txt b/test/fixtures/dnf-list-updates.txt new file mode 100644 index 0000000..4fa4d83 --- /dev/null +++ b/test/fixtures/dnf-list-updates.txt @@ -0,0 +1 @@ +outdated_package diff --git a/test/type-dnf.bats b/test/type-dnf.bats new file mode 100755 index 0000000..ee8d693 --- /dev/null +++ b/test/type-dnf.bats @@ -0,0 +1,59 @@ +#!/usr/bin/env bats + +. test/helpers.sh + +dnf () { . $BORK_SOURCE_DIR/types/dnf.sh $*; } + +setup () { + respond_to "uname -s" "echo Linux" + respond_to "rpm -qa" "cat $fixtures/rpm-qa.txt" + respond_to "sudo dnf list updates" "cat $fixtures/dnf-list-updates.txt" +} + +@test "dnf status reports incorrect platform" { + respond_to "uname -s" "echo Darwin" + run dnf status some_package + [ "$status" -eq $STATUS_UNSUPPORTED_PLATFORM ] +} + +@test "dnf status reports missing dnf" { + respond_to "which dnf" "return 1" + run dnf status some_package + [ "$status" -eq $STATUS_FAILED_PRECONDITION ] +} + +@test "dnf status reports a package is missing" { + run dnf status missing_package + [ "$status" -eq $STATUS_MISSING ] +} + +@test "dnf status reports a package is outdated" { + run dnf status outdated_package + [ "$status" -eq $STATUS_OUTDATED ] +} + +@test "dnf status reports a package is current" { + run dnf status current_package + [ "$status" -eq $STATUS_OK ] +} + +@test "dnf install runs 'dnf install'" { + run dnf install missing_package + [ "$status" -eq $STATUS_OK ] + run baked_output + [ "$output" = 'sudo dnf -y install missing_package' ] +} + +@test "dnf upgrade runs 'dnf install'" { + run dnf upgrade outdated_package + [ "$status" -eq $STATUS_OK ] + run baked_output + [ "$output" = 'sudo dnf -y install outdated_package' ] +} + +@test "dnf remove runs 'dnf install'" { + run dnf remove unwanted_package + [ "$status" -eq $STATUS_OK ] + run baked_output + [ "$output" = 'sudo dnf -y remove unwanted_package' ] +} diff --git a/types/dnf.sh b/types/dnf.sh new file mode 100755 index 0000000..bdb5bfb --- /dev/null +++ b/types/dnf.sh @@ -0,0 +1,32 @@ + +action=$1 +name=$2 +shift 2 +case $action in + desc) + echo "asserts packages installed via dnf on Fedora, CentOS or RedHat" + echo "* dnf package-name" + ;; + status) + baking_platform_is "Linux" || return $STATUS_UNSUPPORTED_PLATFORM + needs_exec "dnf" 0 + [ "$?" -gt 0 ] && return $STATUS_FAILED_PRECONDITION + + echo "$(bake rpm -qa)" | grep "^$name" + [ "$?" -gt 0 ] && return $STATUS_MISSING + + echo "$(bake sudo dnf list updates)" | grep "^$name" + [ "$?" -eq 0 ] && return $STATUS_OUTDATED + return $STATUS_OK + ;; + + install|upgrade) + bake sudo dnf -y install $name + ;; + + remove) + bake sudo dnf -y remove $name + ;; + + *) return 1 ;; +esac