From d16bf5b8cd994836ec12de080a67a23ddfc0f971 Mon Sep 17 00:00:00 2001 From: Maximilian Bosch Date: Fri, 2 Apr 2021 19:11:25 +0200 Subject: [PATCH] Implement `yath`-test for the new Gitea plugin --- flake.nix | 37 ++++++++++++++++++++ t/plugins/gitea.t | 86 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 123 insertions(+) create mode 100644 t/plugins/gitea.t diff --git a/flake.nix b/flake.nix index 67d318ad..e8b4f9ed 100644 --- a/flake.nix +++ b/flake.nix @@ -56,6 +56,41 @@ }; }; + HTTPParser = final.perlPackages.buildPerlPackage { + pname = "HTTP-Parser"; + version = "0.06"; + src = final.fetchurl { + url = "mirror://cpan/authors/id/E/ED/EDECA/HTTP-Parser-0.06.tar.gz"; + sha256 = "sha256-+MWh4cvY8ndb09HOX6zIQx8FkQi/V1oMjb2kMuXAvEU="; + }; + + buildInputs = with final.perlPackages; [ TestMore URI HTTPMessage ]; + + meta = { + homepage = https://metacpan.org/pod/HTTP::Parser; + description = "HTTP::Parser - parse HTTP/1.1 request into HTTP::Request/Response object"; + license = final.lib.licenses.artistic1; + }; + }; + + TestHTTPMockServer = final.perlPackages.buildPerlModule { + pname = "Test-HTTP-MockServer"; + version = "0.0.1"; + src = final.fetchurl { + url = "mirror://cpan/authors/id/D/DR/DRUOSO/Test-HTTP-MockServer-v0.0.1.tar.gz"; + sha256 = "sha256-cnVjaKGgOxA0IcJiuzk/a2nxQGbhKD3vpaLFWIqINDg="; + }; + + buildInputs = with final.perlPackages; [ JSONXS LWP HTTPMessage HTTPParser ]; + doCheck = false; + + meta = { + homepage = https://metacpan.org/pod/Test::HTTP::MockServer; + description = "Implement a mock HTTP server for use in tests"; + license = final.lib.licenses.asl20; + }; + }; + FunctionParameters = final.buildPerlPackage { pname = "Function-Parameters"; version = "2.001003"; @@ -264,6 +299,7 @@ EmailSender FileSlurp FileWhich + HTTPParser IOCompress IPCRun JSON @@ -281,6 +317,7 @@ Starman SysHostnameLong TermSizeAny + TestHTTPMockServer TestMore TestPostgreSQL TextDiff diff --git a/t/plugins/gitea.t b/t/plugins/gitea.t new file mode 100644 index 00000000..ef2d0b46 --- /dev/null +++ b/t/plugins/gitea.t @@ -0,0 +1,86 @@ +use feature 'unicode_strings'; +use strict; +use warnings; +use JSON; +use Setup; + +use Test::HTTP::MockServer; + +my $server = Test::HTTP::MockServer->new(); +my $url = $server->url_base(); + +my %ctx = test_init( + hydra_config => q| + + root=d7f16a3412e01a43a414535b16007c6931d3a9c7 + +|); + +require Hydra::Schema; +require Hydra::Model::DB; + +use Test2::V0; + +my $db = Hydra::Model::DB->new; +hydra_setup($db); + +my $scratch = "$ctx{tmpdir}/scratch"; +mkdir $scratch; + +my $uri = "file://$scratch/git-repo"; + +my $jobset = createJobsetWithOneInput('gitea', 'git-input.nix', 'src', 'git', $uri, $ctx{jobsdir}); + +sub addStringInput { + my ($jobset, $name, $value) = @_; + my $input = $jobset->jobsetinputs->create({name => $name, type => "string"}); + $input->jobsetinputalts->create({value => $value, altnr => 0}); +} + +addStringInput($jobset, "gitea_repo_owner", "root"); +addStringInput($jobset, "gitea_repo_name", "foo"); +addStringInput($jobset, "gitea_status_repo", "src"); +addStringInput($jobset, "gitea_http_url", "$url/gitea"); + +updateRepository('gitea', "$ctx{testdir}/jobs/git-update.sh", $scratch); + +ok(evalSucceeds($jobset), "Evaluating nix expression"); +is(nrQueuedBuildsForJobset($jobset), 1, "Evaluating jobs/runcommand.nix should result in 1 build1"); + +(my $build) = queuedBuildsForJobset($jobset); +ok(runBuild($build), "Build should succeed with exit code 0"); + +my $filename = $ENV{'HYDRA_DATA'} . "/giteaout.json"; +my $handle = sub { + my ($request, $response) = @_; + + open(FH, ">", $filename) or die("Can't open(): $!\n"); + print FH $request->uri . "\n"; + print FH $request->content . "\n"; + close(FH); + + return $response; +}; + +$server->start_mock_server($handle); +my $newbuild = $db->resultset('Builds')->find($build->id); +is($newbuild->finished, 1, "Build should be finished."); +is($newbuild->buildstatus, 0, "Build should have buildstatus 0."); +ok(sendNotifications(), "Sent notifications"); + +$server->stop_mock_server(); + +open my $fh, $filename or die ("Can't open(): $!\n"); +my $i = 0; +my $uri = <$fh>; +my $data = <$fh>; + +ok(index($uri, "gitea/api/v1/repos/root/foo/statuses") != -1, "Correct URL"); + +my $json = JSON->new; +my $content; +$content = $json->decode($data); + +is($content->{state}, "success", "Success notification"); + +done_testing;