Skip to content

Commit eae46eb

Browse files
Fix paths
1 parent adc8a9d commit eae46eb

File tree

2 files changed

+81
-6
lines changed

2 files changed

+81
-6
lines changed

shared/python/utils.py

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,13 +337,55 @@ def create_bicep_deployment_group(rg_name: str, rg_location: str, deployment: st
337337
"parameters": bicep_parameters
338338
}
339339

340+
# Get the current working directory to ensure files are found in the correct location
341+
current_dir = os.getcwd()
342+
343+
# Determine the correct infrastructure directory based on the deployment parameter
344+
if hasattr(deployment, 'value'):
345+
deployment_name = deployment.value
346+
infrastructure_dir = deployment.value
347+
else:
348+
deployment_name = deployment
349+
infrastructure_dir = deployment
350+
351+
# Check if we're already in the correct infrastructure directory
352+
if os.path.basename(current_dir) == infrastructure_dir:
353+
# We're already in the right directory
354+
bicep_dir = current_dir
355+
else:
356+
# Look for the infrastructure directory from the current location
357+
bicep_dir = os.path.join(current_dir, 'infrastructure', infrastructure_dir)
358+
359+
# If that doesn't exist, try going up one level and looking again
360+
if not os.path.exists(bicep_dir):
361+
parent_dir = os.path.dirname(current_dir)
362+
bicep_dir = os.path.join(parent_dir, 'infrastructure', infrastructure_dir)
363+
364+
# If still not found, check if we can find the infrastructure directory by searching
365+
if not os.path.exists(bicep_dir):
366+
# Try to find the project root and construct the path from there
367+
# This handles cases where we're in subdirectories or different locations
368+
try:
369+
from apimtypes import _get_project_root
370+
project_root = _get_project_root()
371+
bicep_dir = os.path.join(str(project_root), 'infrastructure', infrastructure_dir)
372+
except Exception:
373+
bicep_dir = os.path.join(current_dir, 'infrastructure', infrastructure_dir)
374+
375+
main_bicep_path = os.path.join(bicep_dir, 'main.bicep')
376+
params_file_path = os.path.join(bicep_dir, bicep_parameters_file)
377+
340378
# Write the updated bicep parameters to the specified parameters file
341-
with open(bicep_parameters_file, 'w') as file:
379+
with open(params_file_path, 'w') as file:
342380
file.write(json.dumps(bicep_parameters_format))
343381

344382
print(f"📝 Updated the policy XML in the bicep parameters file '{bicep_parameters_file}'")
383+
384+
# Verify that main.bicep exists in the infrastructure directory
385+
if not os.path.exists(main_bicep_path):
386+
raise FileNotFoundError(f"main.bicep file not found in expected infrastructure directory: {bicep_dir}")
345387

346-
return run(f"az deployment group create --name {deployment_name} --resource-group {rg_name} --template-file main.bicep --parameters {bicep_parameters_file} --query \"properties.outputs\"",
388+
return run(f"az deployment group create --name {deployment_name} --resource-group {rg_name} --template-file \"{main_bicep_path}\" --parameters \"{params_file_path}\" --query \"properties.outputs\"",
347389
f"Deployment '{deployment_name}' succeeded", f"Deployment '{deployment_name}' failed.")
348390

349391
def create_resource_group(rg_name: str, resource_group_location: str | None = None, tags: dict | None = None) -> None:

tests/python/test_utils.py

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,10 @@ def test_create_bicep_deployment_group_with_enum(monkeypatch):
413413
mock_open_func = mock_open()
414414
monkeypatch.setattr(builtins, 'open', mock_open_func)
415415
monkeypatch.setattr(builtins, 'print', MagicMock())
416+
# Mock os functions for file path operations
417+
monkeypatch.setattr('os.getcwd', MagicMock(return_value='/test/dir'))
418+
monkeypatch.setattr('os.path.exists', MagicMock(return_value=True))
419+
monkeypatch.setattr('os.path.basename', MagicMock(return_value='test-dir'))
416420

417421
bicep_params = {'param1': {'value': 'test'}}
418422
rg_tags = {'infrastructure': 'simple-apim'}
@@ -440,6 +444,10 @@ def test_create_bicep_deployment_group_with_string(monkeypatch):
440444
mock_open_func = mock_open()
441445
monkeypatch.setattr(builtins, 'open', mock_open_func)
442446
monkeypatch.setattr(builtins, 'print', MagicMock())
447+
# Mock os functions for file path operations
448+
monkeypatch.setattr('os.getcwd', MagicMock(return_value='/test/dir'))
449+
monkeypatch.setattr('os.path.exists', MagicMock(return_value=True))
450+
monkeypatch.setattr('os.path.basename', MagicMock(return_value='test-dir'))
443451

444452
bicep_params = {'param1': {'value': 'test'}}
445453

@@ -464,6 +472,10 @@ def test_create_bicep_deployment_group_params_file_written(monkeypatch):
464472
mock_open_func = mock_open()
465473
monkeypatch.setattr(builtins, 'open', mock_open_func)
466474
monkeypatch.setattr(builtins, 'print', MagicMock())
475+
# Mock os functions for file path operations
476+
monkeypatch.setattr('os.getcwd', MagicMock(return_value='/test/dir'))
477+
monkeypatch.setattr('os.path.exists', MagicMock(return_value=True))
478+
monkeypatch.setattr('os.path.basename', MagicMock(return_value='test-dir'))
467479

468480
bicep_params = {
469481
'apiManagementName': {'value': 'test-apim'},
@@ -474,8 +486,9 @@ def test_create_bicep_deployment_group_params_file_written(monkeypatch):
474486
'test-rg', 'eastus', INFRASTRUCTURE.APIM_ACA, bicep_params, 'custom-params.json'
475487
)
476488

477-
# Verify file was opened for writing
478-
mock_open_func.assert_called_once_with('custom-params.json', 'w')
489+
# Verify file was opened for writing with resolved infrastructure path
490+
expected_path = os.path.join('/test/dir', 'infrastructure', 'apim-aca', 'custom-params.json')
491+
mock_open_func.assert_called_once_with(expected_path, 'w')
479492

480493
# Verify the correct JSON structure was written
481494
written_content = ''.join(call.args[0] for call in mock_open_func().write.call_args_list)
@@ -495,7 +508,11 @@ def test_create_bicep_deployment_group_no_tags(monkeypatch):
495508
mock_open_func = mock_open()
496509
monkeypatch.setattr(builtins, 'open', mock_open_func)
497510
monkeypatch.setattr(builtins, 'print', MagicMock())
498-
511+
# Mock os functions for file path operations
512+
monkeypatch.setattr('os.getcwd', MagicMock(return_value='/test/dir'))
513+
monkeypatch.setattr('os.path.exists', MagicMock(return_value=True))
514+
monkeypatch.setattr('os.path.basename', MagicMock(return_value='test-dir'))
515+
499516
bicep_params = {'param1': {'value': 'test'}}
500517

501518
utils.create_bicep_deployment_group('test-rg', 'eastus', 'test-deployment', bicep_params)
@@ -512,7 +529,11 @@ def test_create_bicep_deployment_group_deployment_failure(monkeypatch):
512529
mock_open_func = mock_open()
513530
monkeypatch.setattr(builtins, 'open', mock_open_func)
514531
monkeypatch.setattr(builtins, 'print', MagicMock())
515-
532+
# Mock os functions for file path operations
533+
monkeypatch.setattr('os.getcwd', MagicMock(return_value='/test/dir'))
534+
monkeypatch.setattr('os.path.exists', MagicMock(return_value=True))
535+
monkeypatch.setattr('os.path.basename', MagicMock(return_value='test-dir'))
536+
516537
bicep_params = {'param1': {'value': 'test'}}
517538

518539
result = utils.create_bicep_deployment_group('test-rg', 'eastus', 'test-deployment', bicep_params)
@@ -571,6 +592,12 @@ def test_create_bicep_deployment_group_success(monkeypatch):
571592
mock_output = MagicMock(success=True, json_data={"id": "deployment-id"})
572593
monkeypatch.setattr(utils, 'run', lambda *a, **kw: mock_output)
573594
monkeypatch.setattr(utils, 'does_resource_group_exist', lambda x: True)
595+
# Mock os functions for file path operations
596+
monkeypatch.setattr('os.getcwd', MagicMock(return_value='/test/dir'))
597+
monkeypatch.setattr('os.path.exists', MagicMock(return_value=True))
598+
monkeypatch.setattr('os.path.basename', MagicMock(return_value='test-dir'))
599+
mock_open_func = mock_open()
600+
monkeypatch.setattr(builtins, 'open', mock_open_func)
574601

575602
result = utils.create_bicep_deployment_group(
576603
"test-rg", "eastus", INFRASTRUCTURE.SIMPLE_APIM, {"param1": "value1"}
@@ -583,6 +610,12 @@ def test_create_bicep_deployment_group_creates_rg(monkeypatch):
583610
monkeypatch.setattr(utils, 'run', lambda *a, **kw: mock_output)
584611
monkeypatch.setattr(utils, 'does_resource_group_exist', lambda x: False)
585612
monkeypatch.setattr(utils, 'create_resource_group', lambda *a, **kw: None)
613+
# Mock os functions for file path operations
614+
monkeypatch.setattr('os.getcwd', MagicMock(return_value='/test/dir'))
615+
monkeypatch.setattr('os.path.exists', MagicMock(return_value=True))
616+
monkeypatch.setattr('os.path.basename', MagicMock(return_value='test-dir'))
617+
mock_open_func = mock_open()
618+
monkeypatch.setattr(builtins, 'open', mock_open_func)
586619

587620
result = utils.create_bicep_deployment_group(
588621
"test-rg", "eastus", INFRASTRUCTURE.SIMPLE_APIM, {"param1": "value1"}

0 commit comments

Comments
 (0)