Files

221 lines
6.4 KiB
Perl
Raw Permalink Normal View History

#!/usr/bin/perl
# Stub commands for the sglang-deploy.pl rig. One file, dispatched by $0's basename,
# so every stubbed tool answers from the fixture under /rig/fixture and /rig/state.
use strict;
use warnings;
# Everything this stub reads and writes lives in the work directory the rig exports to
# the script under test, which is why it is read from the environment: the stub is
# reached through a link named after the command it stands in for.
my $RIG = $ENV{STUB_WORK} // '/tmp/sglang-rig';
my $FIX = "$RIG/fixture";
my $ST = "$RIG/state";
my $LOG = "$RIG/log/stubs.log";
my ($name) = $0 =~ m{([^/]+)$};
my @args = @ARGV;
sub log_call {
if (open(my $fh, '>>', $LOG)) {
print {$fh} "$name @args\n";
close($fh);
}
return;
}
sub fixture_text {
my ($file, $default) = @_;
my $path = "$FIX/$file";
return $default unless -f $path;
open(my $fh, '<', $path) or return $default;
local $/ = undef;
my $text = <$fh>;
close($fh);
return defined $text ? $text : $default;
}
# Image names carry slashes and colons, which are not file names; the marker files keep
# the image identity without them.
sub state_name { my ($name) = @_; $name =~ s{[/:]}{_}g; return $name }
sub state_exists { return -e "$ST/" . state_name($_[0]) ? 1 : 0 }
sub touch_state {
my $fh;
open($fh, '>', "$ST/" . state_name($_[0])) and close($fh);
return;
}
sub drop_state { unlink("$ST/" . state_name($_[0])); return }
log_call();
if ($name eq 'lspci') {
print fixture_text('gpu.txt', '');
exit 0;
}
if ($name eq 'rpm') {
my $joined = join(' ', @args);
if ($joined =~ /%\{VERSION\}/ && $joined =~ /rocm-runtime/) {
my $version = fixture_text('rocm.txt', '');
$version =~ s/\s+$//;
exit 1 unless length $version;
print "$version\n";
exit 0;
}
my $pkg = $args[-1];
my $installed = fixture_text('rpm-installed', '');
if ($installed =~ /^\Q$pkg\E$/m) {
print "$pkg-1.0-1.x86_64\n";
exit 0;
}
print STDERR "package $pkg is not installed\n";
exit 1;
}
if ($name eq 'dnf') {
my @pkgs = grep { !/^-/ && $_ ne 'install' } @args;
my $installed = fixture_text('rpm-installed', '');
for my $pkg (@pkgs) {
$installed .= "$pkg\n" unless $installed =~ /^\Q$pkg\E$/m;
}
if (open(my $fh, '>', "$FIX/rpm-installed")) {
print {$fh} $installed;
close($fh);
}
print "Installing: @pkgs\n";
exit 0;
}
if ($name eq 'podman') {
my $sub = $args[0] // '';
if ($sub eq '--version') { print "podman version 5.8.4\n"; exit 0 }
if ($sub eq 'pull') {
my $image = $args[-1];
touch_state("image.$image");
print "Pulled $image\n";
exit 0;
}
if ($sub eq 'image' && ($args[1] // '') eq 'exists') {
exit(state_exists("image.$args[2]") ? 0 : 1);
}
if ($sub eq 'container' && ($args[1] // '') eq 'exists') {
exit(state_exists("container.$args[2]") ? 0 : 1);
}
if ($sub eq 'rm') {
my $target = $args[-1];
drop_state("container.$target");
print "$target\n";
exit 0;
}
exit 0;
}
if ($name eq 'systemctl') {
my @rest = grep { $_ !~ /^--/ } @args;
my $verb = shift @rest // '';
my $svc = $rest[0] // '';
$svc =~ s/\.service$//;
if ($verb eq 'is-active') { exit(state_exists("active.$svc") ? 0 : 1) }
if ($verb eq 'is-enabled') { exit(state_exists("enabled.$svc") ? 0 : 1) }
if ($verb eq 'start') { touch_state("active.$svc"); exit 0 }
if ($verb eq 'stop') { drop_state("active.$svc"); exit 0 }
if ($verb eq 'enable') { touch_state("enabled.$svc"); touch_state("active.$svc") if grep { $_ eq q{--now} } @args; exit 0 }
if ($verb eq 'disable') { drop_state("enabled.$svc"); exit 0 }
if ($verb eq 'try-restart') { exit(state_exists("active.$svc") ? 0 : 1) }
if ($verb eq 'reload' || $verb eq 'daemon-reload') { exit 0 }
exit 0;
}
if ($name eq 'curl') {
my ($url) = grep { /^https?:\/\// } @args;
my $joined = join(' ', @args);
$url //= '';
if ($url =~ m{modelscope\.cn/api/v1/models/}) {
my $code = fixture_text('ms.code', '200');
$code =~ s/\s+$//;
my $body = $joined =~ /__HTTP__/ ? fixture_text('ms.json', '{"Data":{"Name":"GLM-5.3"}}') : '';
print $body;
print "\n__HTTP__$code\n" if $joined =~ /__HTTP__/;
exit 0;
}
if ($url =~ m{api\.github\.com}) {
print fixture_text('releases.json', qq({"tag_name":"v0.5.19"}\n));
exit 0;
}
if ($url =~ m{hub\.docker\.com}) {
if (-f "$FIX/image-missing") {
print STDERR "curl: (22) The requested URL returned error: 404\n";
exit 22;
}
if ($url =~ /tags\?/) {
# A tag listing, newest first: the rig's AMD development build.
print qq({"results":[{"name":"v0.5.19-rocm724-gfx1151-20260916"}]}\n);
exit 0;
}
print qq({"name":"ok"}\n);
exit 0;
}
exit 0;
}
if ($name eq 'openssl') {
my %want;
for my $i (0 .. $#args) {
if ($args[$i] eq '-keyout') { $want{key} = $args[$i + 1] }
if ($args[$i] eq '-out') { $want{crt} = $args[$i + 1] }
if ($args[$i] eq '-subj') { $want{subj} = $args[$i + 1] }
if ($args[$i] eq '-addext') { $want{san} = $args[$i + 1] }
}
for my $kind (qw(key crt)) {
my $path = $want{$kind};
next unless defined $path;
if (open(my $fh, '>', $path)) {
print {$fh} "stub $kind $want{subj} $want{san}\n";
close($fh);
}
else {
print STDERR "can't open $path\n";
exit 1;
}
}
exit 0;
}
if ($name eq 'nginx') {
print "nginx: configuration file /etc/nginx/nginx.conf test is successful\n";
exit 0;
}
if ($name eq 'firewall-cmd') {
my $joined = join(' ', @args);
if ($joined =~ /--list-services/) {
print fixture_text('fw-services', "\n");
exit 0;
}
if ($joined =~ /--add-service=https/) {
my $fh;
open($fh, q{>>}, "$FIX/fw-services") and print {$fh} "https\n" and close($fh);
exit 0;
}
exit 0;
}
if ($name eq 'getenforce') {
my $mode = $ENV{STUB_SELINUX} // 'Enforcing';
print "$mode\n";
exit 0;
}
if ($name eq 'getsebool') {
my $value = state_exists('sebool') ? 'on' : 'off';
print "httpd_can_network_connect --> $value\n";
exit 0;
}
if ($name eq 'setsebool') {
touch_state('sebool');
exit 0;
}
exit 0;