aboutsummaryrefslogtreecommitdiff
path: root/tests.sh
blob: f5c4120054af30a0678e1327f17baa683d852f1a (plain)
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
#!/bin/bash

source server.sh

# TODO: testing remote directories
BACKUP_DIRS=(".bak")
mkdir ".bak"

function test_backend() {
	BACKUP_BACKEND="$1"

	# doing tests inside /tmp/
	DIR=$(mktemp -d)
	cd "$DIR"
	local status=$?
	if [ $status -ne 0 ] ; then
		echo "Failed to make a temporary directory"
		return 1
	fi
	function cleanup() {
		rm -r "$DIR"
	}

	# make a "world" with some volatile data
	function make_world() {
		mkdir -p "$DIR/$1/DIM0"
		date > "$1/data0"
		shuf -e {1..100} | tr '\n' ' ' > "$DIR/$1/DIM0/data1"
	}

	# make two versions of a world and back up both
	make_world "$WORLD_NAME"
	local old_world="${WORLD_NAME}.orig0"
	cp -r "$DIR/$WORLD_NAME" "$DIR/$old_world"
	if ! server_backup ; then
		cleanup
		exit
	fi

	# backup time in archive's name is specified up to seconds, so subsequent backups without some delay will have the same name and previous backup be overwritten
	if [ $BACKUP_BACKEND = "tar" ]; then
		sleep 1
	fi

	make_world "$WORLD_NAME"
	local new_world="${WORLD_NAME}.orig1"
	cp -r "$DIR/$WORLD_NAME" "$DIR/$new_world"
	if ! server_backup ; then
		cleanup
		exit
	fi

	function same_world() {
		delta=$(diff -r "$DIR/$1" "$DIR/$2")
		if [ -z "$delta" ] ; then
			return 0
		fi
		return 1
	}

	# corrupting current (new) world
	find "$DIR/$WORLD_NAME" -type f -exec shred {} \;
	if same_world "$WORLD_NAME" "$new_world" ; then
		echo "Failed to corrupt new world"
		cleanup
		exit
	fi


	# restore new backup
	server_restore "${BACKUP_DIRS[0]}" 0
	# must be: new backup == new world
	if ! same_world "$WORLD_NAME" "$new_world" ; then
		echo "${BACKUP_BACKEND}: new backup != new world"
		cleanup
		exit
	fi
	# must be: new backup != old world
	if same_world "$WORLD_NAME" "$old_world" ; then
		echo "${BACKUP_BACKEND}: new backup == old world"
		cleanup
		exit
	fi


	# restore old backup
	if [ $BACKUP_BACKEND = "bup" ]; then
		# bup's 0th option is "latest", which links to 1st option, this is not present in tar and borg
		server_restore "${BACKUP_DIRS[0]}" 2
	else
		server_restore "${BACKUP_DIRS[0]}" 1
	fi
	# must be: old backup == old world
	if ! same_world "$WORLD_NAME" "$old_world" ; then
		echo "${BACKUP_BACKEND}: old backup != old world"
		cleanup
		exit
	fi
	# must be: old backup != new world
	if same_world "$WORLD_NAME" "$new_world" ; then
		echo "${BACKUP_BACKEND}: old backup == new world"
		cleanup
		exit
	fi

	cleanup
}

echo "Testing tar backend"
test_backend "tar"

echo "Testing bup backend"
test_backend "bup"

echo "Testing borg backend"
test_backend "borg"

echo "All tests passed"