Use 'warnings' module for tag deprecation. Improve tests

Change-Id: Iae70789ba5948eb1ea259b6456dd0ba262c4eee3
This commit is contained in:
Vsevolod Fedorov 2024-02-06 11:12:43 +03:00
parent 9e05f9bcba
commit 3cf741985f
18 changed files with 182 additions and 103 deletions

View File

@ -12,6 +12,7 @@
import io
import logging
import warnings
from functools import partial
from .errors import JenkinsJobsException
@ -71,7 +72,10 @@ class Loader(LocLoader):
def load_deprecated_yaml(tag, cls, loader, node):
logger.warning("Tag %r is deprecated, switch to using %r", tag, cls.yaml_tag)
warnings.warn(
f"Tag {tag!r} is deprecated, switch to using {cls.yaml_tag!r}",
UserWarning,
)
return cls.from_yaml(loader, node)

View File

@ -1,14 +0,0 @@
[
{
"builders": [
{
"shell": "#!/bin/bash\n#\n# Sample script showing how the yaml include-raw tag can be used\n# to inline scripts that are maintained outside of the jenkins\n# job yaml configuration.\n\necho \"hello world\"\n\nexit 0\n"
},
{
"shell": "#!/bin/bash\n#\n# sample script to check that brackets aren't escaped\n# when using the include-raw application yaml tag\n\nVAR1=\"hello\"\nVAR2=\"world\"\nVAR3=\"${VAR1} ${VAR2}\"\n\n[[ -n \"${VAR3}\" ]] && {\n # this next section is executed as one\n echo \"${VAR3}\"\n exit 0\n}\n"
}
],
"description": "<!-- Managed by Jenkins Job Builder -->",
"name": "test-job-include-raw-1"
}
]

View File

@ -1,14 +0,0 @@
[
{
"builders": [
{
"shell": "#!/bin/bash\n#\n# Sample script showing how the yaml include-raw tag can be used\n# to inline scripts that are maintained outside of the jenkins\n# job yaml configuration.\n\necho \"hello world\"\n\nexit 0\n"
},
{
"shell": "#!/bin/bash\n#\n# sample script to check that brackets aren't escaped\n# when using the include-raw application yaml tag\n\nVAR1=\"hello\"\nVAR2=\"world\"\nVAR3=\"${VAR1} ${VAR2}\"\n\n[[ -n \"${VAR3}\" ]] && {\n # this next section is executed as one\n echo \"${VAR3}\"\n exit 0\n}\n"
}
],
"description": "<!-- Managed by Jenkins Job Builder -->",
"name": "test-job-include-raw-1"
}
]

View File

@ -1,7 +0,0 @@
- job:
name: test-job-include-raw-1
builders:
- shell:
!include-raw-escape include-raw001-hello-world.sh
- shell:
!include-raw-escape include-raw001-vars.sh

View File

@ -1,45 +0,0 @@
[
{
"builders": [
{
"copyartifact": {
"project": "foo",
"filter": "*.tar.gz",
"target": "/home/foo",
"which-build": "last-successful",
"optional": true,
"flatten": true,
"do-not-fingerprint": true,
"parameter-filters": "PUBLISH=true"
}
},
{
"copyartifact": {
"project": "bar",
"filter": "*.tar.gz",
"target": "/home/foo",
"which-build": "specific-build",
"optional": true,
"flatten": true,
"do-not-fingerprint": true,
"parameter-filters": "PUBLISH=true",
"build-number": 123
}
},
{
"copyartifact": {
"project": "baz",
"filter": "*.tar.gz",
"target": "/home/foo",
"which-build": "upstream-build",
"optional": true,
"flatten": true,
"do-not-fingerprint": true,
"parameter-filters": "PUBLISH=true"
}
}
],
"description": "<!-- Managed by Jenkins Job Builder -->",
"name": "test-job-1"
}
]

View File

@ -1,4 +0,0 @@
- job:
name: test-job-1
builders:
!include include001.yaml.inc

View File

@ -0,0 +1,9 @@
#!/bin/bash
#
# Sample script showing how the yaml include-raw-escape tag can be used
# to inline scripts that are maintained outside of the jenkins
# job yaml configuration.
echo "hello world"
exit 0

View File

@ -0,0 +1,14 @@
#!/bin/bash
#
# sample script to check that brackets aren't expanded
# when using the include-raw-escape application yaml tag
VAR1="hello"
VAR2="world"
VAR3="${VAR1} ${VAR2}"
[[ -n "${VAR3}" ]] && {
# this next section is executed as one
echo "${VAR3}"
exit 0
}

View File

@ -13,7 +13,7 @@
<hudson.tasks.Shell>
<command>#!/bin/bash
#
# Sample script showing how the yaml include-raw tag can be used
# Sample script showing how the yaml include-raw-escape tag can be used
# to inline scripts that are maintained outside of the jenkins
# job yaml configuration.
@ -25,8 +25,8 @@ exit 0
<hudson.tasks.Shell>
<command>#!/bin/bash
#
# sample script to check that brackets aren't escaped
# when using the include-raw application yaml tag
# sample script to check that brackets aren't expanded
# when using the include-raw-escape application yaml tag
VAR1=&quot;hello&quot;
VAR2=&quot;world&quot;

View File

@ -2,9 +2,9 @@
name: test-job-include-raw-{num}
builders:
- shell:
!include-raw-escape include-raw001-hello-world.sh
!include-raw-escape deprecated-include-raw-escaped001.hello-world.sh
- shell:
!include-raw-escape include-raw001-vars.sh
!include-raw-escape deprecated-include-raw-escaped001.vars.sh
- project:
name: test-job-template-1

View File

@ -0,0 +1,9 @@
#!/bin/bash
#
# Sample script showing how the yaml include-raw tag can be used
# to inline scripts that are maintained outside of the jenkins
# job yaml configuration.
echo "hello world"
exit 0

View File

@ -0,0 +1,14 @@
#!/bin/bash
#
# sample script to check that brackets are expanded
# when using the include-raw application yaml tag
VAR1="hello"
VAR2="world"
VAR3="${{VAR1}} ${{VAR2}}"
[[ -n "${{VAR3}}" ]] && {{
# this next section is executed as one
echo "${{VAR3}}"
exit 0
}}

View File

@ -0,0 +1,45 @@
<?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>#!/bin/bash
#
# Sample script showing how the yaml include-raw tag can be used
# to inline scripts that are maintained outside of the jenkins
# job yaml configuration.
echo &quot;hello world&quot;
exit 0
</command>
</hudson.tasks.Shell>
<hudson.tasks.Shell>
<command>#!/bin/bash
#
# sample script to check that brackets are expanded
# when using the include-raw application yaml tag
VAR1=&quot;hello&quot;
VAR2=&quot;world&quot;
VAR3=&quot;${VAR1} ${VAR2}&quot;
[[ -n &quot;${VAR3}&quot; ]] &amp;&amp; {
# this next section is executed as one
echo &quot;${VAR3}&quot;
exit 0
}
</command>
</hudson.tasks.Shell>
</builders>
<publishers/>
<buildWrappers/>
</project>

View File

@ -0,0 +1,7 @@
- job:
name: test-job-include-raw-1
builders:
- shell:
!include-raw deprecated-include-raw001.hello-world.sh
- shell:
!include-raw deprecated-include-raw001.vars.sh

View File

@ -9,16 +9,50 @@
<canRoam>true</canRoam>
<properties/>
<scm class="hudson.scm.NullSCM"/>
<builders/>
<builders>
<hudson.plugins.copyartifact.CopyArtifact>
<project>foo</project>
<filter>*.tar.gz</filter>
<target>/home/foo</target>
<flatten>true</flatten>
<optional>true</optional>
<doNotFingerprintArtifacts>true</doNotFingerprintArtifacts>
<parameters>PUBLISH=true</parameters>
<exclude/>
<resultVariableSuffix/>
<selector class="hudson.plugins.copyartifact.StatusBuildSelector">
<stable>false</stable>
</selector>
</hudson.plugins.copyartifact.CopyArtifact>
<hudson.plugins.copyartifact.CopyArtifact>
<project>bar</project>
<filter>*.tar.gz</filter>
<target>/home/foo</target>
<flatten>true</flatten>
<optional>true</optional>
<doNotFingerprintArtifacts>true</doNotFingerprintArtifacts>
<parameters>PUBLISH=true</parameters>
<exclude/>
<resultVariableSuffix/>
<selector class="hudson.plugins.copyartifact.SpecificBuildSelector">
<buildNumber>123</buildNumber>
</selector>
</hudson.plugins.copyartifact.CopyArtifact>
<hudson.plugins.copyartifact.CopyArtifact>
<project>baz</project>
<filter>*.tar.gz</filter>
<target>/home/foo</target>
<flatten>true</flatten>
<optional>true</optional>
<doNotFingerprintArtifacts>true</doNotFingerprintArtifacts>
<parameters>PUBLISH=true</parameters>
<exclude/>
<resultVariableSuffix/>
<selector class="hudson.plugins.copyartifact.TriggeredBuildSelector">
<fallbackToLastSuccessful>false</fallbackToLastSuccessful>
</selector>
</hudson.plugins.copyartifact.CopyArtifact>
</builders>
<publishers/>
<buildWrappers>
<hudson.plugins.build__timeout.BuildTimeoutWrapper>
<timeoutMinutes>3</timeoutMinutes>
<failBuild>true</failBuild>
<writingDescription>false</writingDescription>
<timeoutPercentage>150</timeoutPercentage>
<timeoutMinutesElasticDefault>90</timeoutMinutesElasticDefault>
<timeoutType>elastic</timeoutType>
</hudson.plugins.build__timeout.BuildTimeoutWrapper>
</buildWrappers>
<buildWrappers/>
</project>

View File

@ -0,0 +1,4 @@
- job:
name: test-job-1
builders:
!include deprecated-include001.yaml.inc

View File

@ -0,0 +1,18 @@
- copyartifact: &copytarball
project: foo
filter: "*.tar.gz"
target: /home/foo
which-build: last-successful
optional: true
flatten: true
do-not-fingerprint: true
parameter-filters: PUBLISH=true
- copyartifact:
<<: *copytarball
project: bar
which-build: specific-build
build-number: 123
- copyartifact:
<<: *copytarball
project: baz
which-build: upstream-build

View File

@ -35,7 +35,12 @@ def scenario(request):
return request.param
def test_yaml_snippet(check_job):
def test_yaml_snippet(scenario, check_job):
# Some tests using config with 'include_path' expect JJB root to be current directory.
os.chdir(Path(__file__).parent / "../..")
check_job()
if scenario.name.startswith("deprecated-"):
with pytest.warns(UserWarning) as record:
check_job()
assert "is deprecated" in str(record[0].message)
else:
check_job()