From 21f4e45020016a641a9134bec9b918f370157ec9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Ferr=C3=A3o?= <2031761+viniciusferrao@users.noreply.github.com> Date: Tue, 24 Nov 2026 21:30:19 -0300 Subject: [PATCH] test(build): cover reusable policy synchronization --- xCAT-test/unit/generated_policy.t | 49 +++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 xCAT-test/unit/generated_policy.t diff --git a/xCAT-test/unit/generated_policy.t b/xCAT-test/unit/generated_policy.t new file mode 100644 index 0000000000..04621393d0 --- /dev/null +++ b/xCAT-test/unit/generated_policy.t @@ -0,0 +1,49 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use FindBin; +use lib "$FindBin::Bin/../../build-utils/lib", "$FindBin::Bin/../lib"; +use File::Slurper qw(read_text write_text); +use File::Temp qw(tempdir); +use Test::More; +use XCAT::BuildUtils qw(generated_policy_plan sync_generated_policy); +use XCAT::Test::Process qw(run_command); + +my $root = tempdir(CLEANUP => 1); +my %options = (root => $root, source => 'policy.sh', label => 'fixture', + command => 'sync-fixture', artifacts => ['consumer.sh']); +my $begin = "# BEGIN generated fixture policy: sync-fixture --write\n"; +my $end = "# END generated fixture policy\n"; +write_text("$root/policy.sh", "policy() { printf '%s' new; }\n"); +my $original = "#!/bin/sh\n$begin" . "policy() { printf '%s' old; }\n$end" . "policy\n"; +write_text("$root/consumer.sh", $original); +chmod oct('0750'), "$root/consumer.sh" or die $!; +my $plan = generated_policy_plan(%options); +is_deeply([map { $_->{relative} } @{$plan->{artifacts}}], ['consumer.sh'], 'plans the requested artifact'); +is(read_text("$root/consumer.sh"), $original, 'planning leaves the consumer unchanged'); +my $check = sync_generated_policy(%options, mode => 'check'); +is_deeply($check->{stale}, ['consumer.sh'], 'check finds a stale consumer'); +is_deeply($check->{written}, [], 'check writes nothing'); +is_deeply($check->{messages}, ['consumer.sh is out of date; run sync-fixture --write'], + 'diagnostic names the policy-specific repair command'); +my $write = sync_generated_policy(%options, mode => 'write'); +is_deeply($write->{written}, ['consumer.sh'], 'regenerates the consumer'); +my ($status, $output) = run_command('/bin/sh', "$root/consumer.sh"); +is($status, 0, 'executes the regenerated consumer'); +is($output, 'new', 'the consumer uses the new policy'); +is((stat("$root/consumer.sh"))[2] & oct('0777'), oct('0750'), 'retains its mode'); +is_deeply(sync_generated_policy(%options, mode => 'write')->{written}, [], + 'a repeated write changes nothing'); + +write_text("$root/policy.sh", "policy() { printf '%s' newer; }\n"); +write_text("$root/broken.sh", "#!/bin/sh\n$end$begin"); +my $before = read_text("$root/consumer.sh"); +for my $mode (qw(check write)) { + my $result = eval { sync_generated_policy(%options, mode => $mode, + artifacts => ['consumer.sh', 'broken.sh']) }; + is($result, undef, "$mode rejects malformed markers in a later artifact"); + like($@, qr/broken\.sh must contain one fixture policy block/, 'identifies the malformed artifact'); + is(read_text("$root/consumer.sh"), $before, 'leaves the earlier stale consumer unchanged'); +} + +done_testing();