def get_maintenance_report(self): if self._maintenance_report: report = MaintenanceReport._from_data( json.loads(self._maintenance_report)) else: report = MaintenanceReport() return f_proxy(f_return(report))
def test_export_empty_report(): # create an empty report with out data passed report = MaintenanceReport() assert report.last_updated is None # export empty report shouldn't have problem data = report._export_dict() # exported report should have a timestamp assert datetime.datetime.strptime(data["last_updated"], "%Y-%m-%dT%H:%M:%SZ")
def test_set_maintenance(client, requests_mocker): maintenance_report = { "last_updated": "2019-08-15T14:21:12Z", "last_updated_by": "pubtools.pulplib", "repos": { "repo1": { "message": "Maintenance Mode Enabled", "owner": "pubtools.pulplib", "started": "2019-08-15T14:21:12Z", } }, } requests_mocker.post( "https://pulp.example.com/pulp/api/v2/repositories/search/", [{ "json": [{ "id": "redhat-maintenance", "notes": { "_repo-type": "iso-repo" } }] }], ) report = MaintenanceReport._from_data(maintenance_report) with patch("pubtools.pulplib.FileRepository.upload_file") as mocked_upload: with patch("pubtools.pulplib.Repository.publish") as mocked_publish: upload_task = Task(id="upload-task", completed=True, succeeded=True) publish_task = [ Task(id="publish-task", completed=True, succeeded=True) ] mocked_upload.return_value = f_return(upload_task) mocked_publish.return_value = f_return(publish_task) # set_maintenance.result() should return whatever publish.result() returns assert client.set_maintenance(report).result() is publish_task # upload_file should be called with (file_obj, 'repos.json') args = mocked_upload.call_args report_file = args[0][0] report = MaintenanceReport()._from_data(json.loads(report_file.read())) assert len(report.entries) == 1 assert report.entries[0].repo_id == "repo1" assert report.last_updated_by == "pubtools.pulplib" # search repo, upload and publish should be called once each assert requests_mocker.call_count == 1 assert mocked_publish.call_count == 1 assert mocked_upload.call_count == 1
def get_maintenance_report(self): self._ensure_alive() with self._state.lock: if self._state.maintenance_report: report = MaintenanceReport._from_data( json.loads(self._state.maintenance_report) ) else: report = MaintenanceReport() return f_proxy(f_return(report))
def test_load_invalid_report_raise_exception(): data = { "last_updated": "2019-08-15T14:21:1211", # invalid timestamp "last_updated_by": "pubtools.pulplib", "repos": { "repo1": { "message": "Maintenance Mode Enabled", "owner": "pubtools.pulplib", "started": "2019-08-15T14:21:12Z", } }, } with pytest.raises(InvalidDataException): MaintenanceReport._from_data(data)
def test_add_entry_existed(): entry = MaintenanceEntry( repo_id="repo1", owner="someone", message="Enabled", started=datetime.datetime(2019, 8, 15, 14, 21, 12), ) report = MaintenanceReport(entries=(entry,)) report = report.add(repo_ids=["repo1"], owner="someone_else") # add duplicated entry to report assert len(report.entries) == 1 assert report.entries[0].owner == "someone_else"
def test_create_export_report(): data = { "last_updated": "2019-08-15T14:21:12Z", "last_updated_by": "pubtools.pulplib", "repos": { "repo1": { "message": "Maintenance Mode Enabled", "owner": "pubtools.pulplib", "started": "2019-08-15T14:21:12Z", } }, } report = MaintenanceReport._from_data(data) exported_data = report._export_dict() assert data == exported_data
def test_report_add_remove(): data = { "last_updated": "2019-08-15T14:21:12Z", "last_updated_by": "pubtools.pulplib", "repos": { "repo1": { "message": "Maintenance Mode Enabled", "owner": "pubtools.pulplib", "started": "2019-08-15T14:21:12Z", } }, } report = MaintenanceReport._from_data(data) report = report.add(repo_ids=["repo2", "repo3"]) assert len(report.entries) == 3 assert report.last_updated_by == "ContentDelivery" report = report.remove(repo_ids=["repo1", "repo2"], owner="jazhang") assert len(report.entries) == 1 assert report.last_updated_by == "jazhang"
def test_create_report_with_duplicate_entries(): entry = MaintenanceEntry(repo_id="repo1") dup_entry = MaintenanceEntry(repo_id="repo1") with pytest.raises(ValueError): MaintenanceReport(entries=(entry, dup_entry))