Moto. Mock aws

Les tests sont une partie importante du processus de développement. Et parfois, les développeurs doivent exécuter des tests localement, avant de valider les modifications.

Si votre application utilise Amazon Web Services , moto de bibliothèque python est la chose parfaite pour cela.



La liste complète de la couverture de la mise en œuvre est ici .

J'ai trouvé le repo de Hugo Picado sur Github - moto-server . Image prête à démarrer. La seule nuance est qu'après le lancement, aucune ressource AWS ne sera créée.

Eh bien, c'est assez facile à réparer.

Alors que nous devons définir la variable env MOTO_SERVICE pour spécifier le type de ressource au lancement, il ne nous reste plus qu'à définir la création de la ressource.

Mettez à jour start.sh :

Remplacer

moto_server $MOTO_SERVICE -H $MOTO_HOST -p $MOTO_PORT 

avec

 if [ -f /opt/init/bootstrap.py ]; then moto_server $MOTO_SERVICE -H $MOTO_HOST -p $MOTO_PORT & (sleep 5 && echo "Executing bootstrap script." && python /opt/init/bootstrap.py) else moto_server $MOTO_SERVICE -H $MOTO_HOST -p $MOTO_PORT fi wait 

Le fichier doit ressembler à:

start.sh
 #!/bin/sh # validate required input if [ -z "$MOTO_SERVICE" ]; then echo "Please define AWS service to run with Moto Server (eg s3, ec2, etc)" exit 1 fi # setting defaults for optional input if [ -z "$MOTO_HOST" ]; then MOTO_HOST="0.0.0.0" fi if [ -z "$MOTO_PORT" ]; then MOTO_PORT="5000" fi echo "Starting service $MOTO_SERVICE at $MOTO_HOST:$MOTO_PORT" if [ -f /opt/init/bootstrap.py ]; then moto_server $MOTO_SERVICE -H $MOTO_HOST -p $MOTO_PORT & (sleep 5 && echo "Executing bootstrap script." && python /opt/init/bootstrap.py) else moto_server $MOTO_SERVICE -H $MOTO_HOST -p $MOTO_PORT fi # Prevent container from exiting when bootstrap.py finishing wait 


Créez une nouvelle image et envoyez-la dans votre registre.

Plus loin, permet de créer un script d'initialisation des ressources, en utilisant la bibliothèque pour travailler avec AWS - boto3. Par exemple, domaine SWF :

bootstrap_swf.py
 import boto3 from botocore.exceptions import ClientError import os os.environ["AWS_ACCESS_KEY_ID"] = "fake" os.environ["AWS_SECRET_ACCESS_KEY"] = "fake" client = boto3.client('swf', region_name='us-west-2', endpoint_url='http://localhost:5000') try: client.register_domain( name='test-swf-mock-domain', description="Test SWF domain", workflowExecutionRetentionPeriodInDays="10" ) except ClientError as e: print "Domain already exists: ", e.response.get("Error", {}).get("Code") response = client.list_domains( registrationStatus='REGISTERED', maximumPageSize=123, reverseOrder=True|False ) print 'Ready' 


Logiquement:

  • Montez notre script d'amorçage sur /opt/init/bootstrap.py
  • Si le fichier est monté - il sera exécuté
  • Sinon - le seul moto-serveur sera lancé

Ainsi, nous pouvons simuler l'ensemble de la ressource en exécutant simplement un seul conteneur:

 docker run --name swf -d \ -e MOTO_SERVICE=swf \ -e MOTO_HOST=0.0.0.0 \ -e MOTO_PORT=5000 \ -p 5001:5000 \ -v /tmp/bootstrap_swf.py:/opt/init/bootstrap.py \ -i awesome-repo.com/moto-server:latest 

Essayons en mode interactif:



Ça marche!

Ainsi, nous pouvons créer docker-compose.yml qui peut aider à gagner du temps pour tester les changements:

docker-compose.yml
 version: '3' services: s3: image: picadoh/motocker environment: - MOTO_SERVICE=s3 - MOTO_HOST=10.0.1.2 ports: - "5002:5000" networks: motonet: ipv4_address: 10.0.1.2 volumes: - /tmp/bootstrap_s3.py:/opt/init/bootstrap.py swf: image: picadoh/motocker environment: - MOTO_SERVICE=swf - MOTO_HOST=10.0.1.3 ports: - "5001:5000" networks: motonet: ipv4_address: 10.0.1.3 volumes: - /tmp/bootstrap_swf.py:/opt/init/bootstrap.py ec2: image: picadoh/motocker environment: - MOTO_SERVICE=ec2 - MOTO_HOST=10.0.1.4 ports: - "5003:5000" networks: motonet: ipv4_address: 10.0.1.4 volumes: - /tmp/bootstrap_ec2.py:/opt/init/bootstrap.py networks: motonet: driver: bridge ipam: config: - subnet: 10.0.0.0/16 


Soit dit en passant, nous pouvons utiliser cette approche non seulement sur l'ordinateur portable du développeur. Des tests préliminaires avec des maquettes après la construction nous aideront à nous débarrasser des problèmes possibles sur les environnements dev *.

Liens:

Dépôt Motocker - github.com/picadoh/motocker
Dépôt de moto - github.com/spulec/moto
Documents Boto3 - boto3.amazonaws.com/v1/documentation/api/latest/index.html

Source: https://habr.com/ru/post/fr454892/


All Articles