#feature: some update
This commit is contained in:
@@ -0,0 +1,114 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use App\Clients\JenkinsClient;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Tests\TestCase;
|
||||
|
||||
class JenkinsClientTest extends TestCase
|
||||
{
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
config([
|
||||
'jenkins.host' => 'https://jenkins.example.com',
|
||||
'jenkins.username' => 'test-user',
|
||||
'jenkins.api_token' => 'test-token',
|
||||
'jenkins.timeout' => 30,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_parameterized_build_posts_form_json_parameters_and_returns_queue_url(): void
|
||||
{
|
||||
Http::fake([
|
||||
'https://jenkins.example.com/job/deploy/api/json' => Http::response([
|
||||
'nextBuildNumber' => 42,
|
||||
]),
|
||||
'https://jenkins.example.com/crumbIssuer/api/json' => Http::response([
|
||||
'crumbRequestField' => 'Jenkins-Crumb',
|
||||
'crumb' => 'test-crumb',
|
||||
]),
|
||||
'https://jenkins.example.com/job/deploy/build?delay=0sec' => Http::response('', 201, [
|
||||
'Location' => 'https://jenkins.example.com/queue/item/123/',
|
||||
]),
|
||||
]);
|
||||
|
||||
$result = app(JenkinsClient::class)->triggerBuild('deploy', [
|
||||
'project' => 'portal',
|
||||
'branchName' => 'release/1.0',
|
||||
]);
|
||||
|
||||
$this->assertTrue($result['success']);
|
||||
$this->assertSame('https://jenkins.example.com/queue/item/123/', $result['queue_url']);
|
||||
$this->assertSame(42, $result['build_number']);
|
||||
|
||||
Http::assertSent(function ($request) {
|
||||
parse_str($request->body(), $form);
|
||||
$payload = json_decode((string) ($form['json'] ?? ''), true);
|
||||
$parameters = collect($payload['parameter'] ?? [])->pluck('value', 'name');
|
||||
|
||||
return $request->method() === 'POST'
|
||||
&& $request->url() === 'https://jenkins.example.com/job/deploy/build?delay=0sec'
|
||||
&& $parameters['project'] === 'portal'
|
||||
&& $parameters['branchName'] === 'release/1.0';
|
||||
});
|
||||
}
|
||||
|
||||
public function test_cancel_build_with_queue_url_cancels_pending_queue_item(): void
|
||||
{
|
||||
Http::fake([
|
||||
'https://jenkins.example.com/queue/item/123/api/json' => Http::response([
|
||||
'why' => 'In the quiet period',
|
||||
]),
|
||||
'https://jenkins.example.com/crumbIssuer/api/json' => Http::response([
|
||||
'crumbRequestField' => 'Jenkins-Crumb',
|
||||
'crumb' => 'test-crumb',
|
||||
]),
|
||||
'https://jenkins.example.com/queue/cancelItem?id=123' => Http::response('', 200),
|
||||
]);
|
||||
|
||||
$result = app(JenkinsClient::class)->cancelBuild('deploy', 'https://jenkins.example.com/queue/item/123/');
|
||||
|
||||
$this->assertTrue($result['success']);
|
||||
$this->assertTrue($result['cancelled_queue']);
|
||||
|
||||
Http::assertSent(function ($request) {
|
||||
return $request->method() === 'POST'
|
||||
&& $request->url() === 'https://jenkins.example.com/queue/cancelItem?id=123';
|
||||
});
|
||||
}
|
||||
|
||||
public function test_cancel_build_without_queue_url_cancels_matching_queued_job(): void
|
||||
{
|
||||
Http::fake([
|
||||
'https://jenkins.example.com/queue/api/json' => Http::response([
|
||||
'items' => [
|
||||
[
|
||||
'id' => 456,
|
||||
'task' => [
|
||||
'fullName' => 'deploy',
|
||||
'name' => 'deploy',
|
||||
],
|
||||
],
|
||||
],
|
||||
]),
|
||||
'https://jenkins.example.com/crumbIssuer/api/json' => Http::response([
|
||||
'crumbRequestField' => 'Jenkins-Crumb',
|
||||
'crumb' => 'test-crumb',
|
||||
]),
|
||||
'https://jenkins.example.com/queue/cancelItem?id=456' => Http::response('', 200),
|
||||
]);
|
||||
|
||||
$result = app(JenkinsClient::class)->cancelBuild('deploy', null, 42);
|
||||
|
||||
$this->assertTrue($result['success']);
|
||||
$this->assertTrue($result['cancelled_queue']);
|
||||
|
||||
Http::assertSent(function ($request) {
|
||||
return $request->method() === 'POST'
|
||||
&& $request->url() === 'https://jenkins.example.com/queue/cancelItem?id=456';
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user