Skip to content

Commit

Permalink
Work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
gpoblon committed Mar 5, 2020
2 parents d25ff95 + f0bf4e3 commit 4ef8cf2
Show file tree
Hide file tree
Showing 10 changed files with 279 additions and 13 deletions.
34 changes: 34 additions & 0 deletions rudder-lang/tests/test_files/tester/parameters/technique.json
@@ -0,0 +1,34 @@
{
"name": "parameters",
"description": "technique using parameters",
"version": "1.0",
"parameter": [
{
"name": "paramtest",
"id": "d74a03dd-5b0b-4b06-8dcf-b4e0cb387c60",
"constraints": {
"allow_whitespace_string": false,
"allow_empty_string": false,
"max_length": 16384
},
"type": "string"
}
],
"bundle_name": "parameters",
"bundle_args": [
"paramtest"
],
"method_calls": [
{
"class_context": "any",
"component": "Package absent",
"method_name": "package_absent",
"args": [
"vpn",
"",
"",
""
]
}
]
}
14 changes: 14 additions & 0 deletions rudder-lang/tests/test_files/tester/parameters/technique.rl
@@ -0,0 +1,14 @@
# This file has been generated with rltranslate
@format=0

@name="parameters"
@description="technique using parameters"
@version="1.0"
@parameters=[{"constraints":{"allow_empty_string":false,"allow_whitespace_string":false,"max_length":16384},"id":"d74a03dd-5b0b-4b06-8dcf-b4e0cb387c60","name":"paramtest","type":"string"}]

resource parameters(paramtest)

parameters state technique() {
@component = "Package absent"
package("vpn").absent("","","") as package_absent_vpn
}
14 changes: 14 additions & 0 deletions rudder-lang/tests/test_files/tester/parameters/technique.rl.cf
@@ -0,0 +1,14 @@
# generated by rudder-lang
# @name parameters
# @description technique using parameters
# @version 1.0
# @parameter { "name": "paramtest", "id": "d74a03dd-5b0b-4b06-8dcf-b4e0cb387c60", "constraints": { "allow_whitespace_string":1,"max_length":16384,"allow_empty_string":1 } }

bundle agent parameters_technique(paramtest)
{
vars:
"resources_dir" string => "${this.promise_dirname}/resources";
methods:
"Package absent_${report_data.directive_id}_0" usebundle => _method_reporting_context("Package absent", "");
"Package absent_${report_data.directive_id}_0" usebundle => package_absent("vpn", "", "", "");
}
@@ -0,0 +1,34 @@
{
"name": "parameters",
"description": "technique using parameters",
"version": "1.0",
"parameter": [
{
"name": "paramtest",
"id": "d74a03dd-5b0b-4b06-8dcf-b4e0cb387c60",
"constraints": {
"allow_whitespace_string": false,
"allow_empty_string": false,
"max_length": 16384
},
"type": "string"
}
],
"bundle_name": "parameters_technique",
"bundle_args": [
"paramtest"
],
"method_calls": [
{
"class_context": "any",
"component": "Package absent",
"method_name": "package_absent",
"args": [
"vpn",
"",
"",
""
]
}
]
}
45 changes: 45 additions & 0 deletions rudder-lang/tests/test_files/tester/parameters_mult/technique.json
@@ -0,0 +1,45 @@
{
"name": "parameters mult",
"description": "technique using multiple parameters",
"version": "1.0",
"parameter": [
{
"name": "paramtest",
"id": "d74a03dd-5b0b-4b06-8dcf-b4e0cb387c60",
"constraints": {
"allow_whitespace_string": false,
"allow_empty_string": false,
"max_length": 16384
},
"type": "string"
},
{
"name": "12",
"id": "6e82d276-1181-4596-8fd8-bc3c007ae9ff",
"constraints": {
"allow_whitespace_string": false,
"allow_empty_string": false,
"max_length": 16384
},
"type": "string"
}
],
"bundle_name": "parameters_mult",
"bundle_args": [
"paramtest",
"12"
],
"method_calls": [
{
"class_context": "any",
"component": "Package absent",
"method_name": "package_absent",
"args": [
"vpn",
"",
"",
""
]
}
]
}
14 changes: 14 additions & 0 deletions rudder-lang/tests/test_files/tester/parameters_mult/technique.rl
@@ -0,0 +1,14 @@
# This file has been generated with rltranslate
@format=0

@name="parameters mult"
@description="technique using multiple parameters"
@version="1.0"
@parameters=[{"constraints":{"allow_empty_string":false,"allow_whitespace_string":false,"max_length":16384},"id":"d74a03dd-5b0b-4b06-8dcf-b4e0cb387c60","name":"paramtest","type":"string"},{"constraints":{"allow_empty_string":false,"allow_whitespace_string":false,"max_length":16384},"id":"6e82d276-1181-4596-8fd8-bc3c007ae9ff","name":"12","type":"string"}]

resource parameters_mult(paramtest,12)

parameters_mult state technique() {
@component = "Package absent"
package("vpn").absent("","","") as package_absent_vpn
}
@@ -0,0 +1,15 @@
# generated by rudder-lang
# @name parameters mult
# @description technique using multiple parameters
# @version 1.0
# @parameter { "name": "paramtest", "id": "d74a03dd-5b0b-4b06-8dcf-b4e0cb387c60", "constraints": { "allow_empty_string":1,"max_length":16384,"allow_whitespace_string":1 } }
# @parameter { "name": "12", "id": "6e82d276-1181-4596-8fd8-bc3c007ae9ff", "constraints": { "allow_empty_string":1,"allow_whitespace_string":1,"max_length":16384 } }

bundle agent parameters_mult_technique(paramtest,12)
{
vars:
"resources_dir" string => "${this.promise_dirname}/resources";
methods:
"Package absent_${report_data.directive_id}_0" usebundle => _method_reporting_context("Package absent", "");
"Package absent_${report_data.directive_id}_0" usebundle => package_absent("vpn", "", "", "");
}
@@ -0,0 +1,45 @@
{
"name": "parameters mult",
"description": "technique using multiple parameters",
"version": "1.0",
"parameter": [
{
"name": "paramtest",
"id": "d74a03dd-5b0b-4b06-8dcf-b4e0cb387c60",
"constraints": {
"allow_whitespace_string": false,
"allow_empty_string": false,
"max_length": 16384
},
"type": "string"
},
{
"name": "12",
"id": "6e82d276-1181-4596-8fd8-bc3c007ae9ff",
"constraints": {
"allow_whitespace_string": false,
"allow_empty_string": false,
"max_length": 16384
},
"type": "string"
}
],
"bundle_name": "parameters_mult_technique",
"bundle_args": [
"paramtest",
"12"
],
"method_calls": [
{
"class_context": "any",
"component": "Package absent",
"method_name": "package_absent",
"args": [
"vpn",
"",
"",
""
]
}
]
}
67 changes: 59 additions & 8 deletions rudder-lang/tools/cfjson_tester
Expand Up @@ -96,23 +96,74 @@ def load_cf(file_name):
def empty_dict_cleaner(diff_dict):
if type(diff_dict) is dict:
for element in diff_dict:
empty_dict_cleaner(diff_dict[element])
if len(diff_dict[element]) == 0:
child = diff_dict[element]
empty_dict_cleaner(child)
if len(child) == 0 or (len(child) == 1 and list(diff_dict[element].keys())[0] == 'line'):
diff_dict.pop(element)
return

def deep_get(haystack, deep_list, needle, index=0):
if type(haystack) is list:
for elem in haystack:
deep_get(elem, deep_list, needle, index)
elif type(haystack) is tuple:
for elem in haystack:
deep_get(elem, deep_list, needle, index)
elif type(haystack) is dict and index < len(deep_list) and deep_list[index] in haystack:
for key, value in haystack[deep_list[index]].copy().items():
deep_get(value, deep_list, needle, index + 1)
if type(needle) is str and needle in value:
return value[needle]
return None


# Warning1: only delete keys from dict!
# Warning2: compare (needle) either from key or with dict content
def deep_del(haystack, deep_list, needle, delfrom, index=0):
## consider list to be iterated on like any other dict content
istodel = False
if type(haystack) is list:
for elem in haystack:
istodel |= deep_del(elem, deep_list, needle, delfrom, index)
elif type(haystack) is tuple:
for elem in haystack:
istodel |= deep_del(elem, deep_list, needle, delfrom, index)
elif type(haystack) is dict and index < len(deep_list) and deep_list[index] in haystack:
for key, value in haystack[deep_list[index]].copy().items():
istodel |= deep_del(value, deep_list, needle, delfrom, index + 1)
## remove seeked child content if needle is a string
if istodel and type(needle) is str and needle in value:
value.pop(needle)
## remove content if needle has matched another dict
if istodel and delfrom == key:
haystack[deep_list[index]].pop(key)

if type(needle) is str and needle in haystack:
## Will need to remove the child element (needle in haystack) from the parent as we recursively go back only from there (never deeper)
return True
if haystack == needle or (type(needle) is str and needle in haystack):
return True
return istodel

# deletes from diff list every i/o difference done on purpose (ie that are not a bug)
def remove_wilful_entries(diff):
## del name since `_technique` is always added
name = deep_get(diff, ['bundles'], 'name')
if name[0] + '_technique' == name[1]:
deep_del(diff, ['bundles'], 'name', 'name')
## del sourcePath since it is not relevant
deep_del(diff, ['bundles'], 'sourcePath', 'sourcePath')
## del superfluous if true equivalent condition
deep_del(diff, ['bundles', 'promiseTypes', 'contexts', 'promises', 'attributes', 'rval', 'arguments'], {'type': 'string', 'value': 'any'}, '$delete')

def compare_cf(first, second):
j1 = load_cf(first)
j2 = load_cf(second)
diff = jsondiff.diff(j1, j2, syntax="symmetric", marshal=True)
if 'bundles' in diff:
if diff['bundles'][0]['sourcePath']:
del diff['bundles'][0]['sourcePath']
if diff['bundles'][0]['name'][0] + '_technique' == diff['bundles'][0]['name'][1]:
del diff['bundles'][0]['name']
remove_wilful_entries(diff)
empty_dict_cleaner(diff)
if len(diff) != 0:
print(json.dumps(diff, indent=4, sort_keys=True))
print(json.dumps(diff, indent=2))

if __name__ == "__main__":
arguments = docopt.docopt(__doc__)
Expand Down
10 changes: 5 additions & 5 deletions rudder-lang/tools/tester-dev.sh
Expand Up @@ -8,20 +8,20 @@ cfjson_tester=$PWD/tools/cfjson_tester
mkdir -p $dir/

# Take original technique an make a json
# $cfjson_tester ncf-to-json $dir/technique.cf $dir/technique.json /tools/rudderc-dev.conf
$cfjson_tester ncf-to-json $dir/technique.cf $dir/technique.json /tools/rudderc-dev.conf

# Take json and produce a rudder-lang technique
# cargo run -- --translate --config ./tools/rudderc-dev.conf -i $dir/technique.json
cargo run -- --translate --config ./tools/rudderc-dev.conf -i $dir/technique.json

# Take rudder lang technique and compile it into cf file
# output format is generated behind the scenes, it actually is a cf file, not rl
# cargo run -- --compile --config ./tools/rudderc-dev.conf -i $dir/technique.rl
cargo run -- --compile --config ./tools/rudderc-dev.conf -i $dir/technique.rl

# take generated cf file a new json
# $cfjson_tester ncf-to-json $dir/technique.rl.cf $dir/technique.rl.cf.json /tools/rudderc-dev.conf
$cfjson_tester ncf-to-json $dir/technique.rl.cf $dir/technique.rl.cf.json /tools/rudderc-dev.conf

# TODO compare generated json
# $cfjson_tester compare-json $dir/technique.json $dir/technique.rl.cf.json /tools/rudderc-dev.conf
$cfjson_tester compare-json $dir/technique.json $dir/technique.rl.cf.json /tools/rudderc-dev.conf

# TODO compare generated cf files
$cfjson_tester compare-cf $dir/technique.cf $dir/technique.rl.cf /tools/rudderc-dev.conf
Expand Down

0 comments on commit 4ef8cf2

Please sign in to comment.