11
11
from ..core import Workflow
12
12
from ..task import AuditFlag , ShellCommandTask , DockerTask , SingularityTask
13
13
from ...utils .messenger import FileMessenger , PrintMessenger , collect_messages
14
- from .utils import gen_basic_wf , use_validator
15
- from ..specs import MultiInputObj , MultiOutputObj , SpecInfo , FunctionSpec , BaseSpec
14
+ from .utils import gen_basic_wf , use_validator , Submitter
15
+ from ..specs import (
16
+ MultiInputObj ,
17
+ MultiOutputObj ,
18
+ SpecInfo ,
19
+ FunctionSpec ,
20
+ BaseSpec ,
21
+ ShellSpec ,
22
+ File ,
23
+ )
24
+ from ..helpers import hash_file
16
25
17
26
no_win = pytest .mark .skipif (
18
27
sys .platform .startswith ("win" ),
@@ -998,15 +1007,30 @@ def testfunc(a: int, b: float = 0.1) -> ty.NamedTuple("Output", [("out", float)]
998
1007
funky .cache_dir = tmpdir
999
1008
funky ()
1000
1009
message_path = tmpdir / funky .checksum / "messages"
1010
+ print (message_path )
1001
1011
# go through each jsonld file in message_path and check if the label field exists
1002
1012
json_content = []
1013
+
1003
1014
for file in glob (str (message_path ) + "/*.jsonld" ):
1004
1015
with open (file , "r" ) as f :
1005
1016
data = json .load (f )
1006
- if "label" in data :
1007
- json_content .append (True )
1008
- assert "testfunc" == data ["label" ]
1009
- assert any (json_content )
1017
+ if "@type" in data :
1018
+ if "AssociatedWith" in data :
1019
+ assert "testfunc" in data ["Label" ]
1020
+
1021
+ if "@type" in data :
1022
+ if data ["@type" ] == "input" :
1023
+ assert None == data ["Label" ]
1024
+ # placeholder for atlocation until
1025
+ # new test is added
1026
+ assert None == data ["AtLocation" ]
1027
+
1028
+ # assert data["Type"] == "input"
1029
+
1030
+ if "AssociatedWith" in data :
1031
+ assert None == data ["AssociatedWith" ]
1032
+
1033
+ # assert any(json_content)
1010
1034
1011
1035
1012
1036
def test_audit_shellcommandtask (tmpdir ):
@@ -1025,20 +1049,106 @@ def test_audit_shellcommandtask(tmpdir):
1025
1049
shelly ()
1026
1050
message_path = tmpdir / shelly .checksum / "messages"
1027
1051
# go through each jsonld file in message_path and check if the label field exists
1028
- label_content = []
1052
+
1029
1053
command_content = []
1030
1054
1031
1055
for file in glob (str (message_path ) + "/*.jsonld" ):
1032
1056
with open (file , "r" ) as f :
1033
1057
data = json .load (f )
1034
- if "label" in data :
1035
- label_content .append (True )
1036
- if "command" in data :
1058
+
1059
+ if "@type" in data :
1060
+ if "AssociatedWith" in data :
1061
+ assert "shelly" in data ["Label" ]
1062
+
1063
+ if "@type" in data :
1064
+ if data ["@type" ] == "input" :
1065
+ assert data ["Label" ] == None
1066
+
1067
+ if "Command" in data :
1037
1068
command_content .append (True )
1038
- assert "ls -l" == data ["command" ]
1069
+ assert "ls -l" == data ["Command" ]
1070
+
1071
+ assert any (command_content )
1072
+
1073
+
1074
+ def test_audit_shellcommandtask_file (tmpdir ):
1075
+ # create test.txt file with "This is a test" in it in the tmpdir
1076
+ with open (tmpdir / "test.txt" , "w" ) as f :
1077
+ f .write ("This is a test." )
1078
+
1079
+ cmd = "cat"
1080
+ file_in = tmpdir / "test.txt"
1081
+ test_file_hash = hash_file (file_in )
1082
+ my_input_spec = SpecInfo (
1083
+ name = "Input" ,
1084
+ fields = [
1085
+ (
1086
+ "in_file" ,
1087
+ attr .ib (
1088
+ type = File ,
1089
+ metadata = {
1090
+ "position" : 1 ,
1091
+ "argstr" : "" ,
1092
+ "help_string" : "text" ,
1093
+ "mandatory" : True ,
1094
+ },
1095
+ ),
1096
+ )
1097
+ ],
1098
+ bases = (ShellSpec ,),
1099
+ )
1100
+ shelly = ShellCommandTask (
1101
+ name = "shelly" ,
1102
+ in_file = file_in ,
1103
+ input_spec = my_input_spec ,
1104
+ executable = cmd ,
1105
+ audit_flags = AuditFlag .PROV ,
1106
+ messengers = PrintMessenger (),
1107
+ )
1108
+ shelly .cache_dir = tmpdir
1109
+ shelly ()
1110
+ message_path = tmpdir / shelly .checksum / "messages"
1111
+ for file in glob .glob (str (message_path ) + "/*.jsonld" ):
1112
+ with open (file , "r" ) as f :
1113
+ data = json .load (f )
1114
+ print (file_in )
1115
+ if "AtLocation" in data :
1116
+ assert data ["AtLocation" ] == str (file_in )
1117
+ if "digest" in data :
1118
+ assert test_file_hash == data ["digest" ]
1119
+
1120
+
1121
+ def test_audit_shellcommandtask_version (tmpdir ):
1122
+ import subprocess as sp
1123
+
1124
+ version_cmd = sp .run ("less --version" , shell = True , stdout = sp .PIPE ).stdout .decode (
1125
+ "utf-8"
1126
+ )
1127
+ version_cmd = version_cmd .splitlines ()[0 ]
1128
+ cmd = "less"
1129
+ shelly = ShellCommandTask (
1130
+ name = "shelly" ,
1131
+ executable = cmd ,
1132
+ args = "test_task.py" ,
1133
+ audit_flags = AuditFlag .PROV ,
1134
+ messengers = FileMessenger (),
1135
+ )
1136
+
1137
+ import glob
1138
+
1139
+ shelly .cache_dir = tmpdir
1140
+ shelly ()
1141
+ message_path = tmpdir / shelly .checksum / "messages"
1142
+ # go through each jsonld file in message_path and check if the label field exists
1143
+ version_content = []
1144
+ for file in glob .glob (str (message_path ) + "/*.jsonld" ):
1145
+ with open (file , "r" ) as f :
1146
+ data = json .load (f )
1147
+ if "AssociatedWith" in data :
1148
+ if version_cmd in data ["AssociatedWith" ]:
1149
+ version_content .append (True )
1039
1150
1040
- print (command_content )
1041
- assert any (label_content )
1151
+ assert any (version_content )
1042
1152
1043
1153
1044
1154
def test_audit_prov_messdir_1 (tmpdir , use_validator ):
@@ -1137,7 +1247,7 @@ def testfunc(a: int, b: float = 0.1) -> ty.NamedTuple("Output", [("out", float)]
1137
1247
from glob import glob
1138
1248
1139
1249
assert len (glob (str (tmpdir / funky .checksum / "proc*.log" ))) == 1
1140
- assert len (glob (str (message_path / "*.jsonld" ))) == 7
1250
+ assert len (glob (str (message_path / "*.jsonld" ))) == 8
1141
1251
1142
1252
# commented out to speed up testing
1143
1253
collect_messages (tmpdir / funky .checksum , message_path , ld_op = "compact" )
0 commit comments