diff --git a/t/api-rest/api-balanceintervals.t b/t/api-rest/api-balanceintervals.t index 1f8e15c074..8c40f7569b 100644 --- a/t/api-rest/api-balanceintervals.t +++ b/t/api-rest/api-balanceintervals.t @@ -28,7 +28,7 @@ use NGCP::Panel::Utils::DateTime qw(); #use NGCP::Panel::Utils::ProfilePackages qw(); #since it depends on Utils::Subscribers and thus Sipwise::Base, importin it causes segfault when creating threads.. my $is_local_env = 0; -my $disable_parallel_catchup = 1; +my $disable_parallel_catchup = 0; my $disable_hourly_intervals = 1; #my $enable_profile_packages = NGCP::Panel::Utils::ProfilePackages::ENABLE_PROFILE_PACKAGES; #my $enable_profile_packages = 1; @@ -1008,7 +1008,7 @@ if (_get_allow_fake_client_time()) { # && $enable_profile_packages) { #is($intervals_b->{total_count},scalar (grep { $_->{contact_id} == $custcontact9->{id} } values %customer_map),"check total count of thread b results"); #is($intervals_c->{total_count},3,"check total count of thread c results"); - ok($t2 - $t1 < $delta_serialized,'expected delay to assume only required contracts were locked and requests were performed in parallel') if !$disable_parallel_catchup; + ok(($t2 - $t1) < $delta_serialized,'expected delay to assume only required contracts were locked and requests were performed in parallel') if !$disable_parallel_catchup; } else { diag('allow_delay_commit not set, skipping ...'); @@ -1059,7 +1059,7 @@ if (_get_allow_fake_client_time()) { # && $enable_profile_packages) { #is($intervals_b->{total_count},scalar (grep { $_->{contact_id} == $custcontact9->{id} } values %customer_map),"check total count of thread b results"); #is($intervals_c->{total_count},3,"check total count of thread c results"); - ok($t2 - $t1 < $delta_serialized,'expected delay to assume only required contracts were locked and requests were performed in parallel') if !$disable_parallel_catchup; + ok(($t2 - $t1) < $delta_serialized,'expected delay to assume only required contracts were locked and requests were performed in parallel') if !$disable_parallel_catchup; } else { @@ -1141,7 +1141,7 @@ if (_get_allow_fake_client_time()) { # && $enable_profile_packages) { is($got_asc->[$i]->{lock},4,"check if subscriber is locked now"); } - ok($t2 - $t1 < $delta_serialized,'expected delay to assume only required contracts were locked and requests were performed in parallel') if !$disable_parallel_catchup; + ok(($t2 - $t1) < $delta_serialized,'expected delay to assume only required contracts were locked and requests were performed in parallel') if !$disable_parallel_catchup; $t1 = time; $t_a = threads->create(\&_fetch_preferences_worker,$delay,'id','asc',$custcontact2); @@ -1236,7 +1236,7 @@ if (_get_allow_fake_client_time()) { # && $enable_profile_packages) { for (my $i = 0; $i < 2*3; $i++) { is($got_asc->[$i]->{lock},4,"check if subscriber is locked now"); } - ok($t2 - $t1 < $delta_serialized,'expected delay to assume only required contracts were locked and requests were performed in parallel') if !$disable_parallel_catchup; + ok(($t2 - $t1) < $delta_serialized,'expected delay to assume only required contracts were locked and requests were performed in parallel') if !$disable_parallel_catchup; $t1 = time; $t_a = threads->create(\&_fetch_subscribers_worker,$delay,'id','asc',$custcontact2); diff --git a/t/api-rest/api-cert-auth.t b/t/api-rest/api-cert-auth.t index 1049390b08..4c903d627d 100644 --- a/t/api-rest/api-cert-auth.t +++ b/t/api-rest/api-cert-auth.t @@ -7,13 +7,10 @@ use Test::More; use File::Temp qw/tempfile/; use Test::Collection; -my $certpath = '/tmp/apicert.pem'; my $uri = $ENV{CATALYST_SERVER} || ('https://'.hostfqdn.':4443'); #docker: CATALYST_SERVER=https://10.15.20.104:1443 perl t/api-rest/api-cert-auth.t --f $certpath && unlink $certpath; - my ($invalid_ssl_client_cert, $valid_ssl_client_cert) = _download_certs($uri); my ($ua, $res); @@ -57,10 +54,12 @@ sub _download_certs { -f $invalid_cert && unlink $invalid_cert; -f $valid_cert && unlink $valid_cert; - $ua = Test::Collection->new()->ua(); - rename $certpath, $invalid_cert; - $ua = Test::Collection->new()->ua(); - rename $certpath, $valid_cert; + + my $coll = Test::Collection->new(); + rename $coll->ssl_cert, $invalid_cert; + $coll->clear_cert; + rename $coll->ssl_cert, $valid_cert; + return ($invalid_cert, $valid_cert); } diff --git a/t/lib/Test/Collection.pm b/t/lib/Test/Collection.pm index 19559b6208..1ae49e00e5 100644 --- a/t/lib/Test/Collection.pm +++ b/t/lib/Test/Collection.pm @@ -20,14 +20,14 @@ use File::Slurp qw/write_file/; use Storable; use Carp qw(cluck longmess shortmess); use IO::Uncompress::Unzip; +use File::Temp qw(); -has 'crt_path' => ( +has 'ssl_cert' => ( is => 'ro', isa => 'Str', lazy => 1, - default => sub {'/tmp/apicert.pem';}, + builder => 'init_ssl_cert', ); - has 'data_cache_file' => ( is => 'ro', isa => 'Str', @@ -251,6 +251,10 @@ sub init_catalyst_config{ } sub init_ua { my $self = shift; + return $self->_create_ua(1); +} +sub _create_ua { + my ($self,$init_cert) = @_; my $ua = LWP::UserAgent->new; my $uri = $self->base_uri; $uri =~ s/^https?:\/\///; @@ -260,48 +264,53 @@ sub init_ua { verify_hostname => 0, SSL_verify_mode => 0, ); - if($role eq "default" || $role eq "admin" || $role eq "reseller") { - $self->init_ssl_cert($ua); + if($init_cert and ($role eq "default" || $role eq "admin" || $role eq "reseller")) { + $ua->ssl_opts( + SSL_cert_file => $self->ssl_cert, + SSL_key_file => $self->ssl_cert, + ); } return $ua; } sub init_ssl_cert { my ($self, $ua) = @_; - unless(-f $self->crt_path) { - my $res = $ua->post( - $self->base_uri . '/api/admincerts/', - Content_Type => 'application/json', - Content => '{}' - ); - unless($res->is_success) { - die "failed to fetch client certificate: " . $res->status_line . "\n"; - } - my $zip = $res->decoded_content; - my $z = IO::Uncompress::Unzip->new(\$zip, MultiStream => 0, Append => 1); - my $data; - while(!$z->eof() && (my $hdr = $z->getHeaderInfo())) { - unless($hdr->{Name} =~ /\.pem$/) { - # wrong file, just read stream, clear buffer and try next - while($z->read($data) > 0) {} - $data = undef; - $z->nextStream(); - next; - } + $ua = $self->_create_ua(0) unless $ua; + my $res = $ua->post( + $self->base_uri . '/api/admincerts/', + Content_Type => 'application/json', + Content => '{}' + ); + unless($res->is_success) { + die "failed to fetch client certificate: " . $res->status_line . "\n"; + } + my $zip = $res->decoded_content; + my $z = IO::Uncompress::Unzip->new(\$zip, MultiStream => 0, Append => 1); + my $data; + while(!$z->eof() && (my $hdr = $z->getHeaderInfo())) { + unless($hdr->{Name} =~ /\.pem$/) { + # wrong file, just read stream, clear buffer and try next while($z->read($data) > 0) {} - last; + $data = undef; + $z->nextStream(); + next; } - $z->close(); - unless($data) { - die "failed to find PEM file in client certificate zip file\n"; - } - open my $fh, ">:raw", $self->crt_path or die "failed to open " . $self->crt_path . ": $!\n"; - print $fh $data; - close $fh; + while($z->read($data) > 0) {} + last; } - $ua->ssl_opts( - SSL_cert_file => $self->crt_path, - SSL_key_file => $self->crt_path, - ); + $z->close(); + unless($data) { + die "failed to find PEM file in client certificate zip file\n"; + } + my ($tmpfh,$tmpfilename) = File::Temp::tempfile('apicert_XXXX', DIR => '/tmp', SUFFIX => '.pem', UNLINK => 0); + print $tmpfh $data; + close $tmpfh; + return $tmpfilename; +} +sub clear_cert { + my $self = shift; + delete $self->{ua}; + delete $self->{ssl_cert}; + return $self; } sub runas { my $self = shift;