def test_callback_depends_update_package(monkeypatch): # Stop bpo server after bpo.repo.build was called 2x global stop_count stop_count = 0 def stop_count_increase(*args, **kwargs): global stop_count stop_count += 1 print("stop_count_increase: " + str(stop_count)) if stop_count == 2: bpo_test.stop_server() monkeypatch.setattr(bpo.repo, "build", stop_count_increase) # Fill the db with "hello-world", "hello-world-wrapper" with bpo_test.BPOServer(): bpo_test.trigger.job_callback_get_depends("master") # hello-world: decrease version, change status to failed session = bpo.db.session() pkgname = "hello-world" arch = "x86_64" branch = "master" package = bpo.db.get_package(session, pkgname, arch, branch) package.version = "0-r0" package.status = bpo.db.PackageStatus.failed session.merge(package) session.commit() # Fill the db with "hello-world", "hello-world-wrapper" again bpo_test.trigger.job_callback_get_depends("master") bpo_test.assert_package(pkgname, status="queued", version="1-r4")
def test_callback_fail_build_package(monkeypatch): """ Trigger job-callback/get-depends, so the db gets filled with the hello-world and hello-world-wrapper packages, and bpo tries to build both of them. Fail building hello-world, let the local job service send the fail request, and finish the test when the bpo server tries to build the next package. """ bpo_repo_build_orig = bpo.repo.build with bpo_test.BPOServer(): global bpo_repo_build_count bpo_repo_build_count = 0 # Fail the build monkeypatch.setattr(bpo.job_services.local.LocalJobServiceThread, "run_print", bpo_test.raise_exception) def bpo_repo_build_fake(force_repo_update=False): global bpo_repo_build_count bpo_repo_build_count += 1 if bpo_repo_build_count < 2: return bpo_repo_build_orig(force_repo_update) return bpo_test.stop_server() # Stop server when trying to build the package for the second time monkeypatch.setattr(bpo.repo, "build", bpo_repo_build_fake) # Fill db and try to build hello-world bpo_test.trigger.job_callback_get_depends("master") # Check package status bpo_test.assert_package("hello-world", status="failed")
def test_push_hook_gitlab_reset_to_queued(monkeypatch): """ Have two failed packages, hello-world and hello-world-wrapper, and test if both get successfully reset to queued because main/hello-world/APKBUILD was modified in the pushed commit. """ with bpo_test.BPOServer(): # Fill the db with "hello-world", "hello-world-wrapper" monkeypatch.setattr(bpo.helpers.job, "update_status", bpo_test.nop) monkeypatch.setattr(bpo.repo, "build", bpo_test.nop) bpo_test.trigger.job_callback_get_depends("master") # Set both packages to failed session = bpo.db.session() for pkgname in ["hello-world", "hello-world-wrapper"]: package = bpo.db.get_package(session, pkgname, "x86_64", "master") package.status = bpo.db.PackageStatus.failed package.retry_count = 2 session.merge(package) session.commit() bpo.ui.log_package(package, "job_update_package_status_failed") print("DEBUG9") bpo_test.assert_package("hello-world", status="failed", retry_count=2) bpo_test.assert_package("hello-world-wrapper", status="failed", retry_count=2) # Run the gitlab push hook where hello-world was modified monkeypatch.setattr(bpo.jobs.get_depends, "run", bpo_test.stop_server) bpo_test.trigger.push_hook_gitlab() # Verify status reset bpo_test.assert_package("hello-world", status="queued", retry_count=0) bpo_test.assert_package("hello-world-wrapper", status="queued", retry_count=0)
def test_remove_deleted_package_SLOW_40s(monkeypatch): # Only one arch, so the bpo server doesn't attempt to run multiple repo # indexing jobs at once. branches = collections.OrderedDict() branches["master"] = {"arches": ["x86_64"], "ignore_errors": False} monkeypatch.setattr(bpo.config.const, "branches", branches) # Stop server when it would publish the packages monkeypatch.setattr(bpo.repo.final, "publish", bpo_test.stop_server) with bpo_test.BPOServer(): # Put test apks in final repo arch = "x86_64" branch = "master" final_path = bpo.repo.final.get_path(arch, branch) testdata = bpo.config.const.top_dir + "/test/testdata/" os.makedirs(final_path, exist_ok=True) for apk in [ "hello-world-1-r3.apk", # wrong version "hello-world-1-r4.apk", "hello-world-wrapper-1-r2.apk" ]: shutil.copy(testdata + "/" + apk, final_path + "/" + apk) # Put into db: hello-world, hello-world-wrapper session = bpo.db.session() published = bpo.db.PackageStatus.published session.merge( bpo.db.Package(arch, branch, "hello-world", "1-r4", published)) session.merge( bpo.db.Package(arch, branch, "hello-world-wrapper", "1-r2", published)) session.commit() # pmaports.git only has "hello-world", not "hello-world-wrapper" payload = "depends.x86_64_hello-world_only.json" bpo_test.trigger.job_callback_get_depends("master", payload) # Check if database was updated properly bpo_test.assert_package("hello-world", status="published", version="1-r4") bpo_test.assert_package("hello-world-wrapper", exists=False) # Check if packages were removed properly assert os.path.exists(final_path + "/hello-world-1-r4.apk") assert not os.path.exists(final_path + "/hello-world-1-r3.apk") assert not os.path.exists(final_path + "/hello-world-wrapper-1-r2.apk") # Check if APKINDEX was created for final repo assert os.path.exists(final_path + "/APKINDEX.tar.gz")
def test_public_update_job_status(monkeypatch): arch = "x86_64" branch = "master" pkgname = "hello-world" pkgname2 = "second-package" version = "1-r4" # Disable retry_count code path (tested separately) monkeypatch.setattr(bpo.config.const, "retry_count_max", 0) # Expect the bpo server to build "second-package" and exit the bpo server # as soon as it tries to do that def fake_build_package(arch, pkgname, branch): if pkgname == "second-package": print("bpo server tries to build expected package") bpo_test.stop_server() else: print("bpo server tries to build something else: " + str(pkgname)) bpo_test.stop_server_nok() # Fake job ID return 1337 monkeypatch.setattr(bpo.jobs.build_package, "run", fake_build_package) with bpo_test.BPOServer(): # Add "hello-world" package to DB, status: building session = bpo.db.session() package = bpo.db.Package(arch, branch, pkgname, version) package.status = bpo.db.PackageStatus.building package.job_id = 1111 session.merge(package) # Add "second-package" to DB, status: queued package2 = bpo.db.Package(arch, branch, pkgname2, version) session.merge(package2) session.commit() # Call update-job-status to set "hello-world" status to "failed" and # to start building "second-package" requests.post("http://127.0.0.1:5000/api/public/update-job-status") # Verify package status bpo_test.assert_package(pkgname, status="failed")
def test_build_package_run_skip_existing(monkeypatch): # Fill the db with "hello-world", "hello-world-wrapper" with bpo_test.BPOServer(): monkeypatch.setattr(bpo.repo, "build", bpo_test.stop_server) bpo_test.trigger.job_callback_get_depends("master") # Package status should be "queued" session = bpo.db.session() pkgname = "hello-world" arch = "x86_64" branch = "master" package = bpo.db.get_package(session, pkgname, arch, branch) assert package.status == bpo.db.PackageStatus.queued # Copy hello-world apk to wip repo testdata_path = bpo.config.const.top_dir + "/test/testdata/" apk_hello = testdata_path + "/hello-world-1-r4.apk" wip_path = bpo.repo.wip.get_path(arch, branch) os.makedirs(wip_path) shutil.copy(apk_hello, wip_path) # Build should be skipped assert bpo.jobs.build_package.run(arch, pkgname, branch) is False bpo_test.assert_package(pkgname, status="built")
def test_retry_build(monkeypatch): """ High level test for the retry_count logic """ global job_run_fake_count job_run_fake_count = 0 def job_run_fake(name, note, tasks, branch, arch, pkgname, version): global job_run_fake_count job_run_fake_count += 1 logging.info("job_run_fake_count: " + str(job_run_fake_count)) assert name == "build_package" assert branch == "master" assert arch == "x86_64" assert pkgname == "hello-world" assert version == "1-r4" assert job_run_fake_count < 3 return 1234 monkeypatch.setattr(bpo.helpers.job, "run", job_run_fake) monkeypatch.setattr(bpo.config.const, "retry_count_max", 1) monkeypatch.setattr(bpo.repo, "set_stuck", bpo_test.stop_server) pkgname = "hello-world" with bpo_test.BPOServer(): # Fill the db with "hello-world", "hello-world-wrapper" and let the bpo # server start building "hello-world". The actual building is prevented # in this test by the job_run_fake() override above. bpo_test.trigger.job_callback_get_depends("master") bpo_test.assert_package(pkgname, status="building", retry_count=0, job_id=1234) assert job_run_fake_count == 1 # Pretend that the build failed: call api/public/update-job-status, # just like sourcehut would do it. The bpo server then calls # bpo.helpers.jobs.update_status(), which reports a job # failure for the local job service. The bpo server starts the next # build. job_run_fake() gets called again and stops the bpo server. bpo_test.trigger.public_update_job_status() bpo_test.assert_package(pkgname, status="building", retry_count=1) assert job_run_fake_count == 2 # Pretend that the build failed again. Now retry_count_max=1 is # reached, and the other package in the db, hello-world-wrapper, # depends on the failed package hello-world. The repo is stuck. # This will stop the server (see monkeypatch above). bpo_test.trigger.public_update_job_status() bpo_test.assert_package(pkgname, status="failed", retry_count=1) assert job_run_fake_count == 2