Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mock AWS with moto #52

Merged
merged 4 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@
**/.mypy_cache/
**/.ruff_cache/
**/.vscode/
**/.moto_recording
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ test_app/database/*
test_app/cerbos/*
test_app/support/*
test_app/test_infra/*
test_app/.moto_recording

# temp files
/tmp/*
Expand Down
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ codegen-tests: codegen ## Run tests
.PHONY: gha-setup
gha-setup: ## Set up the environment in CI
docker swarm init

touch test_app/.moto_recording
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add this file to the .dockerignore and .gitignore files as well?


.PHONY: build ## Build python packages and docker images
build:
Expand All @@ -87,6 +87,7 @@ dev:
.PHONY: clean
clean: ## Remove all build artifacts
rm -rf dist
rm -rf test_app/.moto_recording
$(docker_compose) down
$(MAKE_TEST_APP) clean

Expand Down
5 changes: 0 additions & 5 deletions platformics/codegen/templates/cerbos/policies/entity.yaml.j2
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ resourcePolicy:
derivedRoles:
- project_member

- actions: ['view', 'update']
effect: EFFECT_ALLOW
derivedRoles:
- single_user_owner

- actions: ['download']
effect: EFFECT_ALLOW
derivedRoles:
Expand Down
9 changes: 8 additions & 1 deletion test_app/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,22 @@ init:
# $(docker_compose_run) $(CONTAINER) ruff check --fix .
$(docker_compose_run) $(APP_CONTAINER) sh -c 'strawberry export-schema main:schema > /app/api/schema.graphql'
docker compose up -d
$(MAKE) seed-moto
sleep 5 # wait for the app to reload after having files updated.
docker compose exec $(APP_CONTAINER) python3 -m sgqlc.introspection --exclude-deprecated --exclude-description http://localhost:9009/graphql api/schema.json

.PHONY: seed-moto
seed-moto: ## Seed the moto db by running the ./bin/seed_moto.sh script
touch .moto_recording
./bin/seed_moto.sh

.PHONY: clean
clean: ## Remove all codegen'd artifacts.
rm -rf api
rm -rf cerbos
rm -rf support
rm -rf database
rm -f .moto_recording
rm -rf test_infra
$(docker_compose) --profile '*' down

Expand Down Expand Up @@ -81,7 +88,7 @@ restart: ## Restart the GQL service
stop: ## Stop the local dev environment.
$(docker_compose) --profile '*' stop

.PHONY: local-seed
.PHONY: seed
seed: ## Seed the dev db with a reasonable set of starting data.
$(docker_compose) exec $(APP_CONTAINER) python3 scripts/seed.py

Expand Down
12 changes: 12 additions & 0 deletions test_app/bin/init_moto.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

# Script to initialize moto server; runs inside the motoserver container

# Launch moto server
moto_server --host 0.0.0.0 --port $MOTO_PORT &

# Initialize data once server is ready
sleep 1 && curl -X POST "http://localhost:${MOTO_PORT}/moto-api/recorder/replay-recording"

# Go back to moto server
wait
14 changes: 14 additions & 0 deletions test_app/bin/seed_moto.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash

# Script to seed moto server; runs outside the motoserver container for development

aws="aws --endpoint-url=http://localhost:4000"
export AWS_ACCESS_KEY_ID=test
export AWS_SECRET_ACCESS_KEY=test
export AWS_REGION=us-west-2
export S3_BUCKET=local-bucket

if aws s3 ls "s3://$S3_BUCKET" 2>&1 | grep -q 'NoSuchBucket'
then
$aws s3 mb $S3_BUCKET
fi
20 changes: 20 additions & 0 deletions test_app/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,24 @@
services:
# Mock boto with persistence.
# To use it from the CLI: aws --endpoint-url=http://localhost:4000 s3 ls
# To reset all services without restarting the container: curl -X POST http://localhost:4000/moto-api/reset
motoserver:
image: motoserver/moto:latest
ports:
- "4000:4000"
environment:
- MOTO_PORT=4000
- MOTO_ENABLE_RECORDING=True
- MOTO_S3_CUSTOM_ENDPOINTS=http://motoserver.platformics:4000
- S3_IGNORE_SUBDOMAIN_BUCKETNAME=True
- MOTO_DOCKER_NETWORK_NAME=platformics
- MOTO_DOCKER_NETWORK_MODE=overlay
volumes:
- .moto_recording:/moto/moto_recording
- ./bin:/moto/bin
- "/var/run/docker.sock:/var/run/docker.sock"
entrypoint: ["/bin/bash"]
command: ["/moto/bin/init_moto.sh"]
platformics-db:
image: postgres:15
restart: always
Expand Down
3 changes: 2 additions & 1 deletion test_app/tests/test_file_uploads.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ async def test_upload_process(
}}
}}
"""
output = await gql_client.query(mutation, member_projects=member_projects)

output = await gql_client.query(mutation, member_projects=member_projects, user_id=user_id)
file_id = output["data"]["uploadFile"]["file"]["id"]
credentials = output["data"]["uploadFile"]["credentials"]

Expand Down
Loading