Skip to content

Commit b1066b5

Browse files
committed
Merge branch 'main' of github.com:xp-forge/lambda
2 parents c48c6b0 + d6a2c10 commit b1066b5

File tree

4 files changed

+54
-9
lines changed

4 files changed

+54
-9
lines changed

ChangeLog.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,17 @@ AWS Lambda change log
33

44
## ?.?.? / ????-??-??
55

6+
## 3.2.0 / 2021-11-15
7+
8+
* Merged PR #14: Add ability to pass environment variables to tested lambda
9+
(@thekid)
10+
11+
## 3.1.0 / 2021-11-11
12+
13+
* Enabled SimpleXML extension in order to be able to support AWS SDK
14+
see https://github.com/xp-forge/lambda/issues/8#issuecomment-966308720
15+
(@thekid)
16+
617
## 3.0.2 / 2021-10-21
718

819
* Made library compatible with new major release of `xp-forge/json`

src/main/php/xp/lambda/Dockerfile.runtime

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ RUN cd php-src-php-${php_version} && ./buildconf --force && ./configure \
2626
--without-sqlite3 \
2727
--with-openssl \
2828
--enable-bcmath \
29-
--disable-simplexml \
3029
--disable-pdo && \
3130
make -j $(nproc) all install
3231

src/main/php/xp/lambda/Runner.class.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
* ```sh
2525
* $ xp lambda test Greet '{"name":"Test"}'
2626
* ```
27+
* - Test lambda, pass environment variables:
28+
* ```sh
29+
* $ xp lambda test -e PROFILE=prod Audit
30+
* ```
2731
* - Package single file in `function.zip` file for deployment:
2832
* ```sh
2933
* $ xp lambda package Greet.class.php
@@ -63,7 +67,7 @@ private static function command(string $name, array $args): object {
6367
switch ($command) {
6468
case 'package': return new PackageLambda(new Path('function.zip'), new Sources(new Path('.'), [...$args, 'vendor']));
6569
case 'runtime': return new CreateRuntime(self::resolve($version), new Path('runtime-%s.zip'), in_array('-b', $args));
66-
case 'test': return new TestLambda(self::resolve($version), new Path('.'), $args[0] ?? 'Handler', $args[1] ?? '{}');
70+
case 'test': return new TestLambda(self::resolve($version), new Path('.'), $args);
6771
default: return new DisplayError('Unknown command "'.$args[0].'"');
6872
}
6973
}

src/main/php/xp/lambda/TestLambda.class.php

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,50 @@
66
class TestLambda {
77
use Docker;
88

9-
private $version, $path, $handler, $payload;
9+
private $version, $path;
10+
private $environment= [];
11+
private $handler= 'Handler';
12+
private $payload= '{}';
1013

11-
public function __construct(string $version, Path $path, string $handler, string $payload) {
14+
public function __construct(string $version, Path $path, array $args) {
1215
$this->version= $version;
1316
$this->path= $path->asRealpath();
14-
$this->handler= $handler;
15-
$this->payload= $payload;
17+
18+
// Separate `-e NAME=VALUE` from handler and payload
19+
for ($i= 0, $s= sizeof($args); $i < $s; $i++) {
20+
if ('-e' === $args[$i]) {
21+
$this->environment[]= $args[++$i];
22+
} else {
23+
$this->handler= $args[$i];
24+
$this->payload= $args[++$i] ?? '{}';
25+
break;
26+
}
27+
}
28+
}
29+
30+
/** Passes multiple arguments via command line */
31+
private function pass($flag, $list) {
32+
$r= [];
33+
foreach ($list as $element) {
34+
$r[]= $flag;
35+
$r[]= $element;
36+
}
37+
return $r;
1638
}
1739

40+
/** Runs this command */
1841
public function run(): int {
19-
$test= $this->image('test', $this->version, ['runtime' => []])['test'];
20-
if (null === $test) return 1;
42+
$image= $this->image('test', $this->version, ['runtime' => []])['test'];
43+
if (null === $image) return 1;
2144

22-
return $this->passthru(['run', '--rm', '-v', "{$this->path}:/var/task:ro", $test, $this->handler, $this->payload]);
45+
return $this->passthru([
46+
'run',
47+
'--rm',
48+
'-v', "{$this->path}:/var/task:ro",
49+
...$this->pass('-e', $this->environment),
50+
$image,
51+
$this->handler,
52+
$this->payload
53+
]);
2354
}
2455
}

0 commit comments

Comments
 (0)