Skip to content

Commit

Permalink
feat: add validation for the splunktaucclib to be included (#549)
Browse files Browse the repository at this point in the history
  • Loading branch information
artemrys authored Nov 7, 2022
1 parent 0514cf6 commit b7eacb6
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 15 deletions.
9 changes: 8 additions & 1 deletion splunk_add_on_ucc_framework/commands/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
utils,
)
from splunk_add_on_ucc_framework.install_python_libraries import (
SplunktaucclibNotFound,
install_python_libraries,
)
from splunk_add_on_ucc_framework.start_alert_build import alert_build
Expand Down Expand Up @@ -535,7 +536,13 @@ def generate(source, config, ta_version, outputdir=None, python_binary_name="pyt
)
ucc_lib_target = os.path.join(outputdir, ta_name, "lib")
logger.info(f"Install add-on requirements into {ucc_lib_target} from {source}")
install_python_libraries(source, ucc_lib_target, python_binary_name)
try:
install_python_libraries(
source, ucc_lib_target, python_binary_name, includes_ui=True
)
except SplunktaucclibNotFound as e:
logger.error(str(e))
sys.exit(1)

_replace_token(ta_name, outputdir)

Expand Down
56 changes: 42 additions & 14 deletions splunk_add_on_ucc_framework/install_python_libraries.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,58 @@
logger = logging.getLogger("ucc_gen")


def install_python_libraries(path: str, ucc_lib_target: str, python_binary_name: str):
logger.info(f" Checking for requirements in {path}")
class SplunktaucclibNotFound(Exception):
pass


def _check_ucc_library_in_requirements_file(path_to_requirements: str) -> bool:
with open(path_to_requirements) as f_reqs:
content = f_reqs.readlines()
for line in content:
if "splunktaucclib" in line:
return True
return False


def install_python_libraries(
path: str, ucc_lib_target: str, python_binary_name: str, includes_ui: bool = False
):
if os.path.exists(os.path.join(path, "lib", "requirements.txt")):
logger.info(" Uses common requirements")
install_libraries(
os.path.join(path, "lib", "requirements.txt"),
ucc_lib_target,
python_binary_name,
)
path_to_reqs_file = os.path.join(path, "lib", "requirements.txt")
elif os.path.exists(
os.path.join(os.path.abspath(os.path.join(path, os.pardir)), "requirements.txt")
):
logger.info(" Uses common requirements")
path_to_reqs_file = os.path.join(
os.path.abspath(os.path.join(path, os.pardir)), "requirements.txt"
)
else:
path_to_reqs_file = None
if path_to_reqs_file is not None:
logger.info(f"Installing requirements from {path_to_reqs_file}")
if includes_ui:
ucc_library_present = _check_ucc_library_in_requirements_file(
path_to_reqs_file
)
if not ucc_library_present:
raise SplunktaucclibNotFound(
f"splunktaucclib is not found in {path_to_reqs_file}. "
f"Please add it there because this add-on has UI."
)
install_libraries(
os.path.join(
os.path.abspath(os.path.join(path, os.pardir)), "requirements.txt"
),
path_to_reqs_file,
ucc_lib_target,
python_binary_name,
)
else:
logger.info(" Not using common requirements")
logger.info("Could not find requirements file, nothing to install")

packages_to_remove = ["setuptools", "bin", "pip", "distribute", "wheel"]
packages_to_remove = (
"setuptools",
"bin",
"pip",
"distribute",
"wheel",
)
remove_package_from_installed_path(
ucc_lib_target,
packages_to_remove,
Expand Down
71 changes: 71 additions & 0 deletions tests/unit/test_install_python_libraries.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,68 @@
import stat
from unittest import mock

import pytest

from splunk_add_on_ucc_framework.install_python_libraries import (
SplunktaucclibNotFound,
_check_ucc_library_in_requirements_file,
install_libraries,
install_python_libraries,
remove_execute_bit,
remove_package_from_installed_path,
)


@pytest.mark.parametrize(
"requirements_content,expected_result",
[
("", False),
("splunk-sdk", False),
("splunk-sdk\n", False),
("splunktaucclib", True),
("splunktaucclib\n", True),
("splunktaucclib==6.0.0\n", True),
("solnlib\nsplunktaucclib\n", True),
("solnlib==5.0.0\nsplunktaucclib==6.0.0\n", True),
(
"""splunktalib==2.2.6; python_version >= "3.7" and python_version < "4.0" \
--hash=sha256:bba70ac7407cdedcb45437cb152ac0e43aae16b978031308e6bec548d3543119 \
--hash=sha256:8d58d697a842319b4c675557b0cc4a9c68e8d909389a98ed240e2bb4ff358d31
splunktaucclib==5.0.7; python_version >= "3.7" and python_version < "4.0" \
--hash=sha256:3ddc1276c41c809c16ae810cb20e9eb4abd2f94dba5ddf460cf9c49b50f659ac \
--hash=sha256:a1e3f710fcb0b24dff8913e6e5df0d36f0693b7f3ed7c0a9a43b08372b08eb90""",
True,
),
(
"""splunktaucclib==5.0.7; python_version >= "3.7" and python_version < "4.0" \
--hash=sha256:3ddc1276c41c809c16ae810cb20e9eb4abd2f94dba5ddf460cf9c49b50f659ac \
--hash=sha256:a1e3f710fcb0b24dff8913e6e5df0d36f0693b7f3ed7c0a9a43b08372b08eb90""",
True,
),
(
"""sortedcontainers==2.4.0; python_version >= "3.7" and python_version < "4.0" \
--hash=sha256:a163dcaede0f1c021485e957a39245190e74249897e2ae4b2aa38595db237ee0 \
--hash=sha256:25caa5a06cc30b6b83d11423433f65d1f9d76c4c6a0c90e3379eaa43b9bfdb88
splunk-sdk==1.7.1 \
--hash=sha256:4d0de12a87395f28f2a0c90b179882072a39a1f09a3ec9e79ce0de7a16220fe1""",
False,
),
],
)
def test_check_ucc_library_in_requirements_file(
tmp_path, requirements_content, expected_result
):
tmp_lib_path = tmp_path / "lib"
tmp_lib_path.mkdir()
tmp_lib_reqs_file = tmp_lib_path / "requirements.txt"
tmp_lib_reqs_file.write_text(requirements_content)

assert (
_check_ucc_library_in_requirements_file(str(tmp_lib_reqs_file))
== expected_result
)


@mock.patch("os.system", autospec=True)
@mock.patch("os.path.exists", autospec=True)
def test_install_libraries(mock_os_path_exists, mock_os_system):
Expand All @@ -50,6 +105,22 @@ def test_install_libraries(mock_os_path_exists, mock_os_system):
)


def test_install_libraries_when_no_splunktaucclib_is_present_but_has_ui(tmp_path):
tmp_ucc_lib_target = tmp_path / "ucc-lib-target"
tmp_lib_path = tmp_path / "lib"
tmp_lib_path.mkdir()
tmp_lib_reqs_file = tmp_lib_path / "requirements.txt"
tmp_lib_reqs_file.write_text("solnlib\nsplunk-sdk\n")

with pytest.raises(SplunktaucclibNotFound):
install_python_libraries(
str(tmp_path),
str(tmp_ucc_lib_target),
python_binary_name="python3",
includes_ui=True,
)


def test_remove_package_from_installed_path(tmp_path):
tmp_lib_path = tmp_path / "lib"
tmp_lib_path.mkdir()
Expand Down

0 comments on commit b7eacb6

Please sign in to comment.