Skip to content

Commit

Permalink
add validate-only option for cli (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
jupe authored May 17, 2021
1 parent e238614 commit 1efac22
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Resource is released in following cases:

```
% lockable --help
usage: lockable [-h] [--lock-folder LOCK_FOLDER] [--resources RESOURCES]
usage: lockable [-h] [--validate-only] [--lock-folder LOCK_FOLDER] [--resources RESOURCES]
[--timeout TIMEOUT] [--hostname HOSTNAME]
[--requirements REQUIREMENTS]
[command [command ...]]
Expand All @@ -36,6 +36,7 @@ positional arguments:
optional arguments:
-h, --help show this help message and exit
--validate-only Only validate resources.json
--lock-folder LOCK_FOLDER
lock folder
--resources RESOURCES
Expand Down
9 changes: 9 additions & 0 deletions lockable/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ def get_args():
'Usage example: lockable --requirements {"online":true} '
'echo using resource: $ID',
formatter_class=argparse.RawTextHelpFormatter)

parser.add_argument('--validate-only',
action = "store_true",
default=False,
help='Only validate resources.json')
parser.add_argument('--lock-folder',
default='.',
help='lock folder')
Expand Down Expand Up @@ -49,6 +54,10 @@ def main():
lockable = Lockable(hostname=args.hostname,
resource_list_file=args.resources,
lock_folder=args.lock_folder)

if args.validate_only:
sys.exit(0)

with lockable.auto_lock(args.requirements, timeout_s=args.timeout) as allocation:
resource = allocation.resource_info
env = os.environ.copy()
Expand Down
24 changes: 23 additions & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,26 @@ def test_host_not_found(self):
with self.assertRaises(SystemExit) as cm:
with patch.object(sys, 'argv', testargs):
main()
self.assertEqual(cm.exception.code, 0)
self.assertEqual(cm.exception.code, 0)

def test_validate_only_fail(self):
with TemporaryDirectory() as tmpdirname:
list_file = os.path.join(tmpdirname, 'resources.json')
with open(list_file, 'w') as fp:
fp.write('[{"id": "abc", "hostname": "localhost", "online": true},'
' {"id": "abc", "hostname": "localhost2", "online": true}]')
testargs = ["prog", "--validate-only", "--hostname", "localhost", "--resources", list_file, "echo", "$ID"]
with self.assertRaises(ValueError):
with patch.object(sys, 'argv', testargs):
main()

def test_validate_only_ok(self):
with TemporaryDirectory() as tmpdirname:
list_file = os.path.join(tmpdirname, 'resources.json')
with open(list_file, 'w') as fp:
fp.write('[{"id": "abc", "hostname": "localhost", "online": true}]')
testargs = ["prog", "--validate-only", "--hostname", "localhost", "--resources", list_file, "echo", "$ID"]
with self.assertRaises(SystemExit) as cm:
with patch.object(sys, 'argv', testargs):
main()
self.assertEqual(cm.exception.code, 0)

0 comments on commit 1efac22

Please sign in to comment.