33# file, You can obtain one at http://mozilla.org/MPL/2.0/.
44
55import datetime
6- import os
6+ from unittest . mock import MagicMock
77
88import pytest
99
@@ -19,13 +19,8 @@ def root_url():
1919@pytest .fixture (autouse = True )
2020def 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