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,18 +1007,21 @@ 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"
1001
- # go through each jsonld file in message_path and check if the label field exists
1002
- json_content = []
1010
+
1003
1011
for file in glob (str (message_path ) + "/*.jsonld" ):
1004
1012
with open (file , "r" ) as f :
1005
1013
data = json .load (f )
1006
- print (data )
1007
- if "Label" in data :
1008
- json_content .append (True )
1009
- assert "testfunc" == data ["Label" ]
1014
+ if "@type" in data :
1010
1015
if "AssociatedWith" in data :
1011
- assert None == data ["AssociatedWith" ]
1012
- assert any (json_content )
1016
+ assert "testfunc" in data ["Label" ]
1017
+
1018
+ if "@type" in data :
1019
+ if data ["@type" ] == "input" :
1020
+ assert None == data ["Label" ]
1021
+ if "AssociatedWith" in data :
1022
+ assert None == data ["AssociatedWith" ]
1023
+
1024
+ # assert any(json_content)
1013
1025
1014
1026
1015
1027
def test_audit_shellcommandtask (tmpdir ):
@@ -1028,21 +1040,103 @@ def test_audit_shellcommandtask(tmpdir):
1028
1040
shelly ()
1029
1041
message_path = tmpdir / shelly .checksum / "messages"
1030
1042
# go through each jsonld file in message_path and check if the label field exists
1031
- label_content = []
1043
+
1032
1044
command_content = []
1033
1045
1034
1046
for file in glob (str (message_path ) + "/*.jsonld" ):
1035
1047
with open (file , "r" ) as f :
1036
1048
data = json .load (f )
1037
- print (data )
1038
- if "Label" in data :
1039
- label_content .append (True )
1049
+
1050
+ if "@type" in data :
1051
+ if "AssociatedWith" in data :
1052
+ assert "shelly" in data ["Label" ]
1053
+
1054
+ if "@type" in data :
1055
+ if data ["@type" ] == "input" :
1056
+ assert data ["Label" ] == None
1057
+
1040
1058
if "Command" in data :
1041
1059
command_content .append (True )
1042
1060
assert "ls -l" == data ["Command" ]
1043
1061
1044
- print (command_content )
1045
- assert any (label_content )
1062
+ assert any (command_content )
1063
+
1064
+
1065
+ def test_audit_shellcommandtask_file (tmpdir ):
1066
+ # sourcery skip: use-fstring-for-concatenation
1067
+ import glob
1068
+ import shutil
1069
+
1070
+ # create test.txt file with "This is a test" in it in the tmpdir
1071
+ # create txt file in cwd
1072
+ with open ("test.txt" , "w" ) as f :
1073
+ f .write ("This is a test" )
1074
+
1075
+ with open ("test2.txt" , "w" ) as f :
1076
+ f .write ("This is a test" )
1077
+
1078
+ # copy the test.txt file to the tmpdir
1079
+ shutil .copy ("test.txt" , tmpdir )
1080
+ shutil .copy ("test2.txt" , tmpdir )
1081
+
1082
+ cmd = "cat"
1083
+ file_in = tmpdir / "test.txt"
1084
+ file_in_2 = tmpdir / "test2.txt"
1085
+ test_file_hash = hash_file (file_in )
1086
+ test_file_hash_2 = hash_file (file_in_2 )
1087
+ my_input_spec = SpecInfo (
1088
+ name = "Input" ,
1089
+ fields = [
1090
+ (
1091
+ "in_file" ,
1092
+ attr .ib (
1093
+ type = File ,
1094
+ metadata = {
1095
+ "position" : 1 ,
1096
+ "argstr" : "" ,
1097
+ "help_string" : "text" ,
1098
+ "mandatory" : True ,
1099
+ },
1100
+ ),
1101
+ ),
1102
+ (
1103
+ "in_file_2" ,
1104
+ attr .ib (
1105
+ type = File ,
1106
+ metadata = {
1107
+ "position" : 2 ,
1108
+ "argstr" : "" ,
1109
+ "help_string" : "text" ,
1110
+ "mandatory" : True ,
1111
+ },
1112
+ ),
1113
+ ),
1114
+ ],
1115
+ bases = (ShellSpec ,),
1116
+ )
1117
+ shelly = ShellCommandTask (
1118
+ name = "shelly" ,
1119
+ in_file = file_in ,
1120
+ in_file_2 = file_in_2 ,
1121
+ input_spec = my_input_spec ,
1122
+ executable = cmd ,
1123
+ audit_flags = AuditFlag .PROV ,
1124
+ messengers = FileMessenger (),
1125
+ )
1126
+ shelly .cache_dir = tmpdir
1127
+ shelly ()
1128
+ message_path = tmpdir / shelly .checksum / "messages"
1129
+ for file in glob .glob (str (message_path ) + "/*.jsonld" ):
1130
+ with open (file , "r" ) as x :
1131
+ data = json .load (x )
1132
+ if "@type" in data :
1133
+ if data ["@type" ] == "input" :
1134
+ if data ["Label" ] == "in_file" :
1135
+ assert data ["AtLocation" ] == str (file_in )
1136
+ assert data ["digest" ] == test_file_hash
1137
+ if data ["Label" ] == "in_file_2" :
1138
+ assert data ["AtLocation" ] == str (file_in_2 )
1139
+ assert data ["digest" ] == test_file_hash_2
1046
1140
1047
1141
1048
1142
def test_audit_shellcommandtask_version (tmpdir ):
0 commit comments