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

fix: prevent unnecessary updates to the globalConfig file #1535

Merged
merged 2 commits into from
Jan 21, 2025
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
36 changes: 23 additions & 13 deletions splunk_add_on_ucc_framework/global_config_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@

def _version_tuple(version: str) -> Tuple[str, ...]:
"""
convert string into tuple to compare version
Convert string into tuple to compare versions.

Args:
version_str : raw string
version: raw string
Returns:
tuple : version into tupleformat
tuple: version into tuple format
"""
filled = []
for point in version.split("."):
Expand Down Expand Up @@ -123,16 +123,26 @@ def _handle_xml_dashboard_update(global_config: global_config_lib.GlobalConfig)

def handle_global_config_update(global_config: global_config_lib.GlobalConfig) -> None:
"""Handle changes in globalConfig file."""
current_schema_version = global_config.schema_version or "0.0.0"

logger.info(f"Current globalConfig schema version is {current_schema_version}")

if _version_tuple(global_config.meta.get("_uccVersion", "0.0.0")) >= _version_tuple(
"5.52.0"
):
# we hard-code the value of 5.52.0 as this feature would be shipped in that version
version = current_schema_version
else:
version = global_config.schema_version or "0.0.0"
logger.info(f"Current globalConfig schema version is {version}")

allowed_versions_of_schema_version = {
"0.0.0",
"0.0.1",
"0.0.2",
"0.0.3",
"0.0.4",
"0.0.5",
"0.0.6",
"0.0.7",
"0.0.8",
"0.0.9",
}

if version not in allowed_versions_of_schema_version:
logger.warning(
"Schema version is not in the allowed versions, setting it to 0.0.0"
)
version = "0.0.0"

if _version_tuple(version) < _version_tuple("0.0.1"):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@
"meta": {
"name": "test_addon",
"restRoot": "test_addon",
"version": "5.55.0+dcdb201e",
"version": "5.56.0+12f4cd8cd",
"displayName": "This is my add-on",
"schemaVersion": "0.0.9"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1822,7 +1822,7 @@
"meta": {
"name": "Splunk_TA_UCCExample",
"restRoot": "splunk_ta_uccexample",
"version": "5.55.0+36810ea3",
"version": "5.56.0+12f4cd8cd",
"displayName": "Splunk UCC test Add-on",
"schemaVersion": "0.0.9",
"supportedThemes": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1171,7 +1171,7 @@
"meta": {
"name": "Splunk_TA_UCCExample",
"restRoot": "splunk_ta_uccexample",
"version": "5.55.0+dcdb201e",
"version": "5.56.0+12f4cd8cd",
"displayName": "Splunk UCC test Add-on",
"schemaVersion": "0.0.9"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@
"meta": {
"name": "Splunk_TA_UCCExample",
"restRoot": "splunk_ta_uccexample",
"version": "5.55.0+dcdb201e",
"version": "5.56.0+12f4cd8cd",
"displayName": "Splunk UCC test Add-on",
"schemaVersion": "0.0.9"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"meta": {
"name": "Splunk_TA_UCCExample",
"restRoot": "splunk_ta_uccexample",
"version": "5.55.0+dcdb201e",
"version": "5.56.0+12f4cd8cd",
"displayName": "Splunk UCC test Add-on",
"schemaVersion": "0.0.9"
}
Expand Down
25 changes: 25 additions & 0 deletions tests/unit/test_global_config_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import tests.unit.helpers as helpers
from splunk_add_on_ucc_framework.global_config_update import (
_version_tuple,
_handle_biased_terms_update,
_handle_dropping_api_version_update,
_handle_xml_dashboard_update,
Expand All @@ -12,12 +13,24 @@
_dump_with_migrated_entities,
_stop_build_on_placeholder_usage,
_dump_enable_from_global_config,
handle_global_config_update,
)
from splunk_add_on_ucc_framework.entity import IntervalEntity
from splunk_add_on_ucc_framework import global_config as global_config_lib
from splunk_add_on_ucc_framework.exceptions import GlobalConfigValidatorException


@pytest.mark.parametrize(
"version, expected",
[
("5.52.0", ("00000005", "00000052", "00000000")),
("0.0.9", ("00000000", "00000000", "00000009")),
],
)
def test_version_tuple(version, expected):
assert _version_tuple(version) == expected


@pytest.mark.parametrize(
"filename",
[
Expand Down Expand Up @@ -236,3 +249,15 @@ def test_dump_enable_from_global_config_enable_absent(tmp_path, caplog):

assert expected_schema_version == global_config.schema_version
assert caplog.text == ""


def test_handle_global_config_update_when_valid_config(tmp_path):
tmp_file_gc = tmp_path / "globalConfig.json"

helpers.copy_testdata_gc_to_tmp_file(tmp_file_gc, "valid_config.json")
global_config = global_config_lib.GlobalConfig(str(tmp_file_gc))
expected_schema_version = "0.0.9"

handle_global_config_update(global_config)

assert global_config.schema_version == expected_schema_version
Loading