code: def test_usage_stats_prompt(
      monkeypatch,
      capsys,
      tmp_path,
      reset_usage_stats,
      shutdown_only,
      clear_loggers,
      reset_ray_version_commit,
  ):
      """
      Test usage stats prompt is shown in the proper cases.
      """
      with monkeypatch.context() as m:
          m.setenv("RAY_USAGE_STATS_ENABLED", "1")
          m.setenv("RAY_USAGE_STATS_PROMPT_ENABLED", "0")
          ray_usage_lib.show_usage_stats_prompt(cli=True)
          captured = capsys.readouterr()
          assert (
              usage_constants.USAGE_STATS_ENABLED_FOR_CLI_MESSAGE
              not in captured.out
          )
          assert (
              usage_constants.USAGE_STATS_ENABLED_FOR_CLI_MESSAGE
              not in captured.err
          )
  
      with monkeypatch.context() as m:
          m.setenv("RAY_USAGE_STATS_ENABLED", "1")
          m.setenv("RAY_USAGE_STATS_PROMPT_ENABLED", "0")
          ray_usage_lib.show_usage_stats_prompt(cli=False)
          captured = capsys.readouterr()
          assert (
              usage_constants.USAGE_STATS_ENABLED_FOR_RAY_INIT_MESSAGE
              not in captured.out
          )
          assert (
              usage_constants.USAGE_STATS_ENABLED_FOR_RAY_INIT_MESSAGE
              not in captured.err
          )
  
      with monkeypatch.context() as m:
          m.setenv("RAY_USAGE_STATS_ENABLED", "0")
          ray_usage_lib.show_usage_stats_prompt(cli=True)
          captured = capsys.readouterr()
          assert (
              usage_constants.USAGE_STATS_DISABLED_MESSAGE
              in captured.out
          )
  
      with monkeypatch.context() as m:
          m.delenv("RAY_USAGE_STATS_ENABLED", raising=False)
          tmp_usage_stats_config_path = tmp_path / "config1.json"
          m.setenv(
              "RAY_USAGE_STATS_CONFIG_PATH",
              str(tmp_usage_stats_config_path),
          )
          # Usage stats collection is enabled by default.
          ray_usage_lib.show_usage_stats_prompt(cli=True)
          captured = capsys.readouterr()
          assert (
              usage_constants.USAGE_STATS_ENABLED_BY_DEFAULT_FOR_CLI_MESSAGE
              in captured.out
          )
  
      with monkeypatch.context() as m:
          # Win impl relies on kbhit() instead of select()
          # so the pipe trick won't work.
          if sys.platform != "win32":
              m.delenv("RAY_USAGE_STATS_ENABLED", raising=False)
              saved_interactive = cli_logger.interactive
              saved_stdin = sys.stdin
              tmp_usage_stats_config_path = tmp_path / "config2.json"
              m.setenv(
                  "RAY_USAGE_STATS_CONFIG_PATH",
                  str(tmp_usage_stats_config_path),
              )
              cli_logger.interactive = True
              (r_pipe, w_pipe) = os.pipe()
              sys.stdin = open(r_pipe)
              os.write(w_pipe, b"y\n")
              ray_usage_lib.show_usage_stats_prompt(cli=True)
              captured = capsys.readouterr()
              assert (
                  usage_constants.USAGE_STATS_CONFIRMATION_MESSAGE
                  in captured.out
              )
              assert (
                  usage_constants.USAGE_STATS_ENABLED_FOR_CLI_MESSAGE
                  in captured.out
              )
              cli_logger.interactive = saved_interactive
              sys.stdin = saved_stdin
  
      with monkeypatch.context() as m:
          if sys.platform != "win32":
              m.delenv("RAY_USAGE_STATS_ENABLED", raising=False)
              saved_interactive = cli_logger.interactive
              saved_stdin = sys.stdin
              tmp_usage_stats_config_path = tmp_path / "config3.json"
              m.setenv(
                  "RAY_USAGE_STATS_CONFIG_PATH",
                  str(tmp_usage_stats_config_path),
              )
              cli_logger.interactive = True
              (r_pipe, w_pipe) = os.pipe()
              sys.stdin = open(r_pipe)
              os.write(w_pipe, b"n\n")
              ray_usage_lib.show_usage_stats_prompt(cli=True)
              captured = capsys.readouterr()
              assert (
                  usage_constants.USAGE_STATS_CONFIRMATION_MESSAGE
                  in captured.out
              )
              assert (
                  usage_constants.USAGE_STATS_DISABLED_MESSAGE
                  in captured.out
              )
              cli_logger.interactive = saved_interactive
              sys.stdin = saved_stdin
  
      with monkeypatch.context() as m:
          m.delenv("RAY_USAGE_STATS_ENABLED", raising=False)
          saved_interactive = cli_logger.interactive
          saved_stdin = sys.stdin
          tmp_usage_stats_config_path = tmp_path / "config4.json"
          m.setenv(
              "RAY_USAGE_STATS_CONFIG_PATH",
              str(tmp_usage_stats_config_path),
          )
          cli_logger.interactive = True
          (r_pipe, w_pipe) = os.pipe()
          sys.stdin = open(r_pipe)
          ray_usage_lib.show_usage_stats_prompt(cli=True)
          captured = capsys.readouterr()
          assert (
              usage_constants.USAGE_STATS_CONFIRMATION_MESSAGE
              in captured.out
          )
          assert (
              usage_constants.USAGE_STATS_ENABLED_FOR_CLI_MESSAGE
              in captured.out
          )
          cli_logger.interactive = saved_interactive
          sys.stdin = saved_stdin
  
      with monkeypatch.context() as m:
          # Usage stats is not enabled for ray.init() unless it's nightly wheel.
          m.delenv("RAY_USAGE_STATS_ENABLED", raising=False)
          tmp_usage_stats_config_path = tmp_path / "config5.json"
          m.setenv(
              "RAY_USAGE_STATS_CONFIG_PATH",
              str(tmp_usage_stats_config_path),
          )
          ray.__version__ = "2.0.0"
          ray.__commit__ = "xyzf"
          ray.init()
          ray.shutdown()
          captured = capsys.readouterr()
          assert (
              usage_constants.USAGE_STATS_ENABLED_BY_DEFAULT_FOR_RAY_INIT_MESSAGE
              not in captured.out
          )
          assert (
              usage_constants.USAGE_STATS_ENABLED_FOR_RAY_INIT_MESSAGE
              not in captured.out
          )
  
      with monkeypatch.context() as m:
          # Usage stats is enabled for ray.init() for nightly wheel.
          m.delenv("RAY_USAGE_STATS_ENABLED", raising=False)
          tmp_usage_stats_config_path = tmp_path / "config6.json"
          m.setenv(
              "RAY_USAGE_STATS_CONFIG_PATH",
              str(tmp_usage_stats_config_path),
          )
          ray.__version__ = "2.0.0.dev0"
          ray.__commit__ = "xyzf"
          ray.init()
          ray.shutdown()
          captured = capsys.readouterr()
          assert (
              usage_constants.USAGE_STATS_ENABLED_BY_DEFAULT_FOR_RAY_INIT_MESSAGE
              in captured.out
          )
  
      with monkeypatch.context() as m:
          m.setenv("RAY_USAGE_STATS_ENABLED", "0")
          ray.__version__ = "2.0.0.dev0"
          ray.__commit__ = "xyzf"
          ray.init()
          ray.shutdown()
          captured = capsys.readouterr()
          assert (
              usage_constants.USAGE_STATS_DISABLED_MESSAGE
              in captured.out
          )
  
      with monkeypatch.context() as m:
          m.setenv("RAY_USAGE_STATS_ENABLED", "1")
          ray.__version__ = "2.0.0.dev0"
          ray.__commit__ = "xyzf"
          ray.init()
          ray.shutdown()
          captured = capsys.readouterr()
          assert (
              usage_constants.USAGE_STATS_ENABLED_FOR_RAY_INIT_MESSAGE
              in captured.out
          )
  
imports: import json
  import os
  import pathlib
  import sys
  import time
  from dataclasses import asdict
  from pathlib import Path
  from unittest.mock import patch
  import requests
  import pytest
  from jsonschema import validate
  from http.server import BaseHTTPRequestHandler, HTTPServer
  import ray
  import ray._private.usage.usage_constants
  import ray._private.usage.usage_lib
  from ray._private.test_utils import format_web_url, run_string_as_driver, wait_for_condition, wait_until_server_available
  from ray._private.usage.usage_lib import ClusterConfigToReport, UsageStatsEnabledness
  from ray.autoscaler._private.cli_logger import cli_logger
  from ray.util.placement_group import placement_group
  from ray._private.accelerators import NvidiaGPUAcceleratorManager
label_desc: Use image node_id 15 for calling a global function with example usage: test_usage_stats_prompt(monkeypatch, capsys, tmp_path, reset_usage_stats, shutdown_only, clear_loggers, reset_ray_version_commit) without return types