Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add breaking test case #25

Merged
merged 10 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ jobs:
python-version: ["3.7", "3.8", "3.9", "3.10", "3.11"]

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
Expand All @@ -29,7 +29,7 @@ jobs:
coverage report --show-missing --fail-under=99.0
- name: Lint with flake8
run: |
flake8 .
flake8 gcp_flowlogs_reader tests
- name: Check formatting with black
if: "matrix.python-version == '3.8'"
run: |
Expand All @@ -43,7 +43,7 @@ jobs:
twine check dist/*
- name: Upload packages
if: "matrix.python-version == '3.8'"
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v4
with:
name: gcp-flowlogs-reader-packages
path: dist/*
7 changes: 6 additions & 1 deletion gcp_flowlogs_reader/gcp_flowlogs_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ class ResourceLabels(NamedTuple):
subnetwork_name: str


def safe_tuple_from_dict(cls, attrs):
attr_payload = {k: attrs[k] for k in cls._fields}
return cls(**attr_payload)


class FlowRecord:
src_ip: Union[IPv4Address, IPv6Address]
src_port: int
Expand Down Expand Up @@ -115,7 +120,7 @@ def __init__(self, entry: StructEntry):
('dest_location', GeographicDetails),
]:
try:
value = cls(**flow_payload[attr])
value = safe_tuple_from_dict(cls, flow_payload[attr])
except (KeyError, TypeError):
setattr(self, attr, None)
else:
Expand Down
3 changes: 2 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = gcp_flowlogs_reader
version = 0.9.0
version = 2.0.0
license = Apache
url = https://github.com/obsrvbl-oss/gcp-flowlogs-reader
description = Reader for Google Cloud VPC Flow Logs
Expand All @@ -27,6 +27,7 @@ python_requires = >=3.6
install_requires =
google-cloud-logging < 2.0
google-cloud-resource-manager
six

[options.packages.find]
exclude =
Expand Down
46 changes: 40 additions & 6 deletions tests/test_gcp_flowlogs_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
GeographicDetails,
ResourceLabels,
)
from gcp_flowlogs_reader.gcp_flowlogs_reader import safe_tuple_from_dict


PREFIX = 'gcp_flowlogs_reader.gcp_flowlogs_reader.{}'.format
SAMPLE_PAYLOADS = [
Expand All @@ -44,6 +46,7 @@
'dest_vpc': {
'project_id': 'yoyodyne-102010',
'subnetwork_name': 'yoyo-vpc-1',
'subnetwork_region': 'sunnydale1',
'vpc_name': 'yoyo-vpc-1',
},
'end_time': '2018-04-03T13:47:38.401Z',
Expand Down Expand Up @@ -85,6 +88,7 @@
'src_vpc': {
'project_id': 'yoyodyne-102010',
'subnetwork_name': 'yoyo-vpc-1',
'subnetwork_region': 'sunnydale2',
'vpc_name': 'yoyo-vpc-1',
},
'start_time': '2018-04-03T13:47:32.805417512Z',
Expand Down Expand Up @@ -220,10 +224,23 @@ def test_init_outbound(self):
('rtt_msec', 61),
('reporter', 'DEST'),
('src_instance', None),
('dest_instance', InstanceDetails(**SAMPLE_PAYLOADS[0]['dest_instance'])),
(
'dest_instance',
safe_tuple_from_dict(
InstanceDetails, SAMPLE_PAYLOADS[0]['dest_instance']
),
),
('src_vpc', None),
('dest_vpc', VpcDetails(**SAMPLE_PAYLOADS[0]['dest_vpc'])),
('src_location', GeographicDetails(**SAMPLE_PAYLOADS[0]['src_location'])),
(
'dest_vpc',
safe_tuple_from_dict(VpcDetails, SAMPLE_PAYLOADS[0]['dest_vpc']),
),
(
'src_location',
safe_tuple_from_dict(
GeographicDetails, SAMPLE_PAYLOADS[0]['src_location']
),
),
('dest_location', None),
]:
with self.subTest(attr=attr):
Expand All @@ -245,12 +262,25 @@ def test_init_inbound(self):
('packets_sent', 6),
('rtt_msec', None),
('reporter', 'SRC'),
('src_instance', InstanceDetails(**SAMPLE_PAYLOADS[1]['src_instance'])),
(
'src_instance',
safe_tuple_from_dict(
InstanceDetails, SAMPLE_PAYLOADS[1]['src_instance']
),
),
('dest_instance', None),
('src_vpc', VpcDetails(**SAMPLE_PAYLOADS[1]['src_vpc'])),
(
'src_vpc',
safe_tuple_from_dict(VpcDetails, SAMPLE_PAYLOADS[1]['src_vpc']),
),
('dest_vpc', None),
('src_location', None),
('dest_location', GeographicDetails(**SAMPLE_PAYLOADS[1]['dest_location'])),
(
'dest_location',
safe_tuple_from_dict(
GeographicDetails, SAMPLE_PAYLOADS[1]['dest_location']
),
),
]:
with self.subTest(attr=attr):
actual = getattr(flow_record, attr)
Expand Down Expand Up @@ -317,6 +347,10 @@ def test_to_dict(self):
]:
with self.subTest(attr=attr):
actual = flow_dict[attr]
if isinstance(expected, dict):
expected = {
k: v for k, v in expected.items() if k != 'subnetwork_region'
}
self.assertEqual(actual, expected)

def test_from_payload(self):
Expand Down