Moto. Moka AWS

Il testing è una parte fondamentale del processo di sviluppo. A volte gli sviluppatori devono eseguire i test localmente, prima di effettuare il commit delle modifiche.
Se l'applicazione utilizza Amazon Web Services, python una libreria moto è perfetto per questo.
Moto. Moka AWS

Puoi vedere l'elenco completo delle risorse disponibili qui.
Su Github c'è un repository Hugo Picadomoto-server. Un'immagine pronta, avviala e utilizza. L'unico inconveniente è che, dopo l'avvio, nessuna AWS risorsa è stata ancora creata.

Beh, è abbastanza facile da risolvere.

Poiché durante l'avvio è necessario specificare il tipo di servizio (env variabile MOTO_SERVICE), ci rimane da descrivere la creazione della risorsa.

Modifichiamo un po' start.sh:

Invece di

moto_server $MOTO_SERVICE -H $MOTO_HOST -p $MOTO_PORT

Inseriamo:

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

Il file finale diventa:

start.sh

#!/bin/sh

# validate required input
if [ -z "$MOTO_SERVICE" ]; then
  echo "Please define AWS service to run with Moto Server (e.g. 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

Creiamo una nuova immagine e la pushiamo nel nostro registry.

Poi, scriviamo uno script di inizializzazione della risorsa, ad esempio SWF domain, utilizzando la libreria per interagire con AWS — boto3:

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="Dominio di test SWF",
        workflowExecutionRetentionPeriodInDays="10"
    )
except ClientError as e:
    print "Il dominio esiste già: ", e.response.get("Error", {}).get("Code")

response = client.list_domains(
    registrationStatus='REGISTERED',
    maximumPageSize=123,
    reverseOrder=True|False
)

print 'Pronto'

La logica è la seguente:

  • Montiamo il nostro script all'avvio /opt/init/bootstrap.py.
  • Se il file è montato, verrà eseguito.
  • Se il file non esiste, partirà semplicemente un moto-server nudo.

E possiamo mockare un'intera risorsa avviando un singolo container:

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

Proviamo in modalità interattiva:

Moto. Moka AWS

Funziona!

In questo modo, possiamo creare un docker-compose.yml che aiuterà a risparmiare tempo nel testare le modifiche:

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

Non solo sul laptop dello sviluppatore si può utilizzare questo approccio. I test preliminari con i mock dopo la build aiutano ad eliminare potenziali problemi durante l'esecuzione negli ambienti dev*.

Link:

Repo Motocker — github.com/picadoh/motocker
Repo Moto — github.com/spulec/moto
Documentazione Boto3 — boto3.amazonaws.com/v1/documentation/api/latest/index.html

Fonte: habr.com

Acquista hosting affidabile per siti web con protezione DDoS, VPS VDS server 🔥 Acquista hosting affidabile per siti web con protezione DDoS, VPS VDS server | ProHoster