|
1 | 1 | from unittest import mock |
2 | 2 |
|
| 3 | +import click |
| 4 | +import pytest |
| 5 | + |
3 | 6 | from cumulusci.cli.runtime import CliRuntime |
4 | 7 |
|
5 | 8 | from .. import checks |
6 | 9 | from .utils import run_click_command |
7 | 10 |
|
8 | 11 |
|
9 | | -@mock.patch("click.echo") |
10 | | -def test_flow_info(echo): |
11 | | - |
12 | | - runtime = CliRuntime( |
13 | | - config={ |
14 | | - "flows": { |
15 | | - "test": { |
16 | | - "steps": { |
17 | | - 1: { |
18 | | - "task": "test_task", |
19 | | - "options": {"option_name": "option_value"}, |
| 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!", |
20 | 35 | } |
21 | | - } |
| 36 | + ], |
22 | 37 | } |
23 | 38 | }, |
24 | | - "tasks": { |
25 | | - "test_task": { |
26 | | - "class_path": "cumulusci.cli.tests.test_flow.DummyTask", |
27 | | - "description": "Test Task", |
| 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.", |
28 | 44 | } |
29 | | - }, |
| 45 | + ], |
30 | 46 | }, |
31 | | - load_keychain=False, |
| 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 | + ], |
32 | 66 | ) |
33 | 67 |
|
34 | | - run_click_command(checks.checks_info, runtime=runtime, plan_name="test") |
35 | 68 |
|
36 | | - echo.assert_called_with("This plan has the following preflight checks: ") |
| 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 | + ) |
37 | 91 |
|
38 | 92 |
|
39 | | -@mock.patch("click.echo") |
40 | | -def test_checks_run(echo): |
| 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): |
41 | 96 | org_config = mock.Mock(scratch=True, config={}) |
42 | | - runtime = CliRuntime( |
43 | | - config={ |
44 | | - "flows": {"test": {"steps": {1: {"task": "test_task"}}}}, |
45 | | - "tasks": { |
46 | | - "test_task": { |
47 | | - "class_path": "cumulusci.cli.tests.test_flow.DummyTask", |
48 | | - "description": "Test Task", |
49 | | - } |
50 | | - }, |
51 | | - }, |
52 | | - load_keychain=False, |
53 | | - ) |
| 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" |
54 | 123 | runtime.get_org = mock.Mock(return_value=("test", org_config)) |
55 | 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 | + |
56 | 134 | run_click_command( |
57 | 135 | checks.checks_run, |
58 | 136 | runtime=runtime, |
59 | | - plan_name="test", |
| 137 | + plan_name="plan 1", |
60 | 138 | org="test", |
61 | 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: |
62 | 161 |
|
63 | | - echo.assert_called_with("Running checks for the plan test") |
| 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