首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用moto模拟ECS?

如何使用moto模拟ECS?
EN

Stack Overflow用户
提问于 2020-03-02 21:05:42
回答 1查看 712关注 0票数 0

我想创建一个模拟的ECS集群,但它似乎不能正常工作。尽管有一些东西被模仿了(我没有收到凭证错误),但它似乎并没有“保存”集群。

如何使用moto创建模拟集群?

MVCE

foo.py

代码语言:javascript
复制
import boto3


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

test_foo.py

代码语言:javascript
复制
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"]

会发生什么?

代码语言:javascript
复制
$ 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)。但是这个列表是空的。

EN

回答 1

Stack Overflow用户

发布于 2020-04-07 15:06:48

在创建集群时,您使用的是模拟的ECS客户端。

在列出集群时,您在moto作用域之外创建了一个新的ECS客户端。

换句话说,您在内存中创建了一个集群--但随后向AWS本身索要集群列表。

您可以重写foo-method以使用模拟的ECS客户端:

代码语言:javascript
复制
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"]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60489510

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档