/
engine.t
2423 lines (2244 loc) · 90.5 KB
/
engine.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl -w
use strict;
use warnings;
use 5.010;
use utf8;
use Test::More tests => 543;
#use Test::More 'no_plan';
use App::Sqitch;
use App::Sqitch::Plan;
use Path::Class;
use Test::Exception;
use Test::NoWarnings;
use Test::MockModule;
use Locale::TextDomain qw(App-Sqitch);
use App::Sqitch::X qw(hurl);
use lib 't/lib';
use MockOutput;
my $CLASS;
BEGIN {
$CLASS = 'App::Sqitch::Engine';
use_ok $CLASS or die;
delete $ENV{PGDATABASE};
delete $ENV{PGUSER};
delete $ENV{USER};
$ENV{SQITCH_CONFIG} = 'nonexistent.conf';
}
can_ok $CLASS, qw(load new name no_prompt run_deploy run_revert run_verify);
my ($is_deployed_tag, $is_deployed_change) = (0, 0);
my @deployed_changes;
my @deployed_change_ids;
my @resolved;
my @requiring;
my @load_changes;
my $offset_change;
my $die = '';
my $record_work = 1;
my $updated_idx;
my ( $earliest_change_id, $latest_change_id, $initialized );
ENGINE: {
# Stub out a engine.
package App::Sqitch::Engine::whu;
use Moose;
use App::Sqitch::X qw(hurl);
extends 'App::Sqitch::Engine';
$INC{'App/Sqitch/Engine/whu.pm'} = __FILE__;
my @SEEN;
for my $meth (qw(
run_file
log_deploy_change
log_revert_change
log_fail_change
)) {
no strict 'refs';
*$meth = sub {
hurl 'AAAH!' if $die eq $meth;
push @SEEN => [ $meth => $_[1] ];
};
}
sub is_deployed_tag { push @SEEN => [ is_deployed_tag => $_[1] ]; $is_deployed_tag }
sub is_deployed_change { push @SEEN => [ is_deployed_change => $_[1] ]; $is_deployed_change }
sub are_deployed_changes { shift; push @SEEN => [ are_deployed_changes => [@_] ]; @deployed_change_ids }
sub change_id_for { shift; push @SEEN => [ change_id_for => {@_} ]; shift @resolved }
sub change_offset_from_id { shift; push @SEEN => [ change_offset_from_id => [@_] ]; $offset_change }
sub changes_requiring_change { push @SEEN => [ changes_requiring_change => $_[1] ]; @{ shift @requiring } }
sub earliest_change_id { push @SEEN => [ earliest_change_id => $_[1] ]; $earliest_change_id }
sub latest_change_id { push @SEEN => [ latest_change_id => $_[1] ]; $latest_change_id }
sub initialized { push @SEEN => 'initialized'; $initialized }
sub initialize { push @SEEN => 'initialize' }
sub register_project { push @SEEN => 'register_project' }
sub deployed_changes { push @SEEN => [ deployed_changes => $_[1] ]; @deployed_changes }
sub load_change { push @SEEN => [ load_change => $_[1] ]; @load_changes }
sub deployed_changes_since { push @SEEN => [ deployed_changes_since => $_[1] ]; @deployed_changes }
sub mock_check_deploy { shift; push @SEEN => [ check_deploy_dependencies => [@_] ] }
sub mock_check_revert { shift; push @SEEN => [ check_revert_dependencies => [@_] ] }
sub begin_work { push @SEEN => ['begin_work'] if $record_work }
sub finish_work { push @SEEN => ['finish_work'] if $record_work }
sub _update_ids { push @SEEN => ['_update_ids']; $updated_idx }
sub log_new_tags { push @SEEN => [ log_new_tags => $_[1] ]; $_[0] }
sub seen { [@SEEN] }
after seen => sub { @SEEN = () };
sub name_for_change_id { return 'bugaboo' }
}
ok my $sqitch = App::Sqitch->new(
db_name => 'mydb',
top_dir => dir( qw(t sql) ),
plan_file => file qw(t plans multi.plan)
), 'Load a sqitch sqitch object';
my $mock_engine = Test::MockModule->new($CLASS);
##############################################################################
# Test new().
throws_ok { $CLASS->new }
qr/\QAttribute (sqitch) is required/,
'Should get an exception for missing sqitch param';
my $array = [];
throws_ok { $CLASS->new({ sqitch => $array }) }
qr/\QValidation failed for 'App::Sqitch' with value/,
'Should get an exception for array sqitch param';
throws_ok { $CLASS->new({ sqitch => 'foo' }) }
qr/\QValidation failed for 'App::Sqitch' with value/,
'Should get an exception for string sqitch param';
isa_ok $CLASS->new({sqitch => $sqitch}), $CLASS;
##############################################################################
# Test load().
ok my $engine = $CLASS->load({
sqitch => $sqitch,
engine => 'whu',
}), 'Load a "whu" engine';
isa_ok $engine, 'App::Sqitch::Engine::whu';
is $engine->sqitch, $sqitch, 'The sqitch attribute should be set';
# Test handling of an invalid engine.
throws_ok { $CLASS->load({ engine => 'nonexistent', sqitch => $sqitch }) }
'App::Sqitch::X', 'Should die on invalid engine';
is $@->message, 'Unable to load App::Sqitch::Engine::nonexistent',
'Should get load error message';
like $@->previous_exception, qr/\QCan't locate/,
'Should have relevant previoius exception';
NOENGINE: {
# Test handling of no engine.
throws_ok { $CLASS->load({ engine => '', sqitch => $sqitch }) }
'App::Sqitch::X',
'No engine should die';
is $@->message, 'Missing "engine" parameter to load()',
'It should be the expected message';
}
# Test handling a bad engine implementation.
use lib 't/lib';
throws_ok { $CLASS->load({ engine => 'bad', sqitch => $sqitch }) }
'App::Sqitch::X', 'Should die on bad engine module';
is $@->message, 'Unable to load App::Sqitch::Engine::bad',
'Should get another load error message';
like $@->previous_exception, qr/^LOL BADZ/,
'Should have relevant previoius exception from the bad module';
##############################################################################
# Test name.
can_ok $CLASS, 'name';
ok $engine = $CLASS->new({ sqitch => $sqitch }), "Create a $CLASS object";
is $CLASS->name, '', 'Base class name should be ""';
is $engine->name, '', 'Base object name should be ""';
ok $engine = App::Sqitch::Engine::whu->new({sqitch => $sqitch}),
'Create a subclass name object';
is $engine->name, 'whu', 'Subclass oject name should be "whu"';
is +App::Sqitch::Engine::whu->name, 'whu', 'Subclass class name should be "whu"';
##############################################################################
# Test config_vars.
can_ok $CLASS, 'config_vars';
is_deeply [App::Sqitch::Engine->config_vars], [],
'Should have no config vars in engine base class';
##############################################################################
# Test variables.
can_ok $CLASS, qw(variables set_variables clear_variables);
is_deeply [$engine->variables], [], 'Should have no variables';
ok $engine->set_variables(foo => 'bar'), 'Add a variable';
is_deeply [$engine->variables], [foo => 'bar'], 'Should have the variable';
ok $engine->set_variables(foo => 'baz', whu => 'hi', yo => 'stellar'),
'Set more variables';
is_deeply {$engine->variables}, {foo => 'baz', whu => 'hi', yo => 'stellar'},
'Should have all of the variables';
$engine->clear_variables;
is_deeply [$engine->variables], [], 'Should again have no variables';
##############################################################################
# Test abstract methods.
ok $engine = $CLASS->new({ sqitch => $sqitch }), "Create a $CLASS object again";
for my $abs (qw(
initialized
initialize
register_project
run_file
run_handle
log_deploy_change
log_fail_change
log_revert_change
log_new_tags
is_deployed_tag
is_deployed_change
are_deployed_changes
change_id_for
changes_requiring_change
earliest_change_id
latest_change_id
deployed_changes
deployed_changes_since
load_change
name_for_change_id
current_state
current_changes
current_tags
search_events
registered_projects
change_offset_from_id
)) {
throws_ok { $engine->$abs } qr/\Q$CLASS has not implemented $abs()/,
"Should get an unimplemented exception from $abs()"
}
##############################################################################
# Test _load_changes().
can_ok $engine, '_load_changes';
my $now = App::Sqitch::DateTime->now;
my $plan = $sqitch->plan;
for my $spec (
['no tags' => [
{
id => 'c8a60f1a4fdab2cf91ee7f6da08f4ac52a732b4d',
name => 'howdy',
project => 'engine',
note => 'For realz',
planner_name => 'Barack Obama',
planner_email => 'bo@whitehouse.gov',
timestamp => $now,
},
]],
['multiple hashes with no tags' => [
{
id => 'c8a60f1a4fdab2cf91ee7f6da08f4ac52a732b4d',
name => 'howdy',
project => 'engine',
note => 'For realz',
planner_name => 'Barack Obama',
planner_email => 'bo@whitehouse.gov',
timestamp => $now,
},
{
id => 'ae5b4397f78dfc6072ccf6d505b17f9624d0e3b0',
name => 'booyah',
project => 'engine',
note => 'Whatever',
planner_name => 'Barack Obama',
planner_email => 'bo@whitehouse.gov',
timestamp => $now,
},
]],
['tags' => [
{
id => 'c8a60f1a4fdab2cf91ee7f6da08f4ac52a732b4d',
name => 'howdy',
project => 'engine',
note => 'For realz',
planner_name => 'Barack Obama',
planner_email => 'bo@whitehouse.gov',
timestamp => $now,
tags => [qw(foo bar)],
},
]],
['tags with leading @' => [
{
id => 'c8a60f1a4fdab2cf91ee7f6da08f4ac52a732b4d',
name => 'howdy',
project => 'engine',
note => 'For realz',
planner_name => 'Barack Obama',
planner_email => 'bo@whitehouse.gov',
timestamp => $now,
tags => [qw(@foo @bar)],
},
]],
['multiple hashes with tags' => [
{
id => 'c8a60f1a4fdab2cf91ee7f6da08f4ac52a732b4d',
name => 'howdy',
project => 'engine',
note => 'For realz',
planner_name => 'Barack Obama',
planner_email => 'bo@whitehouse.gov',
timestamp => $now,
tags => [qw(foo bar)],
},
{
id => 'ae5b4397f78dfc6072ccf6d505b17f9624d0e3b0',
name => 'booyah',
project => 'engine',
note => 'Whatever',
planner_name => 'Barack Obama',
planner_email => 'bo@whitehouse.gov',
timestamp => $now,
tags => [qw(@foo @bar)],
},
]],
) {
my ($desc, $args) = @{ $spec };
is_deeply [ $engine->_load_changes(@{ $args }) ], [ map {
my $tags = $_->{tags} || [];
my $c = App::Sqitch::Plan::Change->new(%{ $_ }, plan => $plan );
$c->add_tag(
App::Sqitch::Plan::Tag->new(name => $_, plan => $plan, change => $c )
) for map { s/^@//; $_ } @{ $tags };
$c;
} @{ $args }], "Should load changes with $desc";
}
# Make sure it picks up the suffix from the plan.
my $change = $plan->change_at(1);
$change->suffix('foo');
ok my ($loaded) = $engine->_load_changes({
id => $change->id,
name => $change->name,
project => 'something',
note => 'Whatever',
planner_name => 'Barack Obama',
planner_email => 'bo@whitehouse.gov',
timestamp => $now,
}), 'Load a change corresponding to a planned change with suffix';
is $loaded->suffix, 'foo', 'It should have picked up the suffix from the plan';
$change->suffix('');
##############################################################################
# Test deploy_change and revert_change.
ok $engine = App::Sqitch::Engine::whu->new( sqitch => $sqitch ),
'Create a subclass name object again';
can_ok $engine, 'deploy_change', 'revert_change';
$change = App::Sqitch::Plan::Change->new( name => 'users', plan => $sqitch->plan );
ok $engine->deploy_change($change), 'Deploy a change';
is_deeply $engine->seen, [
['begin_work'],
[run_file => $change->deploy_file ],
[log_deploy_change => $change ],
['finish_work'],
], 'deploy_change should have called the proper methods';
is_deeply +MockOutput->get_info_literal, [[
' + users ..', '' , ' '
]], 'Output should reflect the deployment';
is_deeply +MockOutput->get_info, [[__ 'ok' ]],
'Output should reflect success';
# Have it log only.
ok $engine->deploy_change($change, 1), 'Only log a change';
is_deeply $engine->seen, [
['begin_work'],
[log_deploy_change => $change ],
['finish_work'],
], 'log-only deploy_change should not have called run_file';
is_deeply +MockOutput->get_info_literal, [[
' + users ..', '' , ' '
]], 'Output should reflect the logging';
is_deeply +MockOutput->get_info, [[__ 'ok' ]],
'Output should reflect deploy success';
# Have it verify.
ok $engine->with_verify(1), 'Enable verification';
ok $engine->deploy_change($change), 'Deploy a change to be verified';
is_deeply $engine->seen, [
['begin_work'],
[run_file => $change->deploy_file ],
[run_file => $change->verify_file ],
[log_deploy_change => $change ],
['finish_work'],
], 'deploy_change with verification should run the verify file';
is_deeply +MockOutput->get_info_literal, [[
' + users ..', '' , ' '
]], 'Output should reflect the logging';
is_deeply +MockOutput->get_info, [[__ 'ok' ]],
'Output should reflect deploy success';
# Have it verify *and* log-only.
ok $engine->deploy_change($change, 1), 'Verify and log a change';
is_deeply $engine->seen, [
['begin_work'],
[run_file => $change->verify_file ],
[log_deploy_change => $change ],
['finish_work'],
], 'deploy_change with verification and log-only should not run deploy';
is_deeply +MockOutput->get_info_literal, [[
' + users ..', '' , ' '
]], 'Output should reflect the logging';
is_deeply +MockOutput->get_info, [[__ 'ok' ]],
'Output should reflect deploy success';
# Make it fail.
$die = 'run_file';
throws_ok { $engine->deploy_change($change) } 'App::Sqitch::X',
'Deploy change with error';
is $@->message, 'AAAH!', 'Error should be from run_file';
is_deeply $engine->seen, [
['begin_work'],
[log_fail_change => $change ],
['finish_work'],
], 'Should have logged change failure';
$die = '';
is_deeply +MockOutput->get_info_literal, [[
' + users ..', '' , ' '
]], 'Output should reflect the deployment, even with failure';
is_deeply +MockOutput->get_info, [[__ 'not ok' ]],
'Output should reflect deploy failure';
# Make the verify fail.
$mock_engine->mock( verify_change => sub { hurl 'WTF!' });
throws_ok { $engine->deploy_change($change) } 'App::Sqitch::X',
'Deploy change with failed verification';
is $@->message, 'Deploy failed', 'Error should be from deploy_change';
is_deeply $engine->seen, [
['begin_work'],
[run_file => $change->deploy_file ],
['begin_work'],
[run_file => $change->revert_file ],
[log_fail_change => $change ],
['finish_work'],
], 'Should have logged verify failure';
$die = '';
is_deeply +MockOutput->get_info_literal, [[
' + users ..', '' , ' '
]], 'Output should reflect the deployment, even with verify failure';
is_deeply +MockOutput->get_info, [[__ 'not ok' ]],
'Output should reflect deploy failure';
is_deeply +MockOutput->get_vent, [['WTF!']],
'Verify error should have been vented';
# Try a change with no verify file.
$mock_engine->unmock( 'verify_change' );
$change = App::Sqitch::Plan::Change->new( name => 'foo', plan => $sqitch->plan );
ok $engine->deploy_change($change), 'Deploy a change with no verify script';
is_deeply $engine->seen, [
['begin_work'],
[run_file => $change->deploy_file ],
[log_deploy_change => $change ],
['finish_work'],
], 'deploy_change with no verify file should not run it';
is_deeply +MockOutput->get_info_literal, [[
' + foo ..', '' , ' '
]], 'Output should reflect the logging';
is_deeply +MockOutput->get_info, [[__ 'ok' ]],
'Output should reflect deploy success';
is_deeply +MockOutput->get_vent, [
[__x 'Verify script {file} does not exist', file => $change->verify_file],
], 'A warning about no verify file should have been emitted';
# Alright, disable verify now.
$engine->with_verify(0);
ok $engine->revert_change($change), 'Revert a change';
is_deeply $engine->seen, [
['begin_work'],
[run_file => $change->revert_file ],
[log_revert_change => $change ],
['finish_work'],
], 'revert_change should have called the proper methods';
is_deeply +MockOutput->get_info_literal, [[
' - foo ..', '', ' '
]], 'Output should reflect reversion';
is_deeply +MockOutput->get_info, [[__ 'ok']],
'Output should acknowldge revert success';
# Revert with log-only.
ok $engine->revert_change($change, 1), 'Revert a change with log-only';
is_deeply $engine->seen, [
['begin_work'],
[log_revert_change => $change ],
['finish_work'],
], 'Log-only revert_change should not have run the change script';
is_deeply +MockOutput->get_info_literal, [[
' - foo ..', '', ' '
]], 'Output should reflect logged reversion';
is_deeply +MockOutput->get_info, [[__ 'ok']],
'Output should acknowldge revert success';
$record_work = 0;
##############################################################################
# Test earliest_change() and latest_change().
chdir 't';
my $plan_file = file qw(sql sqitch.plan);
my $sqitch_old = $sqitch; # Hang on to this because $change does not retain it.
$sqitch = App::Sqitch->new( plan_file => $plan_file, top_dir => dir 'sql' );
ok $engine = App::Sqitch::Engine::whu->new( sqitch => $sqitch ),
'Engine with sqitch with plan file';
$plan = $sqitch->plan;
my @changes = $plan->changes;
$latest_change_id = $changes[0]->id;
is $engine->latest_change, $changes[0], 'Should get proper change from latest_change()';
is_deeply $engine->seen, [[ latest_change_id => undef ]],
'Latest change ID should have been called with no arg';
$latest_change_id = $changes[2]->id;
is $engine->latest_change(2), $changes[2],
'Should again get proper change from latest_change()';
is_deeply $engine->seen, [[ latest_change_id => 2 ]],
'Latest change ID should have been called with offset arg';
$latest_change_id = undef;
$earliest_change_id = $changes[0]->id;
is $engine->earliest_change, $changes[0], 'Should get proper change from earliest_change()';
is_deeply $engine->seen, [[ earliest_change_id => undef ]],
'Earliest change ID should have been called with no arg';
$earliest_change_id = $changes[2]->id;
is $engine->earliest_change(4), $changes[2],
'Should again get proper change from earliest_change()';
is_deeply $engine->seen, [[ earliest_change_id => 4 ]],
'Earliest change ID should have been called with offset arg';
$earliest_change_id = undef;
##############################################################################
# Test _sync_plan()
can_ok $CLASS, '_sync_plan';
$engine->seen;
is $plan->position, -1, 'Plan should start at position -1';
is $engine->start_at, undef, 'start_at should be undef';
ok $engine->_sync_plan, 'Sync the plan';
is $plan->position, -1, 'Plan should still be at position -1';
is $engine->start_at, undef, 'start_at should still be undef';
$plan->position(4);
is_deeply $engine->seen, [['latest_change_id', undef]],
'Should not have updated IDs';
ok $engine->_sync_plan, 'Sync the plan again';
is $plan->position, -1, 'Plan should again be at position -1';
is $engine->start_at, undef, 'start_at should again be undef';
is_deeply $engine->seen, [['latest_change_id', undef]],
'Still should not have updated IDs';
# Have latest_item return a tag.
$latest_change_id = $changes[1]->old_id;
$updated_idx = 2;
ok $engine->_sync_plan, 'Sync the plan to a tag';
is $plan->position, 2, 'Plan should now be at position 1';
is $engine->start_at, 'widgets@beta', 'start_at should now be widgets@beta';
is_deeply $engine->seen, [
['latest_change_id', undef],
['_update_ids'],
['log_new_tags' => $plan->change_at(2)],
], 'Should have updated IDs';
##############################################################################
# Test deploy.
can_ok $CLASS, 'deploy';
$latest_change_id = undef;
$plan->reset;
$engine->seen;
@changes = $plan->changes;
# Mock the deploy methods to log which were called.
my $deploy_meth;
for my $meth (qw(_deploy_all _deploy_by_tag _deploy_by_change)) {
my $orig = $CLASS->can($meth);
$mock_engine->mock($meth => sub {
$deploy_meth = $meth;
$orig->(@_);
});
}
# Mock dependency checking to add its call to the seen stuff.
$mock_engine->mock( check_deploy_dependencies => sub {
shift->mock_check_deploy(@_);
});
$mock_engine->mock( check_revert_dependencies => sub {
shift->mock_check_revert(@_);
});
ok $engine->deploy('@alpha'), 'Deploy to @alpha';
is $plan->position, 1, 'Plan should be at position 1';
is_deeply $engine->seen, [
[latest_change_id => undef],
'initialized',
'initialize',
'register_project',
[check_deploy_dependencies => [$plan, 1]],
[run_file => $changes[0]->deploy_file],
[log_deploy_change => $changes[0]],
[run_file => $changes[1]->deploy_file],
[log_deploy_change => $changes[1]],
], 'Should have deployed through @alpha';
is $deploy_meth, '_deploy_all', 'Should have called _deploy_all()';
is_deeply +MockOutput->get_info, [
[__x 'Adding metadata tables to {destination}',
destination => $engine->destination,
],
[__x 'Deploying changes through {target} to {destination}',
destination => $engine->destination,
target => $plan->get('@alpha')->format_name_with_tags,
],
[__ 'ok'],
[__ 'ok'],
], 'Should have seen the output of the deploy to @alpha';
is_deeply +MockOutput->get_info_literal, [
[' + roles ..', '.......', ' '],
[' + users @alpha ..', '', ' '],
], 'Both change names should be output';
# Try with log-only in all modes.
for my $mode (qw(change tag all)) {
ok $engine->deploy('@alpha', $mode, 1), 'Log-only deploy in $mode mode to @alpha';
is $plan->position, 1, 'Plan should be at position 1';
is_deeply $engine->seen, [
[latest_change_id => undef],
'initialized',
'initialize',
'register_project',
[check_deploy_dependencies => [$plan, 1]],
[log_deploy_change => $changes[0]],
[log_deploy_change => $changes[1]],
], 'Should have deployed through @alpha without running files';
my $meth = $mode eq 'all' ? 'all' : ('by_' . $mode);
is $deploy_meth, "_deploy_$meth", "Should have called _deploy_$meth()";
is_deeply +MockOutput->get_info, [
[
__x 'Adding metadata tables to {destination}',
destination => $engine->destination,
],
[
__x 'Deploying changes through {target} to {destination}',
destination => $engine->destination,
target => $plan->get('@alpha')->format_name_with_tags,
],
[__ 'ok'],
[__ 'ok'],
], 'Should have seen the output of the deploy to @alpha';
is_deeply +MockOutput->get_info_literal, [
[' + roles ..', '.......', ' '],
[' + users @alpha ..', '', ' '],
], 'Both change names should be output';
}
# Try with no need to initialize.
$initialized = 1;
$plan->reset;
ok $engine->deploy('@alpha', 'tag'), 'Deploy to @alpha with tag mode';
is $plan->position, 1, 'Plan should again be at position 1';
is_deeply $engine->seen, [
[latest_change_id => undef],
'initialized',
'register_project',
[check_deploy_dependencies => [$plan, 1]],
[run_file => $changes[0]->deploy_file],
[log_deploy_change => $changes[0]],
[run_file => $changes[1]->deploy_file],
[log_deploy_change => $changes[1]],
], 'Should have deployed through @alpha without initialization';
is $deploy_meth, '_deploy_by_tag', 'Should have called _deploy_by_tag()';
is_deeply +MockOutput->get_info, [
[__x 'Deploying changes through {target} to {destination}',
destination => $engine->destination,
target => $plan->get('@alpha')->format_name_with_tags,
],
[__ 'ok'],
[__ 'ok'],
], 'Should have seen the output of the deploy to @alpha';
is_deeply +MockOutput->get_info_literal, [
[' + roles ..', '.......', ' '],
[' + users @alpha ..', '', ' '],
], 'Both change names should be output';
# Try a bogus target.
throws_ok { $engine->deploy('nonexistent') } 'App::Sqitch::X',
'Should get an error for an unknown target';
is $@->message, __x(
'Unknown deploy target: "{target}"',
target => 'nonexistent',
), 'The exception should report the unknown target';
is_deeply $engine->seen, [
[latest_change_id => undef],
], 'Only latest_item() should have been called';
# Start with @alpha.
$latest_change_id = ($changes[1]->tags)[0]->id;
ok $engine->deploy('@alpha'), 'Deploy to alpha thrice';
is_deeply $engine->seen, [
[latest_change_id => undef],
['log_new_tags' => $changes[1]],
], 'Only latest_item() should have been called';
is_deeply +MockOutput->get_info, [
[__x 'Nothing to deploy (already at "{target}"', target => '@alpha'],
], 'Should notify user that already at @alpha';
# Start with widgets.
$latest_change_id = $changes[2]->id;
throws_ok { $engine->deploy('@alpha') } 'App::Sqitch::X',
'Should fail targeting older change';
is $@->ident, 'deploy', 'Should be a "deploy" error';
is $@->message, __ 'Cannot deploy to an earlier target; use "revert" instead',
'It should suggest using "revert"';
is_deeply $engine->seen, [
[latest_change_id => undef],
['log_new_tags' => $changes[2]],
], 'Should have called latest_item() and latest_tag()';
# Make sure we can deploy everything by change.
$latest_change_id = undef;
$plan->reset;
$plan->add( name => 'lolz', note => 'ha ha' );
@changes = $plan->changes;
ok $engine->deploy(undef, 'change'), 'Deploy everything by change';
is $plan->position, 3, 'Plan should be at position 3';
is_deeply $engine->seen, [
[latest_change_id => undef],
'initialized',
'register_project',
[check_deploy_dependencies => [$plan, 3]],
[run_file => $changes[0]->deploy_file],
[log_deploy_change => $changes[0]],
[run_file => $changes[1]->deploy_file],
[log_deploy_change => $changes[1]],
[run_file => $changes[2]->deploy_file],
[log_deploy_change => $changes[2]],
[run_file => $changes[3]->deploy_file],
[log_deploy_change => $changes[3]],
], 'Should have deployed everything';
is $deploy_meth, '_deploy_by_change', 'Should have called _deploy_by_change()';
is_deeply +MockOutput->get_info, [
[__x 'Deploying changes to {destination}', destination => $engine->destination ],
[__ 'ok'],
[__ 'ok'],
[__ 'ok'],
[__ 'ok'],
], 'Should have emitted deploy announcement and successes';
is_deeply +MockOutput->get_info_literal, [
[' + roles ..', '........', ' '],
[' + users @alpha ..', '.', ' '],
[' + widgets @beta ..', '', ' '],
[' + lolz ..', '.........', ' '],
], 'Should have seen the output of the deploy to the end';
# If we deploy again, it should be up-to-date.
$latest_change_id = $changes[-1]->id;
throws_ok { $engine->deploy } 'App::Sqitch::X',
'Should catch exception for attempt to deploy to up-to-date DB';
is $@->ident, 'deploy', 'Should be a "deploy" error';
is $@->message, __ 'Nothing to deploy (up-to-date)',
'And the message should reflect up-to-dateness';
is_deeply $engine->seen, [
[latest_change_id => undef],
], 'It should have just fetched the latest change ID';
$latest_change_id = undef;
# Try invalid mode.
throws_ok { $engine->deploy(undef, 'evil_mode') } 'App::Sqitch::X',
'Should fail on invalid mode';
is $@->ident, 'deploy', 'Should be a "deploy" error';
is $@->message, __x('Unknown deployment mode: "{mode}"', mode => 'evil_mode'),
'And the message should reflect the unknown mode';
is_deeply $engine->seen, [
[latest_change_id => undef],
'initialized',
'register_project',
[check_deploy_dependencies => [$plan, 3]],
], 'It should have check for initialization';
is_deeply +MockOutput->get_info, [
[__x 'Deploying changes to {destination}', destination => $engine->destination ],
], 'Should have announced destination';
# Try a plan with no changes.
NOSTEPS: {
my $plan_file = file qw(empty.plan);
my $fh = $plan_file->open('>') or die "Cannot open $plan_file: $!";
say $fh '%project=empty';
$fh->close or die "Error closing $plan_file: $!";
END { $plan_file->remove }
my $sqitch = App::Sqitch->new( plan_file => $plan_file );
ok $engine = App::Sqitch::Engine::whu->new( sqitch => $sqitch ),
'Engine with sqitch with no file';
throws_ok { $engine->deploy } 'App::Sqitch::X', 'Should die with no changes';
is $@->message, __"Nothing to deploy (empty plan)",
'Should have the localized message';
is_deeply $engine->seen, [
[latest_change_id => undef],
], 'It should have checked for the latest item';
}
##############################################################################
# Test _deploy_by_change()
$plan->reset;
$mock_engine->unmock('_deploy_by_change');
ok $engine->_deploy_by_change($plan, 1), 'Deploy changewise to index 1';
is_deeply $engine->seen, [
[run_file => $changes[0]->deploy_file],
[log_deploy_change => $changes[0]],
[run_file => $changes[1]->deploy_file],
[log_deploy_change => $changes[1]],
], 'Should changewise deploy to index 2';
is_deeply +MockOutput->get_info_literal, [
[' + roles ..', '', ' '],
[' + users @alpha ..', '', ' '],
], 'Should have seen output of each change';
is_deeply +MockOutput->get_info, [[__ 'ok' ], [__ 'ok']],
'Output should reflect deploy successes';
ok $engine->_deploy_by_change($plan, 3), 'Deploy changewise to index 2';
is_deeply $engine->seen, [
[run_file => $changes[2]->deploy_file],
[log_deploy_change => $changes[2]],
[run_file => $changes[3]->deploy_file],
[log_deploy_change => $changes[3]],
], 'Should changewise deploy to from index 2 to index 3';
is_deeply +MockOutput->get_info_literal, [
[' + widgets @beta ..', '', ' '],
[' + lolz ..', '', ' '],
], 'Should have seen output of changes 2-3';
is_deeply +MockOutput->get_info, [[__ 'ok' ], [__ 'ok']],
'Output should reflect deploy successes';
# Make it die.
$plan->reset;
$die = 'run_file';
throws_ok { $engine->_deploy_by_change($plan, 2) } 'App::Sqitch::X',
'Die in _deploy_by_change';
is $@->message, 'AAAH!', 'It should have died in run_file';
is_deeply $engine->seen, [
[log_fail_change => $changes[0] ],
], 'It should have logged the failure';
is_deeply +MockOutput->get_info_literal, [
[' + roles ..', '', ' '],
], 'Should have seen output for first change';
is_deeply +MockOutput->get_info, [[__ 'not ok']],
'Output should reflect deploy failure';
$die = '';
##############################################################################
# Test _deploy_by_tag().
$plan->reset;
$mock_engine->unmock('_deploy_by_tag');
ok $engine->_deploy_by_tag($plan, 1), 'Deploy tagwise to index 1';
is_deeply $engine->seen, [
[run_file => $changes[0]->deploy_file],
[log_deploy_change => $changes[0]],
[run_file => $changes[1]->deploy_file],
[log_deploy_change => $changes[1]],
], 'Should tagwise deploy to index 1';
is_deeply +MockOutput->get_info_literal, [
[' + roles ..', '', ' '],
[' + users @alpha ..', '', ' '],
], 'Should have seen output of each change';
is_deeply +MockOutput->get_info, [[__ 'ok' ], [__ 'ok']],
'Output should reflect deploy successes';
ok $engine->_deploy_by_tag($plan, 3), 'Deploy tagwise to index 3';
is_deeply $engine->seen, [
[run_file => $changes[2]->deploy_file],
[log_deploy_change => $changes[2]],
[run_file => $changes[3]->deploy_file],
[log_deploy_change => $changes[3]],
], 'Should tagwise deploy from index 2 to index 3';
is_deeply +MockOutput->get_info_literal, [
[' + widgets @beta ..', '', ' '],
[' + lolz ..', '', ' '],
], 'Should have seen output of changes 3-3';
is_deeply +MockOutput->get_info, [[__ 'ok' ], [__ 'ok']],
'Output should reflect deploy successes';
# Add another couple of changes.
$plan->add(name => 'tacos' );
$plan->add(name => 'curry' );
@changes = $plan->changes;
# Make it die.
$plan->position(1);
my $mock_whu = Test::MockModule->new('App::Sqitch::Engine::whu');
$mock_whu->mock(log_deploy_change => sub { hurl 'ROFL' if $_[1] eq $changes[-1] });
throws_ok { $engine->_deploy_by_tag($plan, $#changes) } 'App::Sqitch::X',
'Die in log_deploy_change';
is $@->message, __('Deploy failed'), 'Should get final deploy failure message';
is_deeply $engine->seen, [
[run_file => $changes[2]->deploy_file],
[run_file => $changes[3]->deploy_file],
[run_file => $changes[4]->deploy_file],
[run_file => $changes[5]->deploy_file],
[run_file => $changes[5]->revert_file],
[log_fail_change => $changes[5] ],
[run_file => $changes[4]->revert_file],
[log_revert_change => $changes[4]],
[run_file => $changes[3]->revert_file],
[log_revert_change => $changes[3]],
], 'It should have reverted back to the last deployed tag';
is_deeply +MockOutput->get_info_literal, [
[' + widgets @beta ..', '', ' '],
[' + lolz ..', '', ' '],
[' + tacos ..', '', ' '],
[' + curry ..', '', ' '],
[' - tacos ..', '', ' '],
[' - lolz ..', '', ' '],
], 'Should have seen deploy and revert messages (excluding curry revert)';
is_deeply +MockOutput->get_info, [
[__ 'ok' ],
[__ 'ok' ],
[__ 'ok' ],
[__ 'not ok' ],
[__ 'ok' ],
[__ 'ok' ],
], 'Output should reflect deploy successes and failure';
is_deeply +MockOutput->get_vent, [
['ROFL'],
[__x 'Reverting to {target}', target => 'widgets @beta']
], 'The original error should have been vented';
$mock_whu->unmock('log_deploy_change');
# Now have it fail back to the beginning.
$plan->reset;
$mock_whu->mock(run_file => sub { die 'ROFL' if $_[1]->basename eq 'users.sql' });
throws_ok { $engine->_deploy_by_tag($plan, $plan->count -1 ) } 'App::Sqitch::X',
'Die in _deploy_by_tag again';
is $@->message, __('Deploy failed'), 'Should again get final deploy failure message';
is_deeply $engine->seen, [
[log_deploy_change => $changes[0]],
[log_fail_change => $changes[1]],
[log_revert_change => $changes[0]],
], 'Should have logged back to the beginning';
is_deeply +MockOutput->get_info_literal, [
[' + roles ..', '', ' '],
[' + users @alpha ..', '', ' '],
[' - roles ..', '', ' '],
], 'Should have seen deploy and revert messages';
is_deeply +MockOutput->get_info, [
[__ 'ok' ],
[__ 'not ok' ],
[__ 'ok' ],
], 'Output should reflect deploy successes and failure';
my $vented = MockOutput->get_vent;
is @{ $vented }, 2, 'Should have one vented message';
my $errmsg = shift @{ $vented->[0] };
like $errmsg, qr/^ROFL\b/, 'And it should be the underlying error';
is_deeply $vented, [
[],
[__ 'Reverting all changes'],
], 'And it should had notified that all changes were reverted';
# Add a change and deploy to that, to make sure it rolls back any changes since
# last tag.
$plan->add(name => 'dr_evil' );
@changes = $plan->changes;
$plan->reset;
$mock_whu->mock(run_file => sub { hurl 'ROFL' if $_[1]->basename eq 'dr_evil.sql' });
throws_ok { $engine->_deploy_by_tag($plan, $plan->count -1 ) } 'App::Sqitch::X',
'Die in _deploy_by_tag yet again';
is $@->message, __('Deploy failed'), 'Should die "Deploy failed" again';
is_deeply $engine->seen, [
[log_deploy_change => $changes[0]],
[log_deploy_change => $changes[1]],
[log_deploy_change => $changes[2]],
[log_deploy_change => $changes[3]],
[log_deploy_change => $changes[4]],
[log_deploy_change => $changes[5]],
[log_fail_change => $changes[6]],
[log_revert_change => $changes[5] ],
[log_revert_change => $changes[4] ],
[log_revert_change => $changes[3] ],
], 'Should have reverted back to last tag';
is_deeply +MockOutput->get_info_literal, [
[' + roles ..', '', ' '],
[' + users @alpha ..', '', ' '],
[' + widgets @beta ..', '', ' '],
[' + lolz ..', '', ' '],
[' + tacos ..', '', ' '],
[' + curry ..', '', ' '],
[' + dr_evil ..', '', ' '],
[' - curry ..', '', ' '],
[' - tacos ..', '', ' '],
[' - lolz ..', '', ' '],
], 'Should have user change reversion messages';
is_deeply +MockOutput->get_info, [
[__ 'ok' ],
[__ 'ok' ],
[__ 'ok' ],
[__ 'ok' ],
[__ 'ok' ],
[__ 'ok' ],
[__ 'not ok' ],
[__ 'ok' ],
[__ 'ok' ],
[__ 'ok' ],
], 'Output should reflect deploy successes and failure';
is_deeply +MockOutput->get_vent, [
['ROFL'],
[__x 'Reverting to {target}', target => 'widgets @beta']
], 'Should see underlying error and reversion message';
# Make it choke on change reversion.
$mock_whu->unmock_all;
$die = '';
$plan->reset;
$mock_whu->mock(run_file => sub {