Skip to content

Commit 8042c56

Browse files
Added Install for gNOI OS service (sonic-net#270)
1 parent 4b9f126 commit 8042c56

2 files changed

Lines changed: 61 additions & 1 deletion

File tree

host_modules/image_service.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,4 +219,33 @@ def _parse_sonic_installer_list(self, output):
219219
"current": current_image or "",
220220
"next": next_image or "",
221221
"available": available_images or [],
222-
}
222+
}
223+
224+
@host_service.method(
225+
host_service.bus_name(MOD_NAME), in_signature="as", out_signature="is"
226+
)
227+
def gnoi_install_os(self, options):
228+
"""
229+
Handles the gNOI OS Install RPC.
230+
231+
Args:
232+
options (str): JSON-formatted InstallRequest from os.proto
233+
234+
Returns:
235+
int: 0 on success
236+
str: JSON-encoded InstallResponse with TransferReady or TransferProgress or Validated msg
237+
"""
238+
logger.info("installos: gNOI OS Install RPC called.")
239+
240+
# Join list of strings into one JSON string
241+
request_str = " ".join(options)
242+
logger.info("InstallRequest JSON string: %s", request_str)
243+
244+
try:
245+
request_json = json.loads(request_str)
246+
logger.info("Parsed InstallRequest: %s", json.dumps(request_json, indent=2))
247+
except json.JSONDecodeError as e:
248+
logger.error("Failed to parse InstallRequest JSON: %s", e)
249+
return 1, f"Invalid InstallRequest JSON: {e}"
250+
251+
return 1, "ERROR_UNIMPLEMENTED: OS Install not supported"

tests/host_modules/image_service_test.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,3 +658,34 @@ def test_image_set_next_boot_fail_not_exists_generic_rc(self, mock_run, MockInit
658658
stderr=subprocess.PIPE,
659659
)
660660

661+
@mock.patch("dbus.SystemBus")
662+
@mock.patch("dbus.service.BusName")
663+
@mock.patch("dbus.service.Object.__init__")
664+
def test_gnoi_install_os_valid_json(self, MockInit, MockBusName, MockSystemBus):
665+
"""
666+
Test that gnoi_install_os handles valid JSON input and returns ERROR_UNIMPLEMENTED.
667+
"""
668+
image_service = ImageService(mod_name="image_service")
669+
options = ['{', '"version": "1.0.0"', '}'] # Valid JSON when joined
670+
671+
rc, response = image_service.gnoi_install_os(options)
672+
673+
assert rc == 1
674+
assert "ERROR_UNIMPLEMENTED" in response
675+
676+
677+
@mock.patch("dbus.SystemBus")
678+
@mock.patch("dbus.service.BusName")
679+
@mock.patch("dbus.service.Object.__init__")
680+
def test_gnoi_install_os_invalid_json(self, MockInit, MockBusName, MockSystemBus):
681+
"""
682+
Test that gnoi_install_os returns error for invalid JSON input.
683+
"""
684+
image_service = ImageService(mod_name="image_service")
685+
options = ['{', '"version": "1.0.0"', 'invalid'] # Invalid JSON
686+
687+
rc, response = image_service.gnoi_install_os(options)
688+
689+
assert rc == 1
690+
assert "Invalid InstallRequest JSON" in response
691+

0 commit comments

Comments
 (0)