Skip to content

Commit

Permalink
SG-35018 Condition auth for Jenkins environment (#350)
Browse files Browse the repository at this point in the history
* Condition auth for Jenkins environment

* Add prints

* Add more debug logic

* Use session token for Jenkins

* Add debug info

* More debug info

* Revert f4e51cb

* Remove redundant code

* Revert

* Unskip old tests. Skip datetime tests for Jenkins.

* Condition test for SG_JENKINS

* Improve test

* Revert unskip

* Add durations to pytest

* Address feedback

* Refactor find_one_await_thumbnail invocation

* Remove duplicate line

* Unskip test

* Update .coveragerc

* Addressing feedback
  • Loading branch information
carlos-villavicencio-adsk authored Jun 27, 2024
1 parent fc9eff1 commit bd5245d
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 95 deletions.
2 changes: 2 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ source=shotgun_api3
omit=
shotgun_api3/lib/httplib2/*
shotgun_api3/lib/six.py
shotgun_api3/lib/certify/*
shotgun_api3/lib/pyparsing.py
2 changes: 1 addition & 1 deletion azure-pipelines-templates/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ jobs:
# for example 'Windows - 2.7'
- bash: |
cp ./tests/example_config ./tests/config
pytest -v --cov shotgun_api3 --cov-report xml --test-run-title="${{parameters.name}}-$(python.version)"
pytest --durations=0 -v --cov shotgun_api3 --cov-report xml --test-run-title="${{parameters.name}}-$(python.version)"
displayName: Running tests
env:
# Pass the values needed to authenticate with the Flow Production Tracking site and create some entities.
Expand Down
52 changes: 36 additions & 16 deletions tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
import random
import re
import time
import unittest

from . import mock
Expand All @@ -26,6 +27,11 @@ def skip(f):
return lambda self: None


THUMBNAIL_MAX_ATTEMPTS = 30
THUMBNAIL_RETRY_INTERVAL = 10
TRANSIENT_IMAGE_PATH = "images/status/transient"


class TestBase(unittest.TestCase):
'''Base class for tests.
Expand Down Expand Up @@ -59,6 +65,14 @@ def setUpClass(cls):
cur_folder = os.path.dirname(os.path.abspath(__file__))
config_path = os.path.join(cur_folder, "config")
cls.config.read_config(config_path)
if cls.config.jenkins:
cls.auth_args = dict(
login=cls.config.human_login, password=cls.config.human_password
)
else:
cls.auth_args = dict(
script_name=cls.config.script_name, api_key=cls.config.api_key
)

def setUp(self, auth_mode='ApiUser'):
# When running the tests from a pull request from a client, the Shotgun
Expand Down Expand Up @@ -90,9 +104,8 @@ def setUp(self, auth_mode='ApiUser'):
# first make an instance based on script key/name so
# we can generate a session token
sg = api.Shotgun(self.config.server_url,
self.config.script_name,
self.config.api_key,
http_proxy=self.config.http_proxy)
http_proxy=self.config.http_proxy,
**self.auth_args)
self.session_token = sg.get_session_token()
# now log in using session token
self.sg = api.Shotgun(self.config.server_url,
Expand Down Expand Up @@ -234,7 +247,9 @@ def _setup_mock_data(self):
class LiveTestBase(TestBase):
'''Test base for tests relying on connection to server.'''

def setUp(self, auth_mode='ApiUser'):
def setUp(self, auth_mode=None):
if not auth_mode:
auth_mode = 'HumanUser' if self.config.jenkins else 'ApiUser'
super(LiveTestBase, self).setUp(auth_mode)
if self.sg.server_caps.version and \
self.sg.server_caps.version >= (3, 3, 0) and \
Expand All @@ -260,18 +275,10 @@ def setUpClass(cls):
# When running the tests from a pull request from a client, the Shotgun
# site URL won't be set, so do not attempt to connect to Shotgun.
if cls.config.server_url:
if cls.config.jenkins:
sg = api.Shotgun(
cls.config.server_url,
login=cls.config.human_login,
password=cls.config.human_password
)
else:
sg = api.Shotgun(
cls.config.server_url,
cls.config.script_name,
cls.config.api_key
)
sg = api.Shotgun(
cls.config.server_url,
**cls.auth_args,
)
cls.sg_version = tuple(sg.info()['version'][:3])
cls._setup_db(cls.config, sg)

Expand Down Expand Up @@ -365,6 +372,19 @@ def gen_entity(self, entity_type, **kwargs):
rv = self.sg.delete(entity_type, entity["id"])
assert rv == True

def find_one_await_thumbnail(self, entity_type, filters, fields=["image"], thumbnail_field_name="image", **kwargs):
attempts = 0
while attempts < THUMBNAIL_MAX_ATTEMPTS:
result = self.sg.find_one(entity_type, filters, fields=fields, **kwargs)
if TRANSIENT_IMAGE_PATH in result.get(thumbnail_field_name, ""):
return result

time.sleep(THUMBNAIL_RETRY_INTERVAL)
attempts += 1
else:
if self.config.jenkins:
self.skipTest("Jenkins test timed out waiting for thumbnail")


class HumanUserAuthLiveTestBase(LiveTestBase):
'''
Expand Down
Loading

0 comments on commit bd5245d

Please sign in to comment.