Skip to content

Commit

Permalink
feat: working pulumi test case (#1)
Browse files Browse the repository at this point in the history
* feat: working pulumi test case

* test: print directory

* test: print directory

* test: print directory

* feat: add logging to pulumi and output directory artifact for all test steps

* fix: unique output per test step script name

* fix: clean branch name for output artifact

* fix: script name passing in workflow

* fix: more output changes

* fix: output path

* feat: create stack if not exists, let's try it out

* fix: reduce verbosity
  • Loading branch information
acrois authored Feb 4, 2024
1 parent 19ba567 commit 510c3ae
Show file tree
Hide file tree
Showing 12 changed files with 163 additions and 26 deletions.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ AWS_DEFAULT_REGION=us-east-1
AWS_ENDPOINT=http://localhost:4566
AWS_ENDPOINT_URL=http://localhost:4566
PULUMI_CONFIG_PASSPHRASE=
PULUMI_STACK_NAME=dev
15 changes: 14 additions & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ jobs:
DEFAULT_REGION: "us-east-1"
AWS_ACCESS_KEY_ID: "test"
AWS_SECRET_ACCESS_KEY: "test"
AWS_REGION: us-east-1
AWS_DEFAULT_REGION: us-east-1
steps:
- name: Checkout Repository
uses: actions/checkout@v4
Expand All @@ -39,11 +41,22 @@ jobs:
with:
cache: 'pip'
python-version: '3.x'
- name: Set up Bun
- id: setup-env
name: Set up Bun
run: |
curl -fsSL https://bun.sh/install | bash
echo "$HOME/.bun/bin" >> $GITHUB_PATH
echo "TEST_NAME=${TEST_SCRIPT//\//-}" >> $GITHUB_OUTPUT
env:
TEST_SCRIPT: ${{ matrix.script }}
- name: Install AWS CLI
run: pip install awscli pulumi-local terraform-local awscli-local
- name: Execute Script
run: ./${{ matrix.script }}
- name: Upload Output
if: always()
uses: actions/upload-artifact@v4
with:

name: output-${{ steps.setup-env.outputs.TEST_NAME }}
path: ./output
25 changes: 25 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Remote Attach",
"type": "debugpy",
"request": "attach",
"connect": {
"host": "localhost",
"port": 5678
},
"preLaunchTask": "Run pulumi up and wait to start",
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "."
}
],
"justMyCode": true
}
]
}
51 changes: 51 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "pulumi up",
"type": "shell",
"isBackground": true,
"command": "./tests/pulumi_bucket.js",
"presentation": {
"reveal": "silent"
},
// This task is run before some debug tasks.
// Problem is, it's a watch script, and since it never exits, VSCode
// complains. All this is needed so VSCode just lets it run.
"problemMatcher": [
{
"pattern": [
{
"regexp": ".",
"file": 1,
"location": 2,
"message": 3
}
],
"background": {
"activeOnStart": true,
"beginsPattern": ".",
"endsPattern": ".",
}
}
]
},
{
"label": "Wait 35 seconds",
"type": "shell",
"command": "sleep 35",
"presentation": {
"reveal": "silent"
}
},
{
"label": "Run pulumi up and wait to start",
"dependsOn": [
"pulumi up",
"Wait 35 seconds"
]
}
]
}
20 changes: 13 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,24 @@ This repository demonstrates how to use Pulumi, Terraform, and AWS CloudFormatio

## Prerequisites

- Docker and Docker Compose
- Pulumi CLI (for Pulumi)
- Terraform CLI (for Terraform)
- AWS CLI (for CloudFormation and testing)
- [Bun](https://bun.sh/docs/installation)
- [Docker and Docker Compose](https://docs.docker.com/get-docker/)
- [Pulumi CLI](https://www.pulumi.com/docs/install/)
- [Terraform CLI](https://developer.hashicorp.com/terraform/tutorials/aws-get-started/install-cli)
- [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html)
- About 3:50

## Setup

1. Clone this repository.
2. Navigate to the repository directory.
3. Run the following instructions.

## Running LocalStack

1. Start LocalStack using Docker Compose:
```sh
docker-compose up -d
docker compose up -d
```

2. Verify that LocalStack is running:
Expand Down Expand Up @@ -60,5 +63,8 @@ Run the test script:

## Additional Notes

- This demo uses fixed AWS credentials (`test`) as specified in the `.env` file.
- Ensure that the AWS region and endpoint in your IaC scripts match those in LocalStack.
- This demo uses fixed AWS credentials (`test` for access and secret key) as specified in the `.env` file.
- Ensure that the AWS region and endpoint in your IaC scripts match those in LocalStack.
- Instead of explicitly passing credentials to several specific tool config formats, we will use env vars to set them across all tools.
- [Tests](./tests/) are _anything that is executable_ that returns a non-zero (e.g. 1) exit code on failure and zero (0) on success.
- Note that for Bun, it is important to run the script from the project root, and not go into the test directory, because Bun will get the `.env` file [from the project root](https://bun.sh/guides/runtime/set-env)
16 changes: 12 additions & 4 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
version: '3.8'
services:
localstack:
image: localstack/localstack
image: localstack/localstack:3.0
ports:
- "4566:4566"
env_file:
- .env
environment:
- SERVICES=cloudformation,s3,ec2
DEBUG: 1
LS_LOG: trace
SERVICES: cloudformation,s3,ec2
DOCKER_HOST: unix:///var/run/docker.sock
volumes:
- ./localstack/bootstrap:/opt/bootstrap/
- ./localstack/data:/tmp/localstack
- localstack:/var/lib/localstack
- /var/run/docker.sock:/var/run/docker.sock
- ./localstack/init:/etc/localstack/init
volumes:
localstack:

2 changes: 2 additions & 0 deletions localstack/data/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
2 changes: 2 additions & 0 deletions output/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
5 changes: 3 additions & 2 deletions pulumi/Pulumi.dev.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
encryptionsalt: v1:JTcJ77quffg=:v1:QeePUKMMrvZezJ6+:Rad3OPpP4QAU7GlXY3ksPjprjM4P1g==
encryptionsalt: v1:7tH3xgVBUmQ=:v1:ApDf7SN8kcVXnxPT:+JR4IH/5CmKW6TPc/dbUqMH7eCr/xw==
config:
cloud:provider: aws
aws:accessKey: test
aws:endpoints:
- accessanalyzer: http://localhost:4566
Expand Down Expand Up @@ -275,5 +276,5 @@ config:
aws:region: us-east-1
aws:s3UsePathStyle: 'true'
aws:secretKey: test
aws:skipCredentialsValidation: 'true'
aws:skipCredentialsValidation: "true"
aws:skipRequestingAccountId: 'true'
39 changes: 30 additions & 9 deletions pulumi/__main__.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,42 @@
import pulumi
import pulumi_aws as aws
import os
# import debugpy

# # Load environment variables
# config = pulumi.Config()
# for a in os.environ:
# print('Var: ', a, 'Value: ', os.getenv(a))

# debugpy.listen(("0.0.0.0", 5678))
# print("debugpy is listening, attach by pressing F5 or ►")

# debugpy.wait_for_client()
# print("Attached to debugpy!")

# Load environment variables
config = pulumi.Config()
# aws_region = config.require('aws_region')
# aws_endpoint = config.require('aws_endpoint')

# # Configure AWS provider to use LocalStack
# aws_provider = aws.Provider('localstack', region=aws_region,
# endpoint=aws_endpoint, access_key='test',
# secret_key='test')

# Configure AWS provider to use LocalStack
aws_provider = aws.Provider('localstack')
# aws_provider = aws.Provider(
# 'localstack',
# region=os.getenv('AWS_REGION'),
# # endpoint=os.getenv('AWS_ENDPOINT'),
# access_key=os.getenv('AWS_ACCESS_KEY_ID'),
# secret_key=os.getenv('AWS_SECRET_ACCESS_KEY'),
# )

# aws_provider = aws.Provider('localstack')

# opts = pulumi.ResourceOptions(provider=aws_provider)

# def dump(obj):
# for attr in dir(obj):
# print("obj.%s = %r" % (attr, getattr(obj, attr)))
# dump(opts)

# Create an S3 bucket
bucket = aws.s3.Bucket('my-pulumi-bucket', opts=pulumi.ResourceOptions(provider=aws_provider))
bucket = aws.s3.Bucket('my-pulumi-bucket')

# Export the bucket name
pulumi.export('bucket_name', bucket.id)
1 change: 1 addition & 0 deletions pulumi/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pulumi
pulumi_aws
pulumi-local
debugpy
12 changes: 9 additions & 3 deletions tests/pulumi_bucket.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ console.log(`AWS CLI: ${awsCliVersion}`)

if (!awsCliVersion) throw 'Invalid AWS CLI version.';

// await $`env`

$.cwd('./pulumi')

const pipResults = await $`pip install -r requirements.txt`
Expand All @@ -18,13 +20,17 @@ console.log(`Pulumi: ${pulumiVersion}`)

if (!pulumiVersion) throw 'Invalid Pulumi version.'

await $`ls`
await $`pulumi login --local`
const pulumiStackResult = await $`pulumi stack select -c dev`
if (pulumiStackResult.exitCode !== 0) throw pulumiStackResult.stderr.toString()

const pulumiResults = await $`pulumi up -s dev -v 3 --yes --non-interactive --logtostderr --logflow --tracing=file:../output/pulumi-up.trace`

const pulumiResults = await $`pulumilocal up --yes --non-interactive`
if (pulumiResults.exitCode !== 0) throw pulumiResults.stderr.toString()

const bucketName = (await $`pulumilocal stack output bucket_name`.text()).trim()
const bucketName = (await $`pulumi stack output bucket_name`.text()).trim()
console.log(`Checking if S3 bucket ${bucketName} exists...`)

const bucketResults = await $`aws s3 ls s3://${bucketName}`
const bucketResults = await $`awslocal s3 ls s3://${bucketName}`
if (bucketResults.exitCode !== 0) throw bucketResults.stderr.toString()

0 comments on commit 510c3ae

Please sign in to comment.