Merge pull request #29937 from yuwata/network-fix-json-format

network: fix json output
This commit is contained in:
Yu Watanabe
2023-11-09 21:47:37 +09:00
committed by GitHub
2 changed files with 25 additions and 12 deletions

View File

@@ -1057,7 +1057,7 @@ static int dhcp6_client_vendor_options_append_json(Link *link, JsonVariant **v)
JSON_BUILD_PAIR_UNSIGNED("SubOptionCode", (*option)->option),
JSON_BUILD_PAIR_HEX("SubOptionData", (*option)->data, (*option)->length)));
if (r < 0)
return 0;
return r;
}
return json_variant_set_field_non_null(v, "VendorSpecificOptions", array);
@@ -1065,7 +1065,7 @@ static int dhcp6_client_vendor_options_append_json(Link *link, JsonVariant **v)
static int dhcp6_client_lease_append_json(Link *link, JsonVariant **v) {
_cleanup_(json_variant_unrefp) JsonVariant *w = NULL;
usec_t ts, t1, t2;
usec_t ts = USEC_INFINITY, t1 = USEC_INFINITY, t2 = USEC_INFINITY;
int r;
assert(link);
@@ -1075,15 +1075,15 @@ static int dhcp6_client_lease_append_json(Link *link, JsonVariant **v) {
return 0;
r = sd_dhcp6_lease_get_timestamp(link->dhcp6_lease, CLOCK_BOOTTIME, &ts);
if (r < 0)
if (r < 0 && r != -ENODATA)
return r;
r = sd_dhcp6_lease_get_t1_timestamp(link->dhcp6_lease, CLOCK_BOOTTIME, &t1);
if (r < 0)
if (r < 0 && r != -ENODATA)
return r;
r = sd_dhcp6_lease_get_t2_timestamp(link->dhcp6_lease, CLOCK_BOOTTIME, &t2);
if (r < 0)
if (r < 0 && r != -ENODATA)
return r;
r = json_build(&w, JSON_BUILD_OBJECT(
@@ -1161,7 +1161,7 @@ static int dhcp6_client_append_json(Link *link, JsonVariant **v) {
static int dhcp_client_lease_append_json(Link *link, JsonVariant **v) {
_cleanup_(json_variant_unrefp) JsonVariant *w = NULL;
usec_t lease_timestamp_usec, t1, t2;
usec_t lease_timestamp_usec = USEC_INFINITY, t1 = USEC_INFINITY, t2 = USEC_INFINITY;
int r;
assert(link);
@@ -1171,16 +1171,16 @@ static int dhcp_client_lease_append_json(Link *link, JsonVariant **v) {
return 0;
r = sd_dhcp_lease_get_timestamp(link->dhcp_lease, CLOCK_BOOTTIME, &lease_timestamp_usec);
if (r < 0)
return 0;
if (r < 0 && r != -ENODATA)
return r;
r = sd_dhcp_lease_get_t1_timestamp(link->dhcp_lease, CLOCK_BOOTTIME, &t1);
if (r < 0)
return 0;
if (r < 0 && r != -ENODATA)
return r;
r = sd_dhcp_lease_get_t2_timestamp(link->dhcp_lease, CLOCK_BOOTTIME, &t2);
if (r < 0)
return 0;
if (r < 0 && r != -ENODATA)
return r;
r = json_build(&w, JSON_BUILD_OBJECT(
JSON_BUILD_PAIR_FINITE_USEC("LeaseTimestampUSec", lease_timestamp_usec),

View File

@@ -5177,6 +5177,10 @@ class NetworkdDHCPClientTests(unittest.TestCase, Utilities):
self.assertNotIn('DHCPREQUEST(veth-peer)', output)
self.assertNotIn('DHCPREPLY(veth-peer)', output)
# Check json format
output = check_output(*networkctl_cmd, '--json=short', 'status', 'veth99', env=env)
check_json(output)
# solicit mode
stop_dnsmasq()
start_dnsmasq('--dhcp-option=108,00:00:02:00',
@@ -5228,6 +5232,11 @@ class NetworkdDHCPClientTests(unittest.TestCase, Utilities):
self.assertIn('DHCPREPLY(veth-peer)', output)
self.assertIn('sent size: 0 option: 14 rapid-commit', output)
# Check json format
output = check_output(*networkctl_cmd, '--json=short', 'status', 'veth99', env=env)
check_json(output)
# Testing without rapid commit support
with open(os.path.join(network_unit_dir, '25-dhcp-client-ipv6-only.network'), mode='a', encoding='utf-8') as f:
f.write('\n[DHCPv6]\nRapidCommit=no\n')
@@ -5277,6 +5286,10 @@ class NetworkdDHCPClientTests(unittest.TestCase, Utilities):
self.assertIn('DHCPREPLY(veth-peer)', output)
self.assertNotIn('rapid-commit', output)
# Check json format
output = check_output(*networkctl_cmd, '--json=short', 'status', 'veth99', env=env)
check_json(output)
def test_dhcp_client_ipv6_dbus_status(self):
copy_network_unit('25-veth.netdev', '25-dhcp-server-veth-peer.network', '25-dhcp-client-ipv6-only.network')
start_networkd()