-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_backup.bats
executable file
·147 lines (112 loc) · 3.42 KB
/
test_backup.bats
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env bats
export FTP_USER="test"
export FTP_PASSWORD="test"
remove_container() {
docker rm --force mock_seafile || true
rm --force /tmp/directory-{a,b}
docker rm --force mock_ftp_server || true
return 0
}
create_mock_container() {
touch /tmp/directory-{a,b}
docker run \
--name mock_seafile \
--volume /tmp/directory-a:/container-volume-a \
--volume /tmp/directory-b:/container-volume-b \
--detach busybox
}
create_mock_ftp_server() {
docker run \
--name mock_ftp_server \
--publish 21:21 \
--publish 4559-4564:4559-4564 \
--env FTP_USER="$FTP_USER" \
--env FTP_PASSWORD="$FTP_PASSWORD" \
--detach \
panubo/vsftpd
}
log() {
echo "$(tput setaf 6)status: ${status}$(tput sgr 0)"
echo "$(tput setaf 6)output: ${output}$(tput sgr 0)"
}
@test "--get-volumes: return one volume per line as source:destination" {
create_mock_container
run ./backup.bash --get-volumes "mock_seafile"
volumes_unsorted=($output)
IFS=$'\n' volumes=($(sort <<<"${volumes_unsorted[*]}"))
(( ${#volumes[@]} == 2 ))
[[ ${volumes[0]} == "/tmp/directory-a" ]]
remove_container
}
@test "--help: show help" {
run ./backup.bash --help
[[ "$output" == "Usage:"* ]]
}
@test "-h: show help" {
run ./backup.bash -h
[[ "$output" == "Usage:"* ]]
}
@test "--xyz: invalid argument" {
run ./backup.bash --xyz
[[ "$output" == "Error unknown argument." ]]
}
@test "--create: return archive filepath" {
create_mock_container
container_name="mock_seafile"
archive_filepath="/tmp/$container_name.data-$(date '+%Y-%m-%d').tar.gz"
run ./backup.bash --create "$container_name"
last_line="${lines[@]:(-1)}"
[[ "$last_line" == "$archive_filepath" ]]
rm "$archive_filepath"
remove_container
}
@test "--create: create data-volume archive" {
create_mock_container
container_name="mock_seafile"
archive_filepath="/tmp/$container_name.data-$(date '+%Y-%m-%d').tar.gz"
run ./backup.bash --create "$container_name"
[[ -e "$archive_filepath" ]]
rm "$archive_filepath"
remove_container
}
@test "--create: contains the files" {
create_mock_container
container_name="mock_seafile"
archive_filepath="/tmp/$container_name.data-$(date '+%Y-%m-%d').tar.gz"
run ./backup.bash --create "$container_name"
files_count="$(tar --list --file "$archive_filepath" | wc -l)"
(( $files_count == 2 ))
rm "$archive_filepath"
remove_container
}
@test "--create: set user on archive file" {
create_mock_container
container_name="mock_seafile"
archive_filepath="/tmp/$container_name.data-$(date '+%Y-%m-%d').tar.gz"
run ./backup.bash --create "$container_name"
[[ $(stat -c '%U' "$archive_filepath") == "$USER" ]]
rm "$archive_filepath"
remove_container
}
@test "--send: send archive to FTP" {
create_mock_container
create_mock_ftp_server
docker exec mock_ftp_server chown ftp:ftp -R /srv/
container_name="mock_seafile"
archive_filepath="/tmp/$container_name.data-$(date '+%Y-%m-%d').tar.gz"
./backup.bash --create "$container_name"
FTP_HOST="localhost"
run ./backup.bash --send "$archive_filepath" "$FTP_HOST"
[[ $status == 0 ]]
rm "$archive_filepath"
remove_container
}
@test "--remove: remove temporary archive" {
container_name="mock_seafile"
archive_filepath="/tmp/$container_name.data-$(date '+%Y-%m-%d').tar.gz"
touch "$archive_filepath"
run ./backup.bash --remove "$archive_filepath"
[[ $status == 0 ]]
[[ ! -e $archive_filepath ]]
remove_container
}