Skip to content

Commit

Permalink
instantiate client in preferred way;
Browse files Browse the repository at this point in the history
update gh actions order of operations
  • Loading branch information
btylerburton committed Jan 14, 2025
1 parent 1b7d8fc commit 37234aa
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 127 deletions.
4 changes: 1 addition & 3 deletions harvester/lib/cf_handler.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import os

from cloudfoundry_client.client import CloudFoundryClient
from cloudfoundry_client.v3.tasks import TaskManager


class CFHandler:
def __init__(self, url: str, user: str, password: str):
Expand All @@ -14,7 +12,7 @@ def __init__(self, url: str, user: str, password: str):
def setup(self):
self.client = CloudFoundryClient(self.url)
self.client.init_with_user_credentials(self.user, self.password)
self.task_mgr = TaskManager(self.url, self.client)
self.task_mgr = self.client.v3.tasks

def start_task(self, app_guuid, command, task_id):
TASK_MEMORY = os.getenv("HARVEST_RUNNER_TASK_MEM", "4096")
Expand Down
181 changes: 93 additions & 88 deletions poetry.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ flask-sqlalchemy = "^3.1.1"
flask-wtf = "^1.2.1"
flask-migrate = "^4.0.7"
flask-bootstrap = "^3.3.7.1"
cloudfoundry-client = "1.36.0"
cloudfoundry-client = "^1.36.0"
pyjwt = "^2.8.0"
cryptography = "^42.0.8"
boltons = "^24.0.0"
Expand Down
14 changes: 7 additions & 7 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@ cffi==1.17.1 ; python_version >= "3.13" and python_version < "4.0" and platform_
charset-normalizer==3.4.1 ; python_version >= "3.13" and python_version < "4.0"
ckanapi==4.8 ; python_version >= "3.13" and python_version < "4.0"
click==8.1.8 ; python_version >= "3.13" and python_version < "4.0"
cloudfoundry-client==1.36.0 ; python_version >= "3.13" and python_version < "4.0"
cloudfoundry-client==1.38.1 ; python_version >= "3.13" and python_version < "4.0"
colorama==0.4.6 ; python_version >= "3.13" and python_version < "4.0" and platform_system == "Windows"
cryptography==42.0.8 ; python_version >= "3.13" and python_version < "4.0"
deepdiff==8.1.1 ; python_version >= "3.13" and python_version < "4.0"
docopt==0.6.2 ; python_version >= "3.13" and python_version < "4.0"
dominate==2.9.1 ; python_version >= "3.13" and python_version < "4.0"
flask-bootstrap==3.3.7.1 ; python_version >= "3.13" and python_version < "4.0"
flask-htmx==0.3.2 ; python_version >= "3.13" and python_version < "4.0"
flask-migrate==4.0.7 ; python_version >= "3.13" and python_version < "4.0"
flask-migrate==4.1.0 ; python_version >= "3.13" and python_version < "4.0"
flask-sqlalchemy==3.1.1 ; python_version >= "3.13" and python_version < "4.0"
flask-wtf==1.2.2 ; python_version >= "3.13" and python_version < "4.0"
flask==3.1.0 ; python_version >= "3.13" and python_version < "4.0"
frozenlist==1.5.0 ; python_version >= "3.13" and python_version < "4.0"
gevent==24.11.1 ; python_version >= "3.13" and python_version < "4.0"
greenlet==3.1.1 ; python_version >= "3.13" and python_version < "4.0" and platform_python_implementation == "CPython"
greenlet==3.1.1 ; python_version < "3.14" and python_version >= "3.13" and (platform_machine == "aarch64" or platform_machine == "ppc64le" or platform_machine == "x86_64" or platform_machine == "amd64" or platform_machine == "AMD64" or platform_machine == "win32" or platform_machine == "WIN32") or python_version >= "3.13" and platform_python_implementation == "CPython" and python_version < "4.0"
gunicorn==23.0.0 ; python_version >= "3.13" and python_version < "4.0"
idna==3.10 ; python_version >= "3.13" and python_version < "4.0"
itsdangerous==2.2.0 ; python_version >= "3.13" and python_version < "4.0"
Expand All @@ -42,7 +42,7 @@ orderly-set==5.2.3 ; python_version >= "3.13" and python_version < "4.0"
packaging==24.2 ; python_version >= "3.13" and python_version < "4.0"
polling2==0.5.0 ; python_version >= "3.13" and python_version < "4.0"
propcache==0.2.1 ; python_version >= "3.13" and python_version < "4.0"
protobuf==4.25.5 ; python_version >= "3.13" and python_version < "4.0"
protobuf==5.29.3 ; python_version >= "3.13" and python_version < "4.0"
psycopg2-binary==2.9.10 ; python_version >= "3.13" and python_version < "4.0"
pycparser==2.22 ; python_version >= "3.13" and python_version < "4.0" and platform_python_implementation != "PyPy"
pyjwt==2.10.1 ; python_version >= "3.13" and python_version < "4.0"
Expand All @@ -53,16 +53,16 @@ referencing==0.35.1 ; python_version >= "3.13" and python_version < "4.0"
requests==2.32.3 ; python_version >= "3.13" and python_version < "4.0"
rpds-py==0.22.3 ; python_version >= "3.13" and python_version < "4.0"
sansjson==0.3.0 ; python_version >= "3.13" and python_version < "4.0"
setuptools==75.7.0 ; python_version >= "3.13" and python_version < "4.0"
setuptools==75.8.0 ; python_version >= "3.13" and python_version < "4.0"
simplejson==3.19.3 ; python_version >= "3.13" and python_version < "4.0"
six==1.17.0 ; python_version >= "3.13" and python_version < "4.0"
soupsieve==2.6 ; python_version >= "3.13" and python_version < "4.0"
sqlalchemy==2.0.36 ; python_version >= "3.13" and python_version < "4.0"
sqlalchemy==2.0.37 ; python_version >= "3.13" and python_version < "4.0"
text-unidecode==1.3 ; python_version >= "3.13" and python_version < "4.0"
typing-extensions==4.12.2 ; python_version >= "3.13" and python_version < "4.0"
urllib3==2.3.0 ; python_version >= "3.13" and python_version < "4.0"
visitor==0.1.3 ; python_version >= "3.13" and python_version < "4.0"
websocket-client==1.7.0 ; python_version >= "3.13" and python_version < "4.0"
websocket-client==1.8.0 ; python_version >= "3.13" and python_version < "4.0"
werkzeug==3.1.3 ; python_version >= "3.13" and python_version < "4.0"
wtforms==3.2.1 ; python_version >= "3.13" and python_version < "4.0"
yarl==1.18.3 ; python_version >= "3.13" and python_version < "4.0"
Expand Down
4 changes: 1 addition & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@

@pytest.fixture(scope="session", autouse=True)
def default_session_fixture():
with patch("harvester.lib.cf_handler.CloudFoundryClient"), patch(
"harvester.lib.cf_handler.TaskManager"
), patch("app.load_manager.start", lambda: True):
with patch("harvester.lib.cf_handler.CloudFoundryClient"), patch("app.load_manager.start", lambda: True):
yield


Expand Down
23 changes: 5 additions & 18 deletions tests/integration/app/test_load_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,8 @@ def all_tasks_json_fixture():
@freeze_time("Jan 14th, 2012")
class TestLoadManager:
@patch("harvester.lib.cf_handler.CloudFoundryClient")
@patch("harvester.lib.cf_handler.TaskManager")
def test_load_manager_invokes_tasks(
self,
TMMock,
CFCMock,
interface_no_jobs,
source_data_dcatus_orm,
Expand Down Expand Up @@ -66,7 +64,7 @@ def test_load_manager_invokes_tasks(
load_manager.start()

# assert create_task ops
start_task_mock = TMMock.return_value.create
start_task_mock = CFCMock.return_value.v3.tasks.create
assert start_task_mock.call_count == 1
## assert command
assert (
Expand All @@ -89,10 +87,8 @@ def test_load_manager_invokes_tasks(

@patch("harvester.lib.load_manager.logger")
@patch("harvester.lib.cf_handler.CloudFoundryClient")
@patch("harvester.lib.cf_handler.TaskManager")
def test_load_manager_hits_task_limit(
self,
TMMock,
CFCMock,
logger_mock,
interface,
Expand Down Expand Up @@ -131,10 +127,8 @@ def test_load_manager_bails_on_incorrect_index(
)

@patch("harvester.lib.cf_handler.CloudFoundryClient")
@patch("harvester.lib.cf_handler.TaskManager")
def test_load_manager_schedules_first_job(
self,
TMMock,
CFCMock,
interface_with_multiple_jobs,
source_data_dcatus,
Expand All @@ -161,9 +155,8 @@ def test_load_manager_schedules_first_job(
assert new_jobs[0].date_created == datetime.now() + timedelta(days=1)

@patch("harvester.lib.cf_handler.CloudFoundryClient")
@patch("harvester.lib.cf_handler.TaskManager")
def test_manual_job_doesnt_affect_scheduled_jobs(
self, TMMock, CFCMock, mock_good_cf_index, interface_no_jobs, source_data_dcatus
self, CFCMock, mock_good_cf_index, interface_no_jobs, source_data_dcatus,
):
jobs = interface_no_jobs.get_new_harvest_jobs_by_source_in_future(
source_data_dcatus["id"]
Expand Down Expand Up @@ -200,10 +193,8 @@ def test_manual_job_doesnt_affect_scheduled_jobs(
assert jobs[1].status == "in_progress"

@patch("harvester.lib.cf_handler.CloudFoundryClient")
@patch("harvester.lib.cf_handler.TaskManager")
def test_dont_create_new_job_if_job_already_in_progress(
self,
TMMock,
CFCMock,
mock_good_cf_index,
interface_no_jobs,
Expand Down Expand Up @@ -234,10 +225,8 @@ def test_dont_create_new_job_if_job_already_in_progress(
assert jobs[1].status == "in_progress"

@patch("harvester.lib.cf_handler.CloudFoundryClient")
@patch("harvester.lib.cf_handler.TaskManager")
def test_assert_env_var_changes_task_size(
self,
TMMock,
CFCMock,
mock_good_cf_index,
interface_no_jobs,
Expand All @@ -246,7 +235,7 @@ def test_assert_env_var_changes_task_size(
):
load_manager = LoadManager()
load_manager.trigger_manual_job(source_data_dcatus["id"])
start_task_mock = TMMock.return_value.create
start_task_mock = CFCMock.return_value.v3.tasks.create
assert start_task_mock.call_args[0][3] == "4096"
assert start_task_mock.call_args[0][4] == "1536"

Expand All @@ -261,15 +250,13 @@ def test_assert_env_var_changes_task_size(
monkeypatch.setenv("HARVEST_RUNNER_TASK_DISK", "1234")

load_manager.trigger_manual_job(source_data_dcatus["id"])
start_task_mock = TMMock.return_value.create
start_task_mock = CFCMock.return_value.v3.tasks.create
assert start_task_mock.call_args[0][3] == "1234"
assert start_task_mock.call_args[0][4] == "1234"

@patch("harvester.lib.cf_handler.CloudFoundryClient")
@patch("harvester.lib.cf_handler.TaskManager")
def test_trigger_cancel_job(
self,
TMMock,
CFCMock,
all_tasks_json_fixture,
mock_good_cf_index,
Expand Down Expand Up @@ -305,6 +292,6 @@ def test_trigger_cancel_job(
load_manager.stop_job(jobs[0].id)

# assert cancel_task ops
cancel_task_mock = TMMock.return_value.cancel
cancel_task_mock = CFCMock.return_value.v3.tasks.cancel
assert cancel_task_mock.call_count == 1
assert cancel_task_mock.call_args[0][0] == task_guid_val
13 changes: 6 additions & 7 deletions tests/unit/test_cf_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@


@patch("harvester.lib.cf_handler.CloudFoundryClient")
@patch("harvester.lib.cf_handler.TaskManager")
class TestCFTasking:
def test_add_task(self, TMMock, CFClientMock, dhl_cf_task_data):
def test_add_task(self, CFClientMock, dhl_cf_task_data):
CFUtil = CFHandler("url", "user", "password")
assert CFUtil.start_task(**dhl_cf_task_data) is not None

def test_get_task(self, TMMock, CFClientMock, dhl_cf_task_data):
def test_get_task(self, CFClientMock, dhl_cf_task_data):
CFUtil = CFHandler("url", "user", "password")
task = CFUtil.get_task(dhl_cf_task_data["task_id"])
assert task is not None

def test_get_all_app_tasks(self, TMMock, CFClientMock, dhl_cf_task_data):
def test_get_all_app_tasks(self, CFClientMock, dhl_cf_task_data):
CFUtil = CFHandler("url", "user", "password")
# ruff: noqa: E501
CFClientMock.return_value.v3.apps.__getitem__.return_value.tasks.return_value = [
Expand All @@ -24,7 +23,7 @@ def test_get_all_app_tasks(self, TMMock, CFClientMock, dhl_cf_task_data):
tasks = CFUtil.get_all_app_tasks(dhl_cf_task_data["app_guuid"])
assert len(tasks) > 0

def test_get_all_running_app_tasks(self, TMMock, CFClientMock, dhl_cf_task_data):
def test_get_all_running_app_tasks(self, CFClientMock, dhl_cf_task_data):
CFUtil = CFHandler("url", "user", "password")
CFClientMock.return_value.v3.apps.__getitem__.return_value.tasks.return_value = [
{"state": "RUNNING"},
Expand All @@ -33,12 +32,12 @@ def test_get_all_running_app_tasks(self, TMMock, CFClientMock, dhl_cf_task_data)
running_tasks = CFUtil.get_all_running_app_tasks(dhl_cf_task_data["app_guuid"])
assert running_tasks == 1

def test_cancel_task(self, TMMock, CFClientMock, dhl_cf_task_data):
def test_cancel_task(self, CFClientMock, dhl_cf_task_data):
CFUtil = CFHandler("url", "user", "password")
task = CFUtil.stop_task(dhl_cf_task_data["task_id"])
assert task is not None

def test_read_recent_task_logs(self, TMMock, CFClientMock, dhl_cf_task_data):
def test_read_recent_task_logs(self, CFClientMock, dhl_cf_task_data):
CFUtil = CFHandler("url", "user", "password")
logs = CFUtil.read_recent_app_logs(
dhl_cf_task_data["app_guuid"], dhl_cf_task_data["task_id"]
Expand Down

1 comment on commit 37234aa

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests Skipped Failures Errors Time
2 0 💤 0 ❌ 0 🔥 7.291s ⏱️

Please sign in to comment.