From 893c86dc7588f88f44a0727266e0d1dbaafcf0fd Mon Sep 17 00:00:00 2001 From: Ravinou Date: Sun, 27 Oct 2024 13:31:32 +0100 Subject: [PATCH] =?UTF-8?q?test:=20=E2=9C=85=20bats=20versus=20getStorageU?= =?UTF-8?q?sed.sh?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/bats/Dockerfile | 8 +++- tests/bats/getStorageUsed.bats | 78 ++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 tests/bats/getStorageUsed.bats diff --git a/tests/bats/Dockerfile b/tests/bats/Dockerfile index b7834cd..0717179 100644 --- a/tests/bats/Dockerfile +++ b/tests/bats/Dockerfile @@ -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 diff --git a/tests/bats/getStorageUsed.bats b/tests/bats/getStorageUsed.bats new file mode 100644 index 0000000..4c2700b --- /dev/null +++ b/tests/bats/getStorageUsed.bats @@ -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" ] +}