Skip to content

Commit 525358f

Browse files
authored
Azure Pipelines supports for the Python API (#249)
1 parent a4231a5 commit 525358f

11 files changed

Lines changed: 267 additions & 122 deletions

File tree

.travis.yml

Lines changed: 0 additions & 41 deletions
This file was deleted.

appveyor.yml

Lines changed: 0 additions & 50 deletions
This file was deleted.
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
# -----------------------------------------------------------------------------
2+
# Copyright (c) 2009-2021, Shotgun Software Inc.
3+
#
4+
# Redistribution and use in source and binary forms, with or without
5+
# modification, are permitted provided that the following conditions are met:
6+
#
7+
# - Redistributions of source code must retain the above copyright notice, this
8+
# list of conditions and the following disclaimer.
9+
#
10+
# - Redistributions in binary form must reproduce the above copyright notice,
11+
# this list of conditions and the following disclaimer in the documentation
12+
# and/or other materials provided with the distribution.
13+
#
14+
# - Neither the name of the Shotgun Software Inc nor the names of its
15+
# contributors may be used to endorse or promote products derived from this
16+
# software without specific prior written permission.
17+
#
18+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21+
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22+
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24+
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25+
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26+
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27+
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28+
29+
# This is the list of parameters for this template and their default values.
30+
parameters:
31+
name: ''
32+
vm_image: ''
33+
34+
jobs:
35+
# The job will be named after the OS and Azure will suffix the strategy to make it unique
36+
# so we'll have a job name "Windows Python27" for example. What's a strategy? Strategies are the
37+
# name of the keys under the strategy.matrix scope. So for each OS we'll have "<OS> Python27" and
38+
# "<OS> Python37".
39+
- job: ${{ parameters.name }}
40+
pool:
41+
vmImage: ${{ parameters.vm_image }}
42+
# The strategy is another way of removing repetition. It will create one job per entry in the
43+
# matrix.
44+
strategy:
45+
matrix:
46+
# We support these two versions of Python.
47+
Python27:
48+
python.version: '2.7'
49+
Python37:
50+
python.version: '3.7'
51+
52+
# These are the steps that will be executed inside each job.
53+
steps:
54+
# Specifies which version of Python we want to use. That's where the strategy comes in.
55+
# Each job will share this set of steps, but each of them will receive a different
56+
# $(python.version)
57+
- task: UsePythonVersion@0
58+
inputs:
59+
versionSpec: '$(python.version)'
60+
addToPath: True
61+
62+
# Install all dependencies needed for running the tests. This command is good for
63+
# Python 2 and 3, but also for all OSes
64+
- script: |
65+
python -m pip install --upgrade pip setuptools wheel
66+
python -m pip install -r tests/ci_requirements.txt
67+
displayName: Install tools
68+
69+
# The {{}} syntax is meant for the the pre-processor of Azure pipeline. Every statement inside
70+
# a {{}} block will be evaluated and substituted before the file is parsed to create the jobs.
71+
# So here we're inserting an extra step if the template is being invoked for Windows.
72+
- ${{ if eq(parameters.name, 'Windows') }}:
73+
# On Windows, we need to update the certificates, the cert store is missing the newer one
74+
# from Amazon like some clients experienced a while back. Who would have thought Microsoft
75+
# would have been out of date! ;)
76+
- powershell: |
77+
$cert_url = "https://www.amazontrust.com/repository/SFSRootCAG2.cer"
78+
$cert_file = New-TemporaryFile
79+
Invoke-WebRequest -Uri $cert_url -UseBasicParsing -OutFile $cert_file.FullName
80+
Import-Certificate -FilePath $cert_file.FullName -CertStoreLocation Cert:\LocalMachine\Root
81+
displayName: Updating OS Certificates
82+
83+
# Runs the tests and generates test coverage. The tests results are uploaded to Azure Pipelines in the
84+
# Tests tab of the build and each test run will be named after the --test-run-title argument to pytest,
85+
# for example 'Windows - 2.7'
86+
- bash: |
87+
cp ./tests/example_config ./tests/config
88+
pytest -v --cov shotgun_api3 --cov-report xml --test-run-title="${{parameters.name}}-$(python.version)"
89+
displayName: Running tests
90+
env:
91+
# Pass the values needed to authenticate with the Shotgun site and create some entities.
92+
# Remember, on a pull request from a client or on forked repos, those variables
93+
# will be empty!
94+
SG_SERVER_URL: $(ci_site)
95+
SG_SCRIPT_NAME: $(ci_site_script_name)
96+
SG_API_KEY: $(ci_site_script_key)
97+
# The unit tests manipulate the user and project during the tests, which can cause collisions,
98+
# so sandbox each build variant.
99+
# Ideally, we would use the agent name here. The problem is that the agent name is in a build
100+
# variable, so we can't edit it's value through a ${{replace(a,b,c)}} expression, which are evaluated before
101+
# build variables are available. Because of this, we need to find another way to generate a
102+
# unique login. So instead, we'll use the the name of the platform and the python version,
103+
# which should make it unique.
104+
SG_HUMAN_LOGIN: $(python_api_human_login)-${{parameters.name}}-$(python.version)
105+
# This will give a user name like 'something macOS 2.7'
106+
SG_HUMAN_NAME: $(python_api_human_name) ${{parameters.name}} $(python.version)
107+
SG_HUMAN_PASSWORD: $(python_api_human_password)
108+
# So, first, we need to make sure that two builds running at the same time do not manipulate
109+
# the same entities, so we're sandboxing build nodes based on their name.
110+
SG_PROJECT_NAME: Python API CI - $(Agent.Name)
111+
# The entities created and then reused between tests assume that the same user is always
112+
# manipulating them. Because different builds will be assigned different agents and therefore
113+
# different projects, it means each project needs to have an entity specific to a given user.
114+
# Again, this would have been a lot simpler if we could simply have had a login based on the
115+
# agent name, but alas, the agent name has a space in it which needs to be replaced to something
116+
# else and string substitution can't be made on build variables, only template parameters.
117+
SG_ASSET_CODE: CI-$(python_api_human_login)-${{parameters.name}}-$(python.version)
118+
SG_VERSION_CODE: CI-$(python_api_human_login)-${{parameters.name}}-$(python.version)
119+
SG_SHOT_CODE: CI-$(python_api_human_login)-${{parameters.name}}-$(python.version)
120+
SG_TASK_CONTENT: CI-$(python_api_human_login)-${{parameters.name}}-$(python.version)
121+
SG_TICKET_TILE: CI-$(python_api_human_login)-${{parameters.name}}-$(python.version)
122+
SG_PLAYLIST_CODE: CI-$(python_api_human_login)-${{parameters.name}}-$(python.version)
123+
124+
# Upload the code coverage result to codecov.io.
125+
- ${{ if eq(parameters.name, 'Windows') }}:
126+
- powershell: |
127+
$ProgressPreference = 'SilentlyContinue'
128+
Invoke-WebRequest -Uri https://uploader.codecov.io/latest/windows/codecov.exe -Outfile codecov.exe
129+
.\codecov.exe -f coverage.xml
130+
displayName: Uploading code coverage
131+
- ${{ elseif eq(parameters.name, 'Linux') }}:
132+
- script: |
133+
curl -Os https://uploader.codecov.io/latest/linux/codecov
134+
chmod +x codecov
135+
./codecov -f coverage.xml
136+
displayName: Uploading code coverage
137+
- ${{ else }}:
138+
- script: |
139+
curl -Os https://uploader.codecov.io/latest/macos/codecov
140+
chmod +x codecov
141+
./codecov -f coverage.xml
142+
displayName: Uploading code coverage

azure-pipelines.yml

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# -----------------------------------------------------------------------------
2+
# Copyright (c) 2009-2021, Shotgun Software Inc.
3+
#
4+
# Redistribution and use in source and binary forms, with or without
5+
# modification, are permitted provided that the following conditions are met:
6+
#
7+
# - Redistributions of source code must retain the above copyright notice, this
8+
# list of conditions and the following disclaimer.
9+
#
10+
# - Redistributions in binary form must reproduce the above copyright notice,
11+
# this list of conditions and the following disclaimer in the documentation
12+
# and/or other materials provided with the distribution.
13+
#
14+
# - Neither the name of the Shotgun Software Inc nor the names of its
15+
# contributors may be used to endorse or promote products derived from this
16+
# software without specific prior written permission.
17+
#
18+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21+
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22+
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24+
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25+
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26+
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27+
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28+
29+
# We've stored some variables in Azure. They contain credentials
30+
# and are encrypted. They are also not available to clients.
31+
# This statement says which variable groups this repo requires.
32+
# We have two: one that can be shared by all repos that use CI
33+
# and another that is used just by the Python API, which creates
34+
# HumanUser's and as such need a password, which we can't
35+
# hardcode in the .yml files.
36+
variables:
37+
- group: sg-credentials
38+
39+
# We want builds to trigger for 3 reasons:
40+
# - The master branch sees new commits
41+
# - Each PR should get rebuilt when commits are added to it.
42+
trigger:
43+
branches:
44+
include:
45+
- master
46+
pr:
47+
branches:
48+
include:
49+
- "*"
50+
51+
# This here is the list of jobs we want to run for our build.
52+
# Jobs run in parallel.
53+
jobs:
54+
55+
# These are jobs templates, they allow to reduce the redundancy between
56+
# variations of the same build. We pass in the image name
57+
# and a friendly name that then template can then use to create jobs.
58+
- template: azure-pipelines-templates/run-tests.yml
59+
parameters:
60+
name: Linux
61+
vm_image: 'ubuntu-20.04'
62+
63+
- template: azure-pipelines-templates/run-tests.yml
64+
parameters:
65+
name: macOS
66+
vm_image: 'macOS-10.15'
67+
68+
- template: azure-pipelines-templates/run-tests.yml
69+
parameters:
70+
name: Windows
71+
vm_image: 'windows-2019'

shotgun_api3/lib/mockgun/schema.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,8 @@ def get_schemas(cls, schema_path, schema_entity_path):
8080

8181
@classmethod
8282
def _read_file(cls, path):
83-
fh = open(path, "rb")
84-
try:
83+
with open(path, "rb") as fh:
8584
return pickle.load(fh)
86-
finally:
87-
fh.close()
8885

8986

9087
# Highest protocol that Python 2.4 supports, which is the earliest version of Python we support.

tests/base.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ def setUpClass(cls):
6060
cls.config.read_config(config_path)
6161

6262
def setUp(self, auth_mode='ApiUser'):
63+
# When running the tests from a pull request from a client, the Shotgun
64+
# site URL won't be set, so do not attempt to run the test.
65+
if not self.config.server_url:
66+
self.skipTest("Shotgun site URL is not set. Skipping this test.")
67+
6368
self.human_login = self.config.human_login
6469
self.human_password = self.config.human_password
6570
self.server_url = self.config.server_url
@@ -241,13 +246,17 @@ def setUpClass(cls):
241246
# As such, we are using setUpClass to load them once during the
242247
# entire duration of the tests.
243248
super(LiveTestBase, cls).setUpClass()
244-
sg = api.Shotgun(
245-
cls.config.server_url,
246-
cls.config.script_name,
247-
cls.config.api_key
248-
)
249-
cls.sg_version = tuple(sg.info()['version'][:3])
250-
cls._setup_db(cls.config, sg)
249+
250+
# When running the tests from a pull request from a client, the Shotgun
251+
# site URL won't be set, so do not attempt to connect to Shotgun.
252+
if cls.config.server_url:
253+
sg = api.Shotgun(
254+
cls.config.server_url,
255+
cls.config.script_name,
256+
cls.config.api_key
257+
)
258+
cls.sg_version = tuple(sg.info()['version'][:3])
259+
cls._setup_db(cls.config, sg)
251260

252261
@classmethod
253262
def _setup_db(cls, config, sg):

tests/ci_requirements.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,8 @@ nose-exclude==0.5.0
1414
# Don't restrict flake8 version, since we install this in CI against Python 2.6,
1515
# where flake8 has discontinued support for newer releases. On Python 2.7 and
1616
# Python 3.7, linting has been performed with flake8 3.7.8
17-
flake8
17+
flake8
18+
pytest
19+
pytest-azurepipelines
20+
coverage
21+
pytest-coverage

tests/mockgun/schema.pickle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98681,4 +98681,4 @@ I01
9868198681
sS'value'
9868298682
p48535
9868398683
g47
98684-
ssss.
98684+
ssss.

tests/mockgun/schema_entity.pickle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1000,4 +1000,4 @@ sS'value'
10001000
p538
10011001
S'Asset'
10021002
p539
1003-
sss.
1003+
sss.

0 commit comments

Comments
 (0)