Techdebt: Replace sure with regular assertions in OpsWorks (#6673)

Co-authored-by: Karri Balk <kbalk@users.noreply.github.com>
This commit is contained in:
kbalk 2023-08-16 06:10:41 -04:00 committed by GitHub
parent b1195f8eb3
commit 45dfb54469
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 427 additions and 394 deletions

View File

@ -1,75 +1,80 @@
import boto3 import boto3
from freezegun import freeze_time from freezegun import freeze_time
import sure # noqa # pylint: disable=unused-import import pytest
import re
from moto import mock_opsworks
from moto import mock_opsworks
@freeze_time("2015-01-01")
@freeze_time("2015-01-01") @mock_opsworks
@mock_opsworks def test_create_app_response():
def test_create_app_response(): client = boto3.client("opsworks", region_name="us-east-1")
client = boto3.client("opsworks", region_name="us-east-1") stack_id = client.create_stack(
stack_id = client.create_stack( Name="test_stack_1",
Name="test_stack_1", Region="us-east-1",
Region="us-east-1", ServiceRoleArn="service_arn",
ServiceRoleArn="service_arn", DefaultInstanceProfileArn="profile_arn",
DefaultInstanceProfileArn="profile_arn", )["StackId"]
)["StackId"]
response = client.create_app(StackId=stack_id, Type="other", Name="TestApp")
response = client.create_app(StackId=stack_id, Type="other", Name="TestApp")
assert "AppId" in response
response.should.contain("AppId")
second_stack_id = client.create_stack(
second_stack_id = client.create_stack( Name="test_stack_2",
Name="test_stack_2", Region="us-east-1",
Region="us-east-1", ServiceRoleArn="service_arn",
ServiceRoleArn="service_arn", DefaultInstanceProfileArn="profile_arn",
DefaultInstanceProfileArn="profile_arn", )["StackId"]
)["StackId"]
response = client.create_app(StackId=second_stack_id, Type="other", Name="TestApp")
response = client.create_app(StackId=second_stack_id, Type="other", Name="TestApp")
assert "AppId" in response
response.should.contain("AppId")
# ClientError
# ClientError with pytest.raises(Exception) as exc:
client.create_app.when.called_with( client.create_app(StackId=stack_id, Type="other", Name="TestApp")
StackId=stack_id, Type="other", Name="TestApp" assert r'already an app named "TestApp"' in exc.value.response["Error"]["Message"]
).should.throw(Exception, re.compile(r'already an app named "TestApp"'))
# ClientError
# ClientError with pytest.raises(Exception) as exc:
client.create_app.when.called_with( client.create_app(StackId="nothere", Type="other", Name="TestApp")
StackId="nothere", Type="other", Name="TestApp" assert exc.value.response["Error"]["Message"] == "nothere"
).should.throw(Exception, "nothere")
@freeze_time("2015-01-01")
@freeze_time("2015-01-01") @mock_opsworks
@mock_opsworks def test_describe_apps():
def test_describe_apps(): client = boto3.client("opsworks", region_name="us-east-1")
client = boto3.client("opsworks", region_name="us-east-1") stack_id = client.create_stack(
stack_id = client.create_stack( Name="test_stack_1",
Name="test_stack_1", Region="us-east-1",
Region="us-east-1", ServiceRoleArn="service_arn",
ServiceRoleArn="service_arn", DefaultInstanceProfileArn="profile_arn",
DefaultInstanceProfileArn="profile_arn", )["StackId"]
)["StackId"] app_id = client.create_app(StackId=stack_id, Type="other", Name="TestApp")["AppId"]
app_id = client.create_app(StackId=stack_id, Type="other", Name="TestApp")["AppId"]
rv1 = client.describe_apps(StackId=stack_id)
rv1 = client.describe_apps(StackId=stack_id) rv2 = client.describe_apps(AppIds=[app_id])
rv2 = client.describe_apps(AppIds=[app_id]) assert rv1["Apps"] == rv2["Apps"]
rv1["Apps"].should.equal(rv2["Apps"])
assert rv1["Apps"][0]["Name"] == "TestApp"
rv1["Apps"][0]["Name"].should.equal("TestApp")
# ClientError
# ClientError with pytest.raises(Exception) as exc:
client.describe_apps.when.called_with( client.describe_apps(StackId=stack_id, AppIds=[app_id])
StackId=stack_id, AppIds=[app_id] assert exc.value.response["Error"]["Message"] == (
).should.throw(Exception, "Please provide one or more app IDs or a stack ID") "Please provide one or more app IDs or a stack ID"
# ClientError )
client.describe_apps.when.called_with(StackId="nothere").should.throw(
Exception, "Unable to find stack with ID nothere" # ClientError
) with pytest.raises(Exception) as exc:
# ClientError client.describe_apps(StackId="nothere")
client.describe_apps.when.called_with(AppIds=["nothere"]).should.throw( assert exc.value.response["Error"]["Message"] == (
Exception, "nothere" "Unable to find stack with ID nothere"
) )
# ClientError
with pytest.raises(Exception) as exc:
client.describe_apps(AppIds=["nothere"])
assert exc.value.response["Error"]["Message"] == "nothere"

View File

@ -1,213 +1,227 @@
import boto3 import boto3
import sure # noqa # pylint: disable=unused-import import pytest
from moto import mock_opsworks from moto import mock_opsworks
from moto import mock_ec2 from moto import mock_ec2
from tests import EXAMPLE_AMI_ID from tests import EXAMPLE_AMI_ID
@mock_opsworks @mock_opsworks
def test_create_instance(): def test_create_instance():
client = boto3.client("opsworks", region_name="us-east-1") client = boto3.client("opsworks", region_name="us-east-1")
stack_id = client.create_stack( stack_id = client.create_stack(
Name="test_stack_1", Name="test_stack_1",
Region="us-east-1", Region="us-east-1",
ServiceRoleArn="service_arn", ServiceRoleArn="service_arn",
DefaultInstanceProfileArn="profile_arn", DefaultInstanceProfileArn="profile_arn",
)["StackId"] )["StackId"]
layer_id = client.create_layer( layer_id = client.create_layer(
StackId=stack_id, StackId=stack_id,
Type="custom", Type="custom",
Name="TestLayer", Name="TestLayer",
Shortname="TestLayerShortName", Shortname="TestLayerShortName",
)["LayerId"] )["LayerId"]
second_stack_id = client.create_stack( second_stack_id = client.create_stack(
Name="test_stack_2", Name="test_stack_2",
Region="us-east-1", Region="us-east-1",
ServiceRoleArn="service_arn", ServiceRoleArn="service_arn",
DefaultInstanceProfileArn="profile_arn", DefaultInstanceProfileArn="profile_arn",
)["StackId"] )["StackId"]
second_layer_id = client.create_layer( second_layer_id = client.create_layer(
StackId=second_stack_id, StackId=second_stack_id,
Type="custom", Type="custom",
Name="SecondTestLayer", Name="SecondTestLayer",
Shortname="SecondTestLayerShortName", Shortname="SecondTestLayerShortName",
)["LayerId"] )["LayerId"]
response = client.create_instance( response = client.create_instance(
StackId=stack_id, LayerIds=[layer_id], InstanceType="t2.micro" StackId=stack_id, LayerIds=[layer_id], InstanceType="t2.micro"
) )
response.should.contain("InstanceId") assert "InstanceId" in response
client.create_instance.when.called_with( with pytest.raises(Exception) as exc:
StackId="nothere", LayerIds=[layer_id], InstanceType="t2.micro" client.create_instance(
).should.throw(Exception, "Unable to find stack with ID nothere") StackId="nothere", LayerIds=[layer_id], InstanceType="t2.micro"
)
client.create_instance.when.called_with( assert exc.value.response["Error"]["Message"] == (
StackId=stack_id, LayerIds=["nothere"], InstanceType="t2.micro" "Unable to find stack with ID nothere"
).should.throw(Exception, "nothere") )
# ClientError
client.create_instance.when.called_with( with pytest.raises(Exception) as exc:
StackId=stack_id, LayerIds=[second_layer_id], InstanceType="t2.micro" client.create_instance(
).should.throw(Exception, "Please only provide layer IDs from the same stack") StackId=stack_id, LayerIds=["nothere"], InstanceType="t2.micro"
# ClientError )
client.start_instance.when.called_with(InstanceId="nothere").should.throw( assert exc.value.response["Error"]["Message"] == "nothere"
Exception, "Unable to find instance with ID nothere"
) # ClientError
with pytest.raises(Exception) as exc:
client.create_instance(
@mock_opsworks StackId=stack_id, LayerIds=[second_layer_id], InstanceType="t2.micro"
def test_describe_instances(): )
""" assert exc.value.response["Error"]["Message"] == (
create two stacks, with 1 layer and 2 layers (S1L1, S2L1, S2L2) "Please only provide layer IDs from the same stack"
)
populate S1L1 with 2 instances (S1L1_i1, S1L1_i2)
populate S2L1 with 1 instance (S2L1_i1) # ClientError
populate S2L2 with 3 instances (S2L2_i1..2) with pytest.raises(Exception) as exc:
""" client.start_instance(InstanceId="nothere")
assert exc.value.response["Error"]["Message"] == (
client = boto3.client("opsworks", region_name="us-east-1") "Unable to find instance with ID nothere"
S1 = client.create_stack( )
Name="S1",
Region="us-east-1",
ServiceRoleArn="service_arn", @mock_opsworks
DefaultInstanceProfileArn="profile_arn", def test_describe_instances():
)["StackId"] """
S1L1 = client.create_layer( create two stacks, with 1 layer and 2 layers (S1L1, S2L1, S2L2)
StackId=S1, Type="custom", Name="S1L1", Shortname="S1L1"
)["LayerId"] populate S1L1 with 2 instances (S1L1_i1, S1L1_i2)
S2 = client.create_stack( populate S2L1 with 1 instance (S2L1_i1)
Name="S2", populate S2L2 with 3 instances (S2L2_i1..2)
Region="us-east-1", """
ServiceRoleArn="service_arn",
DefaultInstanceProfileArn="profile_arn", client = boto3.client("opsworks", region_name="us-east-1")
)["StackId"] S1 = client.create_stack(
S2L1 = client.create_layer( Name="S1",
StackId=S2, Type="custom", Name="S2L1", Shortname="S2L1" Region="us-east-1",
)["LayerId"] ServiceRoleArn="service_arn",
S2L2 = client.create_layer( DefaultInstanceProfileArn="profile_arn",
StackId=S2, Type="custom", Name="S2L2", Shortname="S2L2" )["StackId"]
)["LayerId"] S1L1 = client.create_layer(
StackId=S1, Type="custom", Name="S1L1", Shortname="S1L1"
S1L1_i1 = client.create_instance( )["LayerId"]
StackId=S1, LayerIds=[S1L1], InstanceType="t2.micro" S2 = client.create_stack(
)["InstanceId"] Name="S2",
S1L1_i2 = client.create_instance( Region="us-east-1",
StackId=S1, LayerIds=[S1L1], InstanceType="t2.micro" ServiceRoleArn="service_arn",
)["InstanceId"] DefaultInstanceProfileArn="profile_arn",
S2L1_i1 = client.create_instance( )["StackId"]
StackId=S2, LayerIds=[S2L1], InstanceType="t2.micro" S2L1 = client.create_layer(
)["InstanceId"] StackId=S2, Type="custom", Name="S2L1", Shortname="S2L1"
S2L2_i1 = client.create_instance( )["LayerId"]
StackId=S2, LayerIds=[S2L2], InstanceType="t2.micro" S2L2 = client.create_layer(
)["InstanceId"] StackId=S2, Type="custom", Name="S2L2", Shortname="S2L2"
S2L2_i2 = client.create_instance( )["LayerId"]
StackId=S2, LayerIds=[S2L2], InstanceType="t2.micro"
)["InstanceId"] S1L1_i1 = client.create_instance(
StackId=S1, LayerIds=[S1L1], InstanceType="t2.micro"
# instances in Stack 1 )["InstanceId"]
response = client.describe_instances(StackId=S1)["Instances"] S1L1_i2 = client.create_instance(
response.should.have.length_of(2) StackId=S1, LayerIds=[S1L1], InstanceType="t2.micro"
S1L1_i1.should.be.within([i["InstanceId"] for i in response]) )["InstanceId"]
S1L1_i2.should.be.within([i["InstanceId"] for i in response]) S2L1_i1 = client.create_instance(
StackId=S2, LayerIds=[S2L1], InstanceType="t2.micro"
response2 = client.describe_instances(InstanceIds=[S1L1_i1, S1L1_i2])["Instances"] )["InstanceId"]
sorted(response2, key=lambda d: d["InstanceId"]).should.equal( S2L2_i1 = client.create_instance(
sorted(response, key=lambda d: d["InstanceId"]) StackId=S2, LayerIds=[S2L2], InstanceType="t2.micro"
) )["InstanceId"]
S2L2_i2 = client.create_instance(
response3 = client.describe_instances(LayerId=S1L1)["Instances"] StackId=S2, LayerIds=[S2L2], InstanceType="t2.micro"
sorted(response3, key=lambda d: d["InstanceId"]).should.equal( )["InstanceId"]
sorted(response, key=lambda d: d["InstanceId"])
) # instances in Stack 1
response = client.describe_instances(StackId=S1)["Instances"]
response = client.describe_instances(StackId=S1)["Instances"] assert len(response) == 2
response.should.have.length_of(2) assert S1L1_i1 in [i["InstanceId"] for i in response]
S1L1_i1.should.be.within([i["InstanceId"] for i in response]) assert S1L1_i2 in [i["InstanceId"] for i in response]
S1L1_i2.should.be.within([i["InstanceId"] for i in response])
response2 = client.describe_instances(InstanceIds=[S1L1_i1, S1L1_i2])["Instances"]
# instances in Stack 2 assert sorted(response2, key=lambda d: d["InstanceId"]) == (
response = client.describe_instances(StackId=S2)["Instances"] sorted(response, key=lambda d: d["InstanceId"])
response.should.have.length_of(3) )
S2L1_i1.should.be.within([i["InstanceId"] for i in response])
S2L2_i1.should.be.within([i["InstanceId"] for i in response]) response3 = client.describe_instances(LayerId=S1L1)["Instances"]
S2L2_i2.should.be.within([i["InstanceId"] for i in response]) assert sorted(response3, key=lambda d: d["InstanceId"]) == (
sorted(response, key=lambda d: d["InstanceId"])
response = client.describe_instances(LayerId=S2L1)["Instances"] )
response.should.have.length_of(1)
S2L1_i1.should.be.within([i["InstanceId"] for i in response]) response = client.describe_instances(StackId=S1)["Instances"]
assert len(response) == 2
response = client.describe_instances(LayerId=S2L2)["Instances"] assert S1L1_i1 in [i["InstanceId"] for i in response]
response.should.have.length_of(2) assert S1L1_i2 in [i["InstanceId"] for i in response]
S2L1_i1.should_not.be.within([i["InstanceId"] for i in response])
# instances in Stack 2
# ClientError response = client.describe_instances(StackId=S2)["Instances"]
client.describe_instances.when.called_with(StackId=S1, LayerId=S1L1).should.throw( assert len(response) == 3
Exception, "Please provide either one or more" assert S2L1_i1 in [i["InstanceId"] for i in response]
) assert S2L2_i1 in [i["InstanceId"] for i in response]
# ClientError assert S2L2_i2 in [i["InstanceId"] for i in response]
client.describe_instances.when.called_with(StackId="nothere").should.throw(
Exception, "nothere" response = client.describe_instances(LayerId=S2L1)["Instances"]
) assert len(response) == 1
# ClientError assert S2L1_i1 in [i["InstanceId"] for i in response]
client.describe_instances.when.called_with(LayerId="nothere").should.throw(
Exception, "nothere" response = client.describe_instances(LayerId=S2L2)["Instances"]
) assert len(response) == 2
# ClientError assert S2L1_i1 not in [i["InstanceId"] for i in response]
client.describe_instances.when.called_with(InstanceIds=["nothere"]).should.throw(
Exception, "nothere" # ClientError
) with pytest.raises(Exception) as exc:
client.describe_instances(StackId=S1, LayerId=S1L1)
assert "Please provide either one or more" in exc.value.response["Error"]["Message"]
@mock_opsworks
@mock_ec2 # ClientError
def test_ec2_integration(): with pytest.raises(Exception) as exc:
""" client.describe_instances(StackId="nothere")
instances created via OpsWorks should be discoverable via ec2 assert "nothere" in exc.value.response["Error"]["Message"]
"""
# ClientError
opsworks = boto3.client("opsworks", region_name="us-east-1") with pytest.raises(Exception) as exc:
stack_id = opsworks.create_stack( client.describe_instances(LayerId="nothere")
Name="S1", assert "nothere" in exc.value.response["Error"]["Message"]
Region="us-east-1",
ServiceRoleArn="service_arn", # ClientError
DefaultInstanceProfileArn="profile_arn", with pytest.raises(Exception) as exc:
)["StackId"] client.describe_instances(InstanceIds=["nothere"])
assert exc.value.response["Error"]["Message"] == "nothere"
layer_id = opsworks.create_layer(
StackId=stack_id, Type="custom", Name="S1L1", Shortname="S1L1"
)["LayerId"] @mock_opsworks
@mock_ec2
instance_id = opsworks.create_instance( def test_ec2_integration():
AmiId=EXAMPLE_AMI_ID, """Verify instances created via OpsWorks are discoverable via ec2."""
StackId=stack_id, opsworks = boto3.client("opsworks", region_name="us-east-1")
LayerIds=[layer_id], stack_id = opsworks.create_stack(
InstanceType="t2.micro", Name="S1",
SshKeyName="testSSH", Region="us-east-1",
)["InstanceId"] ServiceRoleArn="service_arn",
DefaultInstanceProfileArn="profile_arn",
ec2 = boto3.client("ec2", region_name="us-east-1") )["StackId"]
# Before starting the instance, it shouldn't be discoverable via ec2 layer_id = opsworks.create_layer(
reservations = ec2.describe_instances()["Reservations"] StackId=stack_id, Type="custom", Name="S1L1", Shortname="S1L1"
assert reservations.should.be.empty )["LayerId"]
# Before starting the instance, its status should be "stopped" instance_id = opsworks.create_instance(
opsworks_instance = opsworks.describe_instances(StackId=stack_id)["Instances"][0] AmiId=EXAMPLE_AMI_ID,
opsworks_instance["Status"].should.equal("stopped") StackId=stack_id,
LayerIds=[layer_id],
# After starting the instance, it should be discoverable via ec2 InstanceType="t2.micro",
opsworks.start_instance(InstanceId=instance_id) SshKeyName="testSSH",
reservations = ec2.describe_instances()["Reservations"] )["InstanceId"]
reservations[0]["Instances"].should.have.length_of(1)
instance = reservations[0]["Instances"][0] ec2 = boto3.client("ec2", region_name="us-east-1")
opsworks_instance = opsworks.describe_instances(StackId=stack_id)["Instances"][0]
# Before starting the instance, it shouldn't be discoverable via ec2
instance["InstanceId"].should.equal(opsworks_instance["Ec2InstanceId"]) reservations = ec2.describe_instances()["Reservations"]
instance["PrivateIpAddress"].should.equal(opsworks_instance["PrivateIp"]) assert not reservations
# After starting the instance, its status should be "online"
opsworks_instance["Status"].should.equal("online") # Before starting the instance, its status should be "stopped"
opsworks_instance = opsworks.describe_instances(StackId=stack_id)["Instances"][0]
assert opsworks_instance["Status"] == "stopped"
# After starting the instance, it should be discoverable via ec2
opsworks.start_instance(InstanceId=instance_id)
reservations = ec2.describe_instances()["Reservations"]
assert len(reservations[0]["Instances"]) == 1
instance = reservations[0]["Instances"][0]
opsworks_instance = opsworks.describe_instances(StackId=stack_id)["Instances"][0]
assert instance["InstanceId"] == opsworks_instance["Ec2InstanceId"]
assert instance["PrivateIpAddress"] == opsworks_instance["PrivateIp"]
# After starting the instance, its status should be "online"
assert opsworks_instance["Status"] == "online"

View File

@ -1,95 +1,110 @@
import boto3 import boto3
from freezegun import freeze_time from freezegun import freeze_time
import sure # noqa # pylint: disable=unused-import import pytest
import re
from moto import mock_opsworks
from moto import mock_opsworks
@freeze_time("2015-01-01")
@freeze_time("2015-01-01") @mock_opsworks
@mock_opsworks def test_create_layer_response():
def test_create_layer_response(): client = boto3.client("opsworks", region_name="us-east-1")
client = boto3.client("opsworks", region_name="us-east-1") stack_id = client.create_stack(
stack_id = client.create_stack( Name="test_stack_1",
Name="test_stack_1", Region="us-east-1",
Region="us-east-1", ServiceRoleArn="service_arn",
ServiceRoleArn="service_arn", DefaultInstanceProfileArn="profile_arn",
DefaultInstanceProfileArn="profile_arn", )["StackId"]
)["StackId"]
response = client.create_layer(
response = client.create_layer( StackId=stack_id,
StackId=stack_id, Type="custom",
Type="custom", Name="TestLayer",
Name="TestLayer", Shortname="TestLayerShortName",
Shortname="TestLayerShortName", )
)
assert "LayerId" in response
response.should.contain("LayerId")
second_stack_id = client.create_stack(
second_stack_id = client.create_stack( Name="test_stack_2",
Name="test_stack_2", Region="us-east-1",
Region="us-east-1", ServiceRoleArn="service_arn",
ServiceRoleArn="service_arn", DefaultInstanceProfileArn="profile_arn",
DefaultInstanceProfileArn="profile_arn", )["StackId"]
)["StackId"]
response = client.create_layer(
response = client.create_layer( StackId=second_stack_id,
StackId=second_stack_id, Type="custom",
Type="custom", Name="TestLayer",
Name="TestLayer", Shortname="TestLayerShortName",
Shortname="TestLayerShortName", )
)
assert "LayerId" in response
response.should.contain("LayerId")
# ClientError
# ClientError with pytest.raises(Exception) as exc:
client.create_layer.when.called_with( client.create_layer(
StackId=stack_id, Type="custom", Name="TestLayer", Shortname="_" StackId=stack_id, Type="custom", Name="TestLayer", Shortname="_"
).should.throw(Exception, re.compile(r'already a layer named "TestLayer"')) )
# ClientError assert (
client.create_layer.when.called_with( r'already a layer named "TestLayer"' in exc.value.response["Error"]["Message"]
StackId=stack_id, Type="custom", Name="_", Shortname="TestLayerShortName" )
).should.throw(
Exception, re.compile(r'already a layer with shortname "TestLayerShortName"') # ClientError
) with pytest.raises(Exception) as exc:
# ClientError client.create_layer(
client.create_layer.when.called_with( StackId=stack_id, Type="custom", Name="_", Shortname="TestLayerShortName"
StackId="nothere", Type="custom", Name="TestLayer", Shortname="_" )
).should.throw(Exception, "nothere") assert (
r'already a layer with shortname "TestLayerShortName"'
) in exc.value.response["Error"]["Message"]
@freeze_time("2015-01-01")
@mock_opsworks # ClientError
def test_describe_layers(): with pytest.raises(Exception) as exc:
client = boto3.client("opsworks", region_name="us-east-1") client.create_layer(
stack_id = client.create_stack( StackId="nothere", Type="custom", Name="TestLayer", Shortname="_"
Name="test_stack_1", )
Region="us-east-1", assert exc.value.response["Error"]["Message"] == "nothere"
ServiceRoleArn="service_arn",
DefaultInstanceProfileArn="profile_arn",
)["StackId"] @freeze_time("2015-01-01")
layer_id = client.create_layer( @mock_opsworks
StackId=stack_id, def test_describe_layers():
Type="custom", client = boto3.client("opsworks", region_name="us-east-1")
Name="TestLayer", stack_id = client.create_stack(
Shortname="TestLayerShortName", Name="test_stack_1",
)["LayerId"] Region="us-east-1",
ServiceRoleArn="service_arn",
rv1 = client.describe_layers(StackId=stack_id) DefaultInstanceProfileArn="profile_arn",
rv2 = client.describe_layers(LayerIds=[layer_id]) )["StackId"]
rv1["Layers"].should.equal(rv2["Layers"]) layer_id = client.create_layer(
StackId=stack_id,
rv1["Layers"][0]["Name"].should.equal("TestLayer") Type="custom",
Name="TestLayer",
# ClientError Shortname="TestLayerShortName",
client.describe_layers.when.called_with( )["LayerId"]
StackId=stack_id, LayerIds=[layer_id]
).should.throw(Exception, "Please provide one or more layer IDs or a stack ID") rv1 = client.describe_layers(StackId=stack_id)
# ClientError rv2 = client.describe_layers(LayerIds=[layer_id])
client.describe_layers.when.called_with(StackId="nothere").should.throw( assert rv1["Layers"] == rv2["Layers"]
Exception, "Unable to find stack with ID nothere"
) assert rv1["Layers"][0]["Name"] == "TestLayer"
# ClientError
client.describe_layers.when.called_with(LayerIds=["nothere"]).should.throw( # ClientError
Exception, "nothere" with pytest.raises(Exception) as exc:
) client.describe_layers(StackId=stack_id, LayerIds=[layer_id])
assert exc.value.response["Error"]["Message"] == (
"Please provide one or more layer IDs or a stack ID"
)
# ClientError
with pytest.raises(Exception) as exc:
client.describe_layers(StackId="nothere")
assert exc.value.response["Error"]["Message"] == (
"Unable to find stack with ID nothere"
)
# ClientError
with pytest.raises(Exception) as exc:
client.describe_layers(LayerIds=["nothere"])
assert exc.value.response["Error"]["Message"] == "nothere"

View File

@ -1,6 +1,5 @@
import boto3 import boto3
import sure # noqa # pylint: disable=unused-import import pytest
import re
from moto import mock_opsworks from moto import mock_opsworks
@ -14,7 +13,7 @@ def test_create_stack_response():
ServiceRoleArn="service_arn", ServiceRoleArn="service_arn",
DefaultInstanceProfileArn="profile_arn", DefaultInstanceProfileArn="profile_arn",
) )
response.should.contain("StackId") assert "StackId" in response
@mock_opsworks @mock_opsworks
@ -29,17 +28,17 @@ def test_describe_stacks():
) )
response = client.describe_stacks() response = client.describe_stacks()
response["Stacks"].should.have.length_of(3) assert len(response["Stacks"]) == 3
for stack in response["Stacks"]: for stack in response["Stacks"]:
stack["ServiceRoleArn"].should.equal("service_arn") assert stack["ServiceRoleArn"] == "service_arn"
stack["DefaultInstanceProfileArn"].should.equal("profile_arn") assert stack["DefaultInstanceProfileArn"] == "profile_arn"
_id = response["Stacks"][0]["StackId"] _id = response["Stacks"][0]["StackId"]
response = client.describe_stacks(StackIds=[_id]) response = client.describe_stacks(StackIds=[_id])
response["Stacks"].should.have.length_of(1) assert len(response["Stacks"]) == 1
response["Stacks"][0]["Arn"].should.contain(_id) assert _id in response["Stacks"][0]["Arn"]
# ClientError/ResourceNotFoundException # ClientError/ResourceNotFoundException
client.describe_stacks.when.called_with(StackIds=["foo"]).should.throw( with pytest.raises(Exception) as exc:
Exception, re.compile(r"foo") client.describe_stacks(StackIds=["foo"])
) assert r"foo" in exc.value.response["Error"]["Message"]