From 3324dd72507e12fb44eafe74cc30a18bdc8d5b29 Mon Sep 17 00:00:00 2001 From: Daniel Hilst <392820+dhilst@users.noreply.github.com> Date: Tue, 27 Oct 2026 10:07:43 -0300 Subject: [PATCH] test(xcat-core): the Kea subnet-id test did not load the module kea_subnet_id_numeric.t copied the dedupe-key expression into the test and asserted on the copy. It never loaded xCAT::DHCP::Backend::Kea, so restoring the aliasing map in the module left the test passing 4 of 4. It measured Perl, not the product. The test now calls live_upsert_reservations through an injected control_agent_handler, as dhcp_kea_renderer.t does, and asserts on the body that encode_config produces, which is what the HTTP path sends. With the aliasing map restored, 3 of its 6 assertions fail and 3 pass. xcattest also carries the value class for a [Table_*] line. It was [\w\.\-], so a value of "2001:db8:176::" matched "2001" and the row was written with that value, and "/64" matched nothing. The class is now the one [Object_*] already uses. A ';' is deliberately left out: setup_env_by_configure_file builds a shell command from these values. Signed-off-by: Daniel Hilst <392820+dhilst@users.noreply.github.com> --- xCAT-test/unit/kea_subnet_id_numeric.t | 79 +++++++++++++++++--------- xCAT-test/xcattest | 4 +- 2 files changed, 54 insertions(+), 29 deletions(-) diff --git a/xCAT-test/unit/kea_subnet_id_numeric.t b/xCAT-test/unit/kea_subnet_id_numeric.t index bb1fd7150f..5f41064f30 100644 --- a/xCAT-test/unit/kea_subnet_id_numeric.t +++ b/xCAT-test/unit/kea_subnet_id_numeric.t @@ -1,41 +1,64 @@ #!/usr/bin/env perl # # Kea rejects a reservation whose subnet-id arrives as a JSON string, so every Control Agent -# update falls back to a full dhcpd restart. +# update falls back to a full server restart. # -# The cause is Perl aliasing, not a type declaration: map binds $_ to the hash element itself, and -# reading a numeric subnet-id through a string function attaches a string form to that scalar. -# JSON then serialises it as "1" rather than 1. Copying the element before reading it keeps the -# value numeric. +# The cause is Perl aliasing: the dedupe key in live_upsert_reservations read the subnet-id of +# each reservation through an aliased $_, and reading a numeric scalar in string context attaches +# a string form to it. The reservation body is encoded from the same scalar afterwards. use strict; use warnings; -use Test::More; -BEGIN { - eval { require JSON::XS; 1 } or plan skip_all => 'JSON::XS is required to observe the encoding'; +use FindBin; +use lib "$FindBin::Bin/../../perl-xCAT"; + +use Test::More tests => 6; + +use xCAT::DHCP::Backend::Kea; + +# Drive the real sub and encode the captured payload with the backend's own encoder, which is +# what the HTTP path uses. Asserting on the Perl value would pass with the bug in place. +sub upsert { + my (@args) = @_; + my @payloads; + my $backend = xCAT::DHCP::Backend::Kea->new( + control_agent_handler => sub { + my ($payload) = @_; + push @payloads, $payload; + return { ok => 1, result => 0, response => { result => 0 } }; + }, + ); + my $result = $backend->live_upsert_reservations(@args); + my @adds = grep { $_->{command} eq 'reservation-add' } @payloads; + return ( $result, $backend, \@adds ); } -plan tests => 4; -my $enc = JSON::XS->new->canonical; +my $reservation = { + 'subnet-id' => 1, + duid => '00:04:aa:bb', + 'ip-addresses' => ['2001:db8::20'], + hostname => 'node20', +}; -# How the dedupe key is built. The second form is what the backend must use. -sub key_aliased { my ($r) = @_; join('|', map { defined($_) ? lc($_) : '' } $r->{'subnet-id'}, 'duid') } -sub key_copied { my ($r) = @_; join('|', map { my $v = $_; defined($v) ? lc($v) : '' } $r->{'subnet-id'}, 'duid') } +my ( $result, $backend, $adds ) = upsert( [$reservation], service => ['dhcp6'] ); +ok( !$result->{error}, 'the upsert reaches the Control Agent handler' ); +is( scalar(@$adds), 1, 'one reservation-add is sent' ); -my $before = { 'subnet-id' => 1 }; -is($enc->encode({ 'subnet-id' => $before->{'subnet-id'} }), '{"subnet-id":1}', - 'a subnet-id straight from subnet_id_for_ip encodes as a JSON number'); +my $body = $backend->encode_config( $adds->[0] ); +like( $body, qr/"subnet-id"\s*:\s*1\b/, + 'the reservation body encodes subnet-id as a JSON number' ); +unlike( $body, qr/"subnet-id"\s*:\s*"1"/, + 'the reservation body does not encode subnet-id as a JSON string, which Kea rejects' ); -my $aliased = { 'subnet-id' => 1 }; -key_aliased($aliased); -is($enc->encode({ 'subnet-id' => $aliased->{'subnet-id'} }), '{"subnet-id":"1"}', - 'reading the element through an aliased $_ makes it encode as a JSON string, which Kea rejects'); - -my $copied = { 'subnet-id' => 1 }; -key_copied($copied); -is($enc->encode({ 'subnet-id' => $copied->{'subnet-id'} }), '{"subnet-id":1}', - 'copying the element first leaves it a JSON number'); - -is(key_copied({ 'subnet-id' => 1 }), key_aliased({ 'subnet-id' => 1 }), - 'and both forms still build the same dedupe key, so nothing else changes'); +# The dedupe key is built over the stale list as well, so a stale entry reads the same scalar a +# second time. +my $stale = { 'subnet-id' => 1, duid => '00:04:cc:dd' }; +( $result, $backend, $adds ) = upsert( + [ { %$reservation } ], + service => ['dhcp6'], + stale_reservations => [$stale], +); +ok( !$result->{error}, 'the upsert succeeds with a stale reservation to delete' ); +like( $backend->encode_config( $adds->[0] ), qr/"subnet-id"\s*:\s*1\b/, + 'subnet-id stays a JSON number when the dedupe key also reads a stale reservation' ); diff --git a/xCAT-test/xcattest b/xCAT-test/xcattest index a424ec6f1f..994e1b1fcc 100755 --- a/xCAT-test/xcattest +++ b/xCAT-test/xcattest @@ -814,7 +814,9 @@ sub load_config_file { $type = "Varible"; } elsif ($type eq "Table") { ##TABLE BLOCK## - if ($line =~ /(\w+)\s*=\s*([\w\.\-]+)/) { + # The value class carries ':' and '/' so an IPv6 network reaches the table. Without + # them "net=2001:db8:176::" matched "2001" and the row was written with that value. + if ($line =~ /(\w+)\s*=\s*([:,\w\.\-\/]+)/) { $attr = $1; $value = $2; if ($name && ($$config_ref{table}{$sub_type}{$name}{__KEY__} ne $attr)) {