|
Line 0
Link Here
|
|
|
1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
# Copyright 2015 Open Source Freedom Fighters |
| 4 |
# |
| 5 |
# This file is part of Koha. |
| 6 |
# |
| 7 |
# Koha is free software; you can redistribute it and/or modify it |
| 8 |
# under the terms of the GNU General Public License as published by |
| 9 |
# the Free Software Foundation; either version 3 of the License, or |
| 10 |
# (at your option) any later version. |
| 11 |
# |
| 12 |
# Koha is distributed in the hope that it will be useful, but |
| 13 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
# GNU General Public License for more details. |
| 16 |
# |
| 17 |
# You should have received a copy of the GNU General Public License |
| 18 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
| 19 |
|
| 20 |
use Modern::Perl; |
| 21 |
use Test::More; |
| 22 |
use Try::Tiny; |
| 23 |
use Encode; |
| 24 |
|
| 25 |
use t::lib::TestObjects::ObjectFactory; |
| 26 |
use t::lib::TestObjects::AtomicUpdateFactory; |
| 27 |
use t::lib::TestObjects::FileFactory; |
| 28 |
use Koha::AtomicUpdater; |
| 29 |
|
| 30 |
my $testContext = {}; |
| 31 |
my $atomicupdates = t::lib::TestObjects::AtomicUpdateFactory->createTestGroup([ |
| 32 |
{issue_id => 'Bug12', |
| 33 |
filename => 'Bug12-WatchExMachinaYoullLikeIt.pl'}, |
| 34 |
{issue_id => 'Bug14', |
| 35 |
filename => 'Bug14-ReturnOfZorro.perl'}, |
| 36 |
{issue_id => '#14', |
| 37 |
filename => '#14-RobotronInDanger.sql'}, |
| 38 |
{issue_id => '#15', |
| 39 |
filename => '#15-ILikedPrometheusButAlienWasBetter.pl'}, |
| 40 |
], undef, $testContext); |
| 41 |
|
| 42 |
#Make sure we get the correct update order, otherwise we get unpredictable results. |
| 43 |
{ #Overload existing subroutines to provide a Mock implementation |
| 44 |
no warnings 'redefine'; |
| 45 |
package Koha::AtomicUpdater; |
| 46 |
sub _getGitCommits { #instead of requiring a Git repository, we just mock the input. |
| 47 |
return [#Newest commit |
| 48 |
'2e8a39762b506738195f21c8ff67e4e7bfe6d7ab #:-55 : Fiftyfive', |
| 49 |
'2e8a39762b506738195f21c8ff67e4e7bfe6d7ab #54 - KohaCon in Finland next year', |
| 50 |
'b447b595acacb0c4823582acf9d8a08902118e59 #53 - Place to be.pl', |
| 51 |
'2e8a39762b506738195f21c8ff67e4e7bfe6d7ab bug 112 - Lapinlahden linnut', |
| 52 |
'5ac7101d4071fe11f7a5d1445bb97ed1a603a9b5 Bug:-911 - What are you going to do?', |
| 53 |
'1d54601b9cac0bd75ee97e071cf52ed49daef8bd #911 - Who are you going to call', |
| 54 |
'1d54601b9cac0bd75ee97e071cf52ed49daef8bd bug 30 - Feature Yes yes', |
| 55 |
'5ac7101d4071fe11f7a5d1445bb97ed1a603a9b5 #-29 - Bug squashable', |
| 56 |
'2e8a39762b506738195f21c8ff67e4e7bfe6d7ab Bug :- 28 - Feature Squash', |
| 57 |
'b447b595acacb0c4823582acf9d8a08902118e59 BUG 27 - Bug help', |
| 58 |
#Oldest commit |
| 59 |
]; |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
subtest "Create update order from Git repository" => \&createUpdateOrderFromGit; |
| 64 |
sub createUpdateOrderFromGit { |
| 65 |
eval { |
| 66 |
#Create the _updateorder-file to a temp directory and prepare it for autocleanup. |
| 67 |
my $files = t::lib::TestObjects::FileFactory->createTestGroup([ |
| 68 |
{ filepath => 'atomicupdate/', |
| 69 |
filename => '_updateorder', |
| 70 |
content => '',}, |
| 71 |
], |
| 72 |
undef, undef, $testContext); |
| 73 |
#Instantiate the AtomicUpdater to operate on a temp directory. |
| 74 |
my $atomicUpdater = Koha::AtomicUpdater->new({ |
| 75 |
scriptDir => $files->{'_updateorder'}->dirname(), |
| 76 |
}); |
| 77 |
|
| 78 |
#Start real testing. |
| 79 |
my $issueIds = $atomicUpdater->buildUpdateOrderFromGit(4); |
| 80 |
|
| 81 |
is($issueIds->[0], |
| 82 |
'Bug27', |
| 83 |
"First atomicupdate to deploy"); |
| 84 |
is($issueIds->[1], |
| 85 |
'Bug28', |
| 86 |
"Second atomicupdate to deploy"); |
| 87 |
is($issueIds->[2], |
| 88 |
'#29', |
| 89 |
"Third atomicupdate to deploy"); |
| 90 |
is($issueIds->[3], |
| 91 |
'Bug30', |
| 92 |
"Last atomicupdate to deploy"); |
| 93 |
|
| 94 |
#Testing file access |
| 95 |
$issueIds = $atomicUpdater->getUpdateOrder(); |
| 96 |
is($issueIds->[0], |
| 97 |
'Bug27', |
| 98 |
"First atomicupdate to deploy, from _updateorder"); |
| 99 |
is($issueIds->[1], |
| 100 |
'Bug28', |
| 101 |
"Second atomicupdate to deploy, from _updateorder"); |
| 102 |
is($issueIds->[2], |
| 103 |
'#29', |
| 104 |
"Third atomicupdate to deploy, from _updateorder"); |
| 105 |
is($issueIds->[3], |
| 106 |
'Bug30', |
| 107 |
"Last atomicupdate to deploy, from _updateorder"); |
| 108 |
}; |
| 109 |
if ($@) { |
| 110 |
ok(0, $@); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
subtest "List all deployed atomicupdates" => \&listAtomicUpdates; |
| 117 |
sub listAtomicUpdates { |
| 118 |
eval { |
| 119 |
my $atomicUpdater = Koha::AtomicUpdater->new(); |
| 120 |
my $text = $atomicUpdater->listToConsole(); |
| 121 |
print $text; |
| 122 |
|
| 123 |
ok($text =~ m/Bug12-WatchExMachinaYoullLik/, |
| 124 |
"Bug12-WatchExMachinaYoullLikeIt"); |
| 125 |
ok($text =~ m/Bug14-ReturnOfZorro.perl/, |
| 126 |
"Bug14-ReturnOfZorro"); |
| 127 |
ok($text =~ m/#14-RobotronInDanger.sql/, |
| 128 |
"#14-RobotronInDanger"); |
| 129 |
ok($text =~ m/#15-ILikedPrometheusButAli/, |
| 130 |
"#15-ILikedPrometheusButAlienWasBetter"); |
| 131 |
|
| 132 |
}; |
| 133 |
if ($@) { |
| 134 |
ok(0, $@); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
subtest "Delete an atomicupdate entry" => \&deleteAtomicupdate; |
| 139 |
sub deleteAtomicupdate { |
| 140 |
eval { |
| 141 |
my $atomicUpdater = Koha::AtomicUpdater->new(); |
| 142 |
my $atomicupdate = $atomicUpdater->cast($atomicupdates->{Bug12}->id); |
| 143 |
ok($atomicupdate, |
| 144 |
"AtomicUpdate '".$atomicupdates->{Bug12}->issue_id."' exists prior to deletion"); |
| 145 |
|
| 146 |
$atomicUpdater->removeAtomicUpdate($atomicupdate->issue_id); |
| 147 |
$atomicupdate = $atomicUpdater->find($atomicupdates->{Bug12}->id); |
| 148 |
ok(not($atomicupdate), |
| 149 |
"AtomicUpdate '".$atomicupdates->{Bug12}->issue_id."' deleted"); |
| 150 |
|
| 151 |
}; |
| 152 |
if ($@) { |
| 153 |
ok(0, $@); |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
subtest "Insert an atomicupdate entry" => \&insertAtomicupdate; |
| 158 |
sub insertAtomicupdate { |
| 159 |
eval { |
| 160 |
my $atomicUpdater = Koha::AtomicUpdater->new(); |
| 161 |
my $subtestContext = {}; |
| 162 |
my $atomicupdates = t::lib::TestObjects::AtomicUpdateFactory->createTestGroup([ |
| 163 |
{issue_id => 'Bug15', |
| 164 |
filename => 'Bug15-Inserted.pl'}, |
| 165 |
], undef, $subtestContext, $testContext); |
| 166 |
my $atomicupdate = $atomicUpdater->find({issue_id => 'Bug15'}); |
| 167 |
ok($atomicupdate, |
| 168 |
"Bug15-Inserted.pl"); |
| 169 |
|
| 170 |
t::lib::TestObjects::ObjectFactory->tearDownTestContext($subtestContext); |
| 171 |
|
| 172 |
$atomicupdate = $atomicUpdater->find({issue_id => 'Bug15'}); |
| 173 |
ok(not($atomicupdate), |
| 174 |
"Bug15-Inserted.pl deleted"); |
| 175 |
}; |
| 176 |
if ($@) { |
| 177 |
ok(0, $@); |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
subtest "List pending atomicupdates" => \&listPendingAtomicupdates; |
| 182 |
sub listPendingAtomicupdates { |
| 183 |
my ($atomicUpdater, $files, $text, $atomicupdates); |
| 184 |
my $subtestContext = {}; |
| 185 |
eval { |
| 186 |
##Test adding update scripts and deploy them, confirm that no pending scripts detected |
| 187 |
$files = t::lib::TestObjects::FileFactory->createTestGroup([ |
| 188 |
{ filepath => 'atomicupdate/', |
| 189 |
filename => '#911-WhoYouGonnaCall.pl', |
| 190 |
content => '$ENV{ATOMICUPDATE_TESTS} = 1;',}, |
| 191 |
{ filepath => 'atomicupdate/', |
| 192 |
filename => 'Bug911-WhatchaGonnaDo.pl', |
| 193 |
content => '$ENV{ATOMICUPDATE_TESTS}++;',}, |
| 194 |
{ filepath => 'atomicupdate/', |
| 195 |
filename => 'Bug112-LapinlahdenLinnut.pl', |
| 196 |
content => '$ENV{ATOMICUPDATE_TESTS}++;',}, |
| 197 |
], |
| 198 |
undef, $subtestContext, $testContext); |
| 199 |
$atomicUpdater = Koha::AtomicUpdater->new({ |
| 200 |
scriptDir => $files->{'#911-WhoYouGonnaCall.pl'}->dirname() |
| 201 |
}); |
| 202 |
|
| 203 |
$text = $atomicUpdater->listPendingToConsole(); |
| 204 |
print $text; |
| 205 |
|
| 206 |
ok($text =~ m/#911-WhoYouGonnaCall.pl/, |
| 207 |
"#911-WhoYouGonnaCall is pending"); |
| 208 |
ok($text =~ m/Bug911-WhatchaGonnaDo.pl/, |
| 209 |
"Bug911-WhatchaGonnaDo is pending"); |
| 210 |
ok($text =~ m/Bug112-LapinlahdenLinnut.pl/, |
| 211 |
'Bug112-LapinlahdenLinnut is pending'); |
| 212 |
|
| 213 |
$atomicupdates = $atomicUpdater->applyAtomicUpdates(); |
| 214 |
t::lib::TestObjects::AtomicUpdateFactory->addToContext($atomicupdates, undef, $subtestContext, $testContext); #Keep track of changes |
| 215 |
|
| 216 |
is($atomicupdates->{'#911'}->issue_id, |
| 217 |
'#911', |
| 218 |
"#911-WhoYouGonnaCall.pl deployed"); |
| 219 |
is($atomicupdates->{'Bug112'}->issue_id, |
| 220 |
'Bug112', |
| 221 |
'Bug112-LapinlahdenLinnut.pl deployed'); |
| 222 |
is($atomicupdates->{'Bug911'}->issue_id, |
| 223 |
'Bug911', |
| 224 |
"Bug911-WhatchaGonnaDo.pl deployed"); |
| 225 |
|
| 226 |
##Test adding scripts to the atomicupdates directory and how we deal with such change. |
| 227 |
$files = t::lib::TestObjects::FileFactory->createTestGroup([ |
| 228 |
{ filepath => 'atomicupdate/', |
| 229 |
filename => '#53-PlaceToBe.pl', |
| 230 |
content => '$ENV{ATOMICUPDATE_TESTS}++;',}, |
| 231 |
{ filepath => 'atomicupdate/', |
| 232 |
filename => '#54-KohaConInFinlandNextYear.pl', |
| 233 |
content => '$ENV{ATOMICUPDATE_TESTS}++;',}, |
| 234 |
{ filepath => 'atomicupdate/', |
| 235 |
filename => '#55-Fiftyfive.pl', |
| 236 |
content => '$ENV{ATOMICUPDATE_TESTS}++;',}, |
| 237 |
], |
| 238 |
undef, $subtestContext, $testContext); |
| 239 |
|
| 240 |
$text = $atomicUpdater->listPendingToConsole(); |
| 241 |
print $text; |
| 242 |
|
| 243 |
ok($text =~ m/#53-PlaceToBe.pl/, |
| 244 |
"#53-PlaceToBe.pl is pending"); |
| 245 |
ok($text =~ m/#54-KohaConInFinlandNextYear.pl/, |
| 246 |
"#54-KohaConInFinlandNextYear.pl is pending"); |
| 247 |
ok($text =~ m/#55-Fiftyfive.pl/u, |
| 248 |
'#55-Fiftyfive.pl'); |
| 249 |
|
| 250 |
$atomicupdates = $atomicUpdater->applyAtomicUpdates(); |
| 251 |
t::lib::TestObjects::AtomicUpdateFactory->addToContext($atomicupdates, undef, $subtestContext, $testContext); #Keep track of changes |
| 252 |
|
| 253 |
is($atomicupdates->{'#53'}->issue_id, |
| 254 |
'#53', |
| 255 |
"#53-PlaceToBe.pl deployed"); |
| 256 |
is($atomicupdates->{'#54'}->issue_id, |
| 257 |
'#54', |
| 258 |
'#54-KohaConInFinlandNextYear.pl deployed'); |
| 259 |
is($atomicupdates->{'#55'}->issue_id, |
| 260 |
'#55', |
| 261 |
"#55-Fiftyfive.pl deployed"); |
| 262 |
|
| 263 |
is($ENV{ATOMICUPDATE_TESTS}, |
| 264 |
6, |
| 265 |
"All configured AtomicUpdates deployed"); |
| 266 |
}; |
| 267 |
if ($@) { |
| 268 |
ok(0, $@); |
| 269 |
} |
| 270 |
t::lib::TestObjects::AtomicUpdateFactory->tearDownTestContext($subtestContext); |
| 271 |
} |
| 272 |
|
| 273 |
t::lib::TestObjects::ObjectFactory->tearDownTestContext($testContext); |
| 274 |
done_testing; |