Как я могу издеваться над ECS с помощью мото? - PullRequest
0 голосов
/ 02 марта 2020

Я хочу создать фиктивный кластер ECS, но, похоже, он не работает должным образом. Хотя что-то подделано (я не получаю ошибку учетных данных), оно, похоже, не «сохраняет» кластер.

Как создать фиктивный кластер с moto?

MVCE

foo.py

import boto3


def print_clusters():
    client = boto3.client("ecs")
    print(client.list_clusters())
    return client.list_clusters()["clusterArns"]

test_foo.py

import boto3
import pytest
from moto import mock_ecs

import foo

@pytest.fixture
def ecs_cluster():
    with mock_ecs():
        client = boto3.client("ecs", region_name="us-east-1")
        response = client.create_cluster(clusterName="test_ecs_cluster")
        yield client


def test_foo(ecs_cluster):
    assert foo.print_clusters() == ["test_ecs_cluster"]

Что происходит

$ pytest test_foo.py
Test session starts (platform: linux, Python 3.8.1, pytest 5.3.5, pytest-sugar 0.9.2)
rootdir: /home/math/GitHub
plugins: black-0.3.8, mock-2.0.0, cov-2.8.1, mccabe-1.0, flake8-1.0.4, env-0.6.2, sugar-0.9.2, mypy-0.5.0
collecting ... 

―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― test_foo ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――

ecs_cluster = <botocore.client.ECS object at 0x7fe9b0c73580>

    def test_foo(ecs_cluster):
>       assert foo.print_clusters() == ["test_ecs_cluster"]
E       AssertionError: assert [] == ['test_ecs_cluster']
E         Right contains one more item: 'test_ecs_cluster'
E         Use -v to get the full diff

test_foo.py:19: AssertionError
---------------------------------------------------------------------------------------------------------------------------------- Captured stdout call ----------------------------------------------------------------------------------------------------------------------------------
{'clusterArns': [], 'ResponseMetadata': {'HTTPStatusCode': 200, 'HTTPHeaders': {'server': 'amazon.com'}, 'RetryAttempts': 0}}

 test_foo.py ⨯

То, что я ожидал

Я ожидал список кластерных ARN с одним элементом (не тот, что в операторе assert, а ARN). Но список пуст.

1 Ответ

0 голосов
/ 07 апреля 2020

При создании кластера вы используете фиктивный клиент ECS.
При перечислении кластеров вы создаете нового клиента ECS вне области действия moto.

Другими словами, вы вы создаете кластер в памяти - но затем спросите у самого AWS список кластеров.

Вы можете переписать метод foo для использования поддельного клиента ECS:

def print_clusters(client):
    print(client.list_clusters())
    return client.list_clusters()["clusterArns"]


def test_foo(ecs_cluster):
    assert foo.print_clusters(ecs_cluster) == ["test_ecs_cluster"]
...