Added unit tests for auto upgrade logic

This commit is contained in:
Shuai Lin 2018-10-13 08:24:06 +08:00
parent 7b76411537
commit a1d1369680
8 changed files with 62 additions and 3 deletions

View file

@ -5,8 +5,8 @@ services:
- docker
install:
# Must put something here, or travis would ask us for requirments.txt
- echo "Nothing to install"
- pip install -r ci/requirements.txt
script:
- ./run-tests.sh
- ci/ci.sh

2
ci/requirements.txt Normal file
View file

@ -0,0 +1,2 @@
pytest==3.7.1
mock==2.0.0

3
pytest.ini Normal file
View file

@ -0,0 +1,3 @@
[pytest]
addopts = -vv -s
log_format = %(asctime)s:%(name)s:%(levelname)s:%(message)s

10
run-tests.sh Executable file
View file

@ -0,0 +1,10 @@
#!/bin/bash
set -e
cd "$( dirname "${BASH_SOURCE[0]}" )"
export PYTHONPATH=$PWD/scripts:$PYTHONPATH
# This env is required by all the scripts
export SEAFILE_VERSION=$(cat image/seafile/Dockerfile | sed -r -n 's/.*SEAFILE_VERSION=([0-9.]+).*/\1/p')
pytest tests/unit

View file

@ -57,6 +57,10 @@ def run_script_and_update_version_stamp(script, new_version):
call(script)
update_version_stamp(new_version)
def is_minor_upgrade(v1, v2):
get_major_version = lambda x: x.split('.')[:2]
return v1 != v2 and get_major_version(v1) == get_major_version(v2)
def check_upgrade():
last_version = read_version_stamp()
current_version = os.environ['SEAFILE_VERSION']
@ -67,7 +71,7 @@ def check_upgrade():
run_script_and_update_version_stamp(minor_upgrade_script, current_version)
return
# Now we do the major upgrade
scripts_to_run = collect_upgrade_scripts(from_version=last_version, to_version=current_version)
for script in scripts_to_run:
loginfo('Running scripts {}'.format(script))

0
tests/unit/__init__.py Normal file
View file

8
tests/unit/conftest.py Normal file
View file

@ -0,0 +1,8 @@
import os
import sys
from os.path import abspath, basename, exists, dirname, join
import pytest
project_dir = join(dirname(abspath(__file__)), '../..')
scripts_dir = join(project_dir, 'scrpits')

View file

@ -0,0 +1,32 @@
import glob
from os.path import abspath, basename, exists, dirname, join
import pytest
from mock import patch
from upgrade import is_minor_upgrade, collect_upgrade_scripts
@pytest.mark.parametrize("v1, v2, result", [
('6.3.0', '6.3.0', False),
('6.3.0', '6.3.1', True),
('6.3.0', '6.4.0', False),
('6.3.1', '6.4.0', False),
('6.4.0', '6.3.0', False),
])
def test_minor_upgrade(v1, v2, result):
assert is_minor_upgrade(v1, v2) == result
def test_collect_upgrade_scripts():
files = [
'upgrade_4.4_5.0.sh',
'upgrade_5.0_5.1.sh',
'upgrade_5.1_6.0.sh',
'upgrade_6.0_6.1.sh',
]
def fake_glob(pattern):
return [join(dirname(pattern), f) for f in files]
def _basename(files):
return [basename(f) for f in files]
with patch.object(glob, 'glob', fake_glob):
assert _basename(collect_upgrade_scripts('5.0.1', '6.1.0')) == files[1:]