|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# This source code is licensed under the BSD-style license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +# pyre-strict |
| 8 | + |
| 9 | +import json |
| 10 | +import unittest |
| 11 | +from configparser import ConfigParser |
| 12 | +from unittest.mock import patch |
| 13 | + |
| 14 | +from torchx.specs.named_resources_config import ( |
| 15 | + _create_resource_factory, |
| 16 | + _load_config_file, |
| 17 | + _load_named_resources_from_config, |
| 18 | + _parse_resource_config, |
| 19 | + NAMED_RESOURCES, |
| 20 | +) |
| 21 | + |
| 22 | + |
| 23 | +class ConfigNamedResourcesTest(unittest.TestCase): |
| 24 | + def test_parse_resource_config_basic(self) -> None: |
| 25 | + """Test parsing basic resource configuration.""" |
| 26 | + config_str = '{"cpu": 32, "gpu": 4, "memMB": 131072}' |
| 27 | + resource = _parse_resource_config(config_str) |
| 28 | + |
| 29 | + self.assertEqual(resource.cpu, 32) |
| 30 | + self.assertEqual(resource.gpu, 4) |
| 31 | + self.assertEqual(resource.memMB, 131072) |
| 32 | + self.assertEqual(resource.capabilities, {}) |
| 33 | + self.assertEqual(resource.devices, {}) |
| 34 | + |
| 35 | + def test_parse_resource_config_with_devices(self) -> None: |
| 36 | + """Test parsing resource configuration with devices.""" |
| 37 | + config_str = '{"cpu": 100, "gpu": 8, "memMB": 819200, "devices": {"vpc.amazonaws.com/efa": 1}}' |
| 38 | + resource = _parse_resource_config(config_str) |
| 39 | + |
| 40 | + self.assertEqual(resource.cpu, 100) |
| 41 | + self.assertEqual(resource.gpu, 8) |
| 42 | + self.assertEqual(resource.memMB, 819200) |
| 43 | + self.assertEqual(resource.devices, {"vpc.amazonaws.com/efa": 1}) |
| 44 | + |
| 45 | + def test_parse_resource_config_with_capabilities(self) -> None: |
| 46 | + """Test parsing resource configuration with capabilities.""" |
| 47 | + config_str = '{"cpu": 64, "gpu": 0, "memMB": 262144, "capabilities": {"node.kubernetes.io/instance-type": "m5.16xlarge"}}' |
| 48 | + resource = _parse_resource_config(config_str) |
| 49 | + |
| 50 | + self.assertEqual(resource.cpu, 64) |
| 51 | + self.assertEqual(resource.gpu, 0) |
| 52 | + self.assertEqual(resource.memMB, 262144) |
| 53 | + self.assertEqual( |
| 54 | + resource.capabilities, {"node.kubernetes.io/instance-type": "m5.16xlarge"} |
| 55 | + ) |
| 56 | + |
| 57 | + def test_parse_resource_config_defaults(self) -> None: |
| 58 | + """Test parsing resource configuration with default values.""" |
| 59 | + config_str = '{"cpu": 16, "memMB": 65536}' |
| 60 | + resource = _parse_resource_config(config_str) |
| 61 | + |
| 62 | + self.assertEqual(resource.cpu, 16) |
| 63 | + self.assertEqual(resource.gpu, 0) # default |
| 64 | + self.assertEqual(resource.memMB, 65536) |
| 65 | + |
| 66 | + def test_parse_resource_config_invalid_json(self) -> None: |
| 67 | + """Test parsing invalid JSON configuration.""" |
| 68 | + config_str = '{"cpu": 32, "gpu": 4, "memMB": 131072' # missing closing brace |
| 69 | + |
| 70 | + with self.assertRaises(ValueError) as cm: |
| 71 | + _parse_resource_config(config_str) |
| 72 | + |
| 73 | + self.assertIn("Invalid JSON", str(cm.exception)) |
| 74 | + |
| 75 | + def test_create_resource_factory(self) -> None: |
| 76 | + """Test creating resource factory function.""" |
| 77 | + config_str = '{"cpu": 8, "gpu": 1, "memMB": 32768}' |
| 78 | + factory = _create_resource_factory(config_str) |
| 79 | + |
| 80 | + resource = factory() |
| 81 | + self.assertEqual(resource.cpu, 8) |
| 82 | + self.assertEqual(resource.gpu, 1) |
| 83 | + self.assertEqual(resource.memMB, 32768) |
| 84 | + |
| 85 | + def test_load_config_file_not_found(self) -> None: |
| 86 | + """Test loading config file when none exists.""" |
| 87 | + with patch("os.path.exists", return_value=False): |
| 88 | + config = _load_config_file() |
| 89 | + self.assertFalse(config.sections()) |
| 90 | + |
| 91 | + def test_load_config_file_current_directory(self) -> None: |
| 92 | + """Test loading config file from current directory.""" |
| 93 | + with patch.dict("os.environ", {}, clear=True): # Clear TORCHXCONFIG |
| 94 | + with patch( |
| 95 | + "torchx.specs.named_resources_config.os.path.exists", return_value=True |
| 96 | + ) as mock_exists: |
| 97 | + with patch("configparser.ConfigParser.read") as mock_read: |
| 98 | + _load_config_file() |
| 99 | + |
| 100 | + # Verify the method was called with current directory path |
| 101 | + mock_exists.assert_called_with(".torchxconfig") |
| 102 | + mock_read.assert_called_with(".torchxconfig") |
| 103 | + |
| 104 | + def test_load_config_file_with_torchxconfig_env(self) -> None: |
| 105 | + """Test loading config file from TORCHXCONFIG environment variable.""" |
| 106 | + temp_config_path = "/tmp/custom_torchx_config" |
| 107 | + |
| 108 | + with patch.dict("os.environ", {"TORCHXCONFIG": temp_config_path}): |
| 109 | + with patch( |
| 110 | + "torchx.specs.named_resources_config.os.path.exists", return_value=True |
| 111 | + ): |
| 112 | + with patch("configparser.ConfigParser.read") as mock_read: |
| 113 | + _load_config_file() |
| 114 | + |
| 115 | + # Verify the method was called with the env var path |
| 116 | + mock_read.assert_called_with(temp_config_path) |
| 117 | + |
| 118 | + def test_load_named_resources_from_config_empty(self) -> None: |
| 119 | + """Test loading named resources when no config section exists.""" |
| 120 | + with patch( |
| 121 | + "torchx.specs.named_resources_config._load_config_file" |
| 122 | + ) as mock_load: |
| 123 | + mock_config = ConfigParser() |
| 124 | + mock_load.return_value = mock_config |
| 125 | + |
| 126 | + resources = _load_named_resources_from_config() |
| 127 | + self.assertEqual(resources, {}) |
| 128 | + |
| 129 | + def test_load_named_resources_from_config_with_resources(self) -> None: |
| 130 | + """Test loading named resources from config with valid resources.""" |
| 131 | + with patch( |
| 132 | + "torchx.specs.named_resources_config._load_config_file" |
| 133 | + ) as mock_load: |
| 134 | + mock_config = ConfigParser() |
| 135 | + mock_config.add_section("named_resources") |
| 136 | + mock_config.set( |
| 137 | + "named_resources", |
| 138 | + "test_resource", |
| 139 | + json.dumps({"cpu": 32, "gpu": 4, "memMB": 131072}), |
| 140 | + ) |
| 141 | + mock_config.set( |
| 142 | + "named_resources", |
| 143 | + "gpu_resource", |
| 144 | + json.dumps( |
| 145 | + { |
| 146 | + "cpu": 64, |
| 147 | + "gpu": 8, |
| 148 | + "memMB": 262144, |
| 149 | + "devices": {"vpc.amazonaws.com/efa": 2}, |
| 150 | + } |
| 151 | + ), |
| 152 | + ) |
| 153 | + mock_load.return_value = mock_config |
| 154 | + |
| 155 | + resources = _load_named_resources_from_config() |
| 156 | + |
| 157 | + self.assertIn("test_resource", resources) |
| 158 | + self.assertIn("gpu_resource", resources) |
| 159 | + |
| 160 | + # Test the factory functions |
| 161 | + test_res = resources["test_resource"]() |
| 162 | + self.assertEqual(test_res.cpu, 32) |
| 163 | + self.assertEqual(test_res.gpu, 4) |
| 164 | + self.assertEqual(test_res.memMB, 131072) |
| 165 | + |
| 166 | + gpu_res = resources["gpu_resource"]() |
| 167 | + self.assertEqual(gpu_res.cpu, 64) |
| 168 | + self.assertEqual(gpu_res.gpu, 8) |
| 169 | + self.assertEqual(gpu_res.memMB, 262144) |
| 170 | + self.assertEqual(gpu_res.devices, {"vpc.amazonaws.com/efa": 2}) |
| 171 | + |
| 172 | + def test_load_named_resources_from_config_invalid_json(self) -> None: |
| 173 | + """Test loading named resources with invalid JSON (should fail when factory is called).""" |
| 174 | + with patch( |
| 175 | + "torchx.specs.named_resources_config._load_config_file" |
| 176 | + ) as mock_load: |
| 177 | + mock_config = ConfigParser() |
| 178 | + mock_config.add_section("named_resources") |
| 179 | + mock_config.set( |
| 180 | + "named_resources", |
| 181 | + "valid_resource", |
| 182 | + json.dumps({"cpu": 32, "gpu": 4, "memMB": 131072}), |
| 183 | + ) |
| 184 | + mock_config.set( |
| 185 | + "named_resources", |
| 186 | + "invalid_resource", |
| 187 | + '{"cpu": 32, "gpu": 4, "memMB": 131072', |
| 188 | + ) # invalid JSON |
| 189 | + mock_load.return_value = mock_config |
| 190 | + |
| 191 | + resources = _load_named_resources_from_config() |
| 192 | + |
| 193 | + # Should have both resources (validation happens when factory is called) |
| 194 | + self.assertIn("valid_resource", resources) |
| 195 | + self.assertIn("invalid_resource", resources) |
| 196 | + |
| 197 | + # Valid resource should work |
| 198 | + valid_res = resources["valid_resource"]() |
| 199 | + self.assertEqual(valid_res.cpu, 32) |
| 200 | + |
| 201 | + # Invalid resource should raise exception when called |
| 202 | + with self.assertRaises(ValueError): |
| 203 | + resources["invalid_resource"]() |
| 204 | + |
| 205 | + def test_named_resources_module_level(self) -> None: |
| 206 | + """Test that NAMED_RESOURCES is properly loaded at module level.""" |
| 207 | + # This tests the actual module-level NAMED_RESOURCES |
| 208 | + # The exact content depends on the actual .torchxconfig file present |
| 209 | + self.assertIsInstance(NAMED_RESOURCES, dict) |
| 210 | + |
| 211 | + # Test that all values are callable factory functions |
| 212 | + for name, factory in NAMED_RESOURCES.items(): |
| 213 | + self.assertTrue(callable(factory)) |
| 214 | + # Test that calling the factory returns a Resource |
| 215 | + resource = factory() |
| 216 | + self.assertTrue(hasattr(resource, "cpu")) |
| 217 | + self.assertTrue(hasattr(resource, "gpu")) |
| 218 | + self.assertTrue(hasattr(resource, "memMB")) |
0 commit comments