add db_upgrade test

We have a database schema upgrade path, but so far nothing that verifies that
we don't break it. It almost seems like the user data weren't important to us!?

Add a db upgrade test:
- Create a db with an .sql dump taken from a db created with an old osmo-hlr,
  producing DB schema version 0.
- Run osmo-hlr --db-upgrade --db-check
- Verify that the upgrade exited successfully.
- Verify that restarting with the upgraded DB works.

If python tests are enabled, also:
- create a new database using the new osmo-hlr (just built).
- replay a VTY transcript to create subscribers as in the old snapshot.
- replay some sql modifications as done in the old snapshot.
- Get a list of sorted table names,
- a list of their sorted columns with all their properties,
- and dump the table contents in a column- and value-sorted way.
- Compare the resulting dumps and error if there are any diffs.
(This is how I found the difference in the imei column that was fixed in
I68a00014a3d603fcba8781470bc5285f78b538d0)

Change-Id: I0961bab0e17cfde5b030576c5bc243c2b51d9dc4
diff --git a/tests/db_upgrade/db_upgrade_test.sh b/tests/db_upgrade/db_upgrade_test.sh
new file mode 100755
index 0000000..bf56c56
--- /dev/null
+++ b/tests/db_upgrade/db_upgrade_test.sh
@@ -0,0 +1,83 @@
+#!/bin/sh
+srcdir="${1:-.}"
+builddir="${2:-.}"
+do_equivalence_test="$3"
+
+set -e
+
+db="$builddir/test.db"
+osmo_hlr="$builddir/../../src/osmo-hlr"
+cfg="$srcdir/osmo-hlr.cfg"
+
+dump_sorted_schema(){
+	db_file="$1"
+	tables="$(sqlite3 "$db_file" "SELECT name FROM sqlite_master WHERE type = 'table' order by name")"
+	for table in $tables; do
+		echo
+		echo "Table: $table"
+		sqlite3 -header "$db_file" "SELECT name,type,\"notnull\",dflt_value,pk FROM PRAGMA_TABLE_INFO('$table') order by name;"
+		echo
+		echo "Table $table contents:"
+		columns="$(sqlite3 "$db_file" "SELECT name FROM PRAGMA_TABLE_INFO('$table') order by name;")"
+		sqlite3 -header "$db_file" "SELECT $(echo $columns | sed 's/ /,/g') from $table;"
+	done
+}
+
+rm -f "$db"
+echo "Creating db in schema version 0"
+sqlite3 "$db" < "$srcdir/hlr_db_v0.sql"
+
+echo
+echo "Version 0 db:"
+dump_sorted_schema "$db"
+
+set +e
+
+echo
+echo "Launching osmo-hlr to upgrade db:"
+echo osmo-hlr --database '$db' --db-upgrade --db-check --config-file '$srcdir/osmo-hlr.cfg'
+"$osmo_hlr" --database "$db" --db-upgrade --db-check --config-file "$cfg" >log 2>&1
+echo "rc = $?"
+cat log | sed 's@[^ "]*/@<PATH>@g'
+
+echo
+echo "Resulting db:"
+dump_sorted_schema "$db"
+
+echo
+echo "Verify that osmo-hlr can open it:"
+echo osmo-hlr --database '$db' --db-check --config-file '$srcdir/osmo-hlr.cfg'
+"$osmo_hlr" --database "$db" --db-check --config-file "$cfg" >log 2>&1
+echo "rc = $?"
+cat log | sed 's@[^ "]*/@<PATH>@g'
+
+if [ -n "$do_equivalence_test" ]; then
+	# this part requires osmo_interact_vty.py, so this test is not part of the normal run
+	set -e -x
+	mint_db="$builddir/mint.db"
+	rm -f "$mint_db"
+
+	osmo_verify_transcript_vty.py -v \
+		-n OsmoHLR -p 4258 \
+		-r "$osmo_hlr -c $cfg -l $mint_db" \
+		"$srcdir/create_subscribers.vty"
+	sqlite3 "$mint_db" < "$srcdir/create_subscribers_step2.sql"
+
+	set +x
+	test_dump="$builddir/test.dump"
+	mint_dump="$builddir/mint.dump"
+
+	dump_sorted_schema "$db" > "$test_dump"
+	dump_sorted_schema "$mint_db" > "$mint_dump"
+
+	echo
+	echo "Newly created sorted schema is:"
+	cat "$mint_dump"
+	echo
+	echo "Diff to upgraded schema:"
+	diff -u "$mint_dump" "$test_dump"
+	echo "rc=$?"
+fi
+
+rm -f log
+rm -f "$db"