|
| 1 | +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"). You |
| 4 | +# may not use this file except in compliance with the License. A copy of |
| 5 | +# the License is located at |
| 6 | +# |
| 7 | +# http://aws.amazon.com/apache2.0/ |
| 8 | +# |
| 9 | +# or in the "license" file accompanying this file. This file is |
| 10 | +# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF |
| 11 | +# ANY KIND, either express or implied. See the License for the specific |
| 12 | +# language governing permissions and limitations under the License. |
| 13 | + |
| 14 | +import pytest |
| 15 | +import time |
| 16 | + |
| 17 | +from sagemaker.hyperpod.cli.utils import setup_logger |
| 18 | +from test.integration_tests.utils import execute_command |
| 19 | +from test.integration_tests.abstract_integration_tests import AbstractIntegrationTests |
| 20 | + |
| 21 | +logger = setup_logger(__name__) |
| 22 | + |
| 23 | + |
| 24 | +class TestHypCLICommands(AbstractIntegrationTests): |
| 25 | + """Integration tests for HyperPod CLI using hyp commands.""" |
| 26 | + |
| 27 | + def test_list_clusters(self, cluster_name): |
| 28 | + """Test listing clusters """ |
| 29 | + assert cluster_name |
| 30 | + |
| 31 | + def test_set_cluster_context(self, cluster_name): |
| 32 | + """Test setting cluster context.""" |
| 33 | + result = execute_command([ |
| 34 | + "hyp", "set-cluster-context", |
| 35 | + "--cluster-name", cluster_name |
| 36 | + ]) |
| 37 | + assert result.returncode == 0 |
| 38 | + context_line = result.stdout.strip().splitlines()[-1] |
| 39 | + assert any(text in context_line for text in ["Updated context", "Added new context"]) |
| 40 | + |
| 41 | + def test_get_cluster_context(self, cluster_name): |
| 42 | + """Test getting current cluster context.""" |
| 43 | + result = execute_command(["hyp", "get-cluster-context"]) |
| 44 | + assert result.returncode == 0 |
| 45 | + |
| 46 | + context_output = result.stdout.strip() |
| 47 | + assert "Cluster context:" in context_output |
| 48 | + # Just verify we got a valid ARN without checking the specific name |
| 49 | + current_arn = context_output.split("Cluster context:")[-1].strip() |
| 50 | + assert "arn:aws:eks:" in current_arn |
| 51 | + |
| 52 | + def test_create_job(self, test_job_name, image_uri): |
| 53 | + """Test creating a PyTorch job using the correct CLI parameters.""" |
| 54 | + result = execute_command([ |
| 55 | + "hyp", "create", "hp-pytorch-job", |
| 56 | + "--version", "1.0", |
| 57 | + "--job-name", test_job_name, |
| 58 | + "--image", image_uri, |
| 59 | + "--pull-policy", "Always", |
| 60 | + "--tasks-per-node", "1", |
| 61 | + "--max-retry", "1" |
| 62 | + ]) |
| 63 | + assert result.returncode == 0 |
| 64 | + logger.info(f"Created job: {test_job_name}") |
| 65 | + |
| 66 | + # Wait a moment for the job to be created |
| 67 | + time.sleep(5) |
| 68 | + |
| 69 | + def test_list_jobs(self, test_job_name): |
| 70 | + """Test listing jobs and verifying the created job is present.""" |
| 71 | + list_result = execute_command(["hyp", "list", "hp-pytorch-job"]) |
| 72 | + assert list_result.returncode == 0 |
| 73 | + |
| 74 | + # Check if either the job name is in the output or at least the header is present |
| 75 | + assert test_job_name in list_result.stdout |
| 76 | + logger.info("Successfully listed jobs") |
| 77 | + |
| 78 | + def test_list_pods(self, test_job_name): |
| 79 | + """Test listing pods for a specific job.""" |
| 80 | + # Wait a moment to ensure pods are created |
| 81 | + time.sleep(10) |
| 82 | + |
| 83 | + list_pods_result = execute_command([ |
| 84 | + "hyp", "list-pods", "hp-pytorch-job", |
| 85 | + "--job-name", test_job_name |
| 86 | + ]) |
| 87 | + assert list_pods_result.returncode == 0 |
| 88 | + |
| 89 | + # Verify the output contains expected headers and job name |
| 90 | + output = list_pods_result.stdout.strip() |
| 91 | + assert f"Pods for job: {test_job_name}" in output |
| 92 | + assert "POD NAME" in output |
| 93 | + assert "NAMESPACE" in output |
| 94 | + |
| 95 | + # Verify at least one pod is listed (should contain the job name in the pod name) |
| 96 | + assert f"{test_job_name}-pod-" in output |
| 97 | + |
| 98 | + logger.info(f"Successfully listed pods for job: {test_job_name}") |
| 99 | + |
| 100 | + # @pytest.mark.skip(reason="Skipping since there is ") |
| 101 | + def test_get_logs(self, test_job_name): |
| 102 | + """Test getting logs for a specific pod in a job.""" |
| 103 | + # First, get the pod name from list-pods command |
| 104 | + list_pods_result = execute_command([ |
| 105 | + "hyp", "list-pods", "hp-pytorch-job", |
| 106 | + "--job-name", test_job_name |
| 107 | + ]) |
| 108 | + assert list_pods_result.returncode == 0 |
| 109 | + |
| 110 | + # Extract the pod name from the output |
| 111 | + output_lines = list_pods_result.stdout.strip().split('\n') |
| 112 | + pod_name = None |
| 113 | + for line in output_lines: |
| 114 | + if f"{test_job_name}-pod-" in line: |
| 115 | + # Extract the pod name from the line |
| 116 | + pod_name = line.split()[0].strip() |
| 117 | + break |
| 118 | + |
| 119 | + assert pod_name is not None, f"Could not find pod for job {test_job_name}" |
| 120 | + logger.info(f"Found pod: {pod_name}") |
| 121 | + |
| 122 | + # Now get logs for this pod |
| 123 | + get_logs_result = execute_command([ |
| 124 | + "hyp", "get-logs", "hp-pytorch-job", |
| 125 | + "--job-name", test_job_name, |
| 126 | + "--pod-name", pod_name |
| 127 | + ]) |
| 128 | + assert get_logs_result.returncode == 0 |
| 129 | + |
| 130 | + # Verify the output contains the expected header |
| 131 | + logs_output = get_logs_result.stdout.strip() |
| 132 | + assert f"Listing logs for pod: {pod_name}" in logs_output |
| 133 | + |
| 134 | + logger.info(f"Successfully retrieved logs for pod: {pod_name}") |
| 135 | + |
| 136 | + def test_describe_job(self, test_job_name): |
| 137 | + """Test describing a specific job and verifying the output.""" |
| 138 | + describe_result = execute_command(["hyp", "describe", "hp-pytorch-job", "--job-name", test_job_name]) |
| 139 | + assert describe_result.returncode == 0 |
| 140 | + |
| 141 | + # Check if either the job name is in the output or metadata is present |
| 142 | + assert test_job_name in describe_result.stdout |
| 143 | + logger.info(f"Successfully described job: {test_job_name}") |
| 144 | + |
| 145 | + @pytest.mark.run(order=99) |
| 146 | + def test_delete_job(self, test_job_name): |
| 147 | + """Test deleting a job and verifying deletion.""" |
| 148 | + delete_result = execute_command(["hyp", "delete", "hp-pytorch-job", "--job-name", test_job_name]) |
| 149 | + assert delete_result.returncode == 0 |
| 150 | + logger.info(f"Successfully deleted job: {test_job_name}") |
| 151 | + |
| 152 | + # Wait a moment for the job to be deleted |
| 153 | + time.sleep(5) |
| 154 | + |
| 155 | + # Verify the job is no longer listed |
| 156 | + list_result = execute_command(["hyp", "list", "hp-pytorch-job"]) |
| 157 | + assert list_result.returncode == 0 |
| 158 | + |
| 159 | + # The job name should no longer be in the output |
| 160 | + assert test_job_name not in list_result.stdout |
| 161 | + |
| 162 | + |
0 commit comments