Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore transmitting results if testflinger.json is missing #234

Merged
merged 1 commit into from
Mar 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions agent/testflinger_agent/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,16 @@ def transmit_job_outcome(self, rundir):
:param rundir:
Execution dir where the results can be found
"""
with open(os.path.join(rundir, "testflinger.json")) as f:
job_data = json.load(f)
try:
with open(os.path.join(rundir, "testflinger.json")) as f:
job_data = json.load(f)
except OSError:
logger.error(
f"Unable to read job ID from {rundir}/testflinger.json. "
"This may be a job that was already transmitted, but "
"couldn't be removed."
)
return
job_id = job_data.get("job_id")
# If we find an 'artifacts' dir under rundir, archive it, and transmit
# it to the Testflinger server
Expand Down
42 changes: 42 additions & 0 deletions agent/testflinger_agent/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import json
import pytest
import uuid

Expand Down Expand Up @@ -67,3 +68,44 @@ def test_post_advertised_images(self, client, requests_mock):
assert requests_mock.last_request.json() == {
"test_queue": {"test_image": "url: http://foo"}
}

def test_transmit_job_outcome(self, client, requests_mock, tmp_path):
"""
Test that transmit_job_outcome sends results to the server
"""
job_id = str(uuid.uuid1())
testflinger_data = {"job_id": job_id}
testflinger_json = tmp_path / "testflinger.json"
testflinger_json.write_text(json.dumps(testflinger_data))
testflinger_outcome_json = tmp_path / "testflinger-outcome.json"
testflinger_outcome_json.write_text("{}")
requests_mock.post(
f"http://127.0.0.1:8000/v1/result/{job_id}", status_code=200
)
client.transmit_job_outcome(tmp_path)
assert requests_mock.last_request.json() == {"job_state": "complete"}

def test_transmit_job_artifact(self, client, requests_mock, tmp_path):
"""
Test that transmit_job_outcome sends artifacts if they exist
"""
artifacts_dir = tmp_path / "artifacts"
artifacts_dir.mkdir()
job_id = str(uuid.uuid1())
testflinger_data = {"job_id": job_id}
testflinger_json = tmp_path / "testflinger.json"
testflinger_json.write_text(json.dumps(testflinger_data))
requests_mock.post(
f"http://127.0.0.1:8000/v1/result/{job_id}/artifact",
status_code=200,
)
client.transmit_job_outcome(tmp_path)
assert requests_mock.called

def test_transmit_job_outcome_missing_json(self, client, tmp_path, caplog):
"""
Test that transmit_job_outcome logs an error and exits if
testflinger.json is missing
"""
client.transmit_job_outcome(tmp_path)
assert "Unable to read job ID" in caplog.text