Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
updates
  • Loading branch information
abikouo committed Apr 12, 2023
commit 99539b463f0fc3a95425be2343b2192ef9ef0e64
35 changes: 6 additions & 29 deletions .github/actions/ansible_test_splitter/list_changed_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"""Define collection module for list_changed_targets executable."""

import ast
import logging
import os
import re
import subprocess
Expand All @@ -18,12 +17,6 @@
import yaml


FORMAT = "[%(asctime)s] - %(message)s"
logging.basicConfig(format=FORMAT)
logger = logging.getLogger("list_changed_common")
logger.setLevel(logging.DEBUG)


def read_collection_name(collection_path: PosixPath) -> str:
"""Read collection namespace from galaxy.yml.

Expand Down Expand Up @@ -533,24 +526,6 @@ def equal_share(targets: list[Target], nbchunks: int) -> list[dict[str, Any]]:
return [{"total": total_data[i], "targets": targets_data[i]} for i in range(nbchunks)]


def parse_collection_info(element: str) -> tuple[PosixPath, str]:
"""Get collection path and ref from input string.

:param element: input string, e.g: './ansible-network:stable-2.1'
:returns: a tuple of collection path and reference branch
:raises ValueError: when element is not matching expecting format or path does not exist.
"""
info = element.split(":")
if len(info) != 2:
raise ValueError(
f"The following '{element}' is not a valid format for collection definition."
)
col_path, ref = info[0], info[1]
if not PosixPath(col_path).exists():
raise ValueError(f"The following path '{col_path}' does not exit.")
return PosixPath(col_path), ref


def read_test_all_the_targets() -> bool:
"""Test if all targets should be executed.

Expand Down Expand Up @@ -595,11 +570,13 @@ def read_targets_to_test() -> dict[str, list[str]]:
return targets_to_test


def read_collections_to_test() -> list[tuple[PosixPath, str]]:
def read_collections_to_test() -> list[PosixPath]:
"""Read module parameters from environment variables.

:returns: a list of parameters to execute the module
"""
collections_to_tests = os.environ.get("COLLECTIONS_TO_TEST", "").replace("\n", ",").split(",")
logger.info("collections_to_tests => %s", collections_to_tests)
return list(map(parse_collection_info, [x for x in collections_to_tests if x.strip()]))
return [
PosixPath(path)
for path in os.environ.get("COLLECTIONS_TO_TEST", "").replace("\n", ",").split(",")
if path.strip()
]
15 changes: 5 additions & 10 deletions .github/actions/ansible_test_splitter/list_changed_targets.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"""Script to list target to test for a pull request."""

import json
import logging
import os

from pathlib import PosixPath
Expand All @@ -20,12 +19,6 @@
from list_changed_common import read_total_jobs


FORMAT = "[%(asctime)s] - %(message)s"
logging.basicConfig(format=FORMAT)
logger = logging.getLogger("list_changed_targets")
logger.setLevel(logging.DEBUG)


class ListChangedTargets:
"""A class used to list changed impacted for a pull request."""

Expand All @@ -36,6 +29,7 @@ def __init__(self) -> None:

self.test_all_the_targets = read_test_all_the_targets()
self.targets_to_test = read_targets_to_test()
self.base_ref = os.environ.get("PULL_REQUEST_BASE_REF", "")

def make_change_targets_to_test(self, collections: list[Collection]) -> dict[str, list[str]]:
"""Create change for a specific target to test.
Expand Down Expand Up @@ -90,7 +84,7 @@ def _add_changed_target(
for collection in collections:
collection.add_target_to_plan(plugin_file_name)

for whc in [WhatHaveChanged(path, ref) for path, ref in self.collections_to_test]:
for whc in [WhatHaveChanged(path, self.base_ref) for path in self.collections_to_test]:
listed_changes[whc.collection_name] = {
"modules": [],
"inventory": [],
Expand Down Expand Up @@ -119,14 +113,15 @@ def _add_changed_target(
for target in whc.targets():
_add_changed_target(whc.collection_name, target, "targets")

print("----------- Listed Changes -----------\n", json.dumps(listed_changes, indent=2))
return {x: make_unique(y["targets"]) for x, y in listed_changes.items()}

def run(self) -> str:
"""List changes and divide targets into chunk.

:returns: resulting string of targets divide into chunks
"""
collections = [Collection(p) for p, _ in self.collections_to_test]
collections = [Collection(p) for p in self.collections_to_test]

if self.targets_to_test:
changes = self.make_change_targets_to_test(collections)
Expand All @@ -135,7 +130,7 @@ def run(self) -> str:
else:
changes = self.make_changed_targets(collections)

logger.info("changes => %s", json.dumps(changes))
print("----------- Changes -----------\n", json.dumps(changes, indent=2))
egs = ElGrandeSeparator(collections, self.total_jobs)
return egs.output()

Expand Down
39 changes: 3 additions & 36 deletions .github/actions/ansible_test_splitter/test_list_changed_targets.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
from list_changed_common import WhatHaveChanged
from list_changed_common import list_pyimport
from list_changed_common import make_unique
from list_changed_common import parse_collection_info
from list_changed_common import read_collection_name
from list_changed_common import read_collections_to_test
from list_changed_common import read_targets_to_test
Expand Down Expand Up @@ -336,25 +335,6 @@ def test_make_unique() -> None:
assert make_unique(["a", "b"]) == ["a", "b"]


def test_parse_collection_info(tmp_path: PosixPath) -> None:
"""Test parse_collection_info function.

:param tmp_path: temporary path patch
"""
expected = "The following 'some_path' is not a valid format for collection definition."
with pytest.raises(ValueError, match=expected):
parse_collection_info("some_path")

expected = "The following path 'some_path' does not exit."
with pytest.raises(ValueError, match=expected):
parse_collection_info("some_path:main")

collection_dir = tmp_path / "collection_path"
collection_dir.mkdir()
collection_dir_name = str(collection_dir)
assert parse_collection_info(f"{collection_dir_name}:main") == (collection_dir, "main")


def test_read_test_all_the_targets(monkeypatch: pytest.MonkeyPatch) -> None:
"""Test read_test_all_the_targets function.

Expand Down Expand Up @@ -431,24 +411,11 @@ def test_read_targets_to_test(monkeypatch: pytest.MonkeyPatch) -> None:
}


@patch("list_changed_common.parse_collection_info")
def test_read_collections_to_test(
m_parse_collection_info: MagicMock, monkeypatch: pytest.MonkeyPatch
) -> None:
def test_read_collections_to_test(monkeypatch: pytest.MonkeyPatch) -> None:
"""Test read_collections_to_test function.

:param m_parse_collection_info: parse_collection_info patched method
:param monkeypatch: monkey patch
"""
m_parse_collection_info.side_effect = lambda x: (
PosixPath(x.split(":", maxsplit=1)[0]),
x.split(":", maxsplit=1)[1],
)

collection_to_test = "col1:main,col2:stable-1\n ,col3:release"
collection_to_test = "col1,col2\n ,col3"
monkeypatch.setenv("COLLECTIONS_TO_TEST", collection_to_test)
assert read_collections_to_test() == [
(PosixPath("col1"), "main"),
(PosixPath("col2"), "stable-1"),
(PosixPath("col3"), "release"),
]
assert read_collections_to_test() == [PosixPath("col1"), PosixPath("col2"), PosixPath("col3")]