Merge "Tests: Add tests for missing and default parameters"

This commit is contained in:
Zuul 2023-02-02 10:55:23 +00:00 committed by Gerrit Code Review
commit 05950e2a6f
35 changed files with 462 additions and 2 deletions

View File

@ -106,6 +106,14 @@ def expected_output(scenario):
return "".join(path.read_text() for path in sorted(scenario.out_paths))
@pytest.fixture
def expected_error(scenario):
if scenario.error_path.exists():
return scenario.error_path.read_text().rstrip()
else:
return None
def check_folder(scenario, jjb_config, input):
if "name" not in input:
return
@ -142,7 +150,10 @@ def check_parser(jjb_config, registry):
def check(in_path):
parser.parse(str(in_path))
_ = parser.expandYaml(registry)
registry.set_parser_data(parser.data)
job_data_list, job_view_list = parser.expandYaml(registry)
generator = XmlJobGenerator(registry)
_ = generator.generateXML(job_data_list)
return check

View File

@ -21,7 +21,7 @@ from collections import namedtuple
Scenario = namedtuple(
"Scnenario", "name in_path out_paths config_path plugins_info_path"
"Scnenario", "name in_path out_paths error_path config_path plugins_info_path"
)
@ -35,6 +35,7 @@ def scenario_list(fixtures_dir, in_ext=".yaml", out_ext=".xml"):
name=path.stem,
in_path=path,
out_paths=out_path_list,
error_path=path.with_suffix(".error"),
# When config file is missing it will still be passed and not None,
# so JJBConfig will prefer it over system and user configs.
config_path=path.with_suffix(".conf"),

View File

@ -0,0 +1 @@
'missing_param' is undefined

View File

@ -0,0 +1,16 @@
- project:
name: sample-project
jobs:
- sample-job
- builder:
name: sample-builder
builders:
- shell: !j2: |
echo {{ missing_param }} {{ other_param }}
- job-template:
name: sample-job
builders:
- sample-builder:
other_param: abc

View File

@ -0,0 +1 @@
'missing_param' is undefined

View File

@ -0,0 +1,21 @@
- project:
name: sample-project
param_1: !j2: '{{ missing_param }}'
param_2: '{param_1}'
jobs:
- sample-job
- builder:
name: sample-builder
builders:
- shell: !j2: |
# This param_2 usage does not actually trigger underined error.
# Error is triggered before macro substitution.
echo {{ param_3 }} {{ other_param }}
- job-template:
name: sample-job
param_3: '{param_2}-plus'
builders:
- sample-builder:
other_param: abc

View File

@ -0,0 +1 @@
'missing_param' is undefined

View File

@ -0,0 +1,10 @@
- project:
name: sample-project
jobs:
- sample-job
- job-template:
name: sample-job
builders:
- shell: !j2: |
echo {{ missing_param }}

View File

@ -0,0 +1 @@
'missing_param' is undefined

View File

@ -0,0 +1,13 @@
- project:
name: sample-project
param_1: !j2: '{{ missing_param }}'
param_2: '{param_1}'
jobs:
- sample-job
- job-template:
name: sample-job
param_3: '{param_2}-plus'
builders:
- shell: !j2: |
echo {{ param_3 }}

View File

@ -0,0 +1,4 @@
missing_param parameter missing to format echo {missing_param} {other_param}
Given:
{'other_param': 'abc'}

View File

@ -0,0 +1,16 @@
- project:
name: sample-project
jobs:
- sample-job
- builder:
name: sample-builder
builders:
- shell: |
echo {missing_param} {other_param}
- job-template:
name: sample-job
builders:
- sample-builder:
other_param: abc

View File

@ -0,0 +1,7 @@
missing_param parameter missing to format {missing_param}
Given:
{'': '',
'name': 'sample-project',
'param_1': '{missing_param}',
'param_2': '{param_1}',
'template-name': 'sample-job'}

View File

@ -0,0 +1,21 @@
- project:
name: sample-project
param_1: '{missing_param}'
param_2: '{param_1}'
jobs:
- sample-job
- builder:
name: sample-builder
builders:
- shell: |
# This param_2 usage does not actually trigger underined error.
# Error is triggered before macro substitution.
echo {param_3} {other_param}
- job-template:
name: sample-job
param_3: '{param_2}-plus'
builders:
- sample-builder:
other_param: abc

View File

@ -0,0 +1,7 @@
missing_param parameter missing to format echo {missing_param}
Given:
{'': '',
'builders': [OrderedDict([('shell', 'echo {missing_param}\n')])],
'name': 'sample-project',
'template-name': 'sample-job'}

View File

@ -0,0 +1,10 @@
- project:
name: sample-project
jobs:
- sample-job
- job-template:
name: sample-job
builders:
- shell: |
echo {missing_param}

View File

@ -0,0 +1,7 @@
missing_param parameter missing to format {missing_param}
Given:
{'': '',
'name': 'sample-project',
'param_1': '{missing_param}',
'param_2': '{param_1}',
'template-name': 'sample-job'}

View File

@ -0,0 +1,13 @@
- project:
name: sample-project
param_1: '{missing_param}'
param_2: '{param_1}'
jobs:
- sample-job
- job-template:
name: sample-job
param_3: '{param_1}-plus'
builders:
- shell: |
echo {param_3}

View File

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<project>
<actions/>
<description>&lt;!-- Managed by Jenkins Job Builder --&gt;</description>
<keepDependencies>false</keepDependencies>
<blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
<blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
<concurrentBuild>false</concurrentBuild>
<canRoam>true</canRoam>
<properties/>
<scm class="hudson.scm.NullSCM"/>
<builders>
<hudson.tasks.Shell>
<command>echo default_value abc</command>
</hudson.tasks.Shell>
</builders>
<publishers/>
<buildWrappers/>
</project>

View File

@ -0,0 +1,16 @@
- project:
name: sample-project
jobs:
- sample-job
- builder:
name: sample-builder
builders:
- shell: !j2: |
echo {{ missing_param | default('default_value') }} {{ other_param }}
- job-template:
name: sample-job
builders:
- sample-builder:
other_param: abc

View File

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<project>
<actions/>
<description>&lt;!-- Managed by Jenkins Job Builder --&gt;</description>
<keepDependencies>false</keepDependencies>
<blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
<blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
<concurrentBuild>false</concurrentBuild>
<canRoam>true</canRoam>
<properties/>
<scm class="hudson.scm.NullSCM"/>
<builders>
<hudson.tasks.Shell>
<command>echo {param_2}-plus abc</command>
</hudson.tasks.Shell>
</builders>
<publishers/>
<buildWrappers/>
</project>

View File

@ -0,0 +1,20 @@
- project:
name: sample-project
param_1: !j2: '{{ missing_param | default("default_value") }}'
param_2: '{param_1}'
jobs:
- sample-job
- builder:
name: sample-builder
builders:
- shell: !j2: |
echo {{ param_3 }} {{ other_param }}
- job-template:
name: sample-job
param_3: '{param_2}-plus'
builders:
- sample-builder:
param_3: '{param_3}'
other_param: abc

View File

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<project>
<actions/>
<description>&lt;!-- Managed by Jenkins Job Builder --&gt;</description>
<keepDependencies>false</keepDependencies>
<blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
<blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
<concurrentBuild>false</concurrentBuild>
<canRoam>true</canRoam>
<properties/>
<scm class="hudson.scm.NullSCM"/>
<builders>
<hudson.tasks.Shell>
<command>echo default_value</command>
</hudson.tasks.Shell>
</builders>
<publishers/>
<buildWrappers/>
</project>

View File

@ -0,0 +1,10 @@
- project:
name: sample-project
jobs:
- sample-job
- job-template:
name: sample-job
builders:
- shell: !j2: |
echo {{ missing_param | default('default_value') }}

View File

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<project>
<actions/>
<description>&lt;!-- Managed by Jenkins Job Builder --&gt;</description>
<keepDependencies>false</keepDependencies>
<blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
<blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
<concurrentBuild>false</concurrentBuild>
<canRoam>true</canRoam>
<properties/>
<scm class="hudson.scm.NullSCM"/>
<builders>
<hudson.tasks.Shell>
<command>echo {param_2}-plus</command>
</hudson.tasks.Shell>
</builders>
<publishers/>
<buildWrappers/>
</project>

View File

@ -0,0 +1,13 @@
- project:
name: sample-project
param_1: !j2: '{{ missing_param | default("default_value") }}'
param_2: '{param_1}'
jobs:
- sample-job
- job-template:
name: sample-job
param_3: '{param_2}-plus'
builders:
- shell: !j2: |
echo {{ param_3 }}

View File

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<project>
<actions/>
<description>&lt;!-- Managed by Jenkins Job Builder --&gt;</description>
<keepDependencies>false</keepDependencies>
<blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
<blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
<concurrentBuild>false</concurrentBuild>
<canRoam>true</canRoam>
<properties/>
<scm class="hudson.scm.NullSCM"/>
<builders>
<hudson.tasks.Shell>
<command>echo default_value abc
</command>
</hudson.tasks.Shell>
</builders>
<publishers/>
<buildWrappers/>
</project>

View File

@ -0,0 +1,16 @@
- project:
name: sample-project
jobs:
- sample-job
- builder:
name: sample-builder
builders:
- shell: |
echo {missing_param|default_value} {other_param}
- job-template:
name: sample-job
builders:
- sample-builder:
other_param: abc

View File

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<project>
<actions/>
<description>&lt;!-- Managed by Jenkins Job Builder --&gt;</description>
<keepDependencies>false</keepDependencies>
<blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
<blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
<concurrentBuild>false</concurrentBuild>
<canRoam>true</canRoam>
<properties/>
<scm class="hudson.scm.NullSCM"/>
<builders>
<hudson.tasks.Shell>
<command>echo {param_2}-plus abc
</command>
</hudson.tasks.Shell>
</builders>
<publishers/>
<buildWrappers/>
</project>

View File

@ -0,0 +1,20 @@
- project:
name: sample-project
param_1: '{missing_param|default_value}'
param_2: '{param_1}'
jobs:
- sample-job
- builder:
name: sample-builder
builders:
- shell: |
echo {param_3} {other_param}
- job-template:
name: sample-job
param_3: '{param_2}-plus'
builders:
- sample-builder:
param_3: '{param_3}'
other_param: abc

View File

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<project>
<actions/>
<description>&lt;!-- Managed by Jenkins Job Builder --&gt;</description>
<keepDependencies>false</keepDependencies>
<blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
<blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
<concurrentBuild>false</concurrentBuild>
<canRoam>true</canRoam>
<properties/>
<scm class="hudson.scm.NullSCM"/>
<builders>
<hudson.tasks.Shell>
<command>echo default_value
</command>
</hudson.tasks.Shell>
</builders>
<publishers/>
<buildWrappers/>
</project>

View File

@ -0,0 +1,10 @@
- project:
name: sample-project
jobs:
- sample-job
- job-template:
name: sample-job
builders:
- shell: |
echo {missing_param|default_value}

View File

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<project>
<actions/>
<description>&lt;!-- Managed by Jenkins Job Builder --&gt;</description>
<keepDependencies>false</keepDependencies>
<blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
<blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
<concurrentBuild>false</concurrentBuild>
<canRoam>true</canRoam>
<properties/>
<scm class="hudson.scm.NullSCM"/>
<builders>
<hudson.tasks.Shell>
<command>echo {param_2}-plus
</command>
</hudson.tasks.Shell>
</builders>
<publishers/>
<buildWrappers/>
</project>

View File

@ -0,0 +1,13 @@
- project:
name: sample-project
param_1: '{missing_param|default_value}'
param_2: '{param_1}'
jobs:
- sample-job
- job-template:
name: sample-job
param_3: '{param_2}-plus'
builders:
- shell: |
echo {param_3}

View File

@ -16,14 +16,33 @@
# under the License.
import os
from operator import attrgetter
from pathlib import Path
import pytest
from tests.enum_scenarios import scenario_list
fixtures_dir = Path(__file__).parent / "error_fixtures"
@pytest.fixture(
params=[
s
for s in scenario_list(fixtures_dir)
if s.in_path.name
not in {
"incorrect_template_dimensions.yaml",
"failure_formatting_template.yaml",
"failure_formatting_params.yaml",
}
],
ids=attrgetter("name"),
)
def scenario(request):
return request.param
# Override to avoid scenarios usage.
@pytest.fixture
def config_path():
@ -51,3 +70,9 @@ def test_failure_formatting(caplog, check_parser, name):
check_parser(in_path)
assert f"Failure formatting {name}" in caplog.text
assert "Problem formatting with args" in caplog.text
def test_error(check_parser, scenario, expected_error):
with pytest.raises(Exception) as excinfo:
check_parser(scenario.in_path)
assert str(excinfo.value) == expected_error