Skip to content

Commit 60cbe98

Browse files
authored
Merge pull request #181 from cloudfoundry-community/fixes/convert_disk_quota
Fixes 'disk_quota' convertion Closes #179
2 parents 3334af9 + 00b9566 commit 60cbe98

2 files changed

Lines changed: 30 additions & 28 deletions

File tree

main/cloudfoundry_client/operations/push/validation/manifest.py

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77

88
class ManifestReader(object):
9-
MEMORY_PATTERN = re.compile(r"^(\d+)([KMGT])B?$")
9+
SIZE_FIELD_PATTERNS = re.compile(r"^(\d+)([MG])B?$")
1010

1111
POSITIVE_FIELDS = ["instances", "timeout"]
1212

@@ -40,7 +40,7 @@ def _validate_application_manifest(manifest_directory: str, app_manifest: dict):
4040
raise AssertionError("One of path or docker must be set")
4141
else:
4242
ManifestReader._absolute_path(manifest_directory, app_manifest)
43-
ManifestReader._convert_memory(app_manifest)
43+
ManifestReader._convert_size_fields(app_manifest)
4444
for field in ManifestReader.POSITIVE_FIELDS:
4545
ManifestReader._convert_positive(app_manifest, field)
4646
for field in ManifestReader.BOOLEAN_FIELDS:
@@ -61,25 +61,22 @@ def _check_deprecated_attributes(app_manifest: dict):
6161
raise AssertionError("hosts, host, domains, domain and no-hostname are all deprecated. Use the routes attribute")
6262

6363
@staticmethod
64-
def _convert_memory(manifest: dict):
65-
if "memory" in manifest:
66-
memory = manifest["memory"].upper()
67-
match = ManifestReader.MEMORY_PATTERN.match(memory)
68-
if match is None:
69-
raise AssertionError("Invalid memory format: %s" % memory)
70-
71-
memory_number = int(match.group(1))
72-
if match.group(2) == "K":
73-
memory_number *= 1024
74-
elif match.group(2) == "M":
75-
memory_number *= 1024 * 1024
76-
elif match.group(2) == "G":
77-
memory_number *= 1024 * 1024 * 1024
78-
elif match.group(2) == "T":
79-
memory_number *= 1024 * 1024 * 1024 * 1024
80-
else:
81-
raise AssertionError("Invalid memory unit: %s" % memory)
82-
manifest["memory"] = int(memory_number / (1024 * 1024))
64+
def _convert_size_fields(manifest: dict):
65+
for field_name in ["memory", "disk_quota"]:
66+
if field_name in manifest:
67+
field_value = manifest[field_name].upper()
68+
match = ManifestReader.SIZE_FIELD_PATTERNS.match(field_value)
69+
if match is None:
70+
raise AssertionError("Invalid %s format: %s" % (field_name, field_value))
71+
72+
size_converted = int(match.group(1))
73+
if match.group(2) == "M":
74+
size_converted *= 1024 * 1024
75+
elif match.group(2) == "G":
76+
size_converted *= 1024 * 1024 * 1024
77+
else:
78+
raise AssertionError("Invalid %s unit: %s" % (field_name, field_value))
79+
manifest[field_name] = int(size_converted / (1024 * 1024))
8380

8481
@staticmethod
8582
def _convert_positive(manifest: dict, field: str):

test/operations/push/validation/test_manifest_reader.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,17 +104,22 @@ def path_should_be_set_as_absolute(self):
104104
ManifestReader._validate_application_manifest(".", manifest)
105105
self.assertEqual(os.path.abspath("test"), manifest["path"])
106106

107-
def test_memory_in_kb(self):
108-
manifest = dict(memory="2048KB")
109-
ManifestReader._convert_memory(manifest)
110-
self.assertEqual(2, manifest["memory"])
111-
112107
def test_memory_in_mb(self):
113108
manifest = dict(memory="2048MB")
114-
ManifestReader._convert_memory(manifest)
109+
ManifestReader._convert_size_fields(manifest)
115110
self.assertEqual(2048, manifest["memory"])
116111

117112
def test_memory_in_gb(self):
118113
manifest = dict(memory="1G")
119-
ManifestReader._convert_memory(manifest)
114+
ManifestReader._convert_size_fields(manifest)
120115
self.assertEqual(1024, manifest["memory"])
116+
117+
def test_disk_quota_in_mb(self):
118+
manifest = dict(disk_quota="2048MB")
119+
ManifestReader._convert_size_fields(manifest)
120+
self.assertEqual(2048, manifest["disk_quota"])
121+
122+
def test_disk_quota_in_gb(self):
123+
manifest = dict(disk_quota="1G")
124+
ManifestReader._convert_size_fields(manifest)
125+
self.assertEqual(1024, manifest["disk_quota"])

0 commit comments

Comments
 (0)