From bf1be846e5958646a61535cdc36adfe8e947fced Mon Sep 17 00:00:00 2001 From: "Brian C. Lane" Date: Wed, 10 Jun 2020 11:08:41 -0700 Subject: [PATCH] composer-cli: Return a better error with no value And add tests for the get_size function. --- src/composer/cli/compose.py | 2 +- tests/composer/test_compose.py | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/composer/cli/compose.py b/src/composer/cli/compose.py index b20988c9..37240b6f 100644 --- a/src/composer/cli/compose.py +++ b/src/composer/cli/compose.py @@ -95,7 +95,7 @@ def get_size(args): return (args, 0) if len(args) < 2: - return (args, 0) + raise RuntimeError("--size is missing the value, in MiB") # Let this raise an error for non-digit input size = int(args[1]) diff --git a/tests/composer/test_compose.py b/tests/composer/test_compose.py index 5477e26d..f5fb6888 100644 --- a/tests/composer/test_compose.py +++ b/tests/composer/test_compose.py @@ -27,6 +27,7 @@ import unittest from composer import http_client as client import composer.cli as cli from composer.cli.cmdline import composer_cli_parser +from composer.cli.compose import get_size # Use a GLOBAL record the request data for use in the test # because there is no way to access the request handler class from the test methods @@ -367,3 +368,27 @@ class ComposeOsBuildV1TestCase(ComposeTestCase): "ostree": {"ref": "referenceid", "parent": "parenturl"}, "upload": {"image_name": "httpimage", "provider": "aws", "settings": {"aws_access_key": "AWS Access Key", "aws_bucket": "AWS Bucket", "aws_region": "AWS Region", "aws_secret_key": "AWS Secret Key"}}}) + +class SizeTest(unittest.TestCase): + def test_empty(self): + self.assertEqual(get_size([]), ([], 0)) + + def test_no_size(self): + self.assertEqual(get_size(["blueprint", "type", "imagename", "profile"]), + (["blueprint", "type", "imagename", "profile"], 0)) + + def test_size_later(self): + with self.assertRaises(RuntimeError): + get_size(["blueprint", "--size", "100", "type"]) + + def test_size_no_value(self): + with self.assertRaises(RuntimeError): + get_size(["--size"]) + + def test_size_nonint(self): + with self.assertRaises(ValueError): + get_size(["--size", "abc"]) + + def test_get_size(self): + self.assertEqual(get_size(["--size", "1912", "blueprint", "type"]), + (["blueprint", "type"], 2004877312))