SameerMesiah97 commented on code in PR #62081:
URL: https://github.com/apache/airflow/pull/62081#discussion_r2943611533


##########
airflow-core/tests/unit/cli/commands/test_standalone_command.py:
##########
@@ -48,3 +50,255 @@ def test_calculate_env(self, conf_executor_name):
             env = StandaloneCommand().calculate_env()
             # all non local executors will fall back to localesecutor
             assert env["AIRFLOW__CORE__EXECUTOR"] == LOCAL_EXECUTOR
+
+    
@mock.patch("airflow.cli.commands.standalone_command.ExecutorLoader.import_default_executor_cls")
+    @mock.patch("airflow.cli.commands.standalone_command.conf.get")
+    def test_calculate_env_force_executor_and_auth(self, mock_conf_get, 
mock_import):
+        class FakeExecutor:
+            is_local = False
+
+        mock_import.return_value = (FakeExecutor, None)
+        mock_conf_get.return_value = "wrong.auth.manager"
+        cmd = StandaloneCommand()
+        env = cmd.calculate_env()
+
+        assert env["AIRFLOW__CORE__EXECUTOR"] == "LocalExecutor"
+        assert "AIRFLOW__CORE__AUTH_MANAGER" in env
+
+    @mock.patch("airflow.cli.commands.standalone_command.os.path.exists", 
return_value=False)
+    @mock.patch("airflow.cli.commands.standalone_command.create_auth_manager")
+    @mock.patch("airflow.cli.commands.standalone_command.conf.get")
+    def test_find_user_info_generates_password(self, mock_conf, mock_auth, 
mock_exists):
+        def conf_side(section, key):
+            if key == "simple_auth_manager_all_admins":
+                return "false"
+            if key == "simple_auth_manager_users":
+                return "admin:admin"
+            return ""
+
+        mock_conf.side_effect = conf_side
+        am = mock.Mock()
+        am.get_generated_password_file.return_value = "/tmp/fake"
+        mock_auth.return_value = am
+        cmd = StandaloneCommand()
+        cmd.find_user_info()
+
+        am.init.assert_called_once()
+
+    @mock.patch("airflow.cli.commands.standalone_command.create_auth_manager")
+    @mock.patch("airflow.cli.commands.standalone_command.conf.get")
+    def test_find_user_info_skips_when_all_admins_true(self, mock_conf_get, 
mock_create_auth):
+        def conf_side(section, key):
+            if key == "simple_auth_manager_all_admins":
+                return "true"
+            return ""
+
+        mock_conf_get.side_effect = conf_side
+        cmd = StandaloneCommand()
+        cmd.find_user_info()
+
+        mock_create_auth.assert_not_called()
+
+    @mock.patch("airflow.cli.commands.standalone_command.create_auth_manager")
+    @mock.patch("airflow.cli.commands.standalone_command.conf.get")
+    def test_find_user_info_skips_when_users_already_configured(self, 
mock_conf_get, mock_create_auth):
+        def conf_side(section, key):
+            if key == "simple_auth_manager_all_admins":
+                return "false"
+            if key == "simple_auth_manager_users":
+                return "custom:password"
+            return ""
+
+        mock_conf_get.side_effect = conf_side
+        cmd = StandaloneCommand()
+        cmd.find_user_info()
+
+        mock_create_auth.assert_not_called()
+
+    @mock.patch("airflow.cli.commands.standalone_command.os.path.exists", 
return_value=True)
+    @mock.patch("airflow.cli.commands.standalone_command.create_auth_manager")
+    @mock.patch("airflow.cli.commands.standalone_command.conf.get")
+    def test_find_user_info_skips_when_password_file_exists(
+        self, mock_conf_get, mock_create_auth, mock_exists
+    ):
+        def conf_side(section, key):
+            if key == "simple_auth_manager_all_admins":
+                return "false"
+            if key == "simple_auth_manager_users":
+                return "admin:admin"
+            return ""
+
+        mock_conf_get.side_effect = conf_side
+        am = mock.Mock()
+        am.get_generated_password_file.return_value = "/tmp/fake"
+        mock_create_auth.return_value = am
+        cmd = StandaloneCommand()
+        cmd.find_user_info()
+
+        am.init.assert_not_called()
+
+    @mock.patch("airflow.cli.commands.standalone_command.db.initdb")
+    def test_initialize_database(self, mock_initdb, monkeypatch):
+        cmd = StandaloneCommand()
+        monkeypatch.setattr(cmd, "print_output", lambda *a: None)
+        cmd.initialize_database()
+
+        mock_initdb.assert_called_once()
+
+    @mock.patch("builtins.print")
+    def test_print_output(self, mock_print):
+        cmd = StandaloneCommand()
+        cmd.print_output("scheduler", "hello\nworld")
+
+        assert mock_print.call_count == 2
+
+    @mock.patch(
+        "airflow.cli.commands.standalone_command.most_recent_job",
+        return_value=None,
+    )
+    def test_job_running_returns_false_if_no_recent_job(self, 
mock_most_recent_job):
+        result = 
StandaloneCommand().job_running(mock.Mock(job_type="scheduler"))
+        assert result is False
+
+    @mock.patch("airflow.cli.commands.standalone_command.most_recent_job")
+    def test_job_running_returns_false_if_job_not_alive(self, 
mock_most_recent_job):
+        fake_job = mock.Mock()
+        fake_job.is_alive.return_value = False
+        mock_most_recent_job.return_value = fake_job
+
+        result = 
StandaloneCommand().job_running(mock.Mock(job_type="scheduler"))
+        assert result is False
+
+    @mock.patch("airflow.cli.commands.standalone_command.most_recent_job")
+    def test_job_running_returns_true_if_alive(self, mock_most_recent_job):
+        fake_job = mock.Mock()
+        fake_job.is_alive.return_value = True
+        mock_most_recent_job.return_value = fake_job
+
+        result = 
StandaloneCommand().job_running(mock.Mock(job_type="scheduler"))
+        assert result is True
+
+    def test_is_ready_true_when_all_components_running(self, monkeypatch):
+        cmd = StandaloneCommand()
+        monkeypatch.setattr(cmd, "job_running", lambda *_: True)
+
+        assert cmd.is_ready() is True
+
+    def test_is_ready_false_when_any_component_missing(self, monkeypatch):
+        cmd = StandaloneCommand()
+        calls = iter([True, False, True])
+        monkeypatch.setattr(cmd, "job_running", lambda *_: next(calls))
+
+        assert cmd.is_ready() is False
+
+    def test_port_open_returns_false_on_socket_error(self, monkeypatch):
+        cmd = StandaloneCommand()
+
+        class FakeSocket:
+            def settimeout(self, *_):
+                pass
+
+            def connect(self, *_):
+                raise OSError()
+
+            def close(self):
+                pass
+
+        monkeypatch.setattr(
+            "airflow.cli.commands.standalone_command.socket.socket",
+            lambda *a, **k: FakeSocket(),
+        )
+
+        assert cmd.port_open(1234) is False

Review Comment:
   Maybe you can add another case for `ValueError`?



##########
airflow-core/tests/unit/cli/commands/test_standalone_command.py:
##########
@@ -48,3 +50,255 @@ def test_calculate_env(self, conf_executor_name):
             env = StandaloneCommand().calculate_env()
             # all non local executors will fall back to localesecutor
             assert env["AIRFLOW__CORE__EXECUTOR"] == LOCAL_EXECUTOR
+
+    
@mock.patch("airflow.cli.commands.standalone_command.ExecutorLoader.import_default_executor_cls")
+    @mock.patch("airflow.cli.commands.standalone_command.conf.get")
+    def test_calculate_env_force_executor_and_auth(self, mock_conf_get, 
mock_import):

Review Comment:
   Maybe you can add another test for when `conf.get("core", "auth_manager") != 
simple_auth_manager_classpath` does not evaluate to `True` i.e. 
`SimpleAuthManager` is correctly set in the configs?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to