Skip to content

Commit d8179b3

Browse files
authored
fix: get_taskcluster_client should respect PRODUCTION_TASKCLUSTER_ROOT_URL (taskcluster#835)
If no root url is set in the environment but PRODUCTION_TASKCLUSTER_ROOT_URL is set, we should use that. See https://bugzilla.mozilla.org/show_bug.cgi?id=1996183
1 parent e0b8214 commit d8179b3

File tree

2 files changed

+48
-9
lines changed

2 files changed

+48
-9
lines changed

src/taskgraph/util/taskcluster.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def get_taskcluster_client(service: str):
6565
if "TASKCLUSTER_PROXY_URL" in os.environ:
6666
options = {"rootUrl": os.environ["TASKCLUSTER_PROXY_URL"]}
6767
else:
68-
options = taskcluster.optionsFromEnvironment()
68+
options = taskcluster.optionsFromEnvironment({"rootUrl": get_root_url()})
6969

7070
return getattr(taskcluster, service[0].upper() + service[1:])(options)
7171

test/test_util_taskcluster.py

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
44

55
import datetime
6-
import os
6+
from unittest.mock import MagicMock
77

88
import pytest
99

@@ -19,13 +19,8 @@ def root_url():
1919
@pytest.fixture(autouse=True)
2020
def mock_environ(monkeypatch, root_url):
2121
# Ensure user specified environment variables don't interfere with URLs.
22-
monkeypatch.setattr(
23-
os,
24-
"environ",
25-
{
26-
"TASKCLUSTER_ROOT_URL": root_url,
27-
},
28-
)
22+
monkeypatch.setenv("TASKCLUSTER_ROOT_URL", root_url)
23+
monkeypatch.delenv("TASKCLUSTER_PROXY_URL", raising=False)
2924

3025

3126
@pytest.fixture(autouse=True)
@@ -566,3 +561,47 @@ def test_get_ancestors_string(responses, root_url):
566561
"eee": "task-eee",
567562
}
568563
assert got == expected, f"got: {got}, expected: {expected}"
564+
565+
566+
def test_get_taskcluster_client(monkeypatch, root_url):
567+
tc.get_root_url.cache_clear()
568+
tc.get_taskcluster_client.cache_clear()
569+
service_mock = MagicMock()
570+
monkeypatch.setattr("taskcluster.Foo", service_mock, raising=False)
571+
572+
# No environment and no default → error
573+
monkeypatch.delenv("TASKCLUSTER_ROOT_URL", raising=False)
574+
monkeypatch.delenv("TASKCLUSTER_PROXY_URL", raising=False)
575+
monkeypatch.setattr(tc, "PRODUCTION_TASKCLUSTER_ROOT_URL", None)
576+
with pytest.raises(RuntimeError):
577+
tc.get_taskcluster_client("foo")
578+
service_mock.assert_not_called()
579+
580+
tc.get_root_url.cache_clear()
581+
tc.get_taskcluster_client.cache_clear()
582+
service_mock.reset_mock()
583+
584+
# No environment, use default
585+
monkeypatch.setattr(
586+
tc, "PRODUCTION_TASKCLUSTER_ROOT_URL", "http://taskcluster-prod"
587+
)
588+
tc.get_taskcluster_client("foo")
589+
service_mock.assert_called_once_with({"rootUrl": "http://taskcluster-prod"})
590+
591+
tc.get_root_url.cache_clear()
592+
tc.get_taskcluster_client.cache_clear()
593+
service_mock.reset_mock()
594+
595+
# root url from environment
596+
monkeypatch.setenv("TASKCLUSTER_ROOT_URL", "http://taskcluster-env")
597+
tc.get_taskcluster_client("foo")
598+
service_mock.assert_called_once_with({"rootUrl": "http://taskcluster-env"})
599+
600+
tc.get_root_url.cache_clear()
601+
tc.get_taskcluster_client.cache_clear()
602+
service_mock.reset_mock()
603+
604+
# proxy url from environment
605+
monkeypatch.setenv("TASKCLUSTER_PROXY_URL", "http://taskcluster-proxy")
606+
tc.get_taskcluster_client("foo")
607+
service_mock.assert_called_once_with({"rootUrl": "http://taskcluster-proxy"})

0 commit comments

Comments
 (0)