|
| 1 | +from unittest import mock |
| 2 | + |
| 3 | +import click |
| 4 | +import pytest |
| 5 | + |
| 6 | +from cumulusci.cli.runtime import CliRuntime |
| 7 | + |
| 8 | +from .. import checks |
| 9 | +from .utils import run_click_command |
| 10 | + |
| 11 | + |
| 12 | +@pytest.fixture |
| 13 | +def runtime(): |
| 14 | + runtime = CliRuntime() |
| 15 | + runtime.project_config.config["plans"] = { |
| 16 | + "plan 1": { |
| 17 | + "title": "Test Plan #1", |
| 18 | + "slug": "plan1_slug", |
| 19 | + "tier": "primary", |
| 20 | + "preflight_message": "This is a preflight message", |
| 21 | + "error_message": "This is an error message", |
| 22 | + "steps": { |
| 23 | + 1: { |
| 24 | + "task": "run_tests", |
| 25 | + "ui_options": { |
| 26 | + "name": "Run Tests", |
| 27 | + "is_recommended": False, |
| 28 | + "is_required": False, |
| 29 | + }, |
| 30 | + "checks": [ |
| 31 | + { |
| 32 | + "when": "soon", |
| 33 | + "action": "error", |
| 34 | + "message": "Danger Will Robinson!", |
| 35 | + } |
| 36 | + ], |
| 37 | + } |
| 38 | + }, |
| 39 | + "checks": [ |
| 40 | + { |
| 41 | + "when": "'test package' not in tasks.get_installed_packages()", |
| 42 | + "action": "error", |
| 43 | + "message": "Test Package must be installed in your org.", |
| 44 | + } |
| 45 | + ], |
| 46 | + }, |
| 47 | + } |
| 48 | + |
| 49 | + yield runtime |
| 50 | + |
| 51 | + |
| 52 | +@mock.patch("cumulusci.cli.checks.CliTable") |
| 53 | +def test_checks_info(cli_table, runtime): |
| 54 | + |
| 55 | + run_click_command(checks.checks_info, runtime=runtime, plan_name="plan 1") |
| 56 | + cli_table.assert_called_once_with( |
| 57 | + title="Plan Preflights", |
| 58 | + data=[ |
| 59 | + ["Action", "Message", "When"], |
| 60 | + [ |
| 61 | + "error", |
| 62 | + "Test Package must be installed in your org.", |
| 63 | + "'test package' not in tasks.get_installed_packages()", |
| 64 | + ], |
| 65 | + ], |
| 66 | + ) |
| 67 | + |
| 68 | + |
| 69 | +@mock.patch("cumulusci.cli.checks.PreflightFlowCoordinator") |
| 70 | +def test_checks_run(mock_flow_coordinator, runtime): |
| 71 | + org_config = mock.Mock(scratch=True, config={}) |
| 72 | + org_config.username = "test_user" |
| 73 | + org_config.org_id = "test_org_id" |
| 74 | + runtime.get_org = mock.Mock(return_value=("test", org_config)) |
| 75 | + |
| 76 | + mock_flow_coordinator_return_value = mock.Mock() |
| 77 | + mock_flow_coordinator_return_value.preflight_results = {} # No errors or warnings |
| 78 | + mock_flow_coordinator.return_value = mock_flow_coordinator_return_value |
| 79 | + with mock.patch("logging.getLogger") as mock_logger: |
| 80 | + |
| 81 | + run_click_command( |
| 82 | + checks.checks_run, |
| 83 | + runtime=runtime, |
| 84 | + plan_name="plan 1", |
| 85 | + org="test", |
| 86 | + ) |
| 87 | + mock_logger.assert_called() |
| 88 | + mock_flow_coordinator.assert_called_once_with( |
| 89 | + runtime.project_config, mock.ANY, name="preflight" |
| 90 | + ) |
| 91 | + |
| 92 | + |
| 93 | +@mock.patch("cumulusci.cli.checks.PreflightFlowCoordinator") |
| 94 | +@mock.patch("logging.getLogger") |
| 95 | +def test_checks_run_unknown_plan(mock_flow_coordinator, mock_logger, runtime): |
| 96 | + org_config = mock.Mock(scratch=True, config={}) |
| 97 | + org_config.username = "test_user" |
| 98 | + org_config.org_id = "test_org_id" |
| 99 | + runtime.get_org = mock.Mock(return_value=("test", org_config)) |
| 100 | + |
| 101 | + mock_flow_coordinator_return_value = mock.Mock() |
| 102 | + mock_flow_coordinator_return_value.preflight_results = {} # No errors or warnings |
| 103 | + mock_flow_coordinator.return_value = mock_flow_coordinator_return_value |
| 104 | + with pytest.raises(click.UsageError) as error: |
| 105 | + |
| 106 | + run_click_command( |
| 107 | + checks.checks_run, |
| 108 | + runtime=runtime, |
| 109 | + plan_name="unknown plan", |
| 110 | + org="test", |
| 111 | + ) |
| 112 | + mock_flow_coordinator.assert_called_once_with( |
| 113 | + runtime.project_config, mock.ANY, name="preflight" |
| 114 | + ) |
| 115 | + assert "Unknown plan 'unknown_plan'" in error.value |
| 116 | + |
| 117 | + |
| 118 | +@mock.patch("cumulusci.cli.checks.PreflightFlowCoordinator") |
| 119 | +def test_checks_run_with_warnings(mock_flow_coordinator, runtime): |
| 120 | + org_config = mock.Mock(scratch=True, config={}) |
| 121 | + org_config.username = "test_user" |
| 122 | + org_config.org_id = "test_org_id" |
| 123 | + runtime.get_org = mock.Mock(return_value=("test", org_config)) |
| 124 | + |
| 125 | + mock_flow_coordinator.return_value.preflight_results = { |
| 126 | + None: [ |
| 127 | + { |
| 128 | + "status": "warning", |
| 129 | + "message": "You need Context Service AdminPsl in your Org assigned Admin User to use this feature. Contact your Administrator.", |
| 130 | + } |
| 131 | + ] |
| 132 | + } |
| 133 | + |
| 134 | + run_click_command( |
| 135 | + checks.checks_run, |
| 136 | + runtime=runtime, |
| 137 | + plan_name="plan 1", |
| 138 | + org="test", |
| 139 | + ) |
| 140 | + mock_flow_coordinator.assert_called_once_with( |
| 141 | + runtime.project_config, mock.ANY, name="preflight" |
| 142 | + ) |
| 143 | + |
| 144 | + |
| 145 | +@mock.patch("cumulusci.cli.checks.PreflightFlowCoordinator") |
| 146 | +def test_checks_run_with_errors(mock_flow_coordinator, runtime): |
| 147 | + org_config = mock.Mock(scratch=True, config={}) |
| 148 | + org_config.username = "test_user" |
| 149 | + org_config.org_id = "test_org_id" |
| 150 | + runtime.get_org = mock.Mock(return_value=("test", org_config)) |
| 151 | + |
| 152 | + mock_flow_coordinator.return_value.preflight_results = { |
| 153 | + None: [ |
| 154 | + { |
| 155 | + "status": "error", |
| 156 | + "message": "You need Context Service AdminPsl in your Org assigned Admin User to use this feature. Contact your Administrator.", |
| 157 | + } |
| 158 | + ] |
| 159 | + } |
| 160 | + with pytest.raises(Exception) as error: |
| 161 | + |
| 162 | + run_click_command( |
| 163 | + checks.checks_run, |
| 164 | + runtime=runtime, |
| 165 | + plan_name="plan 1", |
| 166 | + org="test", |
| 167 | + ) |
| 168 | + mock_flow_coordinator.assert_called_once_with( |
| 169 | + runtime.project_config, mock.ANY, name="preflight" |
| 170 | + ) |
| 171 | + assert ( |
| 172 | + "Some of the checks failed with errors. Please check the logs for details." |
| 173 | + in error.value |
| 174 | + ) |
0 commit comments