2023-11-30 15:55:51 +00:00
|
|
|
from uuid import uuid4
|
|
|
|
|
2021-10-21 15:13:43 +00:00
|
|
|
import boto3
|
2022-03-26 21:25:56 +00:00
|
|
|
import pytest
|
2023-10-31 19:29:04 +00:00
|
|
|
from botocore.exceptions import ClientError
|
2022-03-26 21:25:56 +00:00
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
from moto import mock_aws
|
2022-08-13 09:49:43 +00:00
|
|
|
from moto.core import DEFAULT_ACCOUNT_ID as ACCOUNT_ID
|
2021-10-21 15:13:43 +00:00
|
|
|
|
2023-10-31 19:29:04 +00:00
|
|
|
from . import timestreamwrite_aws_verified
|
2021-10-21 15:13:43 +00:00
|
|
|
|
2023-10-31 19:29:04 +00:00
|
|
|
|
|
|
|
@pytest.mark.aws_verified
|
|
|
|
@timestreamwrite_aws_verified
|
2021-10-21 15:13:43 +00:00
|
|
|
def test_create_database_simple():
|
|
|
|
ts = boto3.client("timestream-write", region_name="us-east-1")
|
2023-10-31 19:29:04 +00:00
|
|
|
db_name = "db_" + str(uuid4())[0:6]
|
2021-10-21 15:13:43 +00:00
|
|
|
|
2023-10-31 19:29:04 +00:00
|
|
|
identity = boto3.client("sts", region_name="us-east-1").get_caller_identity()
|
|
|
|
account_id = identity["Account"]
|
|
|
|
|
|
|
|
try:
|
|
|
|
database = ts.create_database(DatabaseName=db_name)["Database"]
|
|
|
|
|
|
|
|
assert (
|
|
|
|
database["Arn"]
|
|
|
|
== f"arn:aws:timestream:us-east-1:{account_id}:database/{db_name}"
|
|
|
|
)
|
|
|
|
assert db_name == db_name
|
|
|
|
assert database["TableCount"] == 0
|
|
|
|
assert database["KmsKeyId"].startswith(
|
|
|
|
f"arn:aws:kms:us-east-1:{account_id}:key/"
|
|
|
|
)
|
|
|
|
assert "CreationTime" in database
|
|
|
|
assert "LastUpdatedTime" in database
|
|
|
|
finally:
|
|
|
|
ts.delete_database(DatabaseName=db_name)
|
2021-10-21 15:13:43 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2021-10-21 15:13:43 +00:00
|
|
|
def test_create_database_advanced():
|
|
|
|
ts = boto3.client("timestream-write", region_name="us-east-1")
|
|
|
|
resp = ts.create_database(
|
|
|
|
DatabaseName="mydatabase",
|
|
|
|
KmsKeyId="mykey",
|
|
|
|
Tags=[{"Key": "k1", "Value": "v1"}, {"Key": "k2", "Value": "v2"}],
|
|
|
|
)
|
|
|
|
database = resp["Database"]
|
|
|
|
|
2023-08-14 09:53:02 +00:00
|
|
|
assert database["Arn"] == (
|
2021-10-21 15:13:43 +00:00
|
|
|
f"arn:aws:timestream:us-east-1:{ACCOUNT_ID}:database/mydatabase"
|
|
|
|
)
|
2023-08-14 09:53:02 +00:00
|
|
|
assert database["DatabaseName"] == "mydatabase"
|
|
|
|
assert database["TableCount"] == 0
|
|
|
|
assert database["KmsKeyId"] == "mykey"
|
2021-10-21 15:13:43 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2021-10-21 15:13:43 +00:00
|
|
|
def test_describe_database():
|
|
|
|
ts = boto3.client("timestream-write", region_name="us-east-1")
|
|
|
|
ts.create_database(DatabaseName="mydatabase", KmsKeyId="mykey")
|
|
|
|
|
|
|
|
database = ts.describe_database(DatabaseName="mydatabase")["Database"]
|
|
|
|
|
2023-08-14 09:53:02 +00:00
|
|
|
assert database["Arn"] == (
|
2021-10-21 15:13:43 +00:00
|
|
|
f"arn:aws:timestream:us-east-1:{ACCOUNT_ID}:database/mydatabase"
|
|
|
|
)
|
2023-08-14 09:53:02 +00:00
|
|
|
assert database["DatabaseName"] == "mydatabase"
|
|
|
|
assert database["TableCount"] == 0
|
|
|
|
assert database["KmsKeyId"] == "mykey"
|
2021-10-21 15:13:43 +00:00
|
|
|
|
|
|
|
|
2023-10-31 19:29:04 +00:00
|
|
|
@pytest.mark.aws_verified
|
|
|
|
@timestreamwrite_aws_verified
|
2022-03-26 21:25:56 +00:00
|
|
|
def test_describe_unknown_database():
|
|
|
|
ts = boto3.client("timestream-write", region_name="us-east-1")
|
|
|
|
with pytest.raises(ClientError) as exc:
|
|
|
|
ts.describe_database(DatabaseName="unknown")
|
|
|
|
err = exc.value.response["Error"]
|
2023-08-14 09:53:02 +00:00
|
|
|
assert err["Code"] == "ResourceNotFoundException"
|
|
|
|
assert err["Message"] == "The database unknown does not exist."
|
2022-03-26 21:25:56 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2021-10-21 15:13:43 +00:00
|
|
|
def test_list_databases():
|
|
|
|
ts = boto3.client("timestream-write", region_name="us-east-1")
|
|
|
|
ts.create_database(DatabaseName="db_with", KmsKeyId="mykey")
|
|
|
|
ts.create_database(DatabaseName="db_without")
|
|
|
|
|
|
|
|
resp = ts.list_databases()
|
|
|
|
databases = resp["Databases"]
|
2023-08-14 09:53:02 +00:00
|
|
|
assert len(databases) == 2
|
2023-10-31 19:29:04 +00:00
|
|
|
|
|
|
|
for db in databases:
|
|
|
|
db.pop("CreationTime")
|
|
|
|
db.pop("LastUpdatedTime")
|
|
|
|
|
2023-08-14 09:53:02 +00:00
|
|
|
assert {
|
|
|
|
"Arn": f"arn:aws:timestream:us-east-1:{ACCOUNT_ID}:database/db_with",
|
|
|
|
"DatabaseName": "db_with",
|
|
|
|
"TableCount": 0,
|
|
|
|
"KmsKeyId": "mykey",
|
|
|
|
} in databases
|
|
|
|
assert {
|
|
|
|
"Arn": f"arn:aws:timestream:us-east-1:{ACCOUNT_ID}:database/db_without",
|
|
|
|
"DatabaseName": "db_without",
|
|
|
|
"TableCount": 0,
|
|
|
|
"KmsKeyId": f"arn:aws:kms:us-east-1:{ACCOUNT_ID}:key/default_key",
|
|
|
|
} in databases
|
2021-10-21 15:13:43 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2021-10-21 15:13:43 +00:00
|
|
|
def test_delete_database():
|
|
|
|
ts = boto3.client("timestream-write", region_name="us-east-1")
|
|
|
|
ts.create_database(DatabaseName="db_1", KmsKeyId="mykey")
|
|
|
|
ts.create_database(DatabaseName="db_2")
|
|
|
|
ts.create_database(DatabaseName="db_3", KmsKeyId="mysecondkey")
|
|
|
|
|
2023-08-14 09:53:02 +00:00
|
|
|
assert len(ts.list_databases()["Databases"]) == 3
|
2021-10-21 15:13:43 +00:00
|
|
|
|
|
|
|
ts.delete_database(DatabaseName="db_2")
|
|
|
|
|
|
|
|
databases = ts.list_databases()["Databases"]
|
2023-08-14 09:53:02 +00:00
|
|
|
assert len(databases) == 2
|
|
|
|
assert [db["DatabaseName"] for db in databases] == ["db_1", "db_3"]
|
2021-10-21 15:13:43 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2021-10-21 15:13:43 +00:00
|
|
|
def test_update_database():
|
|
|
|
ts = boto3.client("timestream-write", region_name="us-east-1")
|
|
|
|
ts.create_database(DatabaseName="mydatabase", KmsKeyId="mykey")
|
|
|
|
resp = ts.update_database(DatabaseName="mydatabase", KmsKeyId="updatedkey")
|
2023-08-14 09:53:02 +00:00
|
|
|
assert "Database" in resp
|
2021-10-21 15:13:43 +00:00
|
|
|
database = resp["Database"]
|
2023-08-14 09:53:02 +00:00
|
|
|
assert "Arn" in database
|
|
|
|
assert database["KmsKeyId"] == "updatedkey"
|
2021-10-21 15:13:43 +00:00
|
|
|
|
|
|
|
database = ts.describe_database(DatabaseName="mydatabase")["Database"]
|
2023-08-14 09:53:02 +00:00
|
|
|
assert database["KmsKeyId"] == "updatedkey"
|