captureStdoutStderr*: move to Hydra::Helper::Exec which helps avoid some environment variable fixation problems

This commit is contained in:
Graham Christensen 2022-02-09 13:40:51 -05:00
parent 1abe7f4d80
commit 845e6d4760
16 changed files with 54 additions and 50 deletions

View file

@ -0,0 +1,35 @@
use warnings;
use strict;
use IPC::Run;
package Hydra::Helper::Exec;
our @ISA = qw(Exporter);
our @EXPORT = qw(
captureStdoutStderr
captureStdoutStderrWithStdin
);
sub captureStdoutStderr {
my ($timeout, @cmd) = @_;
return captureStdoutStderrWithStdin($timeout, \@cmd, "");
}
sub captureStdoutStderrWithStdin {
my ($timeout, $cmd, $stdin) = @_;
my $stdout;
my $stderr;
eval {
local $SIG{ALRM} = sub { die "timeout\n" }; # NB: \n required
alarm $timeout;
IPC::Run::run($cmd, \$stdin, \$stdout, \$stderr);
alarm 0;
1;
} or do {
die unless $@ eq "timeout\n"; # propagate unexpected errors
return (-1, $stdout, ($stderr // "") . "timeout\n");
};
return ($?, $stdout, $stderr);
}

View file

@ -18,8 +18,6 @@ use UUID4::Tiny qw(is_uuid4_string);
our @ISA = qw(Exporter); our @ISA = qw(Exporter);
our @EXPORT = qw( our @EXPORT = qw(
cancelBuilds cancelBuilds
captureStdoutStderr
captureStdoutStderrWithStdin
constructRunCommandLogPath constructRunCommandLogPath
findLog findLog
gcRootFor gcRootFor
@ -429,30 +427,7 @@ sub pathIsInsidePrefix {
} }
sub captureStdoutStderr {
my ($timeout, @cmd) = @_;
return captureStdoutStderrWithStdin($timeout, \@cmd, "");
}
sub captureStdoutStderrWithStdin {
my ($timeout, $cmd, $stdin) = @_;
my $stdout;
my $stderr;
eval {
local $SIG{ALRM} = sub { die "timeout\n" }; # NB: \n required
alarm $timeout;
IPC::Run::run($cmd, \$stdin, \$stdout, \$stderr);
alarm 0;
1;
} or do {
die unless $@ eq "timeout\n"; # propagate unexpected errors
return (-1, $stdout, ($stderr // "") . "timeout\n");
};
return ($?, $stdout, $stderr);
}
sub run { sub run {

View file

@ -5,6 +5,7 @@ use warnings;
use parent 'Hydra::Plugin'; use parent 'Hydra::Plugin';
use Digest::SHA qw(sha256_hex); use Digest::SHA qw(sha256_hex);
use File::Path; use File::Path;
use Hydra::Helper::Exec;
use Hydra::Helper::Nix; use Hydra::Helper::Nix;
use Nix::Store; use Nix::Store;

View file

@ -5,6 +5,7 @@ use warnings;
use parent 'Hydra::Plugin'; use parent 'Hydra::Plugin';
use Digest::SHA qw(sha256_hex); use Digest::SHA qw(sha256_hex);
use File::Path; use File::Path;
use Hydra::Helper::Exec;
use Hydra::Helper::Nix; use Hydra::Helper::Nix;
use Nix::Store; use Nix::Store;

View file

@ -6,6 +6,7 @@ use parent 'Hydra::Plugin';
use Digest::SHA qw(sha256_hex); use Digest::SHA qw(sha256_hex);
use File::Path; use File::Path;
use Hydra::Helper::Nix; use Hydra::Helper::Nix;
use Hydra::Helper::Exec;
use Nix::Store; use Nix::Store;
use Fcntl qw(:flock); use Fcntl qw(:flock);

View file

@ -4,6 +4,7 @@ use strict;
use warnings; use warnings;
use parent 'Hydra::Plugin'; use parent 'Hydra::Plugin';
use Digest::SHA qw(sha256_hex); use Digest::SHA qw(sha256_hex);
use Hydra::Helper::Exec;
use Hydra::Helper::Nix; use Hydra::Helper::Nix;
use IPC::Run; use IPC::Run;
use Nix::Store; use Nix::Store;

View file

@ -11,6 +11,7 @@ use File::Slurper qw(read_text);
use Hydra::Helper::AddBuilds; use Hydra::Helper::AddBuilds;
use Hydra::Helper::CatalystUtils; use Hydra::Helper::CatalystUtils;
use Hydra::Helper::Email; use Hydra::Helper::Email;
use Hydra::Helper::Exec;
use Hydra::Helper::Nix; use Hydra::Helper::Nix;
use Hydra::Model::DB; use Hydra::Model::DB;
use Hydra::Plugin; use Hydra::Plugin;

View file

@ -6,13 +6,13 @@ use IO::Uncompress::Bunzip2 qw(bunzip2);
use Archive::Tar; use Archive::Tar;
use JSON::MaybeXS qw(decode_json); use JSON::MaybeXS qw(decode_json);
use Data::Dumper; use Data::Dumper;
use Hydra::Helper::Exec;
my %ctx = test_init( my %ctx = test_init(
use_external_destination_store => 0 use_external_destination_store => 0
); );
require Hydra::Schema; require Hydra::Schema;
require Hydra::Model::DB; require Hydra::Model::DB;
require Hydra::Helper::Nix;
use Test2::V0; use Test2::V0;
require Catalyst::Test; require Catalyst::Test;
@ -47,7 +47,7 @@ $tar->extract_file("channel/default.nix", $defaultnix);
print STDERR $tar->get_content("channel/default.nix"); print STDERR $tar->get_content("channel/default.nix");
(my $status, my $stdout, my $stderr) = Hydra::Helper::Nix::captureStdoutStderr(5, "nix-env", "--json", "--query", "--available", "--attr-path", "--file", $defaultnix); (my $status, my $stdout, my $stderr) = captureStdoutStderr(5, "nix-env", "--json", "--query", "--available", "--attr-path", "--file", $defaultnix);
is($stderr, "", "Stderr should be empty"); is($stderr, "", "Stderr should be empty");
is($status, 0, "Querying the packages should succeed"); is($status, 0, "Querying the packages should succeed");

View file

@ -13,6 +13,8 @@ my %ctx = test_init(
require Hydra::Schema; require Hydra::Schema;
require Hydra::Model::DB; require Hydra::Model::DB;
require Hydra::Helper::Nix;
use Test2::V0; use Test2::V0;

View file

@ -2,35 +2,15 @@ use warnings;
use strict; use strict;
package CliRunners; package CliRunners;
use Hydra::Helper::Exec;
our @ISA = qw(Exporter); our @ISA = qw(Exporter);
our @EXPORT = qw( our @EXPORT = qw(
captureStdoutStderr
captureStdoutStderrWithStdin
evalFails evalFails
evalSucceeds evalSucceeds
runBuild runBuild
sendNotifications sendNotifications
); );
sub captureStdoutStderr {
# "Lazy"-load Hydra::Helper::Nix to avoid the compile-time
# import of Hydra::Model::DB. Early loading of the DB class
# causes fixation of the DSN, and we need to fixate it after
# the temporary DB is setup.
require Hydra::Helper::Nix;
return Hydra::Helper::Nix::captureStdoutStderr(@_)
}
sub captureStdoutStderrWithStdin {
# "Lazy"-load Hydra::Helper::Nix to avoid the compile-time
# import of Hydra::Model::DB. Early loading of the DB class
# causes fixation of the DSN, and we need to fixate it after
# the temporary DB is setup.
require Hydra::Helper::Nix;
return Hydra::Helper::Nix::captureStdoutStderrWithStdin(@_)
}
sub evalSucceeds { sub evalSucceeds {
my ($jobset) = @_; my ($jobset) = @_;
my ($res, $stdout, $stderr) = captureStdoutStderr(60, ("hydra-eval-jobset", $jobset->project->name, $jobset->name)); my ($res, $stdout, $stderr) = captureStdoutStderr(60, ("hydra-eval-jobset", $jobset->project->name, $jobset->name));

View file

@ -8,12 +8,11 @@ use File::Temp;
use File::Path qw(make_path); use File::Path qw(make_path);
use File::Basename; use File::Basename;
use Cwd qw(abs_path getcwd); use Cwd qw(abs_path getcwd);
use Hydra::Helper::Exec;
use CliRunners; use CliRunners;
our @ISA = qw(Exporter); our @ISA = qw(Exporter);
our @EXPORT = qw( our @EXPORT = qw(
captureStdoutStderr
captureStdoutStderrWithStdin
createBaseJobset createBaseJobset
createJobsetWithOneInput createJobsetWithOneInput
evalFails evalFails

View file

@ -3,10 +3,14 @@ use warnings;
use Setup; use Setup;
use Data::Dumper; use Data::Dumper;
use Test2::V0; use Test2::V0;
use Hydra::Helper::Exec;
my $ctx = test_context( my $ctx = test_context(
use_external_destination_store => 1 use_external_destination_store => 1
); );
require Hydra::Helper::Nix;
# This test is regarding https://github.com/NixOS/hydra/pull/1126 # This test is regarding https://github.com/NixOS/hydra/pull/1126
# #
# A hydra instance was regularly failing to build derivations with: # A hydra instance was regularly failing to build derivations with:

View file

@ -2,6 +2,7 @@ use strict;
use warnings; use warnings;
use Setup; use Setup;
use Test2::V0; use Test2::V0;
use Hydra::Helper::Exec;
my $ctx = test_context(); my $ctx = test_context();
my $db = $ctx->db(); my $db = $ctx->db();

View file

@ -2,6 +2,7 @@ use feature 'unicode_strings';
use strict; use strict;
use warnings; use warnings;
use Setup; use Setup;
use Hydra::Helper::Exec;
my %ctx = test_init(); my %ctx = test_init();

View file

@ -2,6 +2,7 @@ use feature 'unicode_strings';
use strict; use strict;
use warnings; use warnings;
use Setup; use Setup;
use Hydra::Helper::Exec;
my %ctx = test_init(); my %ctx = test_init();

View file

@ -3,6 +3,7 @@ use strict;
use warnings; use warnings;
use Setup; use Setup;
use Test2::V0; use Test2::V0;
use Hydra::Helper::Exec;
my $ctx = test_context(); my $ctx = test_context();
my $builds = $ctx->makeAndEvaluateJobset( my $builds = $ctx->makeAndEvaluateJobset(