|
| 1 | +# Copyright 2023–2026 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Unit tests for GCS utility functions.""" |
| 16 | + |
| 17 | +import unittest |
| 18 | +from unittest import mock |
| 19 | +import os |
| 20 | +import tempfile |
| 21 | +import pytest |
| 22 | + |
| 23 | +# Module to be tested |
| 24 | +from maxtext.utils import gcs_utils |
| 25 | + |
| 26 | + |
| 27 | +@pytest.mark.cpu_only |
| 28 | +class GcsUtilsTest(unittest.TestCase): |
| 29 | + """Unit tests for GCS utility functions.""" |
| 30 | + |
| 31 | + def test_add_trailing_slash(self): |
| 32 | + """Tests the simple add_trailing_slash utility.""" |
| 33 | + self.assertEqual(gcs_utils.add_trailing_slash("a/b"), "a/b/") |
| 34 | + self.assertEqual(gcs_utils.add_trailing_slash("a/b/"), "a/b/") |
| 35 | + |
| 36 | + def test_mkdir_non_existing_dir(self): |
| 37 | + """Tests that a non-existing directory is created and is empty.""" |
| 38 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 39 | + new_dir_path = os.path.join(temp_dir, "new_dir") |
| 40 | + self.assertFalse(os.path.exists(new_dir_path)) |
| 41 | + |
| 42 | + # Act |
| 43 | + gcs_utils.mkdir_and_check_permissions(new_dir_path) |
| 44 | + |
| 45 | + # Assert |
| 46 | + self.assertTrue(os.path.isdir(new_dir_path)) |
| 47 | + self.assertEqual(os.listdir(new_dir_path), []) |
| 48 | + |
| 49 | + def test_mkdir_existing_non_empty_dir(self): |
| 50 | + """Tests that an existing, non-empty directory's contents are unmodified.""" |
| 51 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 52 | + existing_dir_path = os.path.join(temp_dir, "existing_dir") |
| 53 | + os.makedirs(existing_dir_path) |
| 54 | + dummy_file_path = os.path.join(existing_dir_path, "dummy.txt") |
| 55 | + with open(dummy_file_path, "w", encoding="utf-8") as f: |
| 56 | + f.write("test") |
| 57 | + |
| 58 | + # Act |
| 59 | + gcs_utils.mkdir_and_check_permissions(existing_dir_path) |
| 60 | + |
| 61 | + # Assert |
| 62 | + self.assertTrue(os.path.isdir(existing_dir_path)) |
| 63 | + self.assertEqual(os.listdir(existing_dir_path), ["dummy.txt"]) |
| 64 | + |
| 65 | + def test_mkdir_existing_read_only_dir(self): |
| 66 | + """Tests that a PermissionError is raised for a read-only directory.""" |
| 67 | + # Ideally, we would create a temporary directory here and mark it read-only. Unfortunately it does not work when the |
| 68 | + # tests run inside a GitHub action. I think that those tests are executed as sudo and they ignore the permissions. |
| 69 | + # Instead we use "/sys", which is a universally read-only directory, even for superusers. |
| 70 | + read_only_dir_path = "/sys" |
| 71 | + with self.assertRaises((PermissionError, OSError)): |
| 72 | + gcs_utils.mkdir_and_check_permissions(read_only_dir_path) |
| 73 | + |
| 74 | + def test_mkdir_read_only_parent_dir(self): |
| 75 | + """Tests that a PermissionError is raised when the parent is read-only.""" |
| 76 | + # Ideally, we would create a temporary directory here and mark it read-only. Unfortunately it does not work when the |
| 77 | + # tests run inside a GitHub action. I think that those tests are executed as sudo and they ignore the permissions. |
| 78 | + # Instead we use "/sys", which is a universally read-only directory, even for superusers. |
| 79 | + new_dir_path = "/sys/new_dir" |
| 80 | + with self.assertRaises((PermissionError, OSError)): |
| 81 | + gcs_utils.mkdir_and_check_permissions(new_dir_path) |
| 82 | + |
| 83 | + @mock.patch("maxtext.utils.gcs_utils.storage.Client") |
| 84 | + def test_mkdir_gcs_no_such_bucket(self, mock_storage_client): |
| 85 | + """Tests that an exception is raised for a non-existent GCS bucket.""" |
| 86 | + mock_client_instance = mock_storage_client.return_value |
| 87 | + mock_client_instance.get_bucket.side_effect = Exception("Bucket not found!") |
| 88 | + gcs_path = "gs://no_such_bucket" |
| 89 | + |
| 90 | + with self.assertRaises(FileNotFoundError): |
| 91 | + gcs_utils.mkdir_and_check_permissions(gcs_path) |
| 92 | + mock_client_instance.get_bucket.assert_called_with("no_such_bucket") |
| 93 | + |
| 94 | + @mock.patch("maxtext.utils.gcs_utils.storage.Client") |
| 95 | + def test_mkdir_gcs_no_such_bucket_with_path(self, mock_storage_client): |
| 96 | + """Tests an exception for a non-existent bucket with a subdirectory.""" |
| 97 | + mock_client_instance = mock_storage_client.return_value |
| 98 | + mock_client_instance.get_bucket.side_effect = Exception("Bucket not found!") |
| 99 | + gcs_path = "gs://no_such_bucket/some/dir" |
| 100 | + |
| 101 | + with self.assertRaises(FileNotFoundError): |
| 102 | + gcs_utils.mkdir_and_check_permissions(gcs_path) |
| 103 | + mock_client_instance.get_bucket.assert_called_with("no_such_bucket") |
| 104 | + |
| 105 | + @mock.patch("maxtext.utils.gcs_utils.epath.Path") |
| 106 | + @mock.patch("maxtext.utils.gcs_utils.storage.Client") |
| 107 | + def test_mkdir_gcs_valid_bucket(self, mock_storage_client, mock_epath): |
| 108 | + """Tests that a valid GCS path is handled correctly without errors.""" |
| 109 | + # Arrange: Mock the GCS client to simulate a valid bucket |
| 110 | + mock_client_instance = mock_storage_client.return_value |
| 111 | + |
| 112 | + # Arrange: Mock epath to prevent real GCS calls |
| 113 | + mock_path_instance = mock.MagicMock() |
| 114 | + mock_path_instance.as_posix.return_value = "gs://valid_bucket/some/dir" |
| 115 | + mock_path_instance.parts = ["gs:", "", "valid_bucket", "some", "dir"] |
| 116 | + mock_path_instance.exists.return_value = True |
| 117 | + |
| 118 | + mock_temp_file_instance = mock.MagicMock() |
| 119 | + mock_path_instance.__truediv__.return_value = mock_temp_file_instance |
| 120 | + |
| 121 | + mock_epath.return_value = mock_path_instance |
| 122 | + gcs_path = "gs://valid_bucket/some/dir" |
| 123 | + |
| 124 | + # Act |
| 125 | + gcs_utils.mkdir_and_check_permissions(gcs_path) |
| 126 | + |
| 127 | + # Assert |
| 128 | + mock_client_instance.get_bucket.assert_called_with("valid_bucket") |
| 129 | + mock_path_instance.mkdir.assert_called_with(exist_ok=True, parents=True) |
| 130 | + mock_path_instance.exists.assert_called_once() |
| 131 | + mock_temp_file_instance.write_text.assert_called_once_with("test") |
| 132 | + mock_temp_file_instance.unlink.assert_called_once() |
0 commit comments