Skip to content

Commit a336ea7

Browse files
Fix utils tests
1 parent b8c76c0 commit a336ea7

File tree

1 file changed

+34
-14
lines changed

1 file changed

+34
-14
lines changed

tests/python/test_utils.py

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -696,8 +696,14 @@ def test_determine_policy_path_full_path():
696696
def test_check_apim_blob_permissions_success(monkeypatch):
697697
"""Test check_apim_blob_permissions with successful permissions."""
698698
def mock_run_success(cmd, **kwargs):
699-
if 'az apim api operation' in cmd:
700-
return utils.Output(success=True, text='{"statusCode": 200}')
699+
if 'az apim show' in cmd and 'identity.principalId' in cmd:
700+
return utils.Output(success=True, text='12345678-1234-1234-1234-123456789012')
701+
elif 'az storage account show' in cmd and '--query id' in cmd:
702+
return utils.Output(success=True, text='/subscriptions/12345678-1234-1234-1234-123456789012/resourceGroups/test-rg/providers/Microsoft.Storage/storageAccounts/test-storage')
703+
elif 'az role assignment list' in cmd:
704+
return utils.Output(success=True, text='/subscriptions/12345678-1234-1234-1234-123456789012/resourceGroups/test-rg/providers/Microsoft.Authorization/roleAssignments/test-assignment')
705+
elif 'az storage blob list' in cmd:
706+
return utils.Output(success=True, text='test-blob.txt')
701707
return utils.Output(success=True, text='{}')
702708

703709
monkeypatch.setattr(utils, 'run', mock_run_success)
@@ -789,13 +795,23 @@ def test_get_infra_rg_name_different_types(infra_type, expected_suffix, monkeypa
789795

790796
def test_create_bicep_deployment_group_for_sample_success(monkeypatch):
791797
"""Test create_bicep_deployment_group_for_sample success case."""
798+
import os
792799
mock_output = utils.Output(success=True, text='{"outputs": {"test": "value"}}')
793800

794-
def mock_create_bicep(sample_name, rg_name, location, params, tags=None):
801+
def mock_create_bicep(rg_name, rg_location, deployment, bicep_parameters, bicep_parameters_file='params.json', rg_tags=None):
795802
return mock_output
796803

804+
# Mock file system checks
805+
def mock_exists(path):
806+
return True # Pretend all paths exist
807+
808+
def mock_chdir(path):
809+
pass # Do nothing
810+
797811
monkeypatch.setattr(utils, 'create_bicep_deployment_group', mock_create_bicep)
798812
monkeypatch.setattr(utils, 'build_infrastructure_tags', lambda x: [])
813+
monkeypatch.setattr(os.path, 'exists', mock_exists)
814+
monkeypatch.setattr(os, 'chdir', mock_chdir)
799815

800816
result = utils.create_bicep_deployment_group_for_sample('test-sample', 'test-rg', 'eastus', {})
801817
assert result.success is True
@@ -836,7 +852,7 @@ def test_output_class_functionality():
836852
output = utils.Output(success=True, text='{"properties": {"outputs": {"test": {"value": "value"}}}}')
837853
assert output.success is True
838854
assert output.get('test') == 'value'
839-
assert output.get('missing', 'default') == 'default'
855+
assert output.get('missing') is None # Should return None for missing key without label
840856

841857
# Test failed output
842858
output = utils.Output(success=False, text='error')
@@ -846,14 +862,14 @@ def test_output_class_functionality():
846862

847863
def test_run_command_with_error_suppression(monkeypatch):
848864
"""Test run command with error output suppression."""
849-
def mock_subprocess_run(cmd, **kwargs):
850-
class MockResult:
851-
returncode = 1
852-
stdout = "test output"
853-
stderr = "test error"
854-
return MockResult()
865+
def mock_subprocess_check_output(cmd, **kwargs):
866+
# Simulate a CalledProcessError with bytes output
867+
import subprocess
868+
error = subprocess.CalledProcessError(1, cmd)
869+
error.output = b"test output" # Return bytes, as subprocess would
870+
raise error
855871

856-
monkeypatch.setattr('subprocess.run', mock_subprocess_run)
872+
monkeypatch.setattr('subprocess.check_output', mock_subprocess_check_output)
857873

858874
output = utils.run("test command", print_errors=False, print_output=False)
859875
assert output.success is False
@@ -918,16 +934,20 @@ def test_get_azure_role_guid_comprehensive(monkeypatch):
918934

919935
def test_cleanup_functions_comprehensive(monkeypatch):
920936
"""Test cleanup functions with various scenarios."""
921-
monkeypatch.setattr(utils, 'run', lambda x, **kw: utils.Output(success=True, text='{}'))
937+
def mock_run(command, ok_message='', error_message='', print_output=False, print_command_to_run=True, print_errors=True, print_warnings=True):
938+
return utils.Output(success=True, text='{}')
939+
940+
monkeypatch.setattr(utils, 'run', mock_run)
922941

923942
# Test _cleanup_resources (private function)
924943
utils._cleanup_resources('test-deployment', 'test-rg') # Should not raise
925944

926945
# Test cleanup_deployment
927946
utils.cleanup_deployment('test-deployment') # Should not raise
928947

929-
# Test cleanup_infra_deployment with string
930-
utils.cleanup_infra_deployment('test-deployment') # Should not raise
948+
# Test cleanup_infra_deployments with INFRASTRUCTURE enum (correct function name and parameter type)
949+
from shared.python.apimtypes import INFRASTRUCTURE
950+
utils.cleanup_infra_deployments(INFRASTRUCTURE.SIMPLE_APIM) # Should not raise
931951

932952
# Test cleanup_deployment with string
933953
utils.cleanup_deployment('test-deployment') # Should not raise

0 commit comments

Comments
 (0)