| 
 | 1 | +import os  | 
 | 2 | +import shutil  | 
 | 3 | +import subprocess  | 
 | 4 | +import sys  | 
 | 5 | + | 
 | 6 | +import pytest  | 
 | 7 | +from clarifai.runners.models.model_run_locally import ModelRunLocally  | 
 | 8 | + | 
 | 9 | +CLARIFAI_USER_ID = os.environ["CLARIFAI_USER_ID"]  | 
 | 10 | +CLARIFAI_PAT = os.environ["CLARIFAI_PAT"]  | 
 | 11 | + | 
 | 12 | + | 
 | 13 | +@pytest.fixture  | 
 | 14 | +def model_run_locally(dummy_models_path):  | 
 | 15 | +  """  | 
 | 16 | +  Fixture that instantiates the ModelRunLocally class  | 
 | 17 | +  with the dummy model_path that already exists.  | 
 | 18 | +  """  | 
 | 19 | +  return ModelRunLocally(dummy_models_path)  | 
 | 20 | + | 
 | 21 | + | 
 | 22 | +@pytest.fixture  | 
 | 23 | +def hf_model_run_locally(dummy_hf_models_path):  | 
 | 24 | +  """  | 
 | 25 | +  Fixture that instantiates the ModelRunLocally class  | 
 | 26 | +  with the dummy model_path that already exists.  | 
 | 27 | +  """  | 
 | 28 | +  return ModelRunLocally(dummy_hf_models_path)  | 
 | 29 | + | 
 | 30 | + | 
 | 31 | +@pytest.mark.skipif(shutil.which("docker") is None, reason="Docker not installed or not in PATH.")  | 
 | 32 | +@pytest.mark.skipif(  | 
 | 33 | +    sys.platform not in ["linux", "darwin"],  | 
 | 34 | +    reason="Test only runs on Linux and macOS because base image only supports those platforms.")  | 
 | 35 | +def test_docker_build_and_test_container(model_run_locally):  | 
 | 36 | +  """  | 
 | 37 | +  Test building a Docker image and running a container test using the dummy model.  | 
 | 38 | +  This test will be skipped if Docker is not installed.  | 
 | 39 | +  """  | 
 | 40 | + | 
 | 41 | +  # Test if Docker is installed  | 
 | 42 | +  assert model_run_locally.is_docker_installed(), "Docker not installed, skipping."  | 
 | 43 | + | 
 | 44 | +  # Build or re-build the Docker image  | 
 | 45 | +  model_run_locally.builder.create_dockerfile()  | 
 | 46 | +  image_tag = model_run_locally._docker_hash()  | 
 | 47 | +  image_name = f"{model_run_locally.config['model']['id']}:{image_tag}"  | 
 | 48 | + | 
 | 49 | +  if not model_run_locally.docker_image_exists(image_name):  | 
 | 50 | +    model_run_locally.build_docker_image(image_name=image_name)  | 
 | 51 | + | 
 | 52 | +  # Run tests inside the container  | 
 | 53 | +  try:  | 
 | 54 | +    model_run_locally.test_model_container(  | 
 | 55 | +        image_name=image_name,  | 
 | 56 | +        container_name="test_clarifai_model_container",  | 
 | 57 | +        env_vars={  | 
 | 58 | +            'CLARIFAI_PAT': CLARIFAI_PAT,  | 
 | 59 | +            'CLARIFAI_API_BASE': os.environ.get('CLARIFAI_API_BASE', 'https://api.clarifai.com')  | 
 | 60 | +        })  | 
 | 61 | +  except subprocess.CalledProcessError:  | 
 | 62 | +    pytest.fail("Failed to test the model inside the docker container.")  | 
 | 63 | +  finally:  | 
 | 64 | +    # Clean up the container if it still exists  | 
 | 65 | +    if model_run_locally.container_exists("test_clarifai_model_container"):  | 
 | 66 | +      model_run_locally.stop_docker_container("test_clarifai_model_container")  | 
 | 67 | +      model_run_locally.remove_docker_container("test_clarifai_model_container")  | 
 | 68 | + | 
 | 69 | +    # Remove the image  | 
 | 70 | +    model_run_locally.remove_docker_image(image_name)  | 
 | 71 | + | 
 | 72 | + | 
 | 73 | +# Skip the test if Docker is not installed or if the platform is not Linux/macOS  | 
 | 74 | +@pytest.mark.skipif(shutil.which("docker") is None, reason="Docker not installed or not in PATH.")  | 
 | 75 | +@pytest.mark.skipif(  | 
 | 76 | +    sys.platform not in ["linux", "darwin"],  | 
 | 77 | +    reason="Test only runs on Linux and macOS because base image only supports those platforms.")  | 
 | 78 | +def test_hf_docker_build_and_test_container(hf_model_run_locally):  | 
 | 79 | +  """  | 
 | 80 | +  Test building a Docker image and running a container test using the dummy model.  | 
 | 81 | +  This test will be skipped if Docker is not installed.  | 
 | 82 | +  """  | 
 | 83 | + | 
 | 84 | +  # Download the checkpoints for the model  | 
 | 85 | +  hf_model_run_locally.builder.download_checkpoints()  | 
 | 86 | + | 
 | 87 | +  # Test if Docker is installed  | 
 | 88 | +  assert hf_model_run_locally.is_docker_installed(), "Docker not installed, skipping."  | 
 | 89 | + | 
 | 90 | +  # Build or re-build the Docker image  | 
 | 91 | +  hf_model_run_locally.builder.create_dockerfile()  | 
 | 92 | +  image_tag = hf_model_run_locally._docker_hash()  | 
 | 93 | +  image_name = f"{hf_model_run_locally.config['model']['id']}:{image_tag}"  | 
 | 94 | + | 
 | 95 | +  if not hf_model_run_locally.docker_image_exists(image_name):  | 
 | 96 | +    hf_model_run_locally.build_docker_image(image_name=image_name)  | 
 | 97 | + | 
 | 98 | +  # Run tests inside the container  | 
 | 99 | +  try:  | 
 | 100 | +    hf_model_run_locally.test_model_container(  | 
 | 101 | +        image_name=image_name,  | 
 | 102 | +        container_name="test_clarifai_model_container",  | 
 | 103 | +        env_vars={  | 
 | 104 | +            'CLARIFAI_PAT': CLARIFAI_PAT,  | 
 | 105 | +            'CLARIFAI_API_BASE': os.environ.get('CLARIFAI_API_BASE', 'https://api.clarifai.com')  | 
 | 106 | +        })  | 
 | 107 | +  except subprocess.CalledProcessError:  | 
 | 108 | +    pytest.fail("Failed to test the model inside the docker container.")  | 
 | 109 | +  finally:  | 
 | 110 | +    # Clean up the container if it still exists  | 
 | 111 | +    if hf_model_run_locally.container_exists("test_clarifai_model_container"):  | 
 | 112 | +      hf_model_run_locally.stop_docker_container("test_clarifai_model_container")  | 
 | 113 | +      hf_model_run_locally.remove_docker_container("test_clarifai_model_container")  | 
 | 114 | + | 
 | 115 | +    # Remove the image  | 
 | 116 | +    hf_model_run_locally.remove_docker_image(image_name)  | 
0 commit comments