I have a click cli app and I am trying to mock AWS SSM Parameter Store, but runner.invoke is not returning the expected results.
This is test_demo.py
:
from click.testing import CliRunner
import os
import boto3
from moto import mock_ssm
import pytest
@pytest.fixture(scope='package')
def aws_credentials():
"""Mocked AWS Credentials for moto."""
os.environ['AWS_ACCESS_KEY_ID'] = 'testing'
os.environ['AWS_SECRET_ACCESS_KEY'] = 'testing'
os.environ['AWS_SECURITY_TOKEN'] = 'testing'
os.environ['AWS_SESSION_TOKEN'] = 'testing'
os.environ['AWS_DEFAULT_REGION'] = 'us-west-2'
@pytest.fixture(scope='package')
def ssm(aws_credentials):
with mock_ssm():
yield boto3.client('ssm')
def test_get_mock_ssm(ssm):
# We need to create the ssm entry first since this is all in Moto's 'virtual' AWS account
ssm.put_parameter(
Name='test',
Description='name',
Value='text',
Type='String',
Overwrite=True,
Tier='Standard',
DataType='text'
)
out = ssm.get_parameter(Name='test')
print('without cli')
print(out)
from src.cli import entrypoint
runner = CliRunner()
response = runner.invoke(entrypoint, ["get", "--name", "test"])
print('with cli')
print(response)
assert response.exit_code == 0
The results of the test are:
without cli
{'Parameter': {'Name': 'test', 'Type': 'String', 'Value': 'text', 'Version': 1, 'LastModifiedDate': datetime.datetime(2020, 5, 31, 9, 3, 21, 920000, tzinfo=tzlocal()), 'ARN': 'arn:aws:ssm:us-west-2:1234567890:parameter/test'}, 'ResponseMetadata': {'HTTPStatusCode': 200, 'HTTPHeaders': {'server': 'amazon.com'}, 'RetryAttempts': 0}}
with cli
<Result okay>
PASSED
I expect the out of the cli to be same as withoutcli.
click code:
import click
import manage_secrets
@click.group()
def entrypoint():
pass
@entrypoint.command()
@click.option('--name', required=True,
help='Name of parameter to get value from SSM')
def get(name):
"""get secrets"""
click.echo("getting value of: {0}" .format(name))
print(manage_secrets.get_value_from_parameter_store(name))
if __name__ == "__main__":
entrypoint()
manage_secrets code:
import boto3
parameter_name = None
def get_value_from_parameter_store(name):
client = boto3.client('ssm')
try:
parameter = client.get_parameter(Name=name, WithDecryption=True)
return parameter
except client.exceptions.ParameterNotFound:
print("parameter not found")
I have added a return and its just default, not parsing and I think it is the same for ssm.get_parameter. I am not sure what I am missing. Any pointers or suggestions?