ef619eca99
We've seen many fails on ofborg, at lot of them ultimately appear to come down to a timeout being hit, resulting in something like this: Failure executing slapadd -F /<path>/slap.d -b dc=example -l /<path>/load.ldif. Hopefully this resolves it for most cases. I've done some endurance testing and this helps a lot. some other commands also regularly time-out with high load: - hydra-init - hydra-create-user - nix-store --delete This should address most issues with tests randomly failing. Used the following script for endurance testing: ``` import os import subprocess run_counter = 0 fail_counter = 0 while True: try: run_counter += 1 print(f"Starting run {run_counter}") env = os.environ env["YATH_JOB_COUNT"] = "20" result = subprocess.run(["perl", "t/test.pl"], env=env) if (result.returncode != 0): fail_counter += 1 print(f"Finish run {run_counter}, total fail count: {fail_counter}") except KeyboardInterrupt: print(f"Finished {run_counter} runs with {fail_counter} fails") break ``` In case someone else wants to do it on their system :). Note that YATH_JOB_COUNT may need to be changed loosely based on your cores. I only have 4 cores (8 threads), so for others higher numbers might yield better results in hashing out unstable tests.
272 lines
7.7 KiB
Perl
272 lines
7.7 KiB
Perl
use strict;
|
|
use warnings;
|
|
|
|
package HydraTestContext;
|
|
use File::Path qw(make_path);
|
|
use File::Basename;
|
|
use File::Copy::Recursive qw(rcopy);
|
|
use File::Which qw(which);
|
|
use Cwd qw(abs_path getcwd);
|
|
use CliRunners;
|
|
use Hydra::Helper::Exec;
|
|
|
|
# Set up the environment for running tests.
|
|
#
|
|
# Hash Parameters:
|
|
#
|
|
# * hydra_config: configuration for the Hydra processes for your test.
|
|
# * nix_config: text to include in the test's nix.conf
|
|
# * use_external_destination_store: Boolean indicating whether hydra should
|
|
# use a destination store different from the evaluation store.
|
|
# True by default.
|
|
# * before_init: a sub which is called after the database is up, but before
|
|
# hydra-init is executed. It receives the HydraTestContext object as
|
|
# its argument.
|
|
#
|
|
# This clears several environment variables and sets them to ephemeral
|
|
# values: a temporary database, temporary Nix store, temporary Hydra
|
|
# data directory, etc.
|
|
#
|
|
# Note: This function must run _very_ early, before nearly any Hydra
|
|
# libraries are loaded. To use this, you very likely need to `use Setup`
|
|
# and then run `test_init`, and then `require` the Hydra libraries you
|
|
# need.
|
|
#
|
|
# It returns a tuple: a handle to a temporary directory and a handle to
|
|
# the postgres service. If either of these variables go out of scope,
|
|
# those resources are released and the test environment becomes invalid.
|
|
#
|
|
# Look at the top of an existing `.t` file to see how this should be used
|
|
# in practice.
|
|
sub new {
|
|
my ($class, %opts) = @_;
|
|
|
|
my $deststoredir;
|
|
|
|
# Cleanup will be managed by yath. By the default it will be cleaned
|
|
# up, but can be kept to aid in debugging test failures.
|
|
my $dir = File::Temp->newdir(CLEANUP => 0);
|
|
|
|
$ENV{'HYDRA_DATA'} = "$dir/hydra-data";
|
|
mkdir $ENV{'HYDRA_DATA'};
|
|
$ENV{'NIX_CONF_DIR'} = "$dir/nix/etc/nix";
|
|
make_path($ENV{'NIX_CONF_DIR'});
|
|
my $nixconf = "$ENV{'NIX_CONF_DIR'}/nix.conf";
|
|
my $nix_config = "sandbox = false\n" . ($opts{'nix_config'} || "");
|
|
write_file($nixconf, $nix_config);
|
|
$ENV{'HYDRA_CONFIG'} = "$dir/hydra.conf";
|
|
|
|
my $hydra_config = $opts{'hydra_config'} || "";
|
|
$hydra_config = "queue_runner_metrics_address = 127.0.0.1:0\n" . $hydra_config;
|
|
if ($opts{'use_external_destination_store'} // 1) {
|
|
$deststoredir = "$dir/nix/dest-store";
|
|
$hydra_config = "store_uri = file://$dir/nix/dest-store\n" . $hydra_config;
|
|
}
|
|
|
|
write_file($ENV{'HYDRA_CONFIG'}, $hydra_config);
|
|
|
|
my $nix_store_dir = "$dir/nix/store";
|
|
my $nix_state_dir = "$dir/nix/var/nix";
|
|
my $nix_log_dir = "$dir/nix/var/log/nix";
|
|
|
|
$ENV{'NIX_REMOTE_SYSTEMS'} = '';
|
|
$ENV{'NIX_REMOTE'} = "local?store=$nix_store_dir&state=$nix_state_dir&log=$nix_log_dir";
|
|
$ENV{'NIX_STATE_DIR'} = $nix_state_dir; # FIXME: remove
|
|
$ENV{'NIX_STORE_DIR'} = $nix_store_dir; # FIXME: remove
|
|
|
|
my $pgsql = Test::PostgreSQL->new(
|
|
extra_initdb_args => "--locale C.UTF-8"
|
|
);
|
|
$ENV{'HYDRA_DBI'} = $pgsql->dsn;
|
|
|
|
my $jobsdir = "$dir/jobs";
|
|
rcopy(abs_path(dirname(__FILE__) . "/../jobs"), $jobsdir);
|
|
|
|
my $coreutils_path = dirname(which 'install');
|
|
replace_variable_in_file($jobsdir . "/config.nix", '@testPath@', $coreutils_path);
|
|
replace_variable_in_file($jobsdir . "/declarative/project.json", '@jobsPath@', $jobsdir);
|
|
|
|
my $self = bless {
|
|
_db => undef,
|
|
db_handle => $pgsql,
|
|
tmpdir => $dir,
|
|
nix_state_dir => $nix_state_dir,
|
|
nix_log_dir => $nix_log_dir,
|
|
testdir => abs_path(dirname(__FILE__) . "/.."),
|
|
jobsdir => $jobsdir,
|
|
deststoredir => $deststoredir,
|
|
}, $class;
|
|
|
|
if ($opts{'before_init'}) {
|
|
$opts{'before_init'}->($self);
|
|
}
|
|
|
|
expectOkay(30, ("hydra-init"));
|
|
|
|
return $self;
|
|
}
|
|
|
|
sub db {
|
|
my ($self, $setup) = @_;
|
|
|
|
if (!defined $self->{_db}) {
|
|
require Hydra::Schema;
|
|
require Hydra::Model::DB;
|
|
$self->{_db} = Hydra::Model::DB->new();
|
|
|
|
if (!(defined $setup && $setup == 0)) {
|
|
$self->{_db}->resultset('Users')->create({
|
|
username => "root",
|
|
emailaddress => 'root@invalid.org',
|
|
password => ''
|
|
});
|
|
}
|
|
}
|
|
|
|
return $self->{_db};
|
|
}
|
|
|
|
sub tmpdir {
|
|
my ($self) = @_;
|
|
|
|
return $self->{tmpdir};
|
|
}
|
|
|
|
sub testdir {
|
|
my ($self) = @_;
|
|
|
|
return $self->{testdir};
|
|
}
|
|
|
|
sub jobsdir {
|
|
my ($self) = @_;
|
|
|
|
return $self->{jobsdir};
|
|
}
|
|
|
|
sub nix_state_dir {
|
|
my ($self) = @_;
|
|
|
|
return $self->{nix_state_dir};
|
|
}
|
|
|
|
# Create a jobset, evaluate it, and optionally build the jobs.
|
|
#
|
|
# In return, you get a hash of all the Builds records, keyed
|
|
# by their Nix attribute name.
|
|
#
|
|
# This always uses an `expression` from the `jobsdir` directory.
|
|
#
|
|
# Hash Parameters:
|
|
#
|
|
# * expression: The file in the jobsdir directory to evaluate
|
|
# * jobsdir: An alternative jobsdir to source the expression from
|
|
# * build: Bool. Attempt to build all the resulting jobs. Default: false.
|
|
sub makeAndEvaluateJobset {
|
|
my ($self, %opts) = @_;
|
|
|
|
my $expression = $opts{'expression'} || die "Mandatory 'expression' option not passed to makeAndEvaluateJobset.\n";
|
|
my $jobsdir = $opts{'jobsdir'} // $self->jobsdir;
|
|
my $should_build = $opts{'build'} // 0;
|
|
|
|
my $jobsetCtx = $self->makeJobset(
|
|
expression => $expression,
|
|
jobsdir => $jobsdir,
|
|
);
|
|
my $jobset = $jobsetCtx->{"jobset"};
|
|
|
|
evalSucceeds($jobset) or die "Evaluating jobs/$expression should exit with return code 0.\n";
|
|
|
|
my $builds = {};
|
|
|
|
for my $build ($jobset->builds) {
|
|
if ($should_build) {
|
|
runBuild($build) or die "Build '".$build->job."' from jobs/$expression should exit with return code 0.\n";
|
|
$build->discard_changes();
|
|
}
|
|
|
|
$builds->{$build->job} = $build;
|
|
}
|
|
|
|
return $builds;
|
|
}
|
|
|
|
# Create a jobset.
|
|
#
|
|
# In return, you get a hash of the user, project, and jobset records.
|
|
#
|
|
# This always uses an `expression` from the `jobsdir` directory.
|
|
#
|
|
# Hash Parameters:
|
|
#
|
|
# * expression: The file in the jobsdir directory to evaluate
|
|
# * jobsdir: An alternative jobsdir to source the expression from
|
|
sub makeJobset {
|
|
my ($self, %opts) = @_;
|
|
|
|
my $expression = $opts{'expression'} || die "Mandatory 'expression' option not passed to makeJobset.\n";
|
|
my $jobsdir = $opts{'jobsdir'} // $self->jobsdir;
|
|
|
|
# Create a new user for this test
|
|
my $user = $self->db()->resultset('Users')->create({
|
|
username => rand_chars(),
|
|
emailaddress => rand_chars() . '@example.org',
|
|
password => ''
|
|
});
|
|
|
|
# Create a new project for this test
|
|
my $project = $self->db()->resultset('Projects')->create({
|
|
name => rand_chars(),
|
|
displayname => rand_chars(),
|
|
owner => $user->username
|
|
});
|
|
|
|
# Create a new jobset for this test and set up the inputs
|
|
my $jobset = $project->jobsets->create({
|
|
name => rand_chars(),
|
|
nixexprinput => "jobs",
|
|
nixexprpath => $expression,
|
|
emailoverride => ""
|
|
});
|
|
my $jobsetinput = $jobset->jobsetinputs->create({name => "jobs", type => "path"});
|
|
$jobsetinput->jobsetinputalts->create({altnr => 0, value => $jobsdir});
|
|
|
|
return {
|
|
user => $user,
|
|
project => $project,
|
|
jobset => $jobset,
|
|
};
|
|
}
|
|
|
|
sub DESTROY
|
|
{
|
|
my ($self) = @_;
|
|
$self->db(0)->schema->storage->disconnect();
|
|
$self->{db_handle}->stop();
|
|
}
|
|
|
|
sub write_file {
|
|
my ($path, $text) = @_;
|
|
open(my $fh, '>', $path) or die "Could not open file '$path' $!\n.";
|
|
print $fh $text || "";
|
|
close $fh;
|
|
}
|
|
|
|
sub replace_variable_in_file {
|
|
my ($fn, $var, $val) = @_;
|
|
|
|
open (my $input, '<', "$fn.in") or die $!;
|
|
open (my $output, '>', $fn) or die $!;
|
|
|
|
while (my $line = <$input>) {
|
|
$line =~ s/$var/$val/g;
|
|
print $output $line;
|
|
}
|
|
}
|
|
|
|
sub rand_chars {
|
|
return sprintf("t%08X", rand(0xFFFFFFFF));
|
|
}
|
|
|
|
1;
|