Skip to content

Commit

Permalink
Merge pull request systemd#29937 from yuwata/network-fix-json-format
Browse files Browse the repository at this point in the history
network: fix json output
  • Loading branch information
yuwata authored Nov 9, 2023
2 parents 4287498 + 3aa4769 commit b5f7d1d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
24 changes: 12 additions & 12 deletions src/network/networkd-json.c
Original file line number Diff line number Diff line change
Expand Up @@ -1057,15 +1057,15 @@ 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);
}

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);
Expand All @@ -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(
Expand Down Expand Up @@ -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);
Expand All @@ -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),
Expand Down
13 changes: 13 additions & 0 deletions test/test-network/systemd-networkd-tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -5177,6 +5177,10 @@ def test_dhcp_client_ipv6_only(self):
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',
Expand Down Expand Up @@ -5228,6 +5232,11 @@ def test_dhcp_client_ipv6_only(self):
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')

Expand Down Expand Up @@ -5277,6 +5286,10 @@ def test_dhcp_client_ipv6_only(self):
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()
Expand Down

0 comments on commit b5f7d1d

Please sign in to comment.