App-Actions

 view release on metacpan or  search on metacpan

t/unit.t  view on Meta::CPAN

		my @started = ('earlier');
		my $services = $object->start_services($service_job, 'net', $context, \@started);
		returns_ok($services, { type => 'arrayref' }, 'an array reference');
		my ($cache, $db) = map { "action-run-34567890-$_-build-1-" } '1-svc', '2-svc';
		is_deeply($services, [ [ 'cache', "${cache}cache" ], [ 'db', "${db}db" ] ], 'id and container name, in id order');
		is_deeply(\@started, [ 'earlier', "${cache}cache", "${db}db" ], 'each added to the started list');
		is_deeply($object->remaining->{containers}, [ "${cache}cache", "${db}db" ], 'and remembered');
		my $starts = $docker->calls_to('container_start');
		is_deeply($words->($starts->[1]), [ '--platform', 'linux/amd64', '--health-cmd', 'pg_isready -U x', '--name', "${db}db",
			'--network', 'net', '--network-alias', 'db', '--user', '1000', '-v', '/data:/var/lib/pg', '-e', 'PASSWORD=pw',
			'-p', '5432:5432', 'postgres:16' ], 'the options, name, alias, user, volumes, environment, ports and image');
		is_deeply($words->($starts->[0]), [ '--platform', 'linux/amd64', '--name', "${cache}cache", '--network', 'net',
			'--network-alias', 'cache', '--user', '1000', 'redis:7' ], 'an image name alone is a service');
		is_deeply(\@said, [ q{action-run: service 'db' credentials are not supported; using the local Docker login} ],
			'credentials are warned about');
		@said = ();
		$containers->(undef, quiet => 1, logger => collecting_logger(\@said))->start_services($service_job, 'net', $context, []);
		is_deeply(\@said, [], 'unless quiet');
		is_deeply($containers->()->start_services({ %{$job} }, 'net', $context, []), [], 'no services');

		my $explained = output_of(stdout => sub {
			$containers->(undef, explain => 1)->start_services({ %{$job}, services => {
				a => { image => 'a:1', options => '--entrypoint=/bin/a --entrypoint /bin/later' },
				b => { image => 'b:1', options => '--entrypoint /bin/b' },
				c => { image => 'c:1' },
			} }, 'net', $context, []);
		});
		is($explained, join('', map {
			my ($id, $n, $entrypoint) = @{$_};
			"==> Service '$id' of job 'build'\n    container  action-run-34567890-$n-svc-build-1-$id\n    image      $id:1\n"
				. "    entrypoint $entrypoint\n    command    (the image's own)\n";
		} [ 'a', 1, '/bin/a' ], [ 'b', 2, '/bin/b' ], [ 'c', 3, "(the image's own)" ]), 'explain says what each service runs');
		keeps_globals(sub { $containers->()->start_services($service_job, 'net', $context, []) }, 'start_services');
	};

	subtest 'start_services: what fails' => sub {
		my $start = sub {
			my ($services, %answer) = @_;
			my $object = $containers->(Local::ContainerDocker->new(%answer));
			my @started;
			my $error = caught { $object->start_services({ %{$job}, services => $services }, 'net', $context, \@started) };
			return ($error, \@started, $object);
		};
		throws_code(sub { die +($start->({ db => [ 'postgres' ] }))[0] }, 'runner.service.not_mapping', q{Service 'db' must be a mapping},
			'a list is not a service');
		for my $case ([ 'a mapping without one', {} ], [ 'an empty name', { image => '' } ], [ 'nothing at all', undef ]) {
			throws_code(sub { die +($start->({ db => $case->[1] }))[0] }, 'runner.service.no_image', q{Service 'db' has no image},
				"no image: $case->[0]");
		}
		throws_code(sub { die +($start->({ db => { image => 'p', options => '--env "A=${{ env.SECRET }}' } }))[0] }, 'runner.options_invalid',
			q{Unable to read the container options '--env "A=${{ env.SECRET }}': a quote is not closed},
			'options with an unclosed quote, quoted as written');

		my ($error, $started, $object) = $start->({ db => 'postgres' }, container_start => 125);
		throws_code(sub { die $error }, 'runner.service.start_failed', q{Failed to start service 'db' (exit 125)},
			'a service the daemon will not start');
		is_deeply($started, [ 'action-run-34567890-1-svc-build-1-db' ], 'is still in the started list');
		is_deeply($object->remaining->{containers}, $started, 'and remembered for removal');

		($error) = $start->({ db => 'postgres' }, container_running => 0, container_exit_code => 1,
			container_log_text => "starting\r\nFATAL: no password\r\n\n  \n");
		my $stopped = throws_code(sub { die $error }, 'runner.container.stopped',
			"Service 'db' did not stay running (exit 1); its last output was:\n  starting\n  FATAL: no password",
			'a service that stopped, with its last output');
		is("$stopped", $stopped->message . "\n", 'which reads as its message, without a source line');
		($error) = $start->({ db => 'postgres' }, container_running => 0);
		throws_code(sub { die $error }, 'runner.container.stopped',
			q{Service 'db' did not stay running (its exit status could not be read); it printed nothing}, 'one that said nothing');
		($error) = $start->({ db => 'postgres' }, container_status => 'restarting', container_restart_count => 3, container_log_text => "oops\n");
		throws_code(sub { die $error }, 'runner.container.restarting',
			"Service 'db' is restarting (restart count 3); its last output was:\n  oops", 'a service that keeps restarting');
	};

	subtest 'check_services' => sub {
		my $check = sub { $containers->(Local::ContainerDocker->new(@_))->check_services([ [ 'db', 'c1' ] ]) };
		my $object = $containers->();
		is_returned($object->check_services([ [ 'db', 'c1' ] ]), { type => 'object', isa => 'App::Actions::Runner::Containers' }, $object,
			'services that stay up');
		ok($check->(container_status => undef, container_restart_count => undef), 'or say nothing');
		throws_code(sub { $check->(container_restart_count => 2) }, 'runner.container.restarting',
			q{Service 'db' is restarting (restart count 2); it printed nothing}, 'running, but restarted');
		throws_code(sub { $check->(container_status => 'dead', container_restart_count => undef, container_log_text => "gone\n") },
			'runner.container.restarting', "Service 'db' is dead (its restart count could not be read); its last output was:\n  gone",
			'dead, with no count');
		throws_code(sub { $check->(container_status => 'restarting') }, 'runner.container.restarting',
			q{Service 'db' is restarting; it printed nothing}, 'restarting, not yet counted');
		keeps_globals(sub { $object->check_services([ [ 'db', 'c1' ] ]) }, 'check_services');
	};

	subtest 'start_job' => sub {
		my $docker = Local::ContainerDocker->new;
		my $object = $containers->($docker, user => '0');
		my $spec_job = { base_id => '_my.job_', matrix_index => 0, container => { image => 'x', options => '--cpus 2',
			volumes => [ '/a:/a' ], ports => '8080:80' } };
		my $name = $object->start_job($spec_job, 'net', { A => '1' }, $context, 'perl:5.40', '/tmp/runner');
		is_returned($name, { type => 'string', min => 1 }, 'action-run-34567890-1-job-_my.job_-0', 'the container name');
		is_deeply($words->($docker->calls_to('container_start_captured')->[0]), [ '--cpus', '2', '--name', $name, '--network', 'net',
			'--hostname', 'my.job', '--user', '0', '-v', "$project:/github/workspace", '-v', '/tmp/runner:/github/actions-runner',
			'-v', '/a:/a', '-p', '8080:80', '-w', '/github/workspace', '-e', 'A=1', '--entrypoint', '/bin/sh', 'perl:5.40',
			'-c', 'while :; do sleep 3600; done' ], 'the project, runner directory, environment and an idle shell');
		is_deeply($docker->calls_to('container_remove'), [], 'a container that starts is kept');
		my $plain = $containers->()->start_job({ base_id => '--', matrix_index => 0 }, 'net', {}, $context, 'img', '/r');
		ok($plain, 'a job with no container mapping');

		my $explained = output_of(stdout => sub { $containers->(undef, explain => 1)->start_job($job, 'net', {}, $context, 'img', '/r') });
		is($explained, "==> Job 'build'\n    container  action-run-34567890-1-job-build-1\n    image      img\n    entrypoint /bin/sh\n"
			. "    command    -c 'while :; do sleep 3600; done'\n", 'explain says what the job container runs');
		keeps_globals(sub { $containers->()->start_job($job, 'net', {}, $context, 'img', '/r') }, 'start_job');
	};

	subtest 'start_job: what fails' => sub {
		my $container = 'action-run-34567890-1-job-build-1';
		my $refuse = sub {
			my (%answer) = @_;
			my $docker = Local::ContainerDocker->new(%answer);
			my $error = caught { $containers->($docker)->start_job($job, 'net', {}, $context, 'perl:5.40', '/r') };
			is_deeply($docker->calls_to('container_remove'), [ [ $container ] ], 'the container is removed');
			die $error;
		};
		my $start_failed = "Job container '$container' could not be started (exit 125)";
		for my $case (
			[ "Error: No such image: perl:5.40\n", "; at fault: the image 'perl:5.40'", 'an image the daemon cannot find' ],
			[ "Bind for 0.0.0.0:80 failed: port is already allocated\r\n", '; at fault: the published port', 'a port in use' ],



( run in 1.420 second using v1.01-cache-2.11-cpan-f5f21f1076c )