Skip to content

Commit

Permalink
check status from _update.log
Browse files Browse the repository at this point in the history
use xterm
  • Loading branch information
hemnstill committed Mar 1, 2024
1 parent 2f5d927 commit 2cf3c34
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 34 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci-test-run.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
if: matrix.artifact == 'ubuntu'
run: |
apt update
apt install -y busybox dos2unix libarchive-tools python3 git
apt install -y busybox dos2unix libarchive-tools python3 git xterm
git config --global --add safe.directory '*'
- name: Install Alpine packages
Expand Down
65 changes: 35 additions & 30 deletions _AutoinstallCreator/test_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@

_self_path: str = os.path.dirname(os.path.realpath(__file__))

_self_body_path = os.path.join(os.path.dirname(_self_path), 'body.md')

_self_tmp_path: str = os.path.join(_self_path, '.tmp')
_root_path: str = os.path.dirname(_self_path)
_tools_path: str = os.path.join(_root_path, '.tools')

busybox_exe_path_arg: list[str] = ['x-terminal-emulator', '-e']
busybox_exe_path_arg: list[str] = ['xterm', '-e']
update_script_name: str = 'update.sh'
package_name = 'AutoinstallCreator.sh'
if sys.platform.startswith('win'):
Expand All @@ -25,49 +27,55 @@
from _AutoinstallCreator import io_tools


def get_version_from_stdout(b_stdout: bytes) -> str:
for b_line in b_stdout.splitlines():
if b_line.startswith(b'version created:'):
version_line = b_line.decode().strip()
prefix, _, raw_version = version_line.partition(':')
return raw_version.strip()


def update_completed(update_log_path: str, version_str: str) -> bool:
update_log = io_tools.read_text(update_log_path)
return update_log.endswith(f'\nUpdate complete: {version_str}\n')


def version_is_up_to_date(update_log_path: str, version_str: str) -> bool:
update_log = io_tools.read_text(update_log_path).strip()
return update_log == f'Version is up to date: {version_str}'


class TestUpdate(unittest.TestCase):

@classmethod
def setUpClass(cls) -> None:
pathlib.Path(os.path.join(_self_path, 'AutoinstallCreator.sh')).unlink(missing_ok=True)
pathlib.Path(os.path.join(_self_path, 'AutoinstallCreator.sh.bat')).unlink(missing_ok=True)
pathlib.Path(_self_body_path).unlink(missing_ok=True)

result = subprocess.run(busybox_exe_path_arg + [os.path.join(_self_path, 'release.sh')],
check=True, stdout=subprocess.PIPE)
cls.version_str = get_version_from_stdout(result.stdout)
subprocess.run(busybox_exe_path_arg + [os.path.join(_self_path, 'release.sh')],
check=True)
cls.version_str = io_tools.read_text(_self_body_path).strip('\r\n')
if not cls.version_str:
raise ValueError('cannot get version from release.sh')

def setUp(self):
self.test_old_version = 'AutoinstallCreator.11.test_old_version'
self.assertTrue(self.version_str.startswith('AutoinstallCreator.'))
self.assertTrue(io_tools.try_create_or_clean_dir(_self_tmp_path))
self.package_filepath = os.path.join(_self_tmp_path, package_name)
self.old_update_log_filepath = os.path.join(_self_tmp_path, self.test_old_version, '_update.log')
self.update_log_filepath = os.path.join(_self_tmp_path, self.version_str, '_update.log')
shutil.copy2(os.path.join(_self_path, package_name), self.package_filepath)

pathlib.Path(self.old_update_log_filepath).unlink(missing_ok=True)
pathlib.Path(self.update_log_filepath).unlink(missing_ok=True)


def test_version_up_to_date(self):
subprocess.run(busybox_exe_path_arg + [self.package_filepath],
cwd=_self_tmp_path,
check=True)

result = subprocess.run(busybox_exe_path_arg + [os.path.join(_self_tmp_path, self.version_str, 'update.sh')],
env={
'MOCK_AUTOINSTALLCREATOR_VERSION_BODY': self.version_str,
'MOCK_AUTOINSTALLCREATOR_PACKAGE_FILEPATH': self.package_filepath
},
check=True, stdout=subprocess.PIPE)
self.assertTrue(result.stdout.endswith(b'\nVersion is up to date\n'))
subprocess.run(busybox_exe_path_arg + [os.path.join(_self_tmp_path, self.version_str, 'update.sh')],
env={
'MOCK_AUTOINSTALLCREATOR_VERSION_BODY': self.version_str,
'MOCK_AUTOINSTALLCREATOR_PACKAGE_FILEPATH': self.package_filepath
},
check=True)
self.assertTrue(version_is_up_to_date(self.update_log_filepath, self.version_str))

def test_update_to_new_version(self):
subprocess.run(busybox_exe_path_arg + [self.package_filepath, '--target', self.test_old_version],
Expand All @@ -76,18 +84,15 @@ def test_update_to_new_version(self):
io_tools.write_text(os.path.join(_self_tmp_path, self.test_old_version, '_AutoinstallCreator', 'version.txt'),
self.test_old_version)

result = subprocess.run([os.path.join(_self_tmp_path, self.test_old_version, update_script_name)],
env={
**os.environ,
'MOCK_AUTOINSTALLCREATOR_VERSION_BODY': self.version_str,
'MOCK_AUTOINSTALLCREATOR_PACKAGE_FILEPATH': self.package_filepath
},
check=True, stdout=subprocess.PIPE)

self.assertIn(f'\nFound new version: {self.test_old_version} -> {self.version_str}\n'.encode(), result.stdout)
subprocess.run([os.path.join(_self_tmp_path, self.test_old_version, update_script_name)],
env={
**os.environ,
'MOCK_AUTOINSTALLCREATOR_VERSION_BODY': self.version_str,
'MOCK_AUTOINSTALLCREATOR_PACKAGE_FILEPATH': self.package_filepath
},
check=True)

update_log_filepath = os.path.join(_self_tmp_path, self.test_old_version, '_update.log')
self.assertTrue(io_tools.wait_for(lambda: update_completed(update_log_filepath, self.version_str),
self.assertTrue(io_tools.wait_for(lambda: update_completed(self.old_update_log_filepath, self.version_str),
err_message='Update failed',
timeout=10,
retry_timeout=0.5,
Expand Down
6 changes: 3 additions & 3 deletions update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ version_hash=$(echo "$version_body" | "$grep" --only-matching "(?<=$self_name\.$
}

if [[ $self_version_count -eq $version_count ]] && [[ $self_version_hash == $version_hash ]]; then
echo "Version is up to date"
echo "Version is up to date: $version_body" > $dp0/_update.log 2>&1
exit 0
fi

Expand Down Expand Up @@ -116,9 +116,9 @@ if [[ -z $MOCK_AUTOINSTALLCREATOR_PACKAGE_FILEPATH ]]; then
fi

echo "Extracting to: $dp0/_$self_name/$version_body"
"$package_filepath" --target "$dp0/_$self_name/tmp_$version_body"
"$package_filepath" --target "$dp0/_$self_name/tmp_$version_body" > $dp0/_update.log 2>&1

echo "Running extracted 'update.sh'"
"$dp0/_$self_name/tmp_$version_body/update.sh" "$dp0" >& $dp0/_update.log
"$dp0/_$self_name/tmp_$version_body/update.sh" "$dp0" >> $dp0/_update.log 2>&1


0 comments on commit 2cf3c34

Please sign in to comment.