2017-09-27 23:04:58 +00:00
|
|
|
import boto3
|
2019-10-21 07:32:07 +00:00
|
|
|
import os
|
2017-09-27 23:04:58 +00:00
|
|
|
import sure # noqa
|
2018-08-11 14:14:04 +00:00
|
|
|
import six
|
2018-01-14 05:35:53 +00:00
|
|
|
from botocore.exceptions import ClientError
|
2017-09-27 23:04:58 +00:00
|
|
|
|
2020-09-13 15:08:23 +00:00
|
|
|
from moto import mock_logs, settings
|
2020-10-06 05:54:49 +00:00
|
|
|
import pytest
|
|
|
|
from unittest import SkipTest
|
2017-09-27 23:04:58 +00:00
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
_logs_region = "us-east-1" if settings.TEST_SERVER_MODE else "us-west-2"
|
2017-09-27 23:04:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mock_logs
|
2020-04-02 00:57:46 +00:00
|
|
|
def test_create_log_group():
|
2019-10-31 15:44:26 +00:00
|
|
|
conn = boto3.client("logs", "us-west-2")
|
2018-04-24 02:12:17 +00:00
|
|
|
|
2020-04-02 00:57:46 +00:00
|
|
|
response = conn.create_log_group(logGroupName="dummy")
|
|
|
|
response = conn.describe_log_groups()
|
2018-04-24 02:12:17 +00:00
|
|
|
|
2020-04-02 00:57:46 +00:00
|
|
|
response["logGroups"].should.have.length_of(1)
|
|
|
|
response["logGroups"][0].should_not.have.key("retentionInDays")
|
2018-01-14 05:35:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mock_logs
|
|
|
|
def test_exceptions():
|
2019-10-31 15:44:26 +00:00
|
|
|
conn = boto3.client("logs", "us-west-2")
|
|
|
|
log_group_name = "dummy"
|
|
|
|
log_stream_name = "dummp-stream"
|
2018-01-14 05:35:53 +00:00
|
|
|
conn.create_log_group(logGroupName=log_group_name)
|
2020-10-06 05:54:49 +00:00
|
|
|
with pytest.raises(ClientError):
|
2018-01-14 05:35:53 +00:00
|
|
|
conn.create_log_group(logGroupName=log_group_name)
|
|
|
|
|
|
|
|
# descrine_log_groups is not implemented yet
|
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
conn.create_log_stream(logGroupName=log_group_name, logStreamName=log_stream_name)
|
2020-10-06 05:54:49 +00:00
|
|
|
with pytest.raises(ClientError):
|
2018-01-14 05:35:53 +00:00
|
|
|
conn.create_log_stream(
|
2019-10-31 15:44:26 +00:00
|
|
|
logGroupName=log_group_name, logStreamName=log_stream_name
|
2018-01-14 05:35:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
conn.put_log_events(
|
|
|
|
logGroupName=log_group_name,
|
|
|
|
logStreamName=log_stream_name,
|
2019-10-31 15:44:26 +00:00
|
|
|
logEvents=[{"timestamp": 0, "message": "line"}],
|
2018-01-14 05:35:53 +00:00
|
|
|
)
|
|
|
|
|
2020-10-06 05:54:49 +00:00
|
|
|
with pytest.raises(ClientError):
|
2018-01-14 05:35:53 +00:00
|
|
|
conn.put_log_events(
|
|
|
|
logGroupName=log_group_name,
|
|
|
|
logStreamName="invalid-stream",
|
2019-10-31 15:44:26 +00:00
|
|
|
logEvents=[{"timestamp": 0, "message": "line"}],
|
2018-01-14 05:35:53 +00:00
|
|
|
)
|
2018-03-21 15:55:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mock_logs
|
|
|
|
def test_put_logs():
|
2019-10-31 15:44:26 +00:00
|
|
|
conn = boto3.client("logs", "us-west-2")
|
|
|
|
log_group_name = "dummy"
|
|
|
|
log_stream_name = "stream"
|
2018-03-21 15:55:03 +00:00
|
|
|
conn.create_log_group(logGroupName=log_group_name)
|
2019-10-31 15:44:26 +00:00
|
|
|
conn.create_log_stream(logGroupName=log_group_name, logStreamName=log_stream_name)
|
2018-03-21 15:55:03 +00:00
|
|
|
messages = [
|
2019-10-31 15:44:26 +00:00
|
|
|
{"timestamp": 0, "message": "hello"},
|
|
|
|
{"timestamp": 0, "message": "world"},
|
2018-03-21 15:55:03 +00:00
|
|
|
]
|
2018-08-11 14:14:04 +00:00
|
|
|
putRes = conn.put_log_events(
|
2019-10-31 15:44:26 +00:00
|
|
|
logGroupName=log_group_name, logStreamName=log_stream_name, logEvents=messages
|
2018-03-21 15:55:03 +00:00
|
|
|
)
|
|
|
|
res = conn.get_log_events(
|
2019-10-31 15:44:26 +00:00
|
|
|
logGroupName=log_group_name, logStreamName=log_stream_name
|
2018-03-21 15:55:03 +00:00
|
|
|
)
|
2019-10-31 15:44:26 +00:00
|
|
|
events = res["events"]
|
|
|
|
nextSequenceToken = putRes["nextSequenceToken"]
|
2018-08-11 14:14:04 +00:00
|
|
|
assert isinstance(nextSequenceToken, six.string_types) == True
|
|
|
|
assert len(nextSequenceToken) == 56
|
2018-04-04 16:24:41 +00:00
|
|
|
events.should.have.length_of(2)
|
|
|
|
|
|
|
|
|
|
|
|
@mock_logs
|
|
|
|
def test_filter_logs_interleaved():
|
2019-10-31 15:44:26 +00:00
|
|
|
conn = boto3.client("logs", "us-west-2")
|
|
|
|
log_group_name = "dummy"
|
|
|
|
log_stream_name = "stream"
|
2018-04-04 16:24:41 +00:00
|
|
|
conn.create_log_group(logGroupName=log_group_name)
|
2019-10-31 15:44:26 +00:00
|
|
|
conn.create_log_stream(logGroupName=log_group_name, logStreamName=log_stream_name)
|
2018-04-04 16:24:41 +00:00
|
|
|
messages = [
|
2019-10-31 15:44:26 +00:00
|
|
|
{"timestamp": 0, "message": "hello"},
|
|
|
|
{"timestamp": 0, "message": "world"},
|
2018-04-04 16:24:41 +00:00
|
|
|
]
|
|
|
|
conn.put_log_events(
|
2019-10-31 15:44:26 +00:00
|
|
|
logGroupName=log_group_name, logStreamName=log_stream_name, logEvents=messages
|
2018-04-04 16:24:41 +00:00
|
|
|
)
|
|
|
|
res = conn.filter_log_events(
|
2019-10-31 15:44:26 +00:00
|
|
|
logGroupName=log_group_name, logStreamNames=[log_stream_name], interleaved=True
|
2018-04-04 16:24:41 +00:00
|
|
|
)
|
2019-10-31 15:44:26 +00:00
|
|
|
events = res["events"]
|
2018-10-01 20:30:23 +00:00
|
|
|
for original_message, resulting_event in zip(messages, events):
|
2019-10-31 15:44:26 +00:00
|
|
|
resulting_event["eventId"].should.equal(str(resulting_event["eventId"]))
|
|
|
|
resulting_event["timestamp"].should.equal(original_message["timestamp"])
|
|
|
|
resulting_event["message"].should.equal(original_message["message"])
|
2018-10-01 20:30:23 +00:00
|
|
|
|
2019-10-18 11:46:27 +00:00
|
|
|
|
|
|
|
@mock_logs
|
|
|
|
def test_filter_logs_raises_if_filter_pattern():
|
2019-10-31 15:44:26 +00:00
|
|
|
if os.environ.get("TEST_SERVER_MODE", "false").lower() == "true":
|
|
|
|
raise SkipTest("Does not work in server mode due to error in Workzeug")
|
|
|
|
conn = boto3.client("logs", "us-west-2")
|
|
|
|
log_group_name = "dummy"
|
|
|
|
log_stream_name = "stream"
|
2019-10-18 11:46:27 +00:00
|
|
|
conn.create_log_group(logGroupName=log_group_name)
|
2019-10-31 15:44:26 +00:00
|
|
|
conn.create_log_stream(logGroupName=log_group_name, logStreamName=log_stream_name)
|
2019-10-18 11:46:27 +00:00
|
|
|
messages = [
|
2019-10-31 15:44:26 +00:00
|
|
|
{"timestamp": 0, "message": "hello"},
|
|
|
|
{"timestamp": 0, "message": "world"},
|
2019-10-18 11:46:27 +00:00
|
|
|
]
|
|
|
|
conn.put_log_events(
|
2019-10-31 15:44:26 +00:00
|
|
|
logGroupName=log_group_name, logStreamName=log_stream_name, logEvents=messages
|
2019-10-18 11:46:27 +00:00
|
|
|
)
|
2020-10-06 05:54:49 +00:00
|
|
|
with pytest.raises(NotImplementedError):
|
2019-10-18 11:46:27 +00:00
|
|
|
conn.filter_log_events(
|
|
|
|
logGroupName=log_group_name,
|
|
|
|
logStreamNames=[log_stream_name],
|
|
|
|
filterPattern='{$.message = "hello"}',
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2019-05-25 09:14:23 +00:00
|
|
|
@mock_logs
|
|
|
|
def test_put_retention_policy():
|
2019-10-31 15:44:26 +00:00
|
|
|
conn = boto3.client("logs", "us-west-2")
|
|
|
|
log_group_name = "dummy"
|
2019-05-25 09:14:23 +00:00
|
|
|
response = conn.create_log_group(logGroupName=log_group_name)
|
|
|
|
|
|
|
|
response = conn.put_retention_policy(logGroupName=log_group_name, retentionInDays=7)
|
|
|
|
|
|
|
|
response = conn.describe_log_groups(logGroupNamePrefix=log_group_name)
|
2019-10-31 15:44:26 +00:00
|
|
|
assert len(response["logGroups"]) == 1
|
|
|
|
assert response["logGroups"][0].get("retentionInDays") == 7
|
2019-05-25 09:14:23 +00:00
|
|
|
|
|
|
|
response = conn.delete_log_group(logGroupName=log_group_name)
|
|
|
|
|
2019-10-18 11:46:27 +00:00
|
|
|
|
2019-05-25 09:14:23 +00:00
|
|
|
@mock_logs
|
|
|
|
def test_delete_retention_policy():
|
2019-10-31 15:44:26 +00:00
|
|
|
conn = boto3.client("logs", "us-west-2")
|
|
|
|
log_group_name = "dummy"
|
2019-05-25 09:14:23 +00:00
|
|
|
response = conn.create_log_group(logGroupName=log_group_name)
|
|
|
|
|
|
|
|
response = conn.put_retention_policy(logGroupName=log_group_name, retentionInDays=7)
|
|
|
|
|
|
|
|
response = conn.describe_log_groups(logGroupNamePrefix=log_group_name)
|
2019-10-31 15:44:26 +00:00
|
|
|
assert len(response["logGroups"]) == 1
|
|
|
|
assert response["logGroups"][0].get("retentionInDays") == 7
|
2019-05-25 09:14:23 +00:00
|
|
|
|
|
|
|
response = conn.delete_retention_policy(logGroupName=log_group_name)
|
|
|
|
|
|
|
|
response = conn.describe_log_groups(logGroupNamePrefix=log_group_name)
|
2019-10-31 15:44:26 +00:00
|
|
|
assert len(response["logGroups"]) == 1
|
|
|
|
assert response["logGroups"][0].get("retentionInDays") == None
|
2019-05-25 09:14:23 +00:00
|
|
|
|
|
|
|
response = conn.delete_log_group(logGroupName=log_group_name)
|
|
|
|
|
2019-08-07 15:37:53 +00:00
|
|
|
|
|
|
|
@mock_logs
|
|
|
|
def test_get_log_events():
|
2019-11-26 22:38:43 +00:00
|
|
|
client = boto3.client("logs", "us-west-2")
|
2019-10-31 15:44:26 +00:00
|
|
|
log_group_name = "test"
|
|
|
|
log_stream_name = "stream"
|
2019-11-26 22:38:43 +00:00
|
|
|
client.create_log_group(logGroupName=log_group_name)
|
|
|
|
client.create_log_stream(logGroupName=log_group_name, logStreamName=log_stream_name)
|
2019-08-07 15:37:53 +00:00
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
events = [{"timestamp": x, "message": str(x)} for x in range(20)]
|
2019-08-07 15:37:53 +00:00
|
|
|
|
2019-11-26 22:38:43 +00:00
|
|
|
client.put_log_events(
|
2019-10-31 15:44:26 +00:00
|
|
|
logGroupName=log_group_name, logStreamName=log_stream_name, logEvents=events
|
2019-08-07 15:37:53 +00:00
|
|
|
)
|
|
|
|
|
2019-11-26 22:38:43 +00:00
|
|
|
resp = client.get_log_events(
|
2019-10-31 15:44:26 +00:00
|
|
|
logGroupName=log_group_name, logStreamName=log_stream_name, limit=10
|
|
|
|
)
|
2019-08-07 15:37:53 +00:00
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
resp["events"].should.have.length_of(10)
|
2019-11-26 22:38:43 +00:00
|
|
|
for i in range(10):
|
|
|
|
resp["events"][i]["timestamp"].should.equal(i + 10)
|
|
|
|
resp["events"][i]["message"].should.equal(str(i + 10))
|
2019-10-31 15:44:26 +00:00
|
|
|
resp["nextForwardToken"].should.equal(
|
2019-11-26 22:38:43 +00:00
|
|
|
"f/00000000000000000000000000000000000000000000000000000019"
|
2019-10-31 15:44:26 +00:00
|
|
|
)
|
|
|
|
resp["nextBackwardToken"].should.equal(
|
2019-11-26 22:38:43 +00:00
|
|
|
"b/00000000000000000000000000000000000000000000000000000010"
|
|
|
|
)
|
|
|
|
|
|
|
|
resp = client.get_log_events(
|
|
|
|
logGroupName=log_group_name,
|
|
|
|
logStreamName=log_stream_name,
|
|
|
|
nextToken=resp["nextBackwardToken"],
|
|
|
|
limit=20,
|
2019-10-31 15:44:26 +00:00
|
|
|
)
|
2019-11-26 22:38:43 +00:00
|
|
|
|
|
|
|
resp["events"].should.have.length_of(10)
|
2019-08-07 15:37:53 +00:00
|
|
|
for i in range(10):
|
2019-10-31 15:44:26 +00:00
|
|
|
resp["events"][i]["timestamp"].should.equal(i)
|
|
|
|
resp["events"][i]["message"].should.equal(str(i))
|
2019-11-26 22:38:43 +00:00
|
|
|
resp["nextForwardToken"].should.equal(
|
|
|
|
"f/00000000000000000000000000000000000000000000000000000009"
|
|
|
|
)
|
|
|
|
resp["nextBackwardToken"].should.equal(
|
|
|
|
"b/00000000000000000000000000000000000000000000000000000000"
|
|
|
|
)
|
|
|
|
|
|
|
|
resp = client.get_log_events(
|
|
|
|
logGroupName=log_group_name,
|
|
|
|
logStreamName=log_stream_name,
|
|
|
|
nextToken=resp["nextBackwardToken"],
|
|
|
|
limit=10,
|
|
|
|
)
|
2019-08-07 15:37:53 +00:00
|
|
|
|
2019-11-26 22:38:43 +00:00
|
|
|
resp["events"].should.have.length_of(0)
|
|
|
|
resp["nextForwardToken"].should.equal(
|
|
|
|
"f/00000000000000000000000000000000000000000000000000000000"
|
|
|
|
)
|
|
|
|
resp["nextBackwardToken"].should.equal(
|
|
|
|
"b/00000000000000000000000000000000000000000000000000000000"
|
|
|
|
)
|
2019-08-07 15:37:53 +00:00
|
|
|
|
2019-11-26 22:38:43 +00:00
|
|
|
resp = client.get_log_events(
|
|
|
|
logGroupName=log_group_name,
|
|
|
|
logStreamName=log_stream_name,
|
|
|
|
nextToken=resp["nextForwardToken"],
|
|
|
|
limit=1,
|
|
|
|
)
|
|
|
|
|
|
|
|
resp["events"].should.have.length_of(1)
|
|
|
|
resp["events"][0]["timestamp"].should.equal(1)
|
|
|
|
resp["events"][0]["message"].should.equal(str(1))
|
|
|
|
resp["nextForwardToken"].should.equal(
|
|
|
|
"f/00000000000000000000000000000000000000000000000000000001"
|
|
|
|
)
|
|
|
|
resp["nextBackwardToken"].should.equal(
|
|
|
|
"b/00000000000000000000000000000000000000000000000000000001"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@mock_logs
|
|
|
|
def test_get_log_events_with_start_from_head():
|
|
|
|
client = boto3.client("logs", "us-west-2")
|
|
|
|
log_group_name = "test"
|
|
|
|
log_stream_name = "stream"
|
|
|
|
client.create_log_group(logGroupName=log_group_name)
|
|
|
|
client.create_log_stream(logGroupName=log_group_name, logStreamName=log_stream_name)
|
|
|
|
|
|
|
|
events = [{"timestamp": x, "message": str(x)} for x in range(20)]
|
|
|
|
|
|
|
|
client.put_log_events(
|
|
|
|
logGroupName=log_group_name, logStreamName=log_stream_name, logEvents=events
|
|
|
|
)
|
|
|
|
|
|
|
|
resp = client.get_log_events(
|
2019-08-07 15:37:53 +00:00
|
|
|
logGroupName=log_group_name,
|
|
|
|
logStreamName=log_stream_name,
|
2019-10-31 15:44:26 +00:00
|
|
|
limit=10,
|
2019-11-26 22:38:43 +00:00
|
|
|
startFromHead=True, # this parameter is only relevant without the usage of nextToken
|
2019-10-31 15:44:26 +00:00
|
|
|
)
|
2019-08-07 15:37:53 +00:00
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
resp["events"].should.have.length_of(10)
|
2019-11-26 22:38:43 +00:00
|
|
|
for i in range(10):
|
|
|
|
resp["events"][i]["timestamp"].should.equal(i)
|
|
|
|
resp["events"][i]["message"].should.equal(str(i))
|
2019-10-31 15:44:26 +00:00
|
|
|
resp["nextForwardToken"].should.equal(
|
2019-11-26 22:38:43 +00:00
|
|
|
"f/00000000000000000000000000000000000000000000000000000009"
|
2019-10-31 15:44:26 +00:00
|
|
|
)
|
|
|
|
resp["nextBackwardToken"].should.equal(
|
|
|
|
"b/00000000000000000000000000000000000000000000000000000000"
|
|
|
|
)
|
2019-11-26 22:38:43 +00:00
|
|
|
|
|
|
|
resp = client.get_log_events(
|
|
|
|
logGroupName=log_group_name,
|
|
|
|
logStreamName=log_stream_name,
|
|
|
|
nextToken=resp["nextForwardToken"],
|
|
|
|
limit=20,
|
|
|
|
)
|
|
|
|
|
|
|
|
resp["events"].should.have.length_of(10)
|
2019-08-07 15:37:53 +00:00
|
|
|
for i in range(10):
|
2019-10-31 15:44:26 +00:00
|
|
|
resp["events"][i]["timestamp"].should.equal(i + 10)
|
|
|
|
resp["events"][i]["message"].should.equal(str(i + 10))
|
2019-11-26 22:38:43 +00:00
|
|
|
resp["nextForwardToken"].should.equal(
|
|
|
|
"f/00000000000000000000000000000000000000000000000000000019"
|
|
|
|
)
|
|
|
|
resp["nextBackwardToken"].should.equal(
|
|
|
|
"b/00000000000000000000000000000000000000000000000000000010"
|
|
|
|
)
|
2019-08-07 15:37:53 +00:00
|
|
|
|
2019-11-26 22:38:43 +00:00
|
|
|
resp = client.get_log_events(
|
2019-08-07 15:37:53 +00:00
|
|
|
logGroupName=log_group_name,
|
|
|
|
logStreamName=log_stream_name,
|
2019-11-26 22:38:43 +00:00
|
|
|
nextToken=resp["nextForwardToken"],
|
2019-10-31 15:44:26 +00:00
|
|
|
limit=10,
|
|
|
|
)
|
2019-08-07 15:37:53 +00:00
|
|
|
|
2019-11-26 22:38:43 +00:00
|
|
|
resp["events"].should.have.length_of(0)
|
|
|
|
resp["nextForwardToken"].should.equal(
|
|
|
|
"f/00000000000000000000000000000000000000000000000000000019"
|
|
|
|
)
|
|
|
|
resp["nextBackwardToken"].should.equal(
|
|
|
|
"b/00000000000000000000000000000000000000000000000000000019"
|
|
|
|
)
|
|
|
|
|
|
|
|
resp = client.get_log_events(
|
|
|
|
logGroupName=log_group_name,
|
|
|
|
logStreamName=log_stream_name,
|
|
|
|
nextToken=resp["nextBackwardToken"],
|
|
|
|
limit=1,
|
|
|
|
)
|
|
|
|
|
|
|
|
resp["events"].should.have.length_of(1)
|
|
|
|
resp["events"][0]["timestamp"].should.equal(18)
|
|
|
|
resp["events"][0]["message"].should.equal(str(18))
|
|
|
|
resp["nextForwardToken"].should.equal(
|
|
|
|
"f/00000000000000000000000000000000000000000000000000000018"
|
|
|
|
)
|
|
|
|
resp["nextBackwardToken"].should.equal(
|
|
|
|
"b/00000000000000000000000000000000000000000000000000000018"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@mock_logs
|
|
|
|
def test_get_log_events_errors():
|
|
|
|
client = boto3.client("logs", "us-west-2")
|
|
|
|
log_group_name = "test"
|
|
|
|
log_stream_name = "stream"
|
|
|
|
client.create_log_group(logGroupName=log_group_name)
|
|
|
|
client.create_log_stream(logGroupName=log_group_name, logStreamName=log_stream_name)
|
|
|
|
|
2020-10-06 05:54:49 +00:00
|
|
|
with pytest.raises(ClientError) as e:
|
2019-11-26 22:38:43 +00:00
|
|
|
client.get_log_events(
|
|
|
|
logGroupName=log_group_name,
|
|
|
|
logStreamName=log_stream_name,
|
|
|
|
nextToken="n/00000000000000000000000000000000000000000000000000000000",
|
|
|
|
)
|
2020-10-06 06:04:09 +00:00
|
|
|
ex = e.value
|
2019-11-26 22:38:43 +00:00
|
|
|
ex.operation_name.should.equal("GetLogEvents")
|
|
|
|
ex.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(400)
|
|
|
|
ex.response["Error"]["Code"].should.equal("InvalidParameterException")
|
|
|
|
ex.response["Error"]["Message"].should.contain(
|
|
|
|
"The specified nextToken is invalid."
|
|
|
|
)
|
|
|
|
|
2020-10-06 05:54:49 +00:00
|
|
|
with pytest.raises(ClientError) as e:
|
2019-11-26 22:38:43 +00:00
|
|
|
client.get_log_events(
|
|
|
|
logGroupName=log_group_name,
|
|
|
|
logStreamName=log_stream_name,
|
|
|
|
nextToken="not-existing-token",
|
|
|
|
)
|
2020-10-06 06:04:09 +00:00
|
|
|
ex = e.value
|
2019-11-26 22:38:43 +00:00
|
|
|
ex.operation_name.should.equal("GetLogEvents")
|
|
|
|
ex.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(400)
|
|
|
|
ex.response["Error"]["Code"].should.equal("InvalidParameterException")
|
|
|
|
ex.response["Error"]["Message"].should.contain(
|
|
|
|
"The specified nextToken is invalid."
|
|
|
|
)
|
2019-09-26 15:20:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mock_logs
|
|
|
|
def test_list_tags_log_group():
|
2019-10-31 15:44:26 +00:00
|
|
|
conn = boto3.client("logs", "us-west-2")
|
|
|
|
log_group_name = "dummy"
|
|
|
|
tags = {"tag_key_1": "tag_value_1", "tag_key_2": "tag_value_2"}
|
2019-09-26 15:20:28 +00:00
|
|
|
|
|
|
|
response = conn.create_log_group(logGroupName=log_group_name)
|
|
|
|
response = conn.list_tags_log_group(logGroupName=log_group_name)
|
2019-10-31 15:44:26 +00:00
|
|
|
assert response["tags"] == {}
|
2019-09-26 15:20:28 +00:00
|
|
|
|
|
|
|
response = conn.delete_log_group(logGroupName=log_group_name)
|
|
|
|
response = conn.create_log_group(logGroupName=log_group_name, tags=tags)
|
|
|
|
response = conn.list_tags_log_group(logGroupName=log_group_name)
|
2019-10-31 15:44:26 +00:00
|
|
|
assert response["tags"] == tags
|
2019-09-26 15:20:28 +00:00
|
|
|
|
|
|
|
response = conn.delete_log_group(logGroupName=log_group_name)
|
2019-09-26 16:09:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mock_logs
|
|
|
|
def test_tag_log_group():
|
2019-10-31 15:44:26 +00:00
|
|
|
conn = boto3.client("logs", "us-west-2")
|
|
|
|
log_group_name = "dummy"
|
|
|
|
tags = {"tag_key_1": "tag_value_1"}
|
2019-09-26 16:09:10 +00:00
|
|
|
response = conn.create_log_group(logGroupName=log_group_name)
|
|
|
|
|
|
|
|
response = conn.tag_log_group(logGroupName=log_group_name, tags=tags)
|
|
|
|
response = conn.list_tags_log_group(logGroupName=log_group_name)
|
2019-10-31 15:44:26 +00:00
|
|
|
assert response["tags"] == tags
|
2019-09-26 16:09:10 +00:00
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
tags_with_added_value = {"tag_key_1": "tag_value_1", "tag_key_2": "tag_value_2"}
|
|
|
|
response = conn.tag_log_group(
|
|
|
|
logGroupName=log_group_name, tags={"tag_key_2": "tag_value_2"}
|
|
|
|
)
|
2019-09-26 16:09:10 +00:00
|
|
|
response = conn.list_tags_log_group(logGroupName=log_group_name)
|
2019-10-31 15:44:26 +00:00
|
|
|
assert response["tags"] == tags_with_added_value
|
2019-09-26 16:09:10 +00:00
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
tags_with_updated_value = {"tag_key_1": "tag_value_XX", "tag_key_2": "tag_value_2"}
|
|
|
|
response = conn.tag_log_group(
|
|
|
|
logGroupName=log_group_name, tags={"tag_key_1": "tag_value_XX"}
|
|
|
|
)
|
2019-09-26 16:09:10 +00:00
|
|
|
response = conn.list_tags_log_group(logGroupName=log_group_name)
|
2019-10-31 15:44:26 +00:00
|
|
|
assert response["tags"] == tags_with_updated_value
|
2019-09-26 16:09:10 +00:00
|
|
|
|
|
|
|
response = conn.delete_log_group(logGroupName=log_group_name)
|
2019-09-26 19:20:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mock_logs
|
|
|
|
def test_untag_log_group():
|
2019-10-31 15:44:26 +00:00
|
|
|
conn = boto3.client("logs", "us-west-2")
|
|
|
|
log_group_name = "dummy"
|
2019-09-26 19:20:53 +00:00
|
|
|
response = conn.create_log_group(logGroupName=log_group_name)
|
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
tags = {"tag_key_1": "tag_value_1", "tag_key_2": "tag_value_2"}
|
2019-09-26 19:20:53 +00:00
|
|
|
response = conn.tag_log_group(logGroupName=log_group_name, tags=tags)
|
|
|
|
response = conn.list_tags_log_group(logGroupName=log_group_name)
|
2019-10-31 15:44:26 +00:00
|
|
|
assert response["tags"] == tags
|
2019-09-26 19:20:53 +00:00
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
tags_to_remove = ["tag_key_1"]
|
|
|
|
remaining_tags = {"tag_key_2": "tag_value_2"}
|
2019-09-26 19:20:53 +00:00
|
|
|
response = conn.untag_log_group(logGroupName=log_group_name, tags=tags_to_remove)
|
|
|
|
response = conn.list_tags_log_group(logGroupName=log_group_name)
|
2019-10-31 15:44:26 +00:00
|
|
|
assert response["tags"] == remaining_tags
|
2019-09-26 19:20:53 +00:00
|
|
|
|
|
|
|
response = conn.delete_log_group(logGroupName=log_group_name)
|
2020-05-12 12:34:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mock_logs
|
|
|
|
def test_describe_subscription_filters():
|
|
|
|
# given
|
|
|
|
client = boto3.client("logs", "us-east-1")
|
|
|
|
log_group_name = "/test"
|
|
|
|
client.create_log_group(logGroupName=log_group_name)
|
|
|
|
|
|
|
|
# when
|
|
|
|
response = client.describe_subscription_filters(logGroupName=log_group_name)
|
|
|
|
|
|
|
|
# then
|
|
|
|
response["subscriptionFilters"].should.have.length_of(0)
|
|
|
|
|
|
|
|
|
|
|
|
@mock_logs
|
|
|
|
def test_describe_subscription_filters_errors():
|
|
|
|
# given
|
|
|
|
client = boto3.client("logs", "us-east-1")
|
|
|
|
|
|
|
|
# when
|
2020-10-06 05:54:49 +00:00
|
|
|
with pytest.raises(ClientError) as e:
|
2020-05-12 12:34:10 +00:00
|
|
|
client.describe_subscription_filters(logGroupName="not-existing-log-group",)
|
|
|
|
|
|
|
|
# then
|
2020-10-06 06:04:09 +00:00
|
|
|
ex = e.value
|
2020-05-12 12:34:10 +00:00
|
|
|
ex.operation_name.should.equal("DescribeSubscriptionFilters")
|
|
|
|
ex.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(400)
|
|
|
|
ex.response["Error"]["Code"].should.contain("ResourceNotFoundException")
|
|
|
|
ex.response["Error"]["Message"].should.equal(
|
|
|
|
"The specified log group does not exist"
|
|
|
|
)
|
2020-10-21 08:47:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mock_logs
|
|
|
|
def test_describe_log_groups_paging():
|
|
|
|
client = boto3.client("logs", "us-east-1")
|
|
|
|
|
|
|
|
group_names = [
|
|
|
|
"/aws/lambda/lowercase-dev",
|
|
|
|
"/aws/lambda/FileMonitoring",
|
|
|
|
"/aws/events/GetMetricData",
|
|
|
|
"/aws/lambda/fileAvailable",
|
|
|
|
]
|
|
|
|
|
|
|
|
for name in group_names:
|
|
|
|
client.create_log_group(logGroupName=name)
|
|
|
|
|
|
|
|
resp = client.describe_log_groups()
|
|
|
|
resp["logGroups"].should.have.length_of(4)
|
|
|
|
resp.should_not.have.key("nextToken")
|
|
|
|
|
|
|
|
resp = client.describe_log_groups(limit=2)
|
|
|
|
resp["logGroups"].should.have.length_of(2)
|
|
|
|
resp["nextToken"].should.equal("/aws/lambda/FileMonitoring")
|
|
|
|
|
|
|
|
resp = client.describe_log_groups(nextToken=resp["nextToken"], limit=1)
|
|
|
|
resp["logGroups"].should.have.length_of(1)
|
|
|
|
resp["nextToken"].should.equal("/aws/lambda/fileAvailable")
|
|
|
|
|
|
|
|
resp = client.describe_log_groups(nextToken=resp["nextToken"])
|
|
|
|
resp["logGroups"].should.have.length_of(1)
|
|
|
|
resp["logGroups"][0]["logGroupName"].should.equal("/aws/lambda/lowercase-dev")
|
|
|
|
resp.should_not.have.key("nextToken")
|
|
|
|
|
|
|
|
resp = client.describe_log_groups(nextToken="invalid-token")
|
|
|
|
resp["logGroups"].should.have.length_of(0)
|
|
|
|
resp.should_not.have.key("nextToken")
|