This repository has been archived by the owner on Mar 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: muhamadto <[email protected]>
- Loading branch information
Showing
28 changed files
with
2,721 additions
and
181 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,42 @@ | ||
# Licensed to Muhammad Hamadto | ||
## Licensed to Muhammad Hamadto | ||
# | ||
## Licensed under the Apache License, Version 2.0 (the "License"); | ||
## you may not use this file except in compliance with the License. | ||
## You may obtain a copy of the License at | ||
## http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
## See the NOTICE file distributed with this work for additional information regarding copyright ownership. | ||
# | ||
## Unless required by applicable law or agreed to in writing, software | ||
## distributed under the License is distributed on an "AS IS" BASIS, | ||
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
## See the License for the specific language governing permissions and | ||
## limitations under the License. | ||
|
||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
# See the NOTICE file distributed with this work for additional information regarding copyright ownership. | ||
|
||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
name: "Deploy to AWS" | ||
#run-name: Deploy to ${{ inputs.deploy_target }} by @${{ github.actor }} | ||
|
||
name: Publish package to GitHub Packages | ||
on: | ||
release: | ||
types: [ published ] | ||
types: [published] | ||
|
||
jobs: | ||
release: | ||
publish: | ||
runs-on: ubuntu-latest | ||
container: | ||
image: ghcr.io/muhamadto/spring-native-amazonlinux2-base:17-amazonlinux2 | ||
options: --user=worker:ci | ||
permissions: | ||
id-token: write | ||
contents: read | ||
env: | ||
ENV: dev | ||
COST_CENTRE: coffeebeans-core | ||
AWS_DEFAULT_REGION: 'ap-southeast-2' | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
steps: | ||
- name: Configure AWS credentials | ||
uses: aws-actions/configure-aws-credentials@master | ||
- uses: actions/checkout@v3 | ||
- name: Set up Maven Central Repository | ||
uses: actions/setup-java@v3 | ||
with: | ||
role-to-assume: ${{ secrets.ROLE_ARN }} | ||
duration_seconds: 3600 | ||
role-session-name: github-actions-example-lambda | ||
aws-region: ap-southeast-2 | ||
- name: Checkout repository | ||
uses: actions/checkout@v3 | ||
- name: Cache Maven packages | ||
uses: actions/cache@v3 | ||
with: | ||
path: ~/.m2 | ||
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }} | ||
restore-keys: ${{ runner.os }}-m2 | ||
- name: Build with Maven | ||
java-version: '17' | ||
distribution: 'temurin' | ||
server-id: ossrh | ||
server-username: MAVEN_USERNAME | ||
server-password: MAVEN_PASSWORD | ||
gpg-private-key: ${{ secrets.MAVEN_GPG_PRIVATE_KEY }} | ||
gpg-passphrase: MAVEN_GPG_PASSPHRASE | ||
- name: Publish package to Maven Central | ||
run: mvn --batch-mode deploy -Ppublisher | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} | ||
ENV: ${{ env.ENV }} | ||
COST_CENTRE: ${{ env.COST_CENTRE }} | ||
run: ./mvnw -ntp -Pnative clean package -DskipTests | ||
- name: cdk diff | ||
uses: noverant/aws-cdk-github-actions@v1 | ||
with: | ||
cdk_subcommand: 'diff' | ||
actions_comment: false | ||
- name: cdk deploy | ||
uses: noverant/aws-cdk-github-actions@v1 | ||
with: | ||
cdk_subcommand: 'deploy' | ||
cdk_args: '--require-approval never' | ||
actions_comment: false | ||
MAVEN_USERNAME: ${{ secrets.OSSRH_USERNAME }} | ||
MAVEN_PASSWORD: ${{ secrets.OSSRH_TOKEN }} | ||
MAVEN_GPG_PASSPHRASE: ${{ secrets.MAVEN_GPG_PASSPHRASE }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,124 @@ | ||
# cdk-assertions | ||
# cdk-fluent-assertions | ||
|
||
AssertJ-based fluent assertions for AWS CDK testing | ||
# cdk-fluent-assertions | ||
|
||
[![CodeQL](https://github.com/muhamadto/cdk-fluent-assertions/actions/workflows/codeql-analysis.yml/badge.svg)](https://github.com/muhamadto/cdk-fluent-assertions/actions/workflows/codeql-analysis.yml) | ||
[![Build](https://github.com/muhamadto/cdk-fluent-assertions/actions/workflows/build.yml/badge.svg)](https://github.com/muhamadto/cdk-fluent-assertions/actions/workflows/build.yml) | ||
[![Publish package to GitHub Packages](https://github.com/muhamadto/cdk-fluent-assertions/actions/workflows/release.yml/badge.svg?event=release)](https://github.com/muhamadto/cdk-fluent-assertions/actions/workflows/release.yml) | ||
|
||
AssertJ-like fluent assertions for AWS CDK testing | ||
|
||
### AWS CDK testing framework | ||
|
||
AWS offers a framework to verify that your stack is well-formed and that it matches your expectations. This framework can be found [here](https://docs.aws.amazon.com/cdk/latest/guide/testing.html). | ||
|
||
Here is an example to very the stack contains a specific role: | ||
|
||
|
||
```java | ||
|
||
template.hasResourceProperties("AWS::IAM::Role", Match.objectEquals( | ||
Collections.singletonMap("AssumeRolePolicyDocument", Map.of( | ||
"Version", "2012-10-17", | ||
"Statement", Collections.singletonList(Map.of( | ||
"Action", "sts:AssumeRole", | ||
"Effect", "Allow", | ||
"Principal", Collections.singletonMap( | ||
"Service", Collections.singletonMap( | ||
"Fn::Join", Arrays.asList( | ||
"", | ||
Arrays.asList("states.", Match.anyValue(), ".amazonaws.com") | ||
) | ||
) | ||
) | ||
)) | ||
)) | ||
)); | ||
``` | ||
|
||
### AWS CDK fluent testing library | ||
While working on a small [open-source project](https://github.com/muhamadto/cdk-fluent-assertions), I was varying a CDK stack containing some resources. I noticed that the tests written using the AWS framework were verbose and presented challenges in terms of readability and maintainability. In response, I decided to create a small library to make it easier to read, write and maintain tests. | ||
|
||
To illustrate, an equivalent representation to the aforementioned example can be found below: as follows" | ||
```java | ||
CDKStackAssert.assertThat(template) | ||
.containsRoleWithManagedPolicyArn(managedPolicyArn) | ||
.hasAssumeRolePolicyDocument("states.amazonaws.com", null, "Allow", "2012-10-17", "sts:AssumeRole"); | ||
``` | ||
|
||
#### How to use it | ||
|
||
* The library is available on Maven Central. To use it, add the following dependency to your project: | ||
|
||
```xml | ||
<dependency> | ||
<groupId>cloud.pianola</groupId> | ||
<artifactId>cdk-fluent-assertions</artifactId> | ||
<version>1.0.0</version> | ||
<scope>test</scope> | ||
</dependency> | ||
``` | ||
|
||
* Create a class `TemplateSupport` in your testing packages. This class will be used to load the template and create the `CDKStackAssert` object. | ||
|
||
```java | ||
public abstract class TemplateSupport { | ||
|
||
public static final String ENV = "test"; | ||
public static final String TEST_CDK_BUCKET = "test-cdk-bucket"; | ||
public static final String QUALIFIER = "test"; | ||
static Template template; | ||
private static final String STACK_NAME = "example-lambda-function-test-stack"; | ||
|
||
@TempDir | ||
private static Path TEMP_DIR; | ||
|
||
@BeforeAll | ||
static void initAll() throws IOException { | ||
final Path lambdaCodePath = TestLambdaUtils.getTestLambdaCodePath(TEMP_DIR); | ||
|
||
final Map<String, String> tags = createTags(ENV, TAG_VALUE_COST_CENTRE); | ||
final App app = new App(); | ||
final ExampleLambdaStack stack = StackUtils.createStack(app, STACK_NAME, lambdaCodePath.toString(), QUALIFIER, TEST_CDK_BUCKET, ENV); | ||
|
||
tags.entrySet().stream() | ||
.filter(tag -> Objects.nonNull(tag.getValue())) | ||
.forEach(tag -> Tags.of(app).add(tag.getKey(), tag.getValue())); | ||
|
||
template = Template.fromStack(stack); | ||
} | ||
|
||
@AfterAll | ||
static void cleanup() { | ||
template = null; | ||
} | ||
} | ||
``` | ||
* Extend the `TemplateSupport` class in your test classes and use the `CDKStackAssert` object to verify your stack. | ||
|
||
```java | ||
class LambdaTest extends TemplateSupport { | ||
|
||
public static final String TEST = "test"; | ||
|
||
@Test | ||
void should_have_lambda_function() { | ||
|
||
CDKStackAssert.assertThat(template) | ||
.containsFunction("example-lambda-function") | ||
.hasHandler("org.springframework.cloud.function.adapter.aws.FunctionInvoker::handleRequest") | ||
.hasCode("test-cdk-bucket", "(.*).zip") | ||
.hasRole("examplelambdafunctionrole(.*)") | ||
.hasDependency("examplelambdafunctionrole(.*)") | ||
.hasDependency("examplelambdafunctionroleDefaultPolicy(.*)") | ||
.hasTag("COST_CENTRE", TAG_VALUE_COST_CENTRE) | ||
.hasTag("ENV", TEST) | ||
.hasEnvironmentVariable("ENV", TEST) | ||
.hasEnvironmentVariable("SPRING_PROFILES_ACTIVE", TEST) | ||
.hasDescription("Lambda example") | ||
.hasMemorySize(512) | ||
.hasRuntime("provided.al2") | ||
.hasTimeout(3); | ||
} | ||
} | ||
``` |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
lombok.addLombokGeneratedAnnotation=true |
Oops, something went wrong.