Trog-DataSource-ProvisionedVirt.t 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. #!/usr/bin/env perl
  2. # Reprovisioning destroys a machine and builds it again, so the whole of this
  3. # file is about what has to be true before anything runs. _spawn is mocked
  4. # throughout: nothing here forks, and nothing here executes a provisioner.
  5. use strict;
  6. use warnings;
  7. use FindBin;
  8. use lib "$FindBin::Bin/../lib";
  9. use Test::More;
  10. use Test::MockModule qw{strict};
  11. use Test::Fatal qw{exception};
  12. our ( $REPO, $ROOT, $OLDCWD );
  13. BEGIN {
  14. require Cwd;
  15. require File::Copy;
  16. require File::Path;
  17. require File::Temp;
  18. $REPO = Cwd::abs_path("$FindBin::Bin/..");
  19. $OLDCWD = Cwd::getcwd();
  20. $ROOT = File::Temp::tempdir( 'tcms-provisioned-XXXXXX', TMPDIR => 1, CLEANUP => 1 );
  21. $ENV{HOME} = $ROOT;
  22. File::Path::make_path("$ROOT/$_") for qw{config logs www/assets};
  23. # A stand-in for the checkout, with the one program a reprovision runs.
  24. # Never executed -- _spawn is mocked -- but it has to be there and
  25. # executable, because reprovision() checks that before it commits to
  26. # anything.
  27. File::Path::make_path("$ROOT/trog-provisioner/bin");
  28. my $program = "$ROOT/trog-provisioner/bin/provision";
  29. open( my $fh, '>', $program ) or die $!;
  30. print {$fh} "#!/bin/sh\nexit 0\n";
  31. close $fh;
  32. chmod( 0755, $program );
  33. # And a stand-in for the installation's own files, which live where the
  34. # provisioner keeps them rather than anywhere tCMS is told about.
  35. $ENV{TROG_PROVISIONER_CONFIG} = "$ROOT/etc";
  36. File::Path::make_path("$ROOT/etc/recipes.d");
  37. open( my $recipe, '>', "$ROOT/etc/recipes.d/guest.example.com.yaml" ) or die $!;
  38. print {$recipe} "---\n_global:\n registrar: secret:x/y/z\n";
  39. close $recipe;
  40. chdir($ROOT) or die "could not chdir to the sandbox: $!";
  41. }
  42. END {
  43. chdir($OLDCWD) if $OLDCWD;
  44. }
  45. sub write_config {
  46. my (%section) = @_;
  47. open( my $fh, '>', "$ROOT/config/default.cfg" ) or die $!;
  48. print {$fh} "[general]\n data_model=FlatFile\n[provisioner]\n";
  49. print {$fh} " $_=$section{$_}\n" foreach sort keys %section;
  50. close $fh;
  51. # Trog::Config memoises, which is what a running server wants and a test
  52. # emphatically does not.
  53. no warnings qw{once redefine};
  54. require Config::Simple;
  55. my $conf = Config::Simple->new("$ROOT/config/default.cfg");
  56. local $@;
  57. *Trog::Config::get = sub { return $conf };
  58. return;
  59. }
  60. require_ok('Trog::DataSource::ProvisionedVirt') or BAIL_OUT("Can't find SUT");
  61. my $log = Test::MockModule->new('Trog::DataSource::ProvisionedVirt');
  62. $log->redefine( WARN => sub { note(shift) } );
  63. $log->redefine( INFO => sub { note(shift) } );
  64. # Nothing in this file forks, and nothing in it runs a provisioner.
  65. our @SPAWNED;
  66. $log->redefine(
  67. _spawn => sub {
  68. my ( $domain, $passphrase, $dir, $command, $user ) = @_;
  69. push( @SPAWNED, { domain => $domain, passphrase => $passphrase, dir => $dir, command => $command, user => $user } );
  70. return ( 1, 'started' );
  71. }
  72. );
  73. # Nor reaches a hypervisor.
  74. my $virt = Test::MockModule->new('Trog::DataSource::Virt');
  75. $virt->redefine( _is_self => sub { return $_[0] eq 'thisserver.example.com' ? 1 : 0 } );
  76. write_config( trog_provisioner => "$ROOT/trog-provisioner" );
  77. sub reprovision {
  78. @SPAWNED = ();
  79. return Trog::DataSource::ProvisionedVirt::reprovision( passphrase => 'hunter2', user => 'admin', @_ );
  80. }
  81. subtest 'it is Virt, with more' => sub {
  82. ok( Trog::DataSource::ProvisionedVirt->isa('Trog::DataSource::Virt'), 'it is a Virt' );
  83. # can() resolves through @ISA, which is how the datasource dispatcher finds
  84. # the halves this does not override.
  85. foreach my $inherited (qw{filter lang help}) {
  86. my $mine = Trog::DataSource::ProvisionedVirt->can($inherited);
  87. my $theirs = Trog::DataSource::Virt->can($inherited);
  88. is( $mine, $theirs, "$inherited is the one Virt already had" );
  89. }
  90. # Virt has no order() of its own, so this correctly finds none and the
  91. # dispatcher falls back to Trog::DataSource::order.
  92. ok( !Trog::DataSource::ProvisionedVirt->can('order'), 'and the shared default ordering still applies' );
  93. # posts() is the one thing it does replace.
  94. isnt(
  95. Trog::DataSource::ProvisionedVirt->can('posts'),
  96. Trog::DataSource::Virt->can('posts'),
  97. 'posts() is its own, since that is where the recipe is added'
  98. );
  99. is( Trog::DataSource::ProvisionedVirt->EDITABLE, 0, 'still nothing to edit' );
  100. is( Trog::DataSource::ProvisionedVirt->CACHEABLE, 0, 'and still never cached, since a guest\'s state is live' );
  101. };
  102. subtest 'the recipe lands on the guest' => sub {
  103. $virt->redefine(
  104. posts => sub {
  105. return (
  106. { title => 'guest.example.com', domain => 'guest.example.com', is_self => 0 },
  107. { title => 'norecipe.example.com', domain => 'norecipe.example.com', is_self => 0 },
  108. { title => 'thisserver.example.com', domain => 'thisserver.example.com', is_self => 1 },
  109. { title => 'a hypervisor', unreachable => 1, state => 'nope' },
  110. );
  111. }
  112. );
  113. my @posts = Trog::DataSource::ProvisionedVirt::posts( {}, {} );
  114. is( scalar(@posts), 4, 'every guest Virt listed is still listed' );
  115. my ($provisioned) = grep { ( $_->{domain} // '' ) eq 'guest.example.com' } @posts;
  116. ok( $provisioned->{has_recipe}, 'the one with a recipe says so' );
  117. like( $provisioned->{recipe}, qr/registrar/, 'and carries the recipe itself' );
  118. like( $provisioned->{recipe_path}, qr{recipes\.d/guest\.example\.com\.yaml$}, 'and where it came from' );
  119. is( $provisioned->{can_reprovision}, 1, 'and may be reprovisioned' );
  120. my ($bare) = grep { ( $_->{domain} // '' ) eq 'norecipe.example.com' } @posts;
  121. ok( !$bare->{has_recipe}, 'a guest with no recipe says so' );
  122. is( $bare->{can_reprovision}, 0, 'and may not be reprovisioned' );
  123. ok( exists $bare->{title}, 'but is listed all the same, since plenty of guests were not built by this' );
  124. my ($self) = grep { ( $_->{domain} // '' ) eq 'thisserver.example.com' } @posts;
  125. is( $self->{can_reprovision}, 0, 'the server drawing the page may never be reprovisioned' );
  126. my ($dead) = grep { $_->{unreachable} } @posts;
  127. ok( !$dead->{has_recipe}, 'an unreachable hypervisor is left exactly as it was' );
  128. };
  129. subtest 'what it refuses to reprovision' => sub {
  130. my ( $ok, $why ) = reprovision( domain => 'thisserver.example.com' );
  131. is( $ok, 0, 'the server it is running on' );
  132. like( $why, qr/this server/, 'and says why' );
  133. is_deeply( \@SPAWNED, [], 'without running anything' );
  134. ( $ok, $why ) = reprovision( domain => 'norecipe.example.com' );
  135. is( $ok, 0, 'a guest with no recipe' );
  136. like( $why, qr/no recipe/, 'and says why' );
  137. ( $ok, $why ) = reprovision( domain => 'guest.example.com', passphrase => '' );
  138. is( $ok, 0, 'a run with no passphrase, which the provisioner would just hang waiting for' );
  139. like( $why, qr/passphrase/, 'and says why' );
  140. # The name reaches the filesystem and an argument list.
  141. foreach my $bad ( '../../etc/passwd', 'guest;rm -rf /', 'guest example.com', '', 'a' x 300, '.hidden' ) {
  142. my ( $refused, $message ) = reprovision( domain => $bad );
  143. is( $refused, 0, "'$bad' is refused" );
  144. like( $message, qr/bad guest name|no recipe/, '...before anything is run' );
  145. }
  146. is_deeply( \@SPAWNED, [], 'and none of that ran a provisioner' );
  147. };
  148. subtest 'what it would run' => sub {
  149. my ( $ok, $why ) = reprovision( domain => 'guest.example.com' );
  150. is( $ok, 1, 'a guest with a recipe is accepted' ) or diag($why);
  151. like( $why, qr/started/, 'and reports that it started rather than that it finished' );
  152. is( scalar(@SPAWNED), 1, 'exactly one run' );
  153. my $run = $SPAWNED[0];
  154. is( $run->{domain}, 'guest.example.com', 'for the guest asked about' );
  155. is( $run->{user}, 'admin', 'recording who asked' );
  156. # One program does the whole of it now: bin/provision generates the guest's
  157. # configuration from its recipe and then builds the machine.
  158. is( $run->{dir}, "$ROOT/trog-provisioner", 'run from the checkout' );
  159. like( $run->{command}[0], qr{/bin/provision$}, 'and it is bin/provision' );
  160. is( $run->{command}[1], 'guest.example.com', 'passed the guest as an argument' );
  161. # Arguments, not a command line: there is no shell between us and this, so a
  162. # guest name can never be read as one.
  163. is( scalar( @{ $run->{command} } ), 2, 'a program and one argument -- nothing to be parsed' );
  164. };
  165. subtest 'without the provisioner configured it is just Virt' => sub {
  166. write_config();
  167. my ( $ok, $why ) = reprovision( domain => 'guest.example.com' );
  168. is( $ok, 0, 'nothing to run' );
  169. like( $why, qr/not configured/, 'and says so plainly' );
  170. is_deeply( \@SPAWNED, [], 'having run nothing' );
  171. $virt->redefine( posts => sub { return ( { title => 'guest.example.com', domain => 'guest.example.com', is_self => 0 } ) } );
  172. my ($post) = Trog::DataSource::ProvisionedVirt::posts( {}, {} );
  173. ok( !$post->{has_recipe}, 'the listing still works' );
  174. is( $post->{can_reprovision}, 0, 'it simply offers no button' );
  175. # A path that is not there is a typo, and better found here than halfway
  176. # through a reprovision.
  177. write_config( trog_provisioner => "$ROOT/nonesuch" );
  178. ( $ok, $why ) = reprovision( domain => 'guest.example.com' );
  179. is( $ok, 0, 'a configured path which is not there is refused' );
  180. like( $why, qr/not configured/, 'the same as never having configured one' );
  181. write_config( trog_provisioner => "$ROOT/trog-provisioner" );
  182. };
  183. subtest 'the log is read back onto the page' => sub {
  184. no warnings qw{once};
  185. File::Path::make_path("$ROOT/logs/reprovision");
  186. open( my $fh, '>', "$ROOT/logs/reprovision/guest.example.com.log" ) or die $!;
  187. print {$fh} ( 'x' x 9000 ) . "\n=== finished ===\n";
  188. close $fh;
  189. my $tail = Trog::DataSource::ProvisionedVirt::_log_tail('guest.example.com');
  190. ok( length($tail) <= $Trog::DataSource::ProvisionedVirt::log_tail_bytes, 'only the tail of it' );
  191. like( $tail, qr/=== finished ===/, 'which is the part that says how it went' );
  192. is( Trog::DataSource::ProvisionedVirt::_log_tail('norecipe.example.com'), undef, 'nothing when it has never run' );
  193. is( Trog::DataSource::ProvisionedVirt::_log_tail('../../etc/passwd'), undef, 'and a name that is not one reads nothing' );
  194. };
  195. subtest 'the state a reprovision leaves behind' => sub {
  196. no warnings qw{once};
  197. File::Path::make_path("$ROOT/logs/reprovision");
  198. my $status = "$ROOT/logs/reprovision/guest.example.com.status";
  199. my $write = sub {
  200. open( my $fh, '>', $status ) or die $!;
  201. print {$fh} join( '', map { "$_\n" } @_ );
  202. close $fh;
  203. };
  204. # Nothing waits on the process which does the work, so this file is the
  205. # whole of what it can say for itself.
  206. $write->( 'state=ok', 'started=100', 'finished=200', 'exit=0', 'user=admin' );
  207. my $got = Trog::DataSource::ProvisionedVirt::status('guest.example.com');
  208. is( $got->{state}, 'ok', 'a finished run reads back as finished' );
  209. is( $got->{exit}, '0', 'with what it exited' );
  210. is( $got->{user}, 'admin', 'and who asked for it' );
  211. is( $got->{finished}, '200', 'and when it stopped' );
  212. $write->( 'state=failed', 'exit=3', 'failed=/x/bin/provision' );
  213. is( Trog::DataSource::ProvisionedVirt::status('guest.example.com')->{state}, 'failed', 'a failed one reads back failed' );
  214. # A live pid means it really is going on. Ours is alive by definition.
  215. $write->( 'state=running', "pid=$$", 'started=100', 'user=admin' );
  216. is( Trog::DataSource::ProvisionedVirt::status('guest.example.com')->{state}, 'running', 'a run whose process is alive is running' );
  217. # And a dead one means it stopped without saying so, which is the case that
  218. # would otherwise leave the page claiming a provision forever.
  219. my $dead = 999999;
  220. $dead++ while kill( 0, $dead ) && $dead < 4194304;
  221. $write->( 'state=running', "pid=$dead", 'started=100' );
  222. is(
  223. Trog::DataSource::ProvisionedVirt::status('guest.example.com')->{state},
  224. 'interrupted',
  225. 'a run whose process is gone reads as interrupted, not as still going'
  226. );
  227. $write->('state=running');
  228. is( Trog::DataSource::ProvisionedVirt::status('guest.example.com')->{state}, 'interrupted', 'and so does one with no pid at all' );
  229. # Half a file is not a status.
  230. $write->('garbage');
  231. is( Trog::DataSource::ProvisionedVirt::status('guest.example.com'), undef, 'an unparseable status is no status' );
  232. is( Trog::DataSource::ProvisionedVirt::status('never.example.com'), undef, 'nothing for a guest that has never run' );
  233. is( Trog::DataSource::ProvisionedVirt::status('../../etc/passwd'), undef, 'and a name that is not one reads nothing' );
  234. unlink($status);
  235. };
  236. subtest 'the listing says what is going on' => sub {
  237. no warnings qw{once};
  238. File::Path::make_path("$ROOT/logs/reprovision");
  239. my $status = "$ROOT/logs/reprovision/guest.example.com.status";
  240. open( my $log, '>', "$ROOT/logs/reprovision/guest.example.com.log" ) or die $!;
  241. print {$log} "some output\n";
  242. close $log;
  243. $virt->redefine( posts => sub { return ( { title => 'guest.example.com', domain => 'guest.example.com', is_self => 0 } ) } );
  244. open( my $fh, '>', $status ) or die $!;
  245. print {$fh} "state=running\npid=$$\nstarted=100\nuser=admin\n";
  246. close $fh;
  247. my ($post) = Trog::DataSource::ProvisionedVirt::posts( {}, {} );
  248. is( $post->{reprovision_state}, 'running', 'the guest says it is reprovisioning' );
  249. is( $post->{reprovision_status}, 'reprovisioning', 'in words a person reads' );
  250. is( $post->{is_reprovisioning}, 1, 'and is flagged as busy' );
  251. is( $post->{reprovision_by}, 'admin', 'naming who started it' );
  252. is( $post->{can_reprovision}, 0, 'and offers no button while one is going on' );
  253. is( $post->{reprovision_log_href}, '/guest/reprovision/log/guest.example.com', 'with a link to read the log' );
  254. # Starting a second one is refused for the same reason.
  255. my ( $ok, $why ) = reprovision( domain => 'guest.example.com' );
  256. is( $ok, 0, 'and a second run is refused while the first is going' );
  257. like( $why, qr/already running/, 'saying so' );
  258. is_deeply( \@SPAWNED, [], 'having started nothing' );
  259. open( $fh, '>', $status ) or die $!;
  260. print {$fh} "state=failed\nexit=3\nstarted=100\nfinished=200\nuser=admin\n";
  261. close $fh;
  262. ($post) = Trog::DataSource::ProvisionedVirt::posts( {}, {} );
  263. is( $post->{reprovision_state}, 'failed', 'a failed run says so' );
  264. is( $post->{reprovision_status}, 'reprovision failed', 'in words' );
  265. is( $post->{reprovision_exit}, '3', 'with what it exited' );
  266. is( $post->{is_reprovisioning}, 0, 'and is not busy' );
  267. is( $post->{can_reprovision}, 1, 'so it may be tried again' );
  268. unlink($status);
  269. ($post) = Trog::DataSource::ProvisionedVirt::posts( {}, {} );
  270. ok( !$post->{reprovision_state}, 'a guest which has never been reprovisioned says nothing about it' );
  271. is( $post->{can_reprovision}, 1, 'and may be' );
  272. };
  273. subtest 'reading the log' => sub {
  274. no warnings qw{once};
  275. is( Trog::DataSource::ProvisionedVirt::log_for('never.example.com'), undef, 'nothing when there has been no run' );
  276. is( Trog::DataSource::ProvisionedVirt::log_for('../../etc/passwd'), undef, 'and a name that is not one reads nothing' );
  277. open( my $fh, '>', "$ROOT/logs/reprovision/guest.example.com.log" ) or die $!;
  278. print {$fh} "the whole log\n";
  279. close $fh;
  280. like( Trog::DataSource::ProvisionedVirt::log_for('guest.example.com'), qr/the whole log/, 'and the whole of it when there has' );
  281. # A provisioner can be chatty, and this goes out in one response.
  282. local $Trog::DataSource::ProvisionedVirt::log_max_bytes = 64;
  283. open( $fh, '>', "$ROOT/logs/reprovision/guest.example.com.log" ) or die $!;
  284. print {$fh} ( 'x' x 500 ) . "END\n";
  285. close $fh;
  286. my $capped = Trog::DataSource::ProvisionedVirt::log_for('guest.example.com');
  287. like( $capped, qr/truncated/, 'a huge one is truncated' );
  288. like( $capped, qr/END/, 'keeping the end, which is where the answer is' );
  289. };
  290. subtest 'the route' => sub {
  291. no warnings qw{once};
  292. require Trog::Routes::HTML;
  293. my $route = $Trog::Routes::HTML::routes{'/guest/reprovision'};
  294. ok( $route, 'is registered' );
  295. is( $route->{method}, 'POST', 'POST only, since it is not a thing to do by following a link' );
  296. is( $route->{auth}, 1, 'and requires a login' );
  297. ok( $route->{nocache}, 'and is never cached' );
  298. ok( $route->{noindex}, 'nor indexed' );
  299. my $tpsgi = bless {}, 'FakeTPSGI3';
  300. {
  301. no warnings qw{once};
  302. *FakeTPSGI3::see_also = sub { return [ 303, [ Location => $_[1] ], [''] ] };
  303. *FakeTPSGI3::forbidden = sub { return [ 403, [], ['no'] ] };
  304. *FakeTPSGI3::notfound = sub { return [ 404, [], ['gone'] ] };
  305. }
  306. # Admin only, like every other thing that changes a guest.
  307. my $res = Trog::Routes::HTML::guest_reprovision( { user => 'bob', user_acls => ['public'], tpsgi => $tpsgi } );
  308. is( $res->[0], 403, 'a non-admin is refused' );
  309. is_deeply( \@SPAWNED, [], 'without running anything' );
  310. $res = Trog::Routes::HTML::guest_reprovision( { user => '', user_acls => [], tpsgi => $tpsgi } );
  311. is( $res->[0], 303, 'and somebody logged out is sent to log in' );
  312. # The passphrase must not survive the request: this hash gets cloned, logged
  313. # around and handed to renderers.
  314. @SPAWNED = ();
  315. my $query = { user => 'admin', user_acls => ['admin'], tpsgi => $tpsgi, guest => 'guest.example.com', passphrase => 'hunter2', to => '/vms' };
  316. Trog::Routes::HTML::guest_reprovision($query);
  317. is( scalar(@SPAWNED), 1, 'an admin gets their run' );
  318. is( $SPAWNED[0]{passphrase}, 'hunter2', 'the passphrase reaches the provisioner' );
  319. ok( !exists $query->{passphrase}, 'and is gone from the query afterwards' );
  320. # The log route. It lives in logs/ rather than under www/, and the output
  321. # of a provisioner is a fine place for a hostname or an IP plan to turn up.
  322. my $logroute = $Trog::Routes::HTML::routes{'/guest/reprovision/log/(.*)'};
  323. ok( $logroute, 'the log route is registered' );
  324. is( $logroute->{method}, 'GET', 'as a GET, since it only reads' );
  325. is( $logroute->{auth}, 1, 'behind a login' );
  326. is_deeply( $logroute->{captures}, ['guest'], "capturing 'guest', which survives routing" );
  327. $res = Trog::Routes::HTML::guest_reprovision_log( { user => 'bob', user_acls => ['public'], tpsgi => $tpsgi } );
  328. is( $res->[0], 403, 'a non-admin cannot read it' );
  329. $res = Trog::Routes::HTML::guest_reprovision_log( { user => 'admin', user_acls => ['admin'], tpsgi => $tpsgi, guest => 'never.example.com' } );
  330. is( $res->[0], 404, 'and a guest with no log is a 404 rather than an empty page' );
  331. };
  332. done_testing();