Add lf_tox_sonar job 94/9894/16
authorJessica Wagantall <jwagantall@linuxfoundation.org>
Tue, 10 Apr 2018 22:24:16 +0000 (15:24 -0700)
committerJessica Wagantall <jwagantall@linuxfoundation.org>
Thu, 12 Apr 2018 21:10:51 +0000 (14:10 -0700)
Python jobs uses tox.ini to configure
their project coverage report. Adding a tox call
before the sonar calls makes the proper updates
to the reports based on that coverage.

Change-Id: I4e6787274ae15c1a9b3b866a55e02f3587b906fb
Signed-off-by: Jessica Wagantall <jwagantall@linuxfoundation.org>
.jjb-test/lf-python-jobs.yaml
docs/jjb/lf-python-jobs.rst
jjb/lf-python-jobs.yaml

index 49214fb..feb154f 100644 (file)
@@ -3,6 +3,7 @@
     name: gerrit-python-jobs
     jobs:
       - "{project-name}-python-jobs"
+      - gerrit-tox-sonar
 
     project-name: gerrit-python
 
@@ -10,5 +11,6 @@
     name: github-python-jobs
     jobs:
       - "{project-name}-github-python-jobs"
+      - github-tox-sonar
 
     project-name: github-python
index 2a8e66b..85103db 100644 (file)
@@ -41,6 +41,68 @@ Runs a shell script that installs tox in a Python virtualenv.
 Job Templates
 =============
 
+Python Sonar with Tox
+---------------------
+
+Sonar scans for Python based repos. This job will perform a tox call which
+runs the coverage command to gather tests results. These test results get
+published back into Sonar after running the Sonar goals.
+
+To get the Sonar coverage results, tox.ini needs to exist and configured
+with coverage commands to run.
+
+The coverage commands define the code that gets executed by the test suites.
+It does not guarantee that the code tests executed properly, but it will help
+pointing out the code that is not tested at all.
+
+For example:
+
+.. code-block:: bash
+
+    [testenv:py27]
+    commands =
+            coverage run --module pytest --junitxml xunit-results.xml
+            coverage xml --omit=".tox/py27/*","tests/*"
+            coverage report --omit=".tox/py27/*","tests/*"
+
+For more details refer to coverage and sonar documentation:
+
+https://coverage.readthedocs.io/
+
+https://docs.sonarqube.org/display/PLUG/Python+Coverage+Results+Import
+
+:Template Names:
+
+    - {project-name}-tox-sonar
+    - gerrit-tox-sonar
+    - github-tox-sonar
+
+:Required parameters:
+
+    :build-node: The node to run build on.
+    :jenkins-ssh-credential: Credential to use for SSH. (Generally should
+        get configured in defaults.yaml)
+    :mvn-settings: The name of settings file containing credentials for the project.
+
+:Optional parameters:
+
+    :build-days-to-keep: Days to keep build logs in Jenkins. (default: 7)
+    :build-timeout: Timeout in seconds before aborting build. (default: 60)
+    :cron: Cron schedule when to trigger the job. This parameter also
+        supports multiline input via YAML pipe | character in cases where
+        one may want to provide more than 1 cron timer.  (default: H 11 * * *
+        to run once a day)
+    :git-url: URL clone project from. (default: $GIT_URL/$PROJECT)
+    :java-version: Version of Java to use for the build. (default: openjdk8)
+    :mvn-global-settings: The name of the Maven global settings to use for
+        Maven configuration. (default: global-settings)
+    :mvn-version: Version of maven to use. (default: mvn33)
+    :stream: Keyword used to represent a release code-name.
+        Often the same as the branch. (default: master)
+    :submodule-recursive: Whether to checkout submodules recursively.
+        (default: true)
+    :gerrit_sonar_triggers: Override Gerrit Triggers.
+
 Tox Verify
 ----------
 
index 671eaf9..cd492ab 100644 (file)
           github_pr_admin_list:
             - ''
 
+########################
+# Python Sonar with Tox #
+########################
+
+- lf_tox_sonar: &lf_tox_sonar
+    name: lf-tox_sonar
+
+    ######################
+    # Default parameters #
+    ######################
+
+    branch: master  # Sonar should always be run on master branch
+    build-days-to-keep: 7
+    build-timeout: 60
+    cron: 'H H * * *'  # run daily
+    git-url: '$GIT_URL/$PROJECT'
+    java-version: openjdk8
+    python-version: python2
+    mvn-global-settings: global-settings
+    mvn-settings: '{mvn-settings}'
+    mvn-version: mvn33
+    sonar-mvn-goal: 'sonar:sonar'
+    stream: master
+    submodule-recursive: true
+
+    gerrit_sonar_triggers:
+      - comment-added-contains-event:
+          comment-contains-value: run-sonar$
+
+    #####################
+    # Job Configuration #
+    #####################
+
+    parameters:
+      - lf-infra-parameters:
+          project: '{project}'
+          branch: '{branch}'
+          stream: '{stream}'
+          lftools-version: '{lftools-version}'
+      - string:
+          name: ARCHIVE_ARTIFACTS
+          default: '{archive-artifacts}'
+          description: Artifacts to archive to the logs server.
+      - string:
+          name: MVN
+          # Sets an env var for shell scripts to be able to call the dynamically
+          # installed maven without having to calculate the path themselves.
+          # yamllint disable-line rule:line-length
+          default: '/w/tools/hudson.tasks.Maven_MavenInstallation/{mvn-version}/bin/mvn'
+          description: 'Maven selector to be used by shell scripts'
+      - string:
+          name: SONAR_MAVEN_GOAL
+          default: '{sonar-mvn-goal}'
+          description: |
+              Maven goals to pass to the Sonar call. Typically sonar:sonar
+              however to use a specific version of the sonar-maven-plugin we
+              can call "org.codehaus.mojo:sonar-maven-plugin:3.3.0.603:sonar".
+
+    triggers:
+      - timed: '{obj:cron}'
+      - gerrit:
+          server-name: '{gerrit-server-name}'
+          trigger-on: '{obj:gerrit_sonar_triggers}'
+          projects:
+            - project-compare-type: 'ANT'
+              project-pattern: '{project}'
+              branches:
+                - branch-compare-type: 'ANT'
+                  branch-pattern: '**/master'
+          skip-vote:
+            successful: true
+            failed: true
+            unstable: true
+            notbuilt: true
+
+    builders:
+      - shell: !include-raw-escape:
+          # Workaround issue where the tox run later breaks the lftools virtualenv.
+          # Without running the install first the run in the publisher will fail
+          # due to missing lftools because it gets installed into a tox venv.
+          - ../shell/lftools-install.sh
+      - lf-infra-tox-install:
+          python-version: '{python-version}'
+      - shell: !include-raw-escape: ../shell/tox-run.sh
+      - lf-provide-maven-settings:
+          global-settings-file: '{mvn-global-settings}'
+          settings-file: '{mvn-settings}'
+      - lf-infra-tox-sonar:
+          java-version: '{java-version}'
+          mvn-settings: '{mvn-settings}'
+          mvn-version: '{mvn-version}'
+
+    publishers:
+      - lf-infra-publish
+
+- builder:
+    name: lf-infra-tox-sonar
+    # Run a Sonar build with Maven
+    builders:
+      - lf-maven-install:
+          mvn-version: '{mvn-version}'
+      - lf-update-java-alternatives:
+          java-version: '{java-version}'
+      - inject:
+          # TODO: Switch this to the sonar wrapper when JJB 2.0 is available
+          properties-content: SONAR_HOST_URL=$SONAR_URL
+      - shell: !include-raw-escape:
+          - ../shell/common-variables.sh
+          - ../shell/maven-sonar.sh
+      - lf-provide-maven-settings-cleanup
+
+- job-template:
+    name: '{project-name}-tox-sonar'
+    id: gerrit-tox-sonar
+    <<: *lf_python_common
+    # yamllint disable-line rule:key-duplicates
+    <<: *lf_tox_sonar
+
+    scm:
+      - lf-infra-gerrit-scm:
+          jenkins-ssh-credential: '{jenkins-ssh-credential}'
+          git-url: '{git-url}'
+          refspec: $GERRIT_REFSPEC
+          branch: $GERRIT_BRANCH
+          submodule-recursive: '{submodule-recursive}'
+          choosing-strategy: default
+
+- job-template:
+    name: '{project-name}-tox-sonar'
+    id: github-tox-sonar
+    <<: *lf_python_common
+    # yamllint disable-line rule:key-duplicates
+    <<: *lf_tox_sonar
+
+    properties:
+      - github:
+          url: '{git-url}/{github-org}/{project}'
+
+    scm:
+      - lf-infra-github-scm:
+          url: '{git-clone-url}{github-org}/{project}'
+          refspec: '+refs/pull/*:refs/remotes/origin/pr/*'
+          branch: '$sha1'
+          submodule-recursive: '{submodule-recursive}'
+          choosing-strategy: default
+          jenkins-ssh-credential: '{jenkins-ssh-credential}'
+
+    triggers:
+      - lf-infra-github-pr-trigger:
+          trigger-phrase: '^run-sonar$'
+          only-trigger-phrase: false
+          status-context: 'Python Sonar'
+          permit-all: true
+          github-hooks: true
+          github-org: ''
+          github_pr_whitelist:
+            - ''
+          github_pr_admin_list:
+            - ''
+
 ##############
 # Tox Verify #
 ##############