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,7 @@ 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 )
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