From 2b4793ac25683e5b5383f6b6972fd8e201653621 Mon Sep 17 00:00:00 2001 From: Victor Seva Date: Fri, 6 Nov 2020 10:35:38 +0100 Subject: [PATCH] TT#97750 fix support devid in scenario.yml * introduced at f1c1379f427c86bc866c6c35f4811005bb49400d * get_subs_info was throwing exception for some cases but it was hided via eval {}. Use Try::Tiny to manage the exception and generate needed default values. * use variable name instead of $_ so it's clear what is what on loops Change-Id: Icc6baa00da6ebfa1d88a7e418edb48c8c8536cd1 --- bin/scenario.pl | 84 ++++++++++++++++++++++++++++--------------------- 1 file changed, 49 insertions(+), 35 deletions(-) diff --git a/bin/scenario.pl b/bin/scenario.pl index 65985dbd..e160d4b8 100755 --- a/bin/scenario.pl +++ b/bin/scenario.pl @@ -29,6 +29,7 @@ use IO::File; use YAML::XS; use Text::CSV; use Template; +use Try::Tiny; use Data::Dumper; sub usage @@ -97,12 +98,12 @@ sub get_subs_info my $subs = $data_sub->{$domain}->{$username}; $data->{password} = $subs->{password}; if(defined($data->{devid})) { - $_->{auth_username} = $data->{devid}; - $_->{number} = $data->{devid}; + $data->{auth_username} = $data->{devid}; + $data->{number} = $data->{devid}; check_devid($subs, $data->{devid}); } else { - $data->{devid} = $data->{username}; - $data->{auth_username} = $data->{username}; + $data->{devid} = $username; + $data->{auth_username} = $username; eval { $data->{number} = $subs->{cc}.$subs->{ac}.$subs->{sn}; } unless defined($presence); } $data->{'pbx_extension'} = $subs->{'pbx_extension'}; @@ -137,49 +138,58 @@ sub generate $csv->{caller}->print($io_caller, $seq); $csv->{callee}->print($io_callee, $seq); - foreach (@{$data->{scenarios}}) + foreach my $scen (@{$data->{scenarios}}) { - eval { get_subs_info($data->{subscribers}, $_); }; - $_->{password} = "" unless defined($_->{password}); + try + { + get_subs_info($data->{subscribers}, $scen); + } catch { + $scen->{devid} = $scen->{username}; + $scen->{auth_username} = $scen->{username}; + }; + $scen->{password} = "" unless defined($scen->{password}); # by default proto is udp - $_->{proto} = "udp" unless defined($_->{proto}); - $_->{password_wrong} = "no" unless defined($_->{password_wrong}); - if($_->{password_wrong} eq "yes") + $scen->{proto} = "udp" unless defined($scen->{proto}); + $scen->{password_wrong} = "no" unless defined($scen->{password_wrong}); + if($scen->{password_wrong} eq "yes") { - $_->{password} = "wrongpass"; + $scen->{password} = "wrongpass"; } - my $auth = "[authentication username=$_->{auth_username} password=$_->{password}]"; - my $csv_data = [$_->{devid}, $auth, $_->{domain}, $test_uuid, $_->{'pbx_extension'}]; + my $auth = "[authentication username=$scen->{auth_username} password=$scen->{password}]"; + my $csv_data = [$scen->{devid}, $auth, $scen->{domain}, $test_uuid, $scen->{'pbx_extension'}]; $csv->{caller}->print($io_caller, $csv_data); - $csv_data = ["sipp_scenario".sprintf("%02i", $id).".xml", $_->{proto}, $_->{ip}]; + $csv_data = ["sipp_scenario".sprintf("%02i", $id).".xml", $scen->{proto}, $scen->{ip}]; $csv->{scenario}->print($io_scenario, $csv_data); - foreach (@{$_->{responders}}) + foreach my $resp (@{$scen->{responders}}) { # by default foreign is no - $_->{foreign} = "no" unless defined($_->{foreign}); - if(not defined($_->{peer_host}) and $_->{foreign} ne "yes") + $resp->{foreign} = "no" unless defined($resp->{foreign}); + if(not defined($resp->{peer_host}) and $resp->{foreign} ne "yes") { - get_subs_info($data->{subscribers}, $_); + get_subs_info($data->{subscribers}, $resp); + } else { + $resp->{devid} = $resp->{username}; + $resp->{auth_username} = $resp->{username}; } - $_->{password} = "" unless defined($_->{password}); + $resp->{password} = "" unless defined($resp->{password}); # by default responder is active - $_->{active} = "yes" unless defined($_->{active}); + $resp->{active} = "yes" unless defined($resp->{active}); # by default peer_host is empty - $_->{peer_host} = "" unless defined($_->{peer_host}); + $resp->{peer_host} = "" unless defined($resp->{peer_host}); # by default proto is udp - $_->{proto} = "udp" unless defined($_->{proto}); - $auth = "[authentication username=$_->{auth_username} password=$_->{password}]"; - $csv_data = [$_->{devid}, $_->{number}, $auth, $_->{domain}, $test_uuid, $_->{'pbx_extension'}]; + $resp->{proto} = "udp" unless defined($resp->{proto}); + $auth = "[authentication username=$resp->{auth_username} password=$resp->{password}]"; + $csv_data = [$resp->{devid}, $resp->{number}, $auth, $resp->{domain}, $test_uuid, $resp->{'pbx_extension'}]; $csv->{callee}->print($io_callee, $csv_data); - $csv_data = ["sipp_scenario_responder".sprintf("%02i", $res_id).".xml", $_->{proto}, $_->{ip}, $_->{peer_host}, $_->{foreign}, $_->{register}, $_->{username}."@".$_->{domain}]; + $csv_data = ["sipp_scenario_responder".sprintf("%02i", $res_id).".xml", $resp->{proto}, $resp->{ip}, $resp->{peer_host}, $resp->{foreign}, $resp->{register}, $resp->{username}."@".$resp->{domain}]; $csv->{scenario}->print($io_scenario, $csv_data); - if($_->{register} eq "yes" && $_->{active} eq "yes") + if($resp->{register} eq "yes" && $resp->{active} eq "yes") { - generate_reg($res_id, $test_uuid, $_->{q}); + generate_reg($res_id, $test_uuid, $resp->{q}); } - if($_->{foreign} eq "yes") + if($resp->{foreign} eq "yes") { - generate_foreign_dom($_->{domain}, $_->{ip}); + generate_foreign_dom($resp->{domain}, $resp->{ip}); } $res_id++; } @@ -201,15 +211,19 @@ sub generate_presence { my @rules; my ($data) = @_; - foreach (@{$data->{presence}}) + foreach my $pres (@{$data->{presence}}) { - eval { get_subs_info($data->{subscribers}, $_); }; - $_->{password} = "" unless defined($_->{password}); - my $vars = { users => @{$_->{allow}} }; - my $fn = File::Spec->catfile($base_check_dir, "presence_".$_->{username}."_".$_->{domain}.".xml"); + try { + get_subs_info($data->{subscribers}, $pres); + } catch { + $pres->{auth_username} = $pres->{username}; + }; + $pres->{password} = "" unless defined($pres->{password}); + my $vars = { users => @{$pres->{allow}} }; + my $fn = File::Spec->catfile($base_check_dir, "presence_".$pres->{auth_username}."_".$pres->{domain}.".xml"); $tt->process($template_presence, $vars, $fn) or die($tt->error(), "\n"); undef $fn; - my $digest = $_->{username}."@".$_->{domain}.":".$_->{password}; + my $digest = $pres->{auth_username}."@".$pres->{domain}.":".$pres->{password}; push @rules, "$bin_dir/presence.sh $digest $template_dir/$template_presence" } if(scalar(@rules)>0)