forked from ansible/ansible-zuul-jobs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
aws: introduce ansible-test-splitter job (ansible#978)
introduce ansible-test-splitter job The goal of the job is to split the work load and prepare a list of targets to run fo the integration jobs.
- Loading branch information
Showing
9 changed files
with
254 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
--- | ||
- hosts: controller | ||
tasks: | ||
- name: Run ansible-test-splitter | ||
import_role: | ||
name: ansible-test-splitter | ||
vars: | ||
ansible_test_test_command: "{{ ansible_test_command }}" | ||
ansible_test_location: "{{ ansible_user_dir }}/{{ zuul.projects[ansible_collections_repo].src_dir }}" | ||
ansible_test_git_branch: "{{ zuul.projects['github.com/ansible/ansible'].checkout }}" | ||
ansible_test_ansible_path: "{{ ansible_user_dir }}/{{ zuul.projects['github.com/ansible/ansible'].src_dir }}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
--- | ||
ansible_test_splitter__test_changed: false | ||
ansible_test_splitter__children_prefix: please_adjust_this |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#!/usr/bin/env python3 | ||
|
||
from pathlib import PosixPath | ||
import random | ||
import sys | ||
import json | ||
|
||
job_prefix = sys.argv[1] | ||
if len(sys.argv) == 3: | ||
targets_from_cli = sys.argv[2].split(" ") | ||
else: | ||
targets_from_cli = [] | ||
jobs = [f"{job_prefix}{i}" for i in range(10)] | ||
targets_per_job = 20 | ||
slow_targets = [] | ||
regular_targets = [] | ||
|
||
batches = [] | ||
|
||
targets = PosixPath("tests/integration/targets/") | ||
for target in targets.glob("*"): | ||
aliases = target / "aliases" | ||
if not target.is_dir(): | ||
continue | ||
if not aliases.is_file(): | ||
continue | ||
if targets_from_cli and target.name not in targets_from_cli: | ||
continue | ||
lines = aliases.read_text().split("\n") | ||
if "disabled" in lines: | ||
continue | ||
if "unstable" in lines: | ||
continue | ||
if "slow" in lines or "# reason: slow" in lines: | ||
batches.append([target.name]) | ||
else: | ||
regular_targets.append(target.name) | ||
|
||
random.shuffle(regular_targets) | ||
|
||
splitted_targets = len(regular_targets) % targets_per_job | ||
|
||
splitted_targets = [] | ||
while regular_targets: | ||
batches.append( | ||
[regular_targets.pop() for i in range(targets_per_job) if regular_targets] | ||
) | ||
|
||
|
||
result = { | ||
"data": { | ||
"zuul": {"child_jobs": jobs[0:len(batches)]}, | ||
"child": {"targets_to_test": batches}, | ||
} | ||
} | ||
|
||
print(json.dumps(result)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#!/usr/bin/env python3 | ||
|
||
from pathlib import PosixPath | ||
import sys | ||
import subprocess | ||
|
||
targets_to_test = [] | ||
targets_dir = PosixPath("tests/integration/targets") | ||
zuul_branch = sys.argv[1] | ||
diff = subprocess.check_output( | ||
["git", "diff", f"origin/{zuul_branch}", "--name-only"] | ||
).decode() | ||
module_files = [PosixPath(d) for d in diff.split("\n") if d.startswith("plugins/")] | ||
for i in module_files: | ||
if not i.is_file(): | ||
continue | ||
target_name = i.stem | ||
|
||
for t in targets_dir.iterdir(): | ||
aliases = t / "aliases" | ||
if not aliases.is_file(): | ||
continue | ||
# There is a target with the module name, let's take that | ||
if t.name == target_name: | ||
targets_to_test.append(target_name) | ||
break | ||
alias_content = aliases.read_text().split("\n") | ||
# The target name is in the aliases file | ||
if target_name in alias_content: | ||
targets_to_test.append(target_name) | ||
break | ||
|
||
target_files = [ | ||
PosixPath(d) for d in diff.split("\n") if d.startswith("tests/integration/targets/") | ||
] | ||
for i in target_files: | ||
splitted = str(i).split("/") | ||
if len(splitted) < 5: | ||
continue | ||
target_name = splitted[3] | ||
aliases = targets_dir / target_name / "aliases" | ||
if aliases.is_file(): | ||
targets_to_test.append(target_name) | ||
|
||
print(" ".join(list(set(targets_to_test)))) |
13 changes: 13 additions & 0 deletions
13
roles/ansible-test-splitter/tasks/ansible_test_changed.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
--- | ||
- copy: | ||
src: test_changed.py | ||
dest: /tmp/test_changed.py | ||
mode: '0700' | ||
|
||
- name: Identify the changed targets | ||
command: python3 /tmp/test_changed.py "{{ zuul.branch }}" | ||
args: | ||
chdir: "{{ ansible_test_location }}" | ||
register: _result | ||
- set_fact: | ||
ansible_test_splitter__changed_targets: "{{ _result.stdout }}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
- name: Identiy the targets associated with the changed files | ||
import_tasks: ansible_test_changed.yaml | ||
when: ansible_test_splitter__test_changed|bool | ||
|
||
- name: Split targets | ||
import_tasks: split_targets.yaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
--- | ||
- copy: | ||
src: split_targets.py | ||
dest: /tmp/split_targets.py | ||
mode: '0700' | ||
|
||
- name: Split the workload | ||
command: python3 /tmp/split_targets.py "{{ ansible_test_splitter__children_prefix }}" "{{ ansible_test_splitter__changed_targets|default('') }}" | ||
args: | ||
chdir: "{{ ansible_test_location }}" | ||
register: _result | ||
- debug: var=_result | ||
- set_fact: | ||
for_zuul_return: '{{ _result.stdout | from_json }}' | ||
- debug: var=for_zuul_return | ||
- name: Register the result | ||
zuul_return: | ||
data: "{{ for_zuul_return.data }}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters