Skip to content

Commit

Permalink
test(unit/report): Add report metadata sanitizing and flattening tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gfieni committed Aug 1, 2024
1 parent 9716cec commit 194d70f
Showing 1 changed file with 48 additions and 37 deletions.
85 changes: 48 additions & 37 deletions tests/unit/report/test_report.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
# Copyright (c) 2021, INRIA
# Copyright (c) 2021, University of Lille
# All rights reserved.

#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:

#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.

#
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.

#
# * Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.

#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
Expand All @@ -27,30 +27,20 @@
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import pytest

from powerapi.report import Report
from datetime import datetime


@pytest.fixture()
def basic_report():
return Report(timestamp=datetime.strptime('1970-09-01T09:09:10.543', "%Y-%m-%dT%H:%M:%S.%f"), sensor='toto',
target='all', metadata={"tag": 1})


@pytest.fixture()
def expected_json_report(basic_report):
return {'timestamp': basic_report.timestamp,
'sensor': basic_report.sensor,
'target': basic_report.target,
'metadata': basic_report.metadata}
from powerapi.report import Report


def test_creating_report_with_metadata():
report = Report(timestamp=datetime.strptime('1970-09-01T09:09:10.543', "%Y-%m-%dT%H:%M:%S.%f"), sensor='toto',
target='all', metadata={"tag": 1})
assert report.metadata["tag"] == 1
"""
Test creating a report with metadata.
"""
report = Report(datetime.now(), 'pytest', 'test', {'tag1': 1, 'tag2': .2, 'tag3': '3'})

assert report.metadata["tag1"] == 1
assert report.metadata["tag2"] == .2
assert report.metadata["tag3"] == '3'


def test_create_two_report_without_metadata_metadata_are_different():
Expand All @@ -65,18 +55,39 @@ def test_create_two_report_without_metadata_metadata_are_different():
assert a.metadata != b.metadata


def test_to_json(basic_report, expected_json_report):

json = Report.to_json(report=basic_report)
assert 'sender_name' not in json
assert 'dispatcher_report_id' not in json
assert json == expected_json_report

def test_sanitize_tags_name():
"""
Test sanitizing tag names from the metadata dictionary.
"""
tags = ['test-tag', 'app.kubernetes.io/name', 'helm.sh/chart']
sanitized_tags = Report.sanitize_tags_name(tags)

def test_to_json_with_dispatcher_report_id(basic_report, expected_json_report):
basic_report.dispatcher_report_id = 10
assert len(sanitized_tags) == len(tags)
assert sanitized_tags['test-tag'] == 'test_tag'
assert sanitized_tags['app.kubernetes.io/name'] == 'app_kubernetes_io_name'
assert sanitized_tags['helm.sh/chart'] == 'helm_sh_chart'

json = Report.to_json(report=basic_report)
assert 'sender_name' not in json
assert 'dispatcher_report_id' not in json
assert json == expected_json_report
def test_flatten_metadata_dict():
"""
Test flattening a report metadata dictionary.
"""
report_metadata = {
'scope': 'cpu',
'socket': 0,
'formula': '0000000000000000000000000000000000000000',
'k8s': {
'app.kubernetes.io/name': 'test',
'app.kubernetes.io/instance': 'test-abcxyz',
'app.kubernetes.io/managed-by': 'pytest',
'helm.sh/chart': 'powerapi-pytest-1.0.0'
}
}
flattened_metadata = Report.flatten_tags(report_metadata)

assert flattened_metadata['scope'] == 'cpu'
assert flattened_metadata['socket'] == 0
assert flattened_metadata['formula'] == '0000000000000000000000000000000000000000'
assert flattened_metadata['k8s/app.kubernetes.io/name'] == 'test'
assert flattened_metadata['k8s/app.kubernetes.io/instance'] == 'test-abcxyz'
assert flattened_metadata['k8s/app.kubernetes.io/managed-by'] == 'pytest'
assert flattened_metadata['k8s/helm.sh/chart'] == 'powerapi-pytest-1.0.0'

0 comments on commit 194d70f

Please sign in to comment.