diff --git a/xCAT-test/lib/xCAT/Test/OS.pm b/xCAT-test/lib/xCAT/Test/OS.pm index 9ddf20625a..a866c6408f 100644 --- a/xCAT-test/lib/xCAT/Test/OS.pm +++ b/xCAT-test/lib/xCAT/Test/OS.pm @@ -120,9 +120,64 @@ sub object_definition_command { my ($type, $name, $attrs) = @_; - my $cmd = "chdef -t $type -o $name"; + my $cmd = "chdef -t " . shell_quote($type) . " -o " . shell_quote($name); foreach my $attr (sort keys %$attrs) { - $cmd .= " $attr=" . shell_quote($attrs->{$attr}); + $cmd .= " " . shell_quote("$attr=" . $attrs->{$attr}); + } + return $cmd; +} + +#------------------------------------------------------- + +=head3 parse_conf_assignment + + Descriptions: Read one "attr=value" line of a conf section. + 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. It carries + ';' for the same reason, because othersettings and + vmothersetting separate their settings with one and the row + was written with the first setting alone. + Arguments: the line + Returns: the attribute and the value, or an empty list when the line is + not an assignment + +=cut + +#------------------------------------------------------- +sub parse_conf_assignment +{ + my ($line) = @_; + + return unless defined $line; + return unless $line =~ /(\w+)\s*=\s*([:,;\w\.\-\/]+)/; + return ($1, $2); +} + +#------------------------------------------------------- + +=head3 table_row_command + + Descriptions: The chtab command that writes one table row from a conf + section. Every argument is quoted, the row selector with + them: a conf carries values a shell reads as syntax, and + othersettings separates its settings with a semicolon, which + would end the command and run the rest. + Arguments: the table name, the key column, the row name, a hash reference + of attributes + Returns: the command + +=cut + +#------------------------------------------------------- +sub table_row_command +{ + my ($table, $key, $name, $attrs) = @_; + + my $cmd = "chtab " . shell_quote("$key=$name"); + foreach my $attr (sort keys %$attrs) { + next if $attr eq '__KEY__'; + $cmd .= " " . shell_quote("$table.$attr=" . $attrs->{$attr}); } return $cmd; } diff --git a/xCAT-test/unit/xcattest_object_quoting.t b/xCAT-test/unit/xcattest_object_quoting.t index c28127b94f..1f6b00ef79 100644 --- a/xCAT-test/unit/xcattest_object_quoting.t +++ b/xCAT-test/unit/xcattest_object_quoting.t @@ -12,19 +12,20 @@ use warnings; use FindBin; use lib "$FindBin::Bin/../lib"; use Test::More; +use Text::ParseWords qw(shellwords); use xCAT::Test::OS; # A value a shell would read as two commands. is( xCAT::Test::OS::object_definition_command( 'node', 'cn1', { vmothersetting => 'cpumode:host-passthrough;firmware:efi-insecure' } ), - q{chdef -t node -o cn1 vmothersetting='cpumode:host-passthrough;firmware:efi-insecure'}, + q{chdef -t 'node' -o 'cn1' 'vmothersetting=cpumode:host-passthrough;firmware:efi-insecure'}, 'a semicolon in a value stays inside one command' ); # Several attributes keep a stable order, so the command is reproducible. is( xCAT::Test::OS::object_definition_command( 'node', 'cn1', { arch => 'x86_64', netboot => 'grub2' } ), - q{chdef -t node -o cn1 arch='x86_64' netboot='grub2'}, + q{chdef -t 'node' -o 'cn1' 'arch=x86_64' 'netboot=grub2'}, 'every value is quoted, in attribute order' ); # The other characters a conf can carry. @@ -34,15 +35,76 @@ is( xCAT::Test::OS::shell_quote('a`b`'), q{'a`b`'}, 'a backtick is quote is( xCAT::Test::OS::shell_quote(q{it's}), q{'it'\''s'}, 'a single quote is escaped' ); is( xCAT::Test::OS::shell_quote(undef), q{''}, 'an undefined value gives an empty argument' ); -# What the shell actually does with the result, which is the point of the quoting. -my $cmd = xCAT::Test::OS::object_definition_command( - 'node', 'cn1', { vmothersetting => 'cpumode:host-passthrough;firmware:efi-insecure' } ); -my ($value) = $cmd =~ /vmothersetting=(.*)$/; -open( my $bash, '-|', '/bin/bash', '-c', "printf %s $value" ) - or die "run bash: $!"; -my $seen = do { local $/; <$bash> }; -close($bash); -is( $seen, 'cpumode:host-passthrough;firmware:efi-insecure', - 'bash passes the whole value through as one argument' ); +# The arguments the command actually delivers, which is the point of the quoting. +is_deeply( + [ shellwords( xCAT::Test::OS::object_definition_command( + 'node', 'cn1', + { vmothersetting => 'cpumode:host-passthrough;firmware:efi-insecure' } ) ) ], + [ 'chdef', '-t', 'node', '-o', 'cn1', + 'vmothersetting=cpumode:host-passthrough;firmware:efi-insecure' ], + 'chdef receives the whole setting as one argument' +); + +# ---- the chtab command for a [Table_*] section ------------------------------------------- +# xcattest built this one by interpolation, with the row selector unquoted. The row of a +# [Table_vm] section carries othersettings, whose settings a semicolon separates, so the +# command ended at the semicolon and the shell ran the rest of the value as a command. +is( xCAT::Test::OS::table_row_command( + 'vm', 'node', 'cn1', + { othersettings => 'cpumode:host-passthrough;firmware:efi-secureboot' } ), + q{chtab 'node=cn1' 'vm.othersettings=cpumode:host-passthrough;firmware:efi-secureboot'}, + 'the row selector is quoted with every other argument' ); + +is_deeply( + [ shellwords( xCAT::Test::OS::table_row_command( + 'vm', 'node', 'cn1', + { othersettings => 'cpumode:host-passthrough;firmware:efi-secureboot' } ) ) ], + [ 'chtab', 'node=cn1', + 'vm.othersettings=cpumode:host-passthrough;firmware:efi-secureboot' ], + 'chtab receives the whole setting as one argument, not two commands' +); + +# __KEY__ names the key column and is not a column of its own. +is_deeply( + [ shellwords( xCAT::Test::OS::table_row_command( + 'vm', 'node', 'cn1', + { __KEY__ => 'node', cpus => '4', memory => '8192' } ) ) ], + [ 'chtab', 'node=cn1', 'vm.cpus=4', 'vm.memory=8192' ], + 'the key marker is not written as an attribute' +); + +# A row name a shell would read as syntax. +is_deeply( + [ shellwords( xCAT::Test::OS::table_row_command( + 'site', 'key', 'a b', { value => 'x;y' } ) ) ], + [ 'chtab', 'key=a b', 'site.value=x;y' ], + 'a space in the row name stays in one argument' +); + +# ---- what the conf parser keeps ---------------------------------------------------------- +# The quoting above is useless if the value never reaches it whole. The value class had no +# ';', so "othersettings=cpumode:host-passthrough;firmware:efi-secureboot" was read as +# "cpumode:host-passthrough" and the rest was dropped with no warning. +is_deeply( + [ xCAT::Test::OS::parse_conf_assignment( + 'othersettings=cpumode:host-passthrough;firmware:efi-secureboot') ], + [ 'othersettings', 'cpumode:host-passthrough;firmware:efi-secureboot' ], + 'a semicolon value is read whole' +); +is_deeply( + [ xCAT::Test::OS::parse_conf_assignment('net=2001:db8:176::') ], + [ 'net', '2001:db8:176::' ], + 'an IPv6 network keeps its colons' +); +is_deeply( + [ xCAT::Test::OS::parse_conf_assignment('export=/opt/xcat-ci-shared/isos') ], + [ 'export', '/opt/xcat-ci-shared/isos' ], + 'a path keeps its slashes' +); +is_deeply( + [ xCAT::Test::OS::parse_conf_assignment('[Table_vm]') ], + [], + 'a section header is not an assignment' +); done_testing(); diff --git a/xCAT-test/xcattest b/xCAT-test/xcattest index 9bca2477e2..f5fe0fca83 100755 --- a/xCAT-test/xcattest +++ b/xCAT-test/xcattest @@ -814,11 +814,9 @@ sub load_config_file { $type = "Varible"; } elsif ($type eq "Table") { ##TABLE BLOCK## - # 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 ( my ($a, $v) = xCAT::Test::OS::parse_conf_assignment($line) ) { + $attr = $a; + $value = $v; if ($name && ($$config_ref{table}{$sub_type}{$name}{__KEY__} ne $attr)) { $$config_ref{table}{$sub_type}{$name}{$attr} = $value; } else { @@ -828,9 +826,9 @@ sub load_config_file { } } elsif ($type eq "Object") { ##OBJECT BLOCK## - if ($line =~ /(\w+)\s*=\s*([:,\w\.\-\/]+)/) { - $attr = $1; - $value = $2; + if ( my ($a, $v) = xCAT::Test::OS::parse_conf_assignment($line) ) { + $attr = $a; + $value = $v; if ($attr eq "Name") { $name = $value; } elsif (!defined($name)) { @@ -1601,13 +1599,13 @@ sub setup_env_by_configure_file { if (exists $$config_ref{table}) { foreach my $type (keys %{ $$config_ref{table} }) { foreach my $name (keys %{ $$config_ref{table}{$type} }) { - $cmd = "chtab $$config_ref{table}{$type}{$name}{__KEY__}=$name"; - foreach my $attr (sort keys %{ $$config_ref{table}{$type}{$name} }) { - if ($attr ne '__KEY__') { - $cmd = $cmd . " $type.$attr=" - . xCAT::Test::OS::shell_quote( $$config_ref{table}{$type}{$name}{$attr} ); - } - } + # Every argument is quoted, the row selector with them. See + # table_row_command: a conf value can carry shell syntax. + $cmd = xCAT::Test::OS::table_row_command( + $type, + $$config_ref{table}{$type}{$name}{__KEY__}, + $name, + $$config_ref{table}{$type}{$name} ); log_this($running_log_fd, "$cmd"); &runcmd($cmd); if ($::RUNCMD_RC != 0) {