Skip to content

Commit 2d4d8eb

Browse files
committed
fix: get_taskcluster_client should respect PRODUCTION_TASKCLUSTER_ROOT_URL
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 2d4d8eb

File tree

2 files changed

+47
-9
lines changed

2 files changed

+47
-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: 46 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,7 @@ 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)
2923

3024

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

0 commit comments

Comments
 (0)