|
1 | 1 | import pytest |
2 | 2 | import logging |
3 | | -import time |
4 | 3 |
|
5 | | -from tests.gnmi.grpc_utils import get_gnoi_system_stubs |
| 4 | +from tests.gnmi.grpc_utils import get_gnoi_system_stubs, create_grpc_channel |
6 | 5 |
|
7 | 6 | pytestmark = [ |
8 | 7 | pytest.mark.topology("any"), |
|
18 | 17 | system_pb2_grpc, system_pb2 = get_gnoi_system_stubs() |
19 | 18 |
|
20 | 19 |
|
21 | | -def test_gnoi_system_time(grpc_channel): |
| 20 | +def test_gnoi_system_time(duthosts, rand_one_dut_hostname): |
22 | 21 | """ |
23 | | - Verify the gNOI System Time API returns the current system time in valid JSON format. |
| 22 | + Verify the gNOI System Time API returns the current system time. |
24 | 23 | """ |
25 | | - # Use the shared gRPC channel |
26 | | - stub = system_pb2_grpc.SystemStub(grpc_channel) |
27 | | - |
28 | | - # Create and send request |
29 | | - request = system_pb2.TimeRequest() |
30 | | - response = stub.Time(request) |
31 | | - |
32 | | - # Log the response |
33 | | - logging.info("Received response: %s", response) |
34 | | - |
35 | | - # Assert the time falls into a reasonable interval |
36 | | - current_time_ns = int(time.time() * 1e9) |
37 | | - reasonable_interval_ns = 60 * 1e9 # 60 seconds in nanoseconds |
38 | | - |
39 | | - assert ( |
40 | | - abs(response.time - current_time_ns) < reasonable_interval_ns |
41 | | - ), f"System time {response.time} is not within the reasonable interval of current time {current_time_ns}" |
| 24 | + duthost = duthosts[rand_one_dut_hostname] |
| 25 | + |
| 26 | + # Get device time in seconds first (before gRPC operations) |
| 27 | + device_time_result = duthost.shell("date +%s", module_ignore_errors=True) |
| 28 | + device_time_s = int(device_time_result["stdout"].strip()) |
| 29 | + device_time_ns = device_time_s * int(1e9) |
| 30 | + |
| 31 | + # Create gRPC channel (no longer a fixture to avoid SSL state sharing) |
| 32 | + channel = create_grpc_channel(duthost) |
| 33 | + |
| 34 | + try: |
| 35 | + # Create gRPC stub |
| 36 | + stub = system_pb2_grpc.SystemStub(channel) |
| 37 | + |
| 38 | + # Create and send request |
| 39 | + request = system_pb2.TimeRequest() |
| 40 | + response = stub.Time(request) |
| 41 | + |
| 42 | + # Log the response |
| 43 | + logging.info("Received response: %s", response) |
| 44 | + logging.info("Device time from shell: %d", device_time_ns) |
| 45 | + |
| 46 | + # Assert the gNOI time is close to device shell time |
| 47 | + reasonable_interval_ns = 60 * 1e9 # 60 seconds in nanoseconds |
| 48 | + |
| 49 | + time_diff = abs(response.time - device_time_ns) |
| 50 | + assert time_diff < reasonable_interval_ns, ( |
| 51 | + f"gNOI time {response.time} differs from device time " |
| 52 | + f"{device_time_ns} by {time_diff}ns (max: {reasonable_interval_ns}ns)" |
| 53 | + ) |
| 54 | + finally: |
| 55 | + # Always close the channel to avoid resource leaks |
| 56 | + channel.close() |
0 commit comments