From d9a4d49ab5d86a5185c7d991e36ed2c148b3041f Mon Sep 17 00:00:00 2001 From: Victor Tsvetov Date: Mon, 27 Jan 2020 22:34:52 +0200 Subject: [PATCH] TT#74160 Recursive vars replacement in API tests Modify NGCP API Test Framework: 1. Add support for using variables in deeply nested parameters in yaml test scenarios, by running vars replacement in recursion. 2. Simplify vars replacement logic in RequestBuilder.pm by deleting conditions that are relevant to TestExecutor.pm only. 3. Allow passing directory as argument to testrunner in order to run all yaml tests in that specific directory. 4. Fix the error message of failed 'code' condition. Change-Id: I05a310609dc1ad15d33e37574e07078dfbe38aa7 --- .../lib/TestFramework/RequestBuilder.pm | 64 ++++++------------- t/api-rest2/lib/TestFramework/TestExecutor.pm | 2 +- t/api-rest2/testrunner | 10 ++- 3 files changed, 27 insertions(+), 49 deletions(-) diff --git a/t/api-rest2/lib/TestFramework/RequestBuilder.pm b/t/api-rest2/lib/TestFramework/RequestBuilder.pm index 99d83bfc0b..76da815bbd 100644 --- a/t/api-rest2/lib/TestFramework/RequestBuilder.pm +++ b/t/api-rest2/lib/TestFramework/RequestBuilder.pm @@ -51,56 +51,30 @@ sub _replace_vars { # substitute variables in content if ( $args->{content} ) { - if ( ref $args->{content} eq 'HASH' ) { - foreach my $content_key (keys %{$args->{content}}) { - if ( $args->{content}->{$content_key} && $args->{content}->{$content_key} =~ /\$\{(.*)\}/ ) { - if ( ref $args->{retain}->{$1} eq 'ARRAY' || ref $args->{retain}->{$1} eq 'HASH' ) { - $args->{content}->{$content_key} = $args->{retain}->{$1}; - } - else { - $args->{content}->{$content_key} =~ s/\$\{(.*)\}/$args->{retain}->{$1}/; - } - } - elsif ( $args->{content}->{$content_key} && $args->{content}->{$content_key} =~ /^\$\{(.*)\}\..+/ ) { - my @splitted_values = split (/\./, $args->{content}->{$content_key}); - $args->{content}->{$content_key} = $self->_retrieve_from_composed_key( \@splitted_values, $args->{retain} ); - } - } - } - elsif ( ref $args->{content} eq 'ARRAY' ) { - foreach my $content ( @{$args->{content}} ) { - foreach my $content_key (keys %$content) { - if ( $content->{$content_key} && $content->{$content_key} =~ /\$\{(.*)\}$/ ) { - if ( ref $args->{retain}->{$1} eq 'ARRAY' || ref $args->{retain}->{$1} eq 'HASH' ) { - $content->{$content_key} = $args->{retain}->{$1}; - } - else { - $content->{$content_key} =~ s/\$\{(.*)\}/$args->{retain}->{$1}/; - } - } - elsif ( $content->{$content_key} && $content->{$content_key} =~ /^\$\{(.*)\}\..+/ ) { - my @splitted_values = split (/\./, $content->{$content_key}); - $content->{$content_key} = $self->_retrieve_from_composed_key( \@splitted_values, $args->{retain} ); - } - } - } - } - else { - if ( $args->{content} =~ /\$\{(.*)\}/ ) { - $args->{content} = $args->{retain}->{$1}; - } - } + $args->{content} = $self->_replace_vars_recursion($args->{content}, $args->{retain}); } } -sub _retrieve_from_composed_key { - my ( $self, $splitted_values, $retained ) = @_; +sub _replace_vars_recursion { + my ( $self, $elem, $retain ) = @_; - if ( $splitted_values->[0] =~ /\$\{(.*)\}/ ) { - my $value = $retained->{$1}; - grep { $value = $value->{$splitted_values->[$_]} } (1..(scalar @$splitted_values - 1)); - return $value; + if ( ref $elem eq 'HASH' ) { + foreach my $k (keys %{$elem}) { + $elem->{$k} = $self->_replace_vars_recursion($elem->{$k}, $retain); + } + } elsif ( ref $elem eq 'ARRAY' ) { + foreach my $e ( @{$elem} ) { + $e = $self->_replace_vars_recursion($e, $retain); + } + } elsif ( ref $elem eq '' and defined $elem and $elem =~ /\$\{(.*)\}/ ) { + if ( ref $retain->{$1} eq '' ) { + $elem =~ s/\$\{(.*)\}/$retain->{$1}/; + } else { + $elem = $retain->{$1}; + } } + + return $elem; } 1; diff --git a/t/api-rest2/lib/TestFramework/TestExecutor.pm b/t/api-rest2/lib/TestFramework/TestExecutor.pm index c7de414157..84df4c1d1e 100644 --- a/t/api-rest2/lib/TestFramework/TestExecutor.pm +++ b/t/api-rest2/lib/TestFramework/TestExecutor.pm @@ -44,7 +44,7 @@ sub run_tests { INFO ( "Check ok." ); } else { - ERROR ( "NOT OK. Expected: ".$result->code.". Got: $check_value" ); + ERROR ( "NOT OK. Expected: $check_value. Got: ".$result->code); $tests_result->{success} = 0; push @{$tests_result->{errors}}, "Error at 'is' condition for test '$test_name'"; } diff --git a/t/api-rest2/testrunner b/t/api-rest2/testrunner index cdc1fe03d9..4d0905f4e5 100755 --- a/t/api-rest2/testrunner +++ b/t/api-rest2/testrunner @@ -15,7 +15,7 @@ if ( !$server ){ print "Usage: \$ perl testrunner.pl [] []\n"; print "Usage example: \$ perl testrunner.pl 192.168.88.162\n"; print "Usage example: \$ perl testrunner.pl 192.168.88.162 fast\n"; - print "Possible test set: all, stable, fast, t/api-rest2/Contracts.yaml\n"; + print "Possible test set: all, stable, fast, t/api-rest2/tests-directory, t/api-rest2/Contracts.yaml\n"; print "Default test set: all\n"; exit(1); } @@ -32,7 +32,11 @@ elsif ( $selected eq 'all' ) { print "Test selection: all\n"; map { push @test_files, "./$_" } `ls ./t/api-rest2/*.yaml`; } -else{ +elsif ( -d $selected ) { + print "Test selection: all files in '$selected' directory\n"; + map { push @test_files, "./$_" } `ls ${selected}/*.yaml`; +} +else { print "Test selection: $selected\n"; push @test_files, $selected; } @@ -90,4 +94,4 @@ sub worker { if ( grep { $_ == 1 } @exit_codes ) { exit 1; } -exit 0; \ No newline at end of file +exit 0;