Skip to content

Commit 0298e71

Browse files
authored
Merge pull request #339 from Ravinou/develop
Integrate unit tests with Bats for shell scripts
2 parents 6a6bf3f + 7f61e46 commit 0298e71

File tree

15 files changed

+536
-30
lines changed

15 files changed

+536
-30
lines changed

.github/workflows/bats.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: Bats
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
- develop
8+
pull_request:
9+
branches:
10+
- main
11+
- develop
12+
jobs:
13+
bats-test:
14+
name: Run bats tests against shells
15+
runs-on: ubuntu-latest
16+
17+
steps:
18+
- name: Checkout
19+
uses: actions/checkout@v4
20+
21+
- name: Set up Docker Buildx
22+
uses: docker/setup-buildx-action@v3
23+
24+
- name: Build container & run bats tests
25+
run: |
26+
docker compose -f tests/bats/docker-compose.yml up --abort-on-container-exit --build
Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
name: Test Docker Container Build on Pull Request
1+
name: Test to build docker container on Pull Request
22

33
on:
4-
pull_request:
5-
branches:
6-
- main
7-
- develop
4+
pull_request:
5+
branches:
6+
- main
7+
- develop
88

99
jobs:
10-
build-container:
11-
runs-on: ubuntu-latest
12-
steps:
13-
- name: Checkout
14-
uses: actions/checkout@v4
15-
- name: Set up QEMU
16-
uses: docker/setup-qemu-action@v3
17-
- name: Set up Docker Buildx
18-
uses: docker/setup-buildx-action@v3
19-
- name: Build Docker Container
20-
run: |
21-
docker buildx build --platform linux/amd64,linux/arm64 -t borgwarehouse:pr-${{ github.event.pull_request.number }} .
10+
build-container:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- name: Checkout
14+
uses: actions/checkout@v4
15+
- name: Set up QEMU
16+
uses: docker/setup-qemu-action@v3
17+
- name: Set up Docker Buildx
18+
uses: docker/setup-buildx-action@v3
19+
- name: Build BorgWarehouse Container
20+
run: |
21+
docker buildx build --platform linux/amd64,linux/arm64 -t borgwarehouse:pr-${{ github.event.pull_request.number }} .

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,7 @@ config/repo.json
111111
config/users.json
112112

113113
# docker files
114-
docker-compose.yml
114+
docker-compose.yml
115+
116+
# Commit tests docker-compose
117+
!tests/bats/docker-compose.yml

helpers/shells/createRepo.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pool="${home}/repos"
3131
authorized_keys="${home}/.ssh/authorized_keys"
3232

3333
# Check args
34-
if [ "$1" == "" ] || [ "$2" == "" ] || [ "$3" != "true" ] && [ "$3" != "false" ];then
34+
if [ "$1" == "" ] || [ "$2" == "" ] || ! [[ "$2" =~ ^[0-9]+$ ]] || [ "$3" != "true" ] && [ "$3" != "false" ]; then
3535
echo -n "This shell takes 3 arguments : SSH Public Key, Quota in Go [e.g. : 10], Append only mode [true|false]"
3636
exit 1
3737
fi

helpers/shells/deleteRepo.sh

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ if [[ $# -ne 1 || $1 = "" ]]; then
2525
exit 1
2626
fi
2727

28-
# Check if the repositoryName length is 8 char. With createRepo.sh our randoms have a length of 8 characters.
29-
# If we receive another length there is necessarily a problem.
28+
# Check if the repositoryName pattern is an hexa 8 char. With createRepo.sh our randoms are hexa of 8 characters.
29+
# If we receive another pattern there is necessarily a problem.
3030
repositoryName=$1
31-
if [ ${#repositoryName} != 8 ]; then
32-
echo -n "Error with the length of the repositoryName."
33-
exit 2
31+
if ! [[ "$repositoryName" =~ ^[a-f0-9]{8}$ ]]; then
32+
echo "Invalid repository name. Must be an 8-character hex string."
33+
exit 2
3434
fi
3535

3636
# Delete the repository and the line associated in the authorized_keys file

helpers/shells/getStorageUsed.sh

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ fi
2222
# Default value if .env not exists
2323
: "${home:=/home/borgwarehouse}"
2424

25-
# Use jc to output a JSON format with du command
25+
# Get the size of each repository and format as JSON
2626
cd "${home}"/repos
27-
du -s -- * | jc --du
27+
output=$(du -s -- * 2>/dev/null | awk '{print "{\"size\":" $1 ",\"name\":\"" $2 "\"}"}' | jq -s '.')
28+
if [ -z "$output" ]; then
29+
output="[]"
30+
fi
31+
32+
# Print the JSON output
33+
echo "$output"

helpers/shells/updateRepo.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ then
3030
exit 2
3131
fi
3232

33-
# Check if repositoryName length is 8 char. With createRepo.sh our randoms have a length of 8 characters.
34-
# If we receive another length, there is necessarily a problem.
33+
# Check if the repositoryName pattern is an hexa 8 char. With createRepo.sh our randoms are hexa of 8 characters.
34+
# If we receive another pattern there is necessarily a problem.
3535
repositoryName=$1
36-
if [ ${#repositoryName} != 8 ]; then
37-
echo -n "Error with the length of the repositoryName."
36+
if ! [[ "$repositoryName" =~ ^[a-f0-9]{8}$ ]]; then
37+
echo "Invalid repository name. Must be an 8-character hex string."
3838
exit 3
3939
fi
4040

tests/README

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## BATS tests against bash scripts
2+
3+
From `tests/bats`, launch `docker compose up --build`

tests/bats/Dockerfile

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
FROM bash:latest
2+
3+
RUN apk add --no-cache \
4+
bats \
5+
openssl \
6+
borgbackup \
7+
jq \
8+
coreutils
9+
10+
COPY helpers/shells/ /test/scripts/
11+
COPY tests/bats/createRepo.bats /test/tests/createRepo.bats
12+
COPY tests/bats/deleteRepo.bats /test/tests/deleteRepo.bats
13+
COPY tests/bats/updateRepo.bats /test/tests/updateRepo.bats
14+
COPY tests/bats/getLastSave.bats /test/tests/getLastSave.bats
15+
COPY tests/bats/getStorageUsed.bats /test/tests/getStorageUsed.bats
16+
17+
RUN chmod +x /test/scripts/*.sh
18+
19+
CMD ["bats", "/test/tests/"]

tests/bats/createRepo.bats

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#!/usr/bin/env bats
2+
3+
setup() {
4+
# Set up the environment for each test
5+
export home="/tmp/borgwarehouse"
6+
mkdir -p /tmp/borgwarehouse
7+
mkdir -p /tmp/borgwarehouse/repos
8+
mkdir -p /tmp/borgwarehouse/.ssh
9+
touch /tmp/borgwarehouse/.ssh/authorized_keys
10+
11+
# SSH keys samples for testing
12+
export SSH_KEY_ED25519="ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAICtujwncNGgdNxWOCQedMCnrhRZT4B7eUyyFJNryvQj9 publickey"
13+
export SSH_KEY_ED25519_SK="sk-ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAICtujwncNGgdNxWOCQedMCnrhRZT4B7eUyyFJNryvQj9 publickey"
14+
export SSH_KEY_RSA="ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDf8SFSuWqPtPYKjoXL8aowdKYfeKFKbE6w4CvqXPSRtgwKGWJva/UVF8Q/jGClwsVpTJfZnA76fnih76cE4ZiucPtDM2dyDILHNSZo/8rwUVkNB4P3aaCxV6lVMurmIgF4ibWQFBdyWKCJM7nQjO71TlMw/HfqpeYXXdjL1MBlMzqOZYLDrPoiJEiAfKheVeCONMlo8HMfEPxiu7bwfF7vQqYstcbZ55RN1t7RYaxlCTZaj0GOxIGKLmmHTDGzQQIaOGSr3+8Gk1I/MFle2/dYKbBEi97NrJowRO4a4pVbVso0YKyESL3U40uZly1bzoNx4DvMBbFwYSE1IJbs/AQIfB6KH4yLtQTmfb4qPRLCS1CBWBZKeKJ304p6rxKuv+CjagsFwdG5cS7cCosfdEU43QuWngnYQGUwMKskxX/7rPm+WZItN7XiNoMRmzaC+T0cIRXH7Cl7VFE3cbTzgmqJPVeUpccjTP/BdDahHKFqyVhAFvyI7JM4ct1/tU8o015TM1EXzMBeJOxalj6RsuDIFjjEaMN5pZmlHGBFBmcRgY7TqYAwr02maKb9BtcPOGIPgpI3AMzqNX+LjFssI0AuqBGTYN8v6OBr2NmTHfZlnClucjoAw71QPeQySABqrX0p9xieX15Ly1z9oMH9lapW6X9e0JnQBMnz1N2eaq1qAQ== publickey"
15+
}
16+
17+
teardown() {
18+
# Clean up the environment after each test
19+
rm -rf /tmp/borgwarehouse
20+
}
21+
22+
@test "Test createRepo.sh with missing arguments" {
23+
run bash /test/scripts/createRepo.sh
24+
[ "$status" -eq 1 ]
25+
[ "$output" == "This shell takes 3 arguments : SSH Public Key, Quota in Go [e.g. : 10], Append only mode [true|false]" ]
26+
}
27+
28+
@test "Test createRepo.sh with missing Quota and append-only mode arguments" {
29+
run bash /test/scripts/createRepo.sh "$SSH_KEY_ED25519"
30+
[ "$status" -eq 1 ]
31+
[ "$output" == "This shell takes 3 arguments : SSH Public Key, Quota in Go [e.g. : 10], Append only mode [true|false]" ]
32+
}
33+
34+
@test "Test createRepo.sh with missing Append-only mode argument" {
35+
run bash /test/scripts/createRepo.sh "$SSH_KEY_ED25519" 10
36+
echo $output
37+
cat ${home}/.ssh/authorized_keys
38+
[ "$status" -eq 1 ]
39+
[ "$output" == "This shell takes 3 arguments : SSH Public Key, Quota in Go [e.g. : 10], Append only mode [true|false]" ]
40+
}
41+
42+
@test "Test createRepo.sh with invalid SSH key format" {
43+
run bash /test/scripts/createRepo.sh "invalid-key" 10 true
44+
[ "$status" -eq 2 ]
45+
[ "$output" == "Invalid public SSH KEY format. Provide a key in OpenSSH format (rsa, ed25519, ed25519-sk)" ]
46+
}
47+
48+
@test "Test createRepo.sh with invalid Quota format" {
49+
run bash /test/scripts/createRepo.sh "$SSH_KEY_ED25519" "AA" true
50+
[ "$status" -eq 1 ]
51+
[ "$output" == "This shell takes 3 arguments : SSH Public Key, Quota in Go [e.g. : 10], Append only mode [true|false]" ]
52+
}
53+
54+
@test "Test createRepo.sh with invalid Append-only mode format" {
55+
run bash /test/scripts/createRepo.sh "$SSH_KEY_ED25519" 10 blabla
56+
[ "$status" -eq 1 ]
57+
[ "$output" == "This shell takes 3 arguments : SSH Public Key, Quota in Go [e.g. : 10], Append only mode [true|false]" ]
58+
}
59+
60+
61+
@test "Test createRepo.sh if authorized_keys is missing" {
62+
rm /tmp/borgwarehouse/.ssh/authorized_keys
63+
run bash /test/scripts/createRepo.sh "$SSH_KEY_ED25519" 10 true
64+
[ "$status" -eq 5 ]
65+
[ "$output" == "${home}/.ssh/authorized_keys must be present" ]
66+
}
67+
68+
@test "Test createRepo.sh if SSH key is already present in authorized_keys" {
69+
# Add a key
70+
echo "$SSH_KEY_ED25519" > /tmp/borgwarehouse/.ssh/authorized_keys
71+
# Try to re-add the same key
72+
run bash /test/scripts/createRepo.sh "$SSH_KEY_ED25519" 10 true
73+
[ "$status" -eq 3 ]
74+
[ "$output" == "SSH pub key already present in authorized_keys" ]
75+
}
76+
77+
@test "Test createRepo.sh repository name generation" {
78+
run bash /test/scripts/createRepo.sh "$SSH_KEY_ED25519" 10 false
79+
[[ "$output" =~ ^[0-9a-f]{8}$ ]] # Must return a 8 characters hexa string
80+
}
81+
82+
@test "Test createRepo.sh key ED25519 insertion in authorized_keys" {
83+
run bash /test/scripts/createRepo.sh "$SSH_KEY_ED25519" 10 false
84+
expected_line="command=\"cd ${home}/repos;borg serve --restrict-to-path ${home}/repos/${output} --storage-quota 10G\",restrict $SSH_KEY_ED25519"
85+
grep -qF "$expected_line" /tmp/borgwarehouse/.ssh/authorized_keys
86+
}
87+
88+
@test "Test createRepo.sh key ED25519-SK insertion in authorized_keys" {
89+
run bash /test/scripts/createRepo.sh "$SSH_KEY_ED25519_SK" 10 false
90+
expected_line="command=\"cd ${home}/repos;borg serve --restrict-to-path ${home}/repos/${output} --storage-quota 10G\",restrict $SSH_KEY_ED25519_SK"
91+
grep -qF "$expected_line" /tmp/borgwarehouse/.ssh/authorized_keys
92+
}
93+
94+
@test "Test createRepo.sh key RSA insertion in authorized_keys" {
95+
run bash /test/scripts/createRepo.sh "$SSH_KEY_RSA" 10 false
96+
expected_line="command=\"cd ${home}/repos;borg serve --restrict-to-path ${home}/repos/${output} --storage-quota 10G\",restrict $SSH_KEY_RSA"
97+
grep -qF "$expected_line" /tmp/borgwarehouse/.ssh/authorized_keys
98+
}
99+
100+
@test "Test createRepo.sh key ED25519 insertion in authorized_keys with append only mode" {
101+
run bash /test/scripts/createRepo.sh "$SSH_KEY_ED25519" 10 true
102+
expected_line="command=\"cd ${home}/repos;borg serve --append-only --restrict-to-path ${home}/repos/${output} --storage-quota 10G\",restrict $SSH_KEY_ED25519"
103+
grep -qF "$expected_line" /tmp/borgwarehouse/.ssh/authorized_keys
104+
}

0 commit comments

Comments
 (0)