Skip to content

Commit

Permalink
Fix workload parameter processing when a key is mapped to a JSON file (
Browse files Browse the repository at this point in the history
…#690)

Signed-off-by: Govind Kamat <[email protected]>
  • Loading branch information
gkamat authored Nov 26, 2024
1 parent 2d14ed4 commit 92f8102
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
2 changes: 1 addition & 1 deletion osbenchmark/utils/opts.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def convert(v):


def to_dict(arg, default_parser=kv_to_map):
if io.has_extension(arg, ".json"):
if io.has_extension(arg, ".json") and ',' not in arg and ':' not in arg:
with open(io.normalize_path(arg), mode="rt", encoding="utf-8") as f:
return json.load(f)
elif arg.startswith("{"):
Expand Down
6 changes: 5 additions & 1 deletion osbenchmark/workload/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -1278,7 +1278,11 @@ def read(self, workload_name, workload_spec_file, mapping_dir):
ve.instance, indent=4, sort_keys=True),
ve.absolute_path, ve.absolute_schema_path))

current_workload = self.read_workload(workload_name, workload_spec, mapping_dir)
try:
current_workload = self.read_workload(workload_name, workload_spec, mapping_dir)
except Exception as e:
console.error(e)
raise

unused_user_defined_workload_params = self.complete_workload_params.unused_user_defined_workload_params()
if len(unused_user_defined_workload_params) > 0:
Expand Down
21 changes: 21 additions & 0 deletions tests/utils/opts_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import os
from unittest import TestCase

import unittest.mock as mock
from osbenchmark.utils import opts


Expand Down Expand Up @@ -141,6 +142,26 @@ def test_make_list_of_close_matches_returns_empty_list_with_no_close_matches(sel
[])
)

def test_to_dict_str(self):
json = '{ "field": 2 }'
rsl = opts.to_dict(json)
self.assertEqual(rsl, {"field": 2})

rsl = opts.to_dict('a:1,b:2')
self.assertEqual(rsl, {'a': 1, 'b': 2})

@mock.patch("json.loads")
def test_to_dict_file(self, json_loads):
mo = mock.mock_open()
with mock.patch("builtins.open", mo):
opts.to_dict('params.json')
mo.assert_called()

mo = mock.mock_open()
with mock.patch("builtins.open", mo):
opts.to_dict('index_body:idx.json')
mo.assert_not_called()


class TestTargetHosts(TestCase):
def test_empty_arg_parses_as_empty_list(self):
Expand Down

0 comments on commit 92f8102

Please sign in to comment.