From 87264d4fdadcddd91289b968dd0e4bf58d449267 Mon Sep 17 00:00:00 2001 From: bashonly Date: Sun, 12 Nov 2023 18:30:55 -0600 Subject: [PATCH] [test:update] Implement simple updater unit tests Authored by: bashonly --- test/test_update.py | 199 +++++++++++++++++++++++++++++++++++ test/test_update.py.disabled | 30 ------ test/versions.json | 34 ------ 3 files changed, 199 insertions(+), 64 deletions(-) create mode 100644 test/test_update.py delete mode 100644 test/test_update.py.disabled delete mode 100644 test/versions.json diff --git a/test/test_update.py b/test/test_update.py new file mode 100644 index 000000000..134424a31 --- /dev/null +++ b/test/test_update.py @@ -0,0 +1,199 @@ +#!/usr/bin/env python3 + +# Allow direct execution +import os +import sys +import unittest + +sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) + + +from test.helper import FakeYDL, report_warning +from yt_dlp.update import Updater, UpdateInfo + +TEST_API_DATA = { + 'yt-dlp/yt-dlp/latest': { + 'tag_name': '2023.12.31', + 'target_commitish': 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb', + 'name': 'yt-dlp 2023.12.31', + 'body': 'BODY', + }, + 'yt-dlp/yt-dlp-nightly-builds/latest': { + 'tag_name': '2023.12.31.123456', + 'target_commitish': 'master', + 'name': 'yt-dlp nightly 2023.12.31.123456', + 'body': 'Generated from: https://github.com/yt-dlp/yt-dlp/commit/cccccccccccccccccccccccccccccccccccccccc', + }, + 'yt-dlp/yt-dlp-master-builds/latest': { + 'tag_name': '2023.12.31.987654', + 'target_commitish': 'master', + 'name': 'yt-dlp master 2023.12.31.987654', + 'body': 'Generated from: https://github.com/yt-dlp/yt-dlp/commit/dddddddddddddddddddddddddddddddddddddddd', + }, + 'yt-dlp/yt-dlp/tags/testing': { + 'tag_name': 'testing', + 'target_commitish': '9999999999999999999999999999999999999999', + 'name': 'testing', + 'body': 'BODY', + }, + 'fork/yt-dlp/latest': { + 'tag_name': '2050.12.31', + 'target_commitish': 'eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee', + 'name': '2050.12.31', + 'body': 'BODY', + }, + 'fork/yt-dlp/tags/pr0000': { + 'tag_name': 'pr0000', + 'target_commitish': 'ffffffffffffffffffffffffffffffffffffffff', + 'name': 'pr1234 2023.11.11.000000', + 'body': 'BODY', + }, + 'fork/yt-dlp/tags/pr1234': { + 'tag_name': 'pr1234', + 'target_commitish': '0000000000000000000000000000000000000000', + 'name': 'pr1234 2023.12.31.555555', + 'body': 'BODY', + }, + 'fork/yt-dlp/tags/pr9999': { + 'tag_name': 'pr9999', + 'target_commitish': '1111111111111111111111111111111111111111', + 'name': 'pr9999', + 'body': 'BODY', + }, + 'fork/yt-dlp-satellite/tags/pr987': { + 'tag_name': 'pr987', + 'target_commitish': 'master', + 'name': 'pr987', + 'body': 'Generated from: https://github.com/yt-dlp/yt-dlp/commit/2222222222222222222222222222222222222222', + }, +} + +TEST_LOCKFILE_V1 = '''# This file is used for regulating self-update +lock 2022.08.18.36 .+ Python 3.6 +lock 2023.11.13 .+ Python 3.7 +''' + +TEST_LOCKFILE_V2 = '''# This file is used for regulating self-update +lockV2 yt-dlp/yt-dlp 2022.08.18.36 .+ Python 3.6 +lockV2 yt-dlp/yt-dlp 2023.11.13 .+ Python 3.7 +''' + +TEST_LOCKFILE_V1_V2 = '''# This file is used for regulating self-update +lock 2022.08.18.36 .+ Python 3.6 +lock 2023.11.13 .+ Python 3.7 +lockV2 yt-dlp/yt-dlp 2022.08.18.36 .+ Python 3.6 +lockV2 yt-dlp/yt-dlp 2023.11.13 .+ Python 3.7 +lockV2 fork/yt-dlp pr0000 .+ Python 3.6 +lockV2 fork/yt-dlp pr1234 .+ Python 3.7 +lockV2 fork/yt-dlp pr9999 .+ Python 3.11 +''' + + +class FakeUpdater(Updater): + current_version = '2022.01.01' + current_commit = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' + + _channel = 'stable' + _origin = 'yt-dlp/yt-dlp' + + def _download_update_spec(self, *args, **kwargs): + return TEST_LOCKFILE_V1_V2 + + def _call_api(self, tag): + tag = f'tags/{tag}' if tag != 'latest' else tag + return TEST_API_DATA[f'{self.requested_repo}/{tag}'] + + def _report_error(self, msg, *args, **kwargs): + report_warning(msg) + + +class TestUpdate(unittest.TestCase): + maxDiff = None + + def test_update_spec(self): + ydl = FakeYDL() + updater = FakeUpdater(ydl, 'stable@latest') + + def test(lockfile, identifier, input_tag, expect_tag, exact=False, repo='yt-dlp/yt-dlp'): + updater._identifier = identifier + updater._exact = exact + updater.requested_repo = repo + result = updater._process_update_spec(lockfile, input_tag) + self.assertEqual( + result, expect_tag, + f'{identifier!r} requesting {repo}@{input_tag} (exact={exact}) ' + f'returned {result!r} instead of {expect_tag!r}') + + test(TEST_LOCKFILE_V1, 'zip Python 3.11.0', '2023.11.13', '2023.11.13') + test(TEST_LOCKFILE_V1, 'zip stable Python 3.11.0', '2023.11.13', '2023.11.13', exact=True) + test(TEST_LOCKFILE_V1, 'zip Python 3.6.0', '2023.11.13', '2022.08.18.36') + test(TEST_LOCKFILE_V1, 'zip stable Python 3.6.0', '2023.11.13', None, exact=True) + test(TEST_LOCKFILE_V1, 'zip Python 3.7.0', '2023.11.13', '2023.11.13') + test(TEST_LOCKFILE_V1, 'zip stable Python 3.7.1', '2023.11.13', '2023.11.13') + test(TEST_LOCKFILE_V1, 'zip Python 3.7.1', '2023.12.31', '2023.11.13') + test(TEST_LOCKFILE_V1, 'zip stable Python 3.7.1', '2023.12.31', '2023.11.13') + + test(TEST_LOCKFILE_V2, 'zip Python 3.11.1', '2023.11.13', '2023.11.13') + test(TEST_LOCKFILE_V2, 'zip stable Python 3.11.1', '2023.12.31', '2023.12.31') + test(TEST_LOCKFILE_V2, 'zip Python 3.6.1', '2023.11.13', '2022.08.18.36') + test(TEST_LOCKFILE_V2, 'zip stable Python 3.7.2', '2023.11.13', '2023.11.13') + test(TEST_LOCKFILE_V2, 'zip Python 3.7.2', '2023.12.31', '2023.11.13') + + test(TEST_LOCKFILE_V1_V2, 'zip Python 3.11.2', '2023.11.13', '2023.11.13') + test(TEST_LOCKFILE_V1_V2, 'zip stable Python 3.11.2', '2023.12.31', '2023.12.31') + test(TEST_LOCKFILE_V1_V2, 'zip Python 3.6.2', '2023.11.13', '2022.08.18.36') + test(TEST_LOCKFILE_V1_V2, 'zip stable Python 3.7.3', '2023.11.13', '2023.11.13') + test(TEST_LOCKFILE_V1_V2, 'zip Python 3.7.3', '2023.12.31', '2023.11.13') + test(TEST_LOCKFILE_V1_V2, 'zip Python 3.6.3', 'pr0000', None, repo='fork/yt-dlp') + test(TEST_LOCKFILE_V1_V2, 'zip stable Python 3.7.4', 'pr0000', 'pr0000', repo='fork/yt-dlp') + test(TEST_LOCKFILE_V1_V2, 'zip Python 3.6.4', 'pr0000', None, repo='fork/yt-dlp') + test(TEST_LOCKFILE_V1_V2, 'zip Python 3.7.4', 'pr1234', None, repo='fork/yt-dlp') + test(TEST_LOCKFILE_V1_V2, 'zip stable Python 3.8.1', 'pr1234', 'pr1234', repo='fork/yt-dlp') + test(TEST_LOCKFILE_V1_V2, 'zip Python 3.7.5', 'pr1234', None, repo='fork/yt-dlp') + test(TEST_LOCKFILE_V1_V2, 'zip Python 3.11.3', 'pr9999', None, repo='fork/yt-dlp') + test(TEST_LOCKFILE_V1_V2, 'zip stable Python 3.12.0', 'pr9999', 'pr9999', repo='fork/yt-dlp') + test(TEST_LOCKFILE_V1_V2, 'zip Python 3.11.4', 'pr9999', None, repo='fork/yt-dlp') + + def test_query_update(self): + ydl = FakeYDL() + + def test(target, expected, current_version=None, current_commit=None, identifier=None): + updater = FakeUpdater(ydl, target) + if current_version: + updater.current_version = current_version + if current_commit: + updater.current_commit = current_commit + updater._identifier = identifier or 'zip' + update_info = updater.query_update(_output=True) + self.assertDictEqual( + update_info.__dict__ if update_info else {}, expected.__dict__ if expected else {}) + + test('yt-dlp/yt-dlp@latest', UpdateInfo( + '2023.12.31', version='2023.12.31', requested_version='2023.12.31', commit='b' * 40)) + test('yt-dlp/yt-dlp-nightly-builds@latest', UpdateInfo( + '2023.12.31.123456', version='2023.12.31.123456', requested_version='2023.12.31.123456', commit='c' * 40)) + test('yt-dlp/yt-dlp-master-builds@latest', UpdateInfo( + '2023.12.31.987654', version='2023.12.31.987654', requested_version='2023.12.31.987654', commit='d' * 40)) + test('fork/yt-dlp@latest', UpdateInfo( + '2050.12.31', version='2050.12.31', requested_version='2050.12.31', commit='e' * 40)) + test('fork/yt-dlp@pr0000', UpdateInfo( + 'pr0000', version='2023.11.11.000000', requested_version='2023.11.11.000000', commit='f' * 40)) + test('fork/yt-dlp@pr1234', UpdateInfo( + 'pr1234', version='2023.12.31.555555', requested_version='2023.12.31.555555', commit='0' * 40)) + test('fork/yt-dlp@pr9999', UpdateInfo( + 'pr9999', version=None, requested_version=None, commit='1' * 40)) + test('fork/yt-dlp-satellite@pr987', UpdateInfo( + 'pr987', version=None, requested_version=None, commit='2' * 40)) + test('yt-dlp/yt-dlp', None, current_version='2024.01.01') + test('stable', UpdateInfo( + '2023.12.31', version='2023.12.31', requested_version='2023.12.31', commit='b' * 40)) + test('nightly', UpdateInfo( + '2023.12.31.123456', version='2023.12.31.123456', requested_version='2023.12.31.123456', commit='c' * 40)) + test('master', UpdateInfo( + '2023.12.31.987654', version='2023.12.31.987654', requested_version='2023.12.31.987654', commit='d' * 40)) + test('testing', None, current_commit='9' * 40) + test('testing', UpdateInfo('testing', commit='9' * 40)) + + +if __name__ == '__main__': + unittest.main() diff --git a/test/test_update.py.disabled b/test/test_update.py.disabled deleted file mode 100644 index 85ac86692..000000000 --- a/test/test_update.py.disabled +++ /dev/null @@ -1,30 +0,0 @@ -#!/usr/bin/env python3 - -# Allow direct execution -import os -import sys -import unittest - -sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) - - -import json - -from yt_dlp.update import rsa_verify - - -class TestUpdate(unittest.TestCase): - def test_rsa_verify(self): - UPDATES_RSA_KEY = (0x9d60ee4d8f805312fdb15a62f87b95bd66177b91df176765d13514a0f1754bcd2057295c5b6f1d35daa6742c3ffc9a82d3e118861c207995a8031e151d863c9927e304576bc80692bc8e094896fcf11b66f3e29e04e3a71e9a11558558acea1840aec37fc396fb6b65dc81a1c4144e03bd1c011de62e3f1357b327d08426fe93, 65537) - with open(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'versions.json'), 'rb') as f: - versions_info = f.read().decode() - versions_info = json.loads(versions_info) - signature = versions_info['signature'] - del versions_info['signature'] - self.assertTrue(rsa_verify( - json.dumps(versions_info, sort_keys=True).encode(), - signature, UPDATES_RSA_KEY)) - - -if __name__ == '__main__': - unittest.main() diff --git a/test/versions.json b/test/versions.json deleted file mode 100644 index 6cccc2259..000000000 --- a/test/versions.json +++ /dev/null @@ -1,34 +0,0 @@ -{ - "latest": "2013.01.06", - "signature": "72158cdba391628569ffdbea259afbcf279bbe3d8aeb7492690735dc1cfa6afa754f55c61196f3871d429599ab22f2667f1fec98865527b32632e7f4b3675a7ef0f0fbe084d359256ae4bba68f0d33854e531a70754712f244be71d4b92e664302aa99653ee4df19800d955b6c4149cd2b3f24288d6e4b40b16126e01f4c8ce6", - "versions": { - "2013.01.02": { - "bin": [ - "http://youtube-dl.org/downloads/2013.01.02/youtube-dl", - "f5b502f8aaa77675c4884938b1e4871ebca2611813a0c0e74f60c0fbd6dcca6b" - ], - "exe": [ - "http://youtube-dl.org/downloads/2013.01.02/youtube-dl.exe", - "75fa89d2ce297d102ff27675aa9d92545bbc91013f52ec52868c069f4f9f0422" - ], - "tar": [ - "http://youtube-dl.org/downloads/2013.01.02/youtube-dl-2013.01.02.tar.gz", - "6a66d022ac8e1c13da284036288a133ec8dba003b7bd3a5179d0c0daca8c8196" - ] - }, - "2013.01.06": { - "bin": [ - "http://youtube-dl.org/downloads/2013.01.06/youtube-dl", - "64b6ed8865735c6302e836d4d832577321b4519aa02640dc508580c1ee824049" - ], - "exe": [ - "http://youtube-dl.org/downloads/2013.01.06/youtube-dl.exe", - "58609baf91e4389d36e3ba586e21dab882daaaee537e4448b1265392ae86ff84" - ], - "tar": [ - "http://youtube-dl.org/downloads/2013.01.06/youtube-dl-2013.01.06.tar.gz", - "fe77ab20a95d980ed17a659aa67e371fdd4d656d19c4c7950e7b720b0c2f1a86" - ] - } - } -} \ No newline at end of file