Copyright © 2021 Pavel Tisnovsky
Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. Unit tests for parquet_output_cluster_info module.
import sys
from datetime import datetime
import pytest
from common import validate
from parquet_output_rule_hits import main, schema
from voluptuous import Invalid
Provide standard schema to check messages.
@pytest.fixture
def validation_schema():
return schema
verbosity level
verbose = (True, False)
attributes to check, update, and remove
attribute = (
"cluster_id",
"rule_id",
"collected_at",
"archive_path",
)
Provide correct message to be tested.
@pytest.fixture
def correct_message():
return {
"cluster_id": b"123e4567-e89b-12d3-a456-426614174000",
"rule_id": b"pods_check|POD_ISSUE",
"collected_at": datetime.now(),
"archive_path":
b"archives/compressed/00/00000000-0000-0000-0000-000000000000/202102/08/002219.tar.gz",
}
Test the main function when -h parameter is given.
def test_main_help():
sys.argv.append("-h")
with pytest.raises(SystemExit) as excinfo:
main()
assert excinfo.value.code == 0
Test the main function when -i parameter is given.
def test_main_input():
sys.argv.append("-i test")
with pytest.raises(SystemExit) as excinfo:
main()
assert excinfo.value.code == 0
Test the validation for improper (no) payload.
@pytest.mark.parametrize("verbose", verbose)
def test_validate_no_payload(validation_schema, verbose):
it should fail
with pytest.raises(ValueError):
validate(schema, None, verbose)
Test the validation for correct payload.
@pytest.mark.parametrize("verbose", verbose)
def test_validate_correct_message(validation_schema, verbose, correct_message):
it should not fail
validate(schema, correct_message, verbose)
Test the validation for improper payload.
@pytest.mark.parametrize("verbose", verbose)
def test_validate_message_without_cluster_id_attribute(validation_schema, verbose, correct_message):
del correct_message["cluster_id"]
it should fail
with pytest.raises(Invalid):
validate(schema, correct_message, verbose)
Test the validation for improper payload.
@pytest.mark.parametrize("attribute", attribute)
@pytest.mark.parametrize("verbose", verbose)
def test_validate_message_without_attributes(validation_schema, verbose, correct_message,
attribute):
del correct_message[attribute]
it should fail
with pytest.raises(Invalid):
validate(schema, correct_message, verbose)
Test the validation for improper payload.
@pytest.mark.parametrize("attribute", attribute)
@pytest.mark.parametrize("verbose", verbose)
def test_validate_message_wrong_attributes(validation_schema, verbose, correct_message, attribute):
check with string not representing number
correct_message[attribute] = b"foobar"
it should fail
with pytest.raises(Invalid):
validate(schema, correct_message, verbose)
check with number
correct_message[attribute] = 123456
it should fail
with pytest.raises(Invalid):
validate(schema, correct_message, verbose)
check with different data type
correct_message[attribute] = []
it should fail
with pytest.raises(Invalid):
validate(schema, correct_message, verbose)