Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(feature) (types) Add 'dnf' type #35

Merged
merged 4 commits into from
Oct 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions docs/_types/dnf.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
name: dnf
---
asserts packages installed via dnf on CentOS or RedHat


### Usage

```bash
* dnf package-name
```
1 change: 1 addition & 0 deletions test/fixtures/dnf-list-updates.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
outdated_package
59 changes: 59 additions & 0 deletions test/type-dnf.bats
Original file line number Diff line number Diff line change
@@ -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' ]
}
32 changes: 32 additions & 0 deletions types/dnf.sh
Original file line number Diff line number Diff line change
@@ -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