|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @author Aaron Francis <aaron@hammerstone.dev> |
| 4 | + */ |
| 5 | + |
| 6 | +namespace Hammerstone\Sidecar\Tests; |
| 7 | + |
| 8 | +use Aws\Lambda\Exception\LambdaException; |
| 9 | +use Hammerstone\Sidecar\Clients\LambdaClient; |
| 10 | +use Hammerstone\Sidecar\Deployment; |
| 11 | +use Hammerstone\Sidecar\Events\AfterFunctionsActivated; |
| 12 | +use Hammerstone\Sidecar\Events\AfterFunctionsDeployed; |
| 13 | +use Hammerstone\Sidecar\Events\BeforeFunctionsActivated; |
| 14 | +use Hammerstone\Sidecar\Events\BeforeFunctionsDeployed; |
| 15 | +use Hammerstone\Sidecar\Sidecar; |
| 16 | +use Hammerstone\Sidecar\Tests\Support\DeploymentTestFunction; |
| 17 | +use Illuminate\Support\Facades\Event; |
| 18 | +use Mockery; |
| 19 | + |
| 20 | +class ActivateTest extends BaseTest |
| 21 | +{ |
| 22 | + protected $lambda; |
| 23 | + |
| 24 | + protected function setUp(): void |
| 25 | + { |
| 26 | + parent::setUp(); |
| 27 | + |
| 28 | + Event::fake(); |
| 29 | + |
| 30 | + $this->lambda = $this->mock(LambdaClient::class); |
| 31 | + } |
| 32 | + |
| 33 | + public function mockListingVersions() |
| 34 | + { |
| 35 | + $this->lambda->shouldReceive('listVersionsByFunction') |
| 36 | + ->once() |
| 37 | + ->with([ |
| 38 | + 'FunctionName' => 'test-FunctionName', |
| 39 | + 'MaxItems' => 100, |
| 40 | + 'Marker' => null |
| 41 | + ]) |
| 42 | + ->andReturn([ |
| 43 | + 'Versions' => [[ |
| 44 | + 'FunctionName' => 'test-FunctionName', |
| 45 | + 'Version' => '10', |
| 46 | + ], [ |
| 47 | + 'FunctionName' => 'test-FunctionName', |
| 48 | + 'Version' => '11', |
| 49 | + ], [ |
| 50 | + 'FunctionName' => 'test-FunctionName', |
| 51 | + 'Version' => '12', |
| 52 | + ]] |
| 53 | + ]); |
| 54 | + } |
| 55 | + |
| 56 | + public function mockActivating() |
| 57 | + { |
| 58 | + $this->mockListingVersions(); |
| 59 | + |
| 60 | + $this->lambda->shouldReceive('deleteAlias')->once()->with([ |
| 61 | + 'FunctionName' => 'test-FunctionName', |
| 62 | + 'Name' => 'active', |
| 63 | + ]); |
| 64 | + |
| 65 | + $this->lambda->shouldReceive('createAlias')->once()->with([ |
| 66 | + 'FunctionName' => 'test-FunctionName', |
| 67 | + 'FunctionVersion' => '12', |
| 68 | + 'Name' => 'active', |
| 69 | + ]); |
| 70 | + } |
| 71 | + |
| 72 | + public function assertEvents($deployed = true, $activated = true) |
| 73 | + { |
| 74 | + if ($deployed) { |
| 75 | + Event::assertDispatched(BeforeFunctionsDeployed::class); |
| 76 | + Event::assertDispatched(AfterFunctionsDeployed::class); |
| 77 | + } else { |
| 78 | + Event::assertNotDispatched(BeforeFunctionsDeployed::class); |
| 79 | + Event::assertNotDispatched(AfterFunctionsDeployed::class); |
| 80 | + } |
| 81 | + |
| 82 | + if ($activated) { |
| 83 | + Event::assertDispatched(BeforeFunctionsActivated::class); |
| 84 | + Event::assertDispatched(AfterFunctionsActivated::class); |
| 85 | + } else { |
| 86 | + Event::assertNotDispatched(BeforeFunctionsActivated::class); |
| 87 | + Event::assertNotDispatched(AfterFunctionsActivated::class); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + /** @test */ |
| 92 | + public function it_should_activate_functions() |
| 93 | + { |
| 94 | + $this->artisan('sidecar:activate'); |
| 95 | + |
| 96 | + $this->assertEvents($deployed = false, $activated = true); |
| 97 | + } |
| 98 | + |
| 99 | + /** @test */ |
| 100 | + public function it_should_activate_functions_with_env() |
| 101 | + { |
| 102 | + $this->artisan('sidecar:activate', [ |
| 103 | + '--env' => 'faked' |
| 104 | + ]); |
| 105 | + |
| 106 | + $this->assertEquals('faked', Sidecar::getEnvironment()); |
| 107 | + |
| 108 | + $this->assertEvents($deployed = false, $activated = true); |
| 109 | + } |
| 110 | + |
| 111 | +} |
0 commit comments