Cmd/Testplan.pm

filter: Statementstmt 45.1% 69 / 153 Branchbran 16.6% 4 / 24 Conditioncond 20.0% 2 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 58.3% 14 / 24 Podpod 90.9% 10 / 11 totaltotal 43.4% 99 / 228 CC 27 SCAR 27.6
CC
27
Cov
40%
add
36.4
del
12.0
cancel
12.0
CRAP 15.7
« Cmd/Testrun.pm ↑ Summary Cmd/Scenario.pm »
1 package Tapper::Cmd::Testplan;
2 3 our $AUTHORITY = 'cpan:TAPPER';
3 3 $Tapper::Cmd::Testplan::VERSION = '5.0.14';
4 3 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use YAML::Syck;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Tapper::Model 'model';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Tapper::Reports::DPath::TT;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use File::Slurp 'slurp';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use Perl6::Junction 'any';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use UUID 'uuid';
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 3 extends 'Tapper::Cmd';
17  
18  
19  
20 sub get_module_for_type
21 1 ▶{
Subroutine get_module_for_type: called CC=3 SCAR=16.0
Pod documented
22 1 my ($self, $type) = @_;
23
24 1 ▶ if ( lc($type) eq 'multitest' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (lc $type eq 'multitest') { } 1 0 1
elsif (lc $type eq 'scenario') { } 0 0 0
25 1 return "Tapper::Cmd::Testrun";
26 }
27 elsif ( lc($type) eq 'scenario') {
28 0 return "Tapper::Cmd::Scenario"
29 }
30 else {
31 0 $type = ucfirst($type); return "Tapper::Cmd::$type";
32 }
33 }
34  
35  
36 1 ▶sub add {
Subroutine add: called CC=17 SCAR=35.9
Pod documented
37 1 my ($self, $plan_content, $path, $name) = @_;
38  
39 1 my @plans = YAML::Syck::Load($plan_content);
40 # use Data::Dumper;
41 # print STDERR "plans: ".Dumper($plan_content);
42 # print STDERR "plans: ".Dumper(\@plans);
43  
44 1 my $instance = model('TestrunDB')->resultset('TestplanInstance')->new({
45 evaluated_testplan => $plan_content,
46 path => $path,
47 name => $name,
48 });
49 1 $instance->insert;
50  
51 # Collect dependencies
52 1 my %plan_lookup;
53 my %plan_dependencies;
54 1 foreach my $plan (@plans) {
55 1 ▶ unless (exists($plan->{identifier})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $plan->{"identifier"}) 1 0 1
56 1 $plan->{identifier} = uuid();
57 }
58 1 $plan_lookup{$plan->{identifier}} = $plan;
59 1 ▶ if (exists($plan->{depends_on})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $plan->{"depends_on"}) 0 1 1
60 0 my $depends_on = $plan->{depends_on};
61 0 ▶ $depends_on = [ $depends_on ] unless ref($depends_on) eq "ARRAY";
Branch: 1 branch
BranchTrueFalseTotal
unless ref $depends_on eq "ARRAY" 0 0 0
62 0 $plan_dependencies{$plan->{identifier}} = $depends_on;
63 }
64 }
65  
66 # Check that all dependencies can be resolved
67 1 foreach my $depender (keys %plan_dependencies) {
68 0 foreach my $dependee (@{$plan_dependencies{$depender}}) {
69 die "Plan dependency $dependee in plan $depender could not be resolved"
70 0 ▶ unless exists $plan_lookup{$dependee};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $plan_lookup{$dependee} 0 0 0
71 }
72 }
73  
74 1 my %plan_testruns;
75 my %plan_status;
76  
77 1 my @testrun_ids;
78 1 foreach my $plan (@plans) {
79 1 ▶ die "Missing plan type for the following testplan: \n".Dump($plan) unless $plan->{type};
Branch: 1 branch
BranchTrueFalseTotal
unless $plan->{"type"} 0 1 1
80 1 my $module = $self->get_module_for_type($plan->{type});
81  
82 1 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
83 1 ▶ eval "use $module";
Subroutine BEGIN: called CC=1 SCAR=0.0
84 0 ▶ } catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
85 0 die "Can not load '$module' to handle testplan of type $plan->{type}: $!";
86 1 };
87  
88 1 my $handler = "$module"->new();
89 1 ▶ my $description = $plan->{testplan_description} || $plan->{description};
Condition33%1/3
$plan->{'testplan_description'} || $plan->{'description'}
l!l&&r!l&&!r
010
MC/DC0%0/2
$plan->{'testplan_description'} || $plan->{'description'}
$plan->{'testplan_description'} $plan->{'description'}
Truth table33%1/3
A || B
A= $plan->{'testplan_description'}B= $plan->{'description'}
ABresult
1X1
011
000
90  
91 # Postpone setting the status to schedule So that testruns don't
92 # start before the dependencies were saved as well. To achieve
93 # this we create all testruns with a prepare status and set the
94 # final status later, either the specified one or schedule as
95 # default.
96 1 ▶ my $status = $description->{status} || "schedule";
Condition50%1/2
$description->{'status'} || 'schedule'
l!l
01
MC/DC0%0/1
$description->{'status'} || 'schedule'
$description->{'status'}
Truth table50%1/2
A
A= $description->{'status'}
Aresult
11
00
97 1 $description->{status} = "prepare";
98  
99 1 my @new_ids = $handler->create($description, $instance->id);
100 1 $plan_testruns{$plan->{identifier}} = [ @new_ids ];
101 1 $plan_status{$plan->{identifier}} = $status;
102 1 push @testrun_ids, @new_ids;
103 }
104  
105 # Apply dependencies
106 1 foreach my $depender (keys %plan_dependencies) {
107 0 foreach my $dependee (@{$plan_dependencies{$depender}}) {
108 0 my @depender_testruns = @{$plan_testruns{$depender}};
109 0 my @dependee_testruns = @{$plan_testruns{$dependee}};
110  
111 0 foreach my $depender_testrun (@depender_testruns) {
112 0 foreach my $dependee_testrun (@dependee_testruns) {
113 0 model('TestrunDB')->resultset('TestrunDependency')->create({
114 dependee_testrun_id => $dependee_testrun,
115 depender_testrun_id => $depender_testrun,
116 });
117 }
118 }
119 }
120 }
121  
122 # Set final status on all created testruns
123 # For each plan set all testruns in one go.
124 1 foreach my $plan_identifier ( keys %plan_status ) {
125 model('TestrunDB')->resultset('TestrunScheduling')
126 ->search({ testrun_id => $plan_testruns{$plan_identifier} })
127 1 ->update({ status => $plan_status{$plan_identifier} });
128 }
129  
130 1 return { testplan_id => $instance->id, testrun_ids => \@testrun_ids };
131 }
132  
133  
134 0 ▶sub del {
Subroutine del: not called CC=3 SCAR=24.8
Pod documented
135 0 my ($self, $id) = @_;
136 0 my $testplan = model('TestrunDB')->resultset('TestplanInstance')->find($id);
137 0 ▶ while(my $testrun = $testplan->testruns->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $testrun = $testplan->testruns->next) 0 0 0
138 0 ▶ if ($testrun->testrun_scheduling->status eq 'running') {
Branch: 1 branch
BranchTrueFalseTotal
if ($testrun->testrun_scheduling->status eq "running") 0 0 0
139 0 my $message = model('TestrunDB')->resultset('Message')->new({testrun_id => $testrun->id,
140 type => 'state',
141 message => {
142 state => 'quit',
143 error => 'Testplan cancelled'
144 }});
145 0 $message->insert();
146 }
147 0 $testrun->testrun_scheduling->testrun->testplan_id(undef);
148 0 $testrun->testrun_scheduling->testrun->update;
149 0 $testrun->testrun_scheduling->status('finished');
150 0 $testrun->testrun_scheduling->update;
151 }
152 0 $testplan->delete();
153 0 return 0;
154 }
155  
156  
157 0 ▶sub cancel {
Subroutine cancel: not called CC=3 SCAR=24.8
Pod documented
158 0 my ($self, $id, $comment) = @_;
159  
160 0 ▶ $comment ||= 'Testplan cancelled';
Condition0%0/2
$comment ||= "Testplan cancelled"
l!l
00
MC/DC0%0/1
$comment ||= "Testplan cancelled"
$comment
Truth table0%0/2
A
A= $comment
Aresult
11
00
161 0 my $testplan = model('TestrunDB')->resultset('TestplanInstance')->find($id);
162 0 require Tapper::Cmd::Testrun;
163 0 my $cmd = Tapper::Cmd::Testrun->new;
164 0 my $testruns = $testplan->testruns;
165 0 ▶ while(my $testrun = $testruns->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $testrun = $testruns->next) 0 0 0
166 0 $cmd->cancel($testrun->id, $comment);
167 }
168 0 return 0;
169 }
170  
171  
172 sub rerun
173 0 ▶{
Subroutine rerun: not called CC=2 SCAR=17.9
Pod documented
174 0 my ($self, $id) = @_;
175  
176 0 my $testplan = model('TestrunDB')->resultset('TestplanInstance')->find($id);
177 0 ▶ die "No testplan with ID $id\n" unless $testplan;
Branch: 1 branch
BranchTrueFalseTotal
unless $testplan 0 0 0
178  
179 0 return $self->add($testplan->evaluated_testplan, $testplan->path, $testplan->name);
180 }
181  
182  
183 sub parse_path
184 0 ▶{
Subroutine parse_path: not called CC=1 SCAR=6.9
Pod documented
185 0 my ($self, $filename) = @_;
186 0 $filename = Cwd::abs_path($filename);
187 0 my $basedir = Tapper::Config->subconfig->{paths}{testplan_path};
188 # splitting filename at basedir returns an array with the empty
189 # string before and the path after the basedir
190 0 my $path = (split $basedir, $filename)[1];
191 0 return $path;
192 }
193  
194 0 ▶sub get_shortname {
Subroutine get_shortname: not called CC=3 SCAR=24.8
Pod undocumented
195  
196 0 my ( $or_self, $s_plan ) = @_;
197  
198 0 foreach my $s_line ( split /\n/, $s_plan ) {
199 0 ▶ if ( $s_line =~ /^[#\s]*-?\s*(?:short)?name\s*:\s*(.+?)\s*$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s_line =~ /^[#\s]*-?\s*(?:short)?name\s*:\s*(.+?)\s*$/i) 0 0 0
200 0 my $shortname = $1;
201 0 $shortname =~ s/['"]+//g;
202 0 return $shortname;
203 }
204 }
205  
206 0 return;
207  
208 }
209  
210  
211 sub guide
212 0 ▶{
Subroutine guide: not called CC=1 SCAR=6.9
Pod documented
213 0 my ($self, $file, $substitutes, $include) = @_;
214 0 my $text;
215 0 my $guide = $self->apply_macro($file,
216 $substitutes,
217 $include);
218  
219 0 my @guide = grep { m/^###/ } split (qr/\n/, $guide);
220 0 $text = "Self-documentation:\n";
221 0 $text = join "\n", map { my $l = $_; $l =~ s/^###/ /; "$l" } @guide;
222 0 return $text;
223 }
224  
225  
226 0 ▶sub testplannew {
Subroutine testplannew: not called CC=2 SCAR=17.9
Pod documented
227 0 my ($self, $opt) = @_;
228  
229 0 my $plan = $self->apply_macro($opt->{file}, $opt->{substitutes}, $opt->{include});
230 0 ▶ my $path = $opt->{path} || $self->parse_path($opt->{file});
Condition0%0/3
$opt->{'path'} || $self->parse_path($opt->{'file'})
l!l&&r!l&&!r
000
MC/DC0%0/2
$opt->{'path'} || $self->parse_path($opt->{'file'})
$opt->{'path'} $self->parse_path($opt->{'file'})
Truth table0%0/3
A || B
A= $opt->{'path'}B= $self->parse_path($opt->{'file'})
ABresult
1X1
011
000
231 0 my $name = $self->get_shortname($plan);
232 0 return $self->add($plan, $path, $name);
233 }
234  
235  
236 sub status
237 0 ▶{
Subroutine status: not called CC=1 SCAR=6.9
Pod documented
238 0 my ($self, $id) = @_;
239 0 my $results = model('TestrunDB')->fetch_raw_sql({
240 query_name => 'testplans::testplan_status',
241 fetch_type => '$%',
242 query_vals => {testplan_id => $id},
243 });
244 0 my $testruns_rs = model->resultset("Testrun")->search({testplan_id => $id});
245 0 my %testrun_ids = map { $_->id => $_->topic_name} $testruns_rs->all;
246 0 $results->{testruns} = \%testrun_ids;
247 0 return $results;
248  
249 # TODO check how we can get old infos back in
250 # * status - one of 'schedule', 'running', 'pass', 'fail'
251 # * complete_percentage - percentage of finished testruns
252 # * started_percentage - percentage of running and finished testruns
253 # * success_percentage - average of success rates of finished testruns
254 }
255  
256  
257 sub testplan_files
258 0 ▶{
Subroutine testplan_files: not called CC=1 SCAR=6.9
Pod documented
259 0 my ($self, $testplan_id, $filter) = @_;
260 0 my $results_rawsql = model('TestrunDB')->fetch_raw_sql({
261 query_name => 'testplans::reportfile',
262 fetch_type => '@@',
263 query_vals => {testplan_id => $testplan_id, filter => $filter},
264 });
265 0 my @results = map { $_->[0] } @$results_rawsql;
266 0 return \@results;
267 }
268  
269  
270 3 1; # End of Tapper::Cmd::Testplan
271  
272 __END__
« Cmd/Testrun.pm ↑ Summary Cmd/Scenario.pm »