Skip to content

Commit

Permalink
Merge pull request #203 from canonical/rearrange-advertised-queues-im…
Browse files Browse the repository at this point in the history
…ages
  • Loading branch information
plars authored Feb 23, 2024
2 parents 755c0ab + 07f9a6f commit 61e0439
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 35 deletions.
22 changes: 2 additions & 20 deletions agent/testflinger_agent/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ def __init__(self, client):
def _post_initial_agent_data(self):
"""Post the initial agent data to the server once on agent startup"""

self._post_advertised_queues()
self._post_advertised_images()
self.client.post_advertised_queues()
self.client.post_advertised_images()

identifier = self.client.config.get("identifier")
location = self.client.config.get("location", "")
Expand All @@ -50,24 +50,6 @@ def _post_initial_agent_data(self):

self.client.post_agent_data(agent_data)

def _post_advertised_queues(self):
"""
Get the advertised queues from the config and send them to the server
:return: Dictionary of advertised queues
"""
advertised_queues = self.client.config.get("advertised_queues", {})
if advertised_queues:
self.client.post_queues(advertised_queues)

def _post_advertised_images(self):
"""
Get the advertised images from the config and post them to the server
"""
advertised_images = self.client.config.get("advertised_images")
if advertised_images:
self.client.post_images(advertised_images)

def set_agent_state(self, state):
"""Send the agent state to the server"""
self.client.post_agent_data({"state": state})
Expand Down
28 changes: 14 additions & 14 deletions agent/testflinger_agent/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,27 +266,27 @@ def post_live_output(self, job_id, data):
return False
return bool(job_request)

def post_queues(self, data):
"""Post the list of advertised queues to testflinger server
:param data:
dict of queue name and descriptions to send to the server
"""
def post_advertised_queues(self):
"""Post the list of advertised queues to testflinger server"""
if "advertised_queues" not in self.config:
return
queues_uri = urljoin(self.server, "/v1/agents/queues")
try:
self.session.post(queues_uri, json=data, timeout=30)
self.session.post(
queues_uri, json=self.config["advertised_queues"], timeout=30
)
except RequestException as exc:
logger.error(exc)

def post_images(self, data):
"""Post the list of advertised images to testflinger server
:param data:
dict of queues containing dicts of imgae names and provision data
"""
def post_advertised_images(self):
"""Post the list of advertised images to testflinger server"""
if "advertised_images" not in self.config:
return
images_uri = urljoin(self.server, "/v1/agents/images")
try:
self.session.post(images_uri, json=data, timeout=30)
self.session.post(
images_uri, json=self.config["advertised_images"], timeout=30
)
except RequestException as exc:
logger.error(exc)

Expand Down
31 changes: 30 additions & 1 deletion agent/testflinger_agent/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,14 @@
class TestClient:
@pytest.fixture
def client(self):
yield _TestflingerClient({"server_address": "127.0.0.1:8000"})
config = {
"server_address": "127.0.0.1:8000",
"advertised_queues": {"test_queue": "test_queue"},
"advertised_images": {
"test_queue": {"test_image": "url: http://foo"}
},
}
yield _TestflingerClient(config)

def test_check_jobs_empty(self, client, requests_mock):
requests_mock.get(rmock.ANY, status_code=200)
Expand All @@ -38,3 +45,25 @@ def test_check_jobs_with_job(self, client, requests_mock):
requests_mock.get(rmock.ANY, json=fake_job_data)
job_data = client.check_jobs()
assert job_data == fake_job_data

def test_post_advertised_queues(self, client, requests_mock):
"""
ensure that the server api /v1/agents/queues was called with
the correct queue data
"""
requests_mock.post(rmock.ANY, status_code=200)
client.post_advertised_queues()
assert requests_mock.last_request.json() == {
"test_queue": "test_queue"
}

def test_post_advertised_images(self, client, requests_mock):
"""
ensure that the server api /v1/agents/images was called with
the correct image data
"""
requests_mock.post(rmock.ANY, status_code=200)
client.post_advertised_images()
assert requests_mock.last_request.json() == {
"test_queue": {"test_image": "url: http://foo"}
}

0 comments on commit 61e0439

Please sign in to comment.