diff --git a/azure-pipelines-templates/run-tests.yml b/azure-pipelines-templates/run-tests.yml deleted file mode 100644 index bfc4e949..00000000 --- a/azure-pipelines-templates/run-tests.yml +++ /dev/null @@ -1,196 +0,0 @@ -# ----------------------------------------------------------------------------- -# Copyright (c) 2009-2021, Shotgun Software Inc. -# -# Redistribution and use in source and binary forms, with or without -# modification, are permitted provided that the following conditions are met: -# -# - Redistributions of source code must retain the above copyright notice, this -# list of conditions and the following disclaimer. -# -# - Redistributions in binary form must reproduce the above copyright notice, -# this list of conditions and the following disclaimer in the documentation -# and/or other materials provided with the distribution. -# -# - Neither the name of the Shotgun Software Inc nor the names of its -# contributors may be used to endorse or promote products derived from this -# software without specific prior written permission. -# -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" -# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE -# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER -# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, -# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -parameters: - # Using the most formal way of defining parameters so we don't define a - # default value for mandatory parameters. This way, CI will fail if we forget - # to pass it when invoking the template. - - - name: codecov_download_url - type: string - - - name: os_name - type: string - - - name: python_version - type: string - - - name: vm_image - type: string - -jobs: -- job: - displayName: "${{ parameters.os_name }} - Python ${{ parameters.python_version }}" - pool: - vmImage: ${{ parameters.vm_image }} - - variables: - group: sg-credentials - - # These are the steps that will be executed inside each job. - steps: - # Specifies which version of Python we want to use. - # TODO: We should provide `githubToken` if we want to download a python release. - # Otherwise we may hit the GitHub anonymous download limit. - - task: UsePythonVersion@0 - inputs: - versionSpec: ${{ parameters.python_version }} - - # Install all dependencies needed for running the tests. This command is good - # for all OSes - - task: Bash@3 - displayName: Install dependencies - inputs: - targetType: inline - script: | - set -e - python -m pip install --upgrade pip - python -m pip install --upgrade setuptools wheel - python -m pip install --upgrade --requirement tests/requirements.txt - # Need to use the "python -m" prefix to make Windows happy. - - # The {{}} syntax is meant for the the pre-processor of Azure pipeline. Every statement inside - # a {{}} block will be evaluated and substituted before the file is parsed to create the jobs. - # So here we're inserting an extra step if the template is being invoked for Windows. - - ${{ if eq(parameters.os_name, 'Windows') }}: - # On Windows, we need to update the certificates, the cert store is missing the newer one - # from Amazon like some clients experienced a while back. Who would have thought Microsoft - # would have been out of date! ;) - - powershell: | - $cert_url = "https://www.amazontrust.com/repository/SFSRootCAG2.cer" - $cert_file = New-TemporaryFile - Invoke-WebRequest -Uri $cert_url -UseBasicParsing -OutFile $cert_file.FullName - Import-Certificate -FilePath $cert_file.FullName -CertStoreLocation Cert:\LocalMachine\Root - displayName: Updating OS Certificates - - # Runs the tests and generates both test report and code coverage - - task: Bash@3 - displayName: Running tests - inputs: - targetType: inline - script: | - set -e - cp ./tests/example_config ./tests/config - python -m pytest \ - --cov \ - --cov-report xml:coverage.xml \ - --durations=0 \ - --nunit-xml=test-results.xml \ - --verbose \ - --reruns 1 \ - --reruns-delay 2 \ - env: - # Tell Pytest that we're running in a CI environment - CI: 1 - - # Pass the values needed to authenticate with the Flow Production Tracking site and create some entities. - # Remember, on a pull request from a client or on forked repos, those variables - # will be empty! - SG_SERVER_URL: $(ci_site) - SG_SCRIPT_NAME: $(ci_site_script_name) - SG_API_KEY: $(ci_site_script_key) - # The unit tests manipulate the user and project during the tests, which can cause collisions, - # so sandbox each build variant. - # Ideally, we would use the agent name here. The problem is that the agent name is in a build - # variable, so we can't edit it's value through a ${{replace(a,b,c)}} expression, which are evaluated before - # build variables are available. Because of this, we need to find another way to generate a - # unique login. So instead, we'll use the the name of the platform and the python version, - # which should make it unique. - SG_HUMAN_LOGIN: $(python_api_human_login)-${{ parameters.os_name }}-${{ parameters.python_version }} - # This will give a user name like 'something macOS 2.7' - SG_HUMAN_NAME: $(python_api_human_name) ${{ parameters.os_name }} ${{ parameters.python_version }} - SG_HUMAN_PASSWORD: $(python_api_human_password) - # Each job gets its own ephemeral project, scoped to this build + OS + Python version. - # This eliminates state bleed between concurrent runs and across successive builds on the - # same agent. The project is retired in the "Cleanup test project" step below. - SG_PROJECT_NAME: Python API CI - $(Build.BuildId) - ${{ parameters.os_name }} - ${{ parameters.python_version }} - - - task: PythonScript@0 - displayName: Cleanup test project - condition: always() - inputs: - scriptSource: inline - workingDirectory: $(Build.SourcesDirectory) - script: | - import os, shotgun_api3, sys - sg = shotgun_api3.Shotgun( - os.environ['SG_SERVER_URL'], - script_name=os.environ['SG_SCRIPT_NAME'], - api_key=os.environ['SG_API_KEY'], - ) - project = sg.find_one( - entity_type='Project', - filters=[['name', 'is', os.environ['SG_PROJECT_NAME']]], - ) - if not project: - print('Project not found, nothing to clean up.') - sys.exit(0) - sg.delete(entity_type='Project', entity_id=project['id']) - print('Retired project:', os.environ['SG_PROJECT_NAME']) - env: - PYTHONPATH: $(Build.SourcesDirectory) - SG_SERVER_URL: $(ci_site) - SG_SCRIPT_NAME: $(ci_site_script_name) - SG_API_KEY: $(ci_site_script_key) - SG_PROJECT_NAME: Python API CI - $(Build.BuildId) - ${{ parameters.os_name }} - ${{ parameters.python_version }} - - # Explicit call to PublishTestResults@2 and PublishCodeCoverageResults@2 here - # instead of relying on pytest-azurepipelines because pytest-azurepipelines - # does not seem to be maintained anymore and is still using earlier versions - # of the PublishTestResults and PublishCodeCoverageResults Azure Pipelines - # jobs. - - - task: PublishTestResults@2 - displayName: Publish test results - inputs: - failTaskOnFailureToPublishResults: true - failTaskOnMissingResultsFile: true - testResultsFiles: test-results.xml - testResultsFormat: NUnit - testRunTitle: "${{ parameters.os_name }} - Python ${{ parameters.python_version }}" - condition: succeededOrFailed() - - - task: PublishCodeCoverageResults@2 - displayName: Publish code coverage - inputs: - summaryFileLocation: coverage.xml - failIfCoverageEmpty: true - - - task: Bash@3 - displayName: Uploading coverage to Codecov.io - inputs: - targetType: inline - script: | - set -e - curl --remote-name --silent "${{ parameters.codecov_download_url }}" - chmod +x codecov - ./codecov \ - --file coverage.xml \ - --flags "${{ parameters.os_name }}" \ - --flags "Python-${{ parameters.python_version }}" \ - --name "Tested on ${{ parameters.os_name }} with Python ${{ parameters.python_version }}" \ diff --git a/azure-pipelines.yml b/azure-pipelines.yml index dfd158cc..f43ef11a 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -31,7 +31,7 @@ resources: - repository: templates type: github name: shotgunsoftware/tk-ci-tools - ref: refs/heads/master + ref: refs/heads/ticket/SG-47353-toolkit-flag endpoint: shotgunsoftware # Despite using the "tk-" prefix, tk-ci-tools is not a Toolkit only tool. # We use it to avoid duplicating and maintaining CI pipeline code. @@ -58,55 +58,92 @@ pr: include: - "*" -parameters: - - name: python_versions - type: object - default: - - '3.9' - - '3.10' - - '3.11' - - '3.13' - - - name: os_versions - type: object - default: - - name: Linux - # Pinned: ubuntu-latest has no Python 3.9, which we still support SG-45286 - vm_image: ubuntu-24.04 - codecov_download_url: https://uploader.codecov.io/latest/linux/codecov - - - name: macOS - vm_image: macOS-latest - codecov_download_url: https://uploader.codecov.io/v0.7.3/macos/codecov - # macOS & codecov note: - # In Nov 2024 (SG-36700), we pinned macOS to codecov v0.7.3 because the - # macOS-latest Azure Pipelines agent is Intel (x86_64), but Codecov - # started shipping arm64-only binaries. - # When we will change the macOs image to a arm64 arch, we can use the - # "latest" version again. - - - name: Windows - vm_image: windows-latest - codecov_download_url: https://uploader.codecov.io/latest/windows/codecov.exe - # This here is the list of jobs we want to run for our build. # Jobs run in parallel. jobs: +- template: azure-pipelines-templates/type_checking.yml + - template: build-pipeline.yml@templates parameters: - # Python API does not follow the exact same Python version lifecycle than - # Toolkit. So we prefer to control the test execution here instead. - has_unit_tests: false - + # The Python API has no UI, so there are no resources to validate. has_ui_resources: false -- template: azure-pipelines-templates/type_checking.yml + # Run the tests with the shared tk-ci-tools test jobs. They come with a lot + # of default behavior (Python versions, Qt wrappers, operating systems and + # runner images), defined in internal/run-tests.yml of tk-ci-tools. Review + # those defaults when they change, and override them with + # run_tests_parameters if this repository needs something else. + has_unit_tests: true + + run_tests_parameters: + # The Python API is not a Toolkit component: no Qt wrapper, tk-toolchain + # or tk-core. With tk-core, the tests would import the shotgun_api3 + # bundled with tk-core instead of this repository's code. + toolkit: false + + dependencies: + - --requirement=tests/requirements.txt + + pre_steps: + # The tests read the test data names from this file. The site and the + # credentials come from the SG_* variables of env below. + - bash: cp ./tests/example_config ./tests/config + displayName: Create the test configuration + + # The Windows certificate store is missing the newer Amazon root + # certificate, which some clients experienced too. + - powershell: | + $cert_url = "https://www.amazontrust.com/repository/SFSRootCAG2.cer" + $cert_file = New-TemporaryFile + Invoke-WebRequest -Uri $cert_url -UseBasicParsing -OutFile $cert_file.FullName + Import-Certificate -FilePath $cert_file.FullName -CertStoreLocation Cert:\LocalMachine\Root + displayName: Updating OS Certificates + condition: and(succeeded(), eq(variables['Agent.OS'], 'Windows_NT')) + + # Authenticate with the test site. On pull requests from forks, the + # secrets are not available. + env: + SG_SERVER_URL: $(ci_site) + SG_SCRIPT_NAME: $(ci_site_script_name) + SG_API_KEY: $(ci_site_script_key) + # The tests modify the human user, so each job uses its own. These + # users exist on the test site, one per OS and Python version, e.g. + # -Linux-3.9. + SG_HUMAN_LOGIN: $(python_api_human_login)-$(tk_ci_os_name)-$(tk_ci_python_version) + SG_HUMAN_NAME: $(python_api_human_name) $(tk_ci_os_name) $(tk_ci_python_version) + SG_HUMAN_PASSWORD: $(python_api_human_password) + # Each job gets its own project, deleted in the cleanup step below. + SG_PROJECT_NAME: Python API CI - $(Build.BuildId) - $(tk_ci_os_name) - $(tk_ci_python_version) + + # Retry the tests that fail because of the test site. + pytest_args: [--reruns, '1', --reruns-delay, '2'] -- ${{ each os_version in parameters.os_versions }}: - - ${{ each python_version in parameters.python_versions }}: - - template: azure-pipelines-templates/run-tests.yml - parameters: - codecov_download_url: ${{ os_version.codecov_download_url }} - os_name: ${{ os_version.name }} - python_version: ${{ python_version }} - vm_image: ${{ os_version.vm_image }} + post_tests_steps: + - task: PythonScript@0 + displayName: Cleanup test project + condition: always() + inputs: + scriptSource: inline + workingDirectory: $(Build.SourcesDirectory) + script: | + import os, shotgun_api3, sys + sg = shotgun_api3.Shotgun( + os.environ['SG_SERVER_URL'], + script_name=os.environ['SG_SCRIPT_NAME'], + api_key=os.environ['SG_API_KEY'], + ) + project = sg.find_one( + entity_type='Project', + filters=[['name', 'is', os.environ['SG_PROJECT_NAME']]], + ) + if not project: + print('Project not found, nothing to clean up.') + sys.exit(0) + sg.delete(entity_type='Project', entity_id=project['id']) + print('Retired project:', os.environ['SG_PROJECT_NAME']) + env: + PYTHONPATH: $(Build.SourcesDirectory) + SG_SERVER_URL: $(ci_site) + SG_SCRIPT_NAME: $(ci_site_script_name) + SG_API_KEY: $(ci_site_script_key) + SG_PROJECT_NAME: Python API CI - $(Build.BuildId) - $(tk_ci_os_name) - $(tk_ci_python_version)