test: bats versus getStorageUsed.sh

This commit is contained in:
Ravinou 2024-10-27 13:31:32 +01:00
commit 893c86dc75
No known key found for this signature in database
GPG key ID: EEEE670C40F6A4D7
2 changed files with 85 additions and 1 deletions

View file

@ -1,12 +1,18 @@
FROM bash:latest
RUN apk add --no-cache bash bats git openssl borgbackup jq coreutils
RUN apk add --no-cache \
bats \
openssl \
borgbackup \
jq \
coreutils
COPY helpers/shells/ /test/scripts/
COPY tests/bats/createRepo.bats /test/tests/createRepo.bats
COPY tests/bats/deleteRepo.bats /test/tests/deleteRepo.bats
COPY tests/bats/updateRepo.bats /test/tests/updateRepo.bats
COPY tests/bats/getLastSave.bats /test/tests/getLastSave.bats
COPY tests/bats/getStorageUsed.bats /test/tests/getStorageUsed.bats
RUN chmod +x /test/scripts/*.sh

View file

@ -0,0 +1,78 @@
#!/usr/bin/env bats
setup() {
export home="/tmp/borgwarehouse"
mkdir -p "${home}/repos/repo1"
mkdir -p "${home}/repos/repo2"
mkdir -p "${home}/repos/repo3"
# Create files with different sizes
dd if=/dev/zero of="${home}/repos/repo1/file1" bs=1K count=32
dd if=/dev/zero of="${home}/repos/repo2/file1" bs=1K count=1156
dd if=/dev/zero of="${home}/repos/repo3/file1" bs=1K count=112
echo "home=${home}" > "${home}/.env"
}
teardown() {
rm -rf /tmp/borgwarehouse
}
@test "Test getStorageUsed.sh returns the size of all repositories in JSON format" {
run bash /test/scripts/getStorageUsed.sh
# Expected output in JSON format with my fake files
expected_output='[
{
"size": 36,
"name": "repo1"
},
{
"size": 1160,
"name": "repo2"
},
{
"size": 116,
"name": "repo3"
}
]'
normalized_output=$(echo "$output" | jq .)
normalized_expected_output=$(echo "$expected_output" | jq .)
[ "$status" -eq 0 ]
[ "$normalized_output" == "$normalized_expected_output" ]
}
@test "Test getStorageUsed.sh when no repositories exist" {
# Delete all repositories
rm -rf "${home}/repos"
mkdir -p "${home}/repos"
run bash /test/scripts/getStorageUsed.sh
normalized_expected_output='[]'
normalized_output=$(echo "$output" | jq .)
[ "$status" -eq 0 ]
[ "$normalized_output" == "$normalized_expected_output" ]
}
@test "Test getStorageUsed.sh with only one repository" {
# Keep only one repository
rm -rf "${home}/repos/repo2" "${home}/repos/repo3"
run bash /test/scripts/getStorageUsed.sh
expected_output='[{"size": 36, "name": "repo1"}]'
normalized_output=$(echo "$output" | jq .)
normalized_expected_output=$(echo "$expected_output" | jq .)
echo "$normalized_output"
echo "$normalized_expected_output"
[ "$status" -eq 0 ]
[ "$normalized_output" == "$normalized_expected_output" ]
}