|
| 1 | +import logging |
| 2 | +import os |
1 | 3 | import subprocess |
2 | 4 |
|
3 | 5 | import pytest |
4 | 6 |
|
| 7 | +from _nebari.cli import configure_logging |
5 | 8 | from _nebari.subcommands.init import InitInputs |
6 | 9 | from nebari.plugins import nebari_plugin_manager |
7 | 10 |
|
@@ -65,3 +68,118 @@ def test_nebari_init(tmp_path, namespace, auth_provider, ci_provider, ssl_cert_e |
65 | 68 | ) |
66 | 69 | def test_nebari_commands_no_args(command): |
67 | 70 | subprocess.run(command, check=True, capture_output=True, text=True).stdout.strip() |
| 71 | + |
| 72 | + |
| 73 | +@pytest.mark.parametrize( |
| 74 | + "log_level,expected_python_level,expected_tf_level", |
| 75 | + [ |
| 76 | + ("trace", logging.DEBUG, "TRACE"), |
| 77 | + ("debug", logging.DEBUG, "DEBUG"), |
| 78 | + ("info", logging.INFO, "INFO"), |
| 79 | + ("warning", logging.WARNING, "WARN"), |
| 80 | + ("error", logging.ERROR, "ERROR"), |
| 81 | + ("critical", logging.CRITICAL, "ERROR"), |
| 82 | + ], |
| 83 | +) |
| 84 | +def test_configure_logging_levels( |
| 85 | + log_level, expected_python_level, expected_tf_level, monkeypatch |
| 86 | +): |
| 87 | + """Test that configure_logging sets correct Python and Terraform log levels.""" |
| 88 | + # Remove TF_LOG from environment if it exists |
| 89 | + monkeypatch.delenv("TF_LOG", raising=False) |
| 90 | + |
| 91 | + # Configure logging with the specified level |
| 92 | + configure_logging(log_level) |
| 93 | + |
| 94 | + # Check that Python logging level is set correctly |
| 95 | + root_logger = logging.getLogger() |
| 96 | + assert root_logger.level == expected_python_level |
| 97 | + |
| 98 | + # Check that TF_LOG environment variable is set correctly |
| 99 | + assert os.environ.get("TF_LOG") == expected_tf_level |
| 100 | + |
| 101 | + |
| 102 | +def test_configure_logging_with_none(monkeypatch): |
| 103 | + """Test that configure_logging returns early when log_level is None.""" |
| 104 | + # Remove TF_LOG from environment if it exists |
| 105 | + monkeypatch.delenv("TF_LOG", raising=False) |
| 106 | + |
| 107 | + # Get initial logging level |
| 108 | + initial_level = logging.getLogger().level |
| 109 | + |
| 110 | + # Call with None |
| 111 | + configure_logging(None) |
| 112 | + |
| 113 | + # Verify logging level hasn't changed |
| 114 | + assert logging.getLogger().level == initial_level |
| 115 | + |
| 116 | + # Verify TF_LOG wasn't set |
| 117 | + assert "TF_LOG" not in os.environ |
| 118 | + |
| 119 | + |
| 120 | +def test_configure_logging_preserves_existing_tf_log(monkeypatch): |
| 121 | + """Test that configure_logging doesn't override existing TF_LOG variable.""" |
| 122 | + # Set TF_LOG to a specific value |
| 123 | + monkeypatch.setenv("TF_LOG", "TRACE") |
| 124 | + |
| 125 | + # Configure logging with a different level |
| 126 | + configure_logging("error") |
| 127 | + |
| 128 | + # Verify TF_LOG wasn't changed |
| 129 | + assert os.environ["TF_LOG"] == "TRACE" |
| 130 | + |
| 131 | + |
| 132 | +def test_configure_logging_debug_format(): |
| 133 | + """Test that DEBUG level uses detailed format with timestamp.""" |
| 134 | + configure_logging("debug") |
| 135 | + |
| 136 | + # Get the root logger |
| 137 | + root_logger = logging.getLogger() |
| 138 | + |
| 139 | + # Check that level is DEBUG |
| 140 | + assert root_logger.level == logging.DEBUG |
| 141 | + |
| 142 | + # Check that at least one handler exists (basicConfig creates one) |
| 143 | + assert len(root_logger.handlers) > 0 |
| 144 | + |
| 145 | + |
| 146 | +def test_configure_logging_non_debug_format(): |
| 147 | + """Test that non-DEBUG levels use simpler format without timestamp.""" |
| 148 | + configure_logging("info") |
| 149 | + |
| 150 | + # Get the root logger |
| 151 | + root_logger = logging.getLogger() |
| 152 | + |
| 153 | + # Check that level is INFO |
| 154 | + assert root_logger.level == logging.INFO |
| 155 | + |
| 156 | + # Check that at least one handler exists |
| 157 | + assert len(root_logger.handlers) > 0 |
| 158 | + |
| 159 | + |
| 160 | +@pytest.mark.parametrize( |
| 161 | + "log_level", |
| 162 | + ["trace", "debug", "info", "warning", "error"], |
| 163 | +) |
| 164 | +def test_cli_with_log_level(log_level, monkeypatch): |
| 165 | + """Test that the CLI accepts and processes the --log-level option.""" |
| 166 | + # Remove TF_LOG from environment if it exists |
| 167 | + monkeypatch.delenv("TF_LOG", raising=False) |
| 168 | + |
| 169 | + command = ["nebari", "--log-level", log_level, "info"] |
| 170 | + result = subprocess.run(command, check=True, capture_output=True, text=True) |
| 171 | + |
| 172 | + # Command should succeed |
| 173 | + assert result.returncode == 0 |
| 174 | + |
| 175 | + |
| 176 | +def test_cli_with_short_log_level_option(monkeypatch): |
| 177 | + """Test that the CLI accepts the -l short option for log level.""" |
| 178 | + # Remove TF_LOG from environment if it exists |
| 179 | + monkeypatch.delenv("TF_LOG", raising=False) |
| 180 | + |
| 181 | + command = ["nebari", "-l", "debug", "info"] |
| 182 | + result = subprocess.run(command, check=True, capture_output=True, text=True) |
| 183 | + |
| 184 | + # Command should succeed |
| 185 | + assert result.returncode == 0 |
0 commit comments