File: vine_python.py

package info (click to toggle)
cctools 1%3A7.14.5-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 36,956 kB
  • sloc: ansic: 114,614; python: 29,532; cpp: 20,313; sh: 13,675; perl: 4,056; xml: 3,688; makefile: 1,436
file content (304 lines) | stat: -rwxr-xr-x 10,275 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#! /usr/bin/env python

# taskvine python binding tests
# tests for missing/recursive inputs/outputs.

import argparse
import atexit
import sys
import tempfile
import os
import os.path as path
import shutil
import stat
import time

import ndcctools.taskvine as vine

test_dir = tempfile.mkdtemp(prefix="vine.test", dir=".")
input_name = "input.file"
exec_name = "exec.file"

error = False


def cleanup():
    shutil.rmtree(test_dir)


atexit.register(cleanup)


def report_task(task, expected_result, expected_exit_code, expected_outputs=None):
    global error
    if not task:
        error = True
        print("\nTask did not complete in expected time.")
    else:
        print("\nTask '{command}' report:".format(command=task.command))
        print("result: {r}".format(r=task.result))
        print("exit code: {exit_code}".format(exit_code=task.exit_code))
        if task.output:
            print("stderr:\n+++\n{stderr}---".format(stderr=task.output.encode("ascii", "replace")))
        if task.result != expected_result:
            error = True
            print("Should have finished with result '{result}', but got '{real}'.".format(result=expected_result, real=task.result))
        elif task.exit_code != expected_exit_code:
            error = True
            print("Should have finished with exit_code {expected_exit_code}, but got {real}.".format(expected_exit_code=str(expected_exit_code), real=str(task.exit_code)))
        elif expected_outputs:
            for out in expected_outputs:
                if not path.isfile(out):
                    error = True
                    print("Should have created file {output} but did not.".format(output=out))
        else:
            print("Completed as expected.")


output_count = 0


def next_output_name():
    global output_count
    output_count += 1
    return "output_file." + str(output_count)


if __name__ == "__main__":
    parser = argparse.ArgumentParser("Test for taskvine python bindings.")
    parser.add_argument("port_file", help="File to write the port the queue is using.")
    parser.add_argument("--ssl_key", default=None, help="SSL key in pem format.")
    parser.add_argument("--ssl_cert", default=None, help="SSL certificate in pem format.")

    args = parser.parse_args()

    wait_time = 30

    with open(path.join(test_dir, input_name), "w") as f:
        f.write("hello world\n")

    shutil.copyfile("/bin/cat", path.join(test_dir, exec_name))
    os.chmod(path.join(test_dir, exec_name), stat.S_IRWXU)

    q = vine.Manager(port=0, ssl=(args.ssl_key, args.ssl_cert))
    q.tune("transient-error-interval", 1)

    exec_file = q.declare_file(path.join(test_dir, exec_name), cache=True)
    input_file = q.declare_file(path.join(test_dir, input_name), cache=True)

    with open(args.port_file, "w") as f:
        print("Writing port {port} to file {file}".format(port=q.port, file=args.port_file))
        f.write(str(q.port))

    # simple task
    # define a task, sending stderr to console, and stdout to output
    output_name = next_output_name()
    t = vine.Task(f"./{exec_name} {input_name} 2>&1 > {output_name}")
    t.add_input(exec_file, exec_name)
    t.add_input(input_file, input_name)
    output_file = q.declare_file(path.join(test_dir, output_name), cache=False)
    t.add_output(output_file, output_name)

    q.submit(t)
    t = q.wait(wait_time)
    report_task(t, "success", 0, [path.join(test_dir, output_name)])

    # same simple task, but now we send the directory as an input
    output_name = next_output_name()
    t = vine.Task(f"cd my_dir && ./{exec_name} {input_name} 2>&1 > {output_name}")
    in_dir = q.declare_file(test_dir, cache=True)
    t.add_input(exec_file, exec_name)
    t.add_input(in_dir, "my_dir")
    output_file = q.declare_file(path.join(test_dir, output_name), cache=False)
    t.add_output(output_file, path.join("my_dir", output_name))

    q.submit(t)
    t = q.wait(wait_time)
    report_task(t, "success", 0, [path.join(test_dir, output_name)])

    # same thing, but this time symlink the input directory.
    output_name = next_output_name()
    t = vine.Task(f"cd my_dir && ./{exec_name} {input_name} 2>&1 > {output_name}")
    in_dir = q.declare_file(test_dir, cache=True)
    t.add_input(exec_file, exec_name)
    t.add_input(in_dir, "my_dir", mount_symlink=True)
    output_file = q.declare_file(path.join(test_dir, output_name), cache=False)
    t.add_output(output_file, path.join("my_dir", output_name))

    q.submit(t)
    t = q.wait(wait_time)
    report_task(t, "success", 0, [path.join(test_dir, output_name)])
    
    # we bring back the outputs from a directory:
    output_name = next_output_name()
    t = vine.Task(f"mkdir outs && ./{exec_name} {input_name} 2>&1 > outs/{output_name}")
    t.add_input(exec_file, exec_name)
    t.add_input(input_file, input_name)
    outs = q.declare_file(path.join(test_dir, "outs"), cache=False)
    t.add_output(outs, "outs")

    q.submit(t)
    t = q.wait(wait_time)
    report_task(t, "success", 0, [path.join(test_dir, "outs", output_name)])

    # Execute a task that only communicates through buffers:
    inbuf = q.declare_buffer(bytes("This is only a test!", "utf-8"))
    outbuf1 = q.declare_buffer()
    outbuf2 = q.declare_buffer()

    t = vine.Task("cp input.txt output1.txt && cp input.txt output2.txt")
    t.add_input(inbuf, "input.txt")
    t.add_output(outbuf1, "output1.txt")
    t.add_output(outbuf2, "output2.txt")
    q.submit(t)
    t = q.wait(wait_time)
    report_task(t, "success", 0)

    if outbuf1.contents() != inbuf.contents() or outbuf2.contents() != inbuf.contents():
        print("incorrect output:\nout1: {}\nout2: {}\n".format(outbuf1.contents(), outbuf2.contents()))
        sys.exit(1)
    else:
        print("buffer outputs match: {}".format(inbuf.contents()))

    # should fail because the 'executable' cannot be executed:
    t = vine.Task(f"./{input_name}".format(input=input_name))
    t.add_input(input_file, input_name)

    q.submit(t)
    t = q.wait(wait_time)
    report_task(t, "success", 126)

    # should fail because the 'executable' cannot be found:
    t = vine.Task("./notacommand")

    q.submit(t)
    t = q.wait(wait_time)
    report_task(t, "success", 127)

    # should fail because an input file does not exists:
    t = vine.Task("./notacommand")
    t.add_input(q.declare_file("notacommand"), "notacommand")

    q.submit(t)
    t = q.wait(wait_time)
    report_task(t, "input missing", -1)

    # should fail because an output file was not created:
    output_name = next_output_name()
    t = vine.Task(f"./{exec_name} {input_name} 2>&1")
    t.add_input(exec_file, exec_name)
    t.add_input(input_file, input_name)
    output_file = q.declare_file(path.join(test_dir, output_name), cache=False)
    t.add_output(output_file, output_name)

    q.submit(t)
    t = q.wait(wait_time)
    report_task(t, "output missing", 0)

    # should succeed in the alloted time
    t = vine.Task("/bin/sleep 1")
    t.set_time_max(10)
    q.submit(t)
    t = q.wait(wait_time)
    report_task(t, "success", 0)

    # should fail in the alloted time
    t = vine.Task("/bin/sleep 100")
    t.set_time_max(1)
    q.submit(t)
    t = q.wait(wait_time)
    report_task(t, "max wall time", 9)

    # should run in the alloted absolute time
    t = vine.Task("/bin/sleep 1")
    t.set_time_end((time.time() + 5) * 1e6)
    q.submit(t)
    t = q.wait(wait_time)
    report_task(t, "success", 0)

    # should fail in the alloted absolute time
    t = vine.Task("/bin/sleep 10")
    t.set_time_end((time.time() + 2) * 1e6)
    q.submit(t)
    t = q.wait(30)
    report_task(t, "max end time", 9)

    # Pull down data from a url and unpack it via a minitask.
    # Note that we use a local file url of a small tarball to test the mechanism without placing a load on the network.
    f = q.declare_untar(q.declare_url("file://{}/dummy.tar.gz".format(os.getcwd())))
    t = vine.Task("ls -lR cctools | wc -l")
    t.add_input(f, "cctools")
    q.submit(t)
    t = q.wait(wait_time)
    report_task(t, "success", 0)

    # Create an explicit minitask description to run curl
    minitask = vine.Task("curl https://www.nd.edu -o output")
    infile = q.declare_minitask(minitask,"output")
    infile.set_mode(0o600)

    # Now generate an input file from a shell command:
    t = vine.Task("wc -l infile")
    t.add_input(infile, "infile")
    q.submit(t)
    t = q.wait(wait_time)
    report_task(t, "success", 0)

    # second time should have it cached (though we can't tell from here)
    t = vine.Task("wc -l infile")
    t.add_input(infile, "infile")
    q.submit(t)
    t = q.wait(wait_time)
    report_task(t, "success", 0)

    # Now generate an input file from a shell command:
    t = vine.Task("wc -l infile")
    url = q.declare_url("https://www.nd.edu", cache=True)
    t.add_input(url, "infile")
    q.submit(t)
    t = q.wait(wait_time)
    report_task(t, "success", 0)

    # second time should have it cached (though we can't tell from here)
    t = vine.Task("wc -l infile")
    t.add_input(url, "infile")
    q.submit(t)
    t = q.wait(wait_time)
    report_task(t, "success", 0)

    # generate an invalid remote input file, should get an input missing error.
    t = vine.Task("wc -l infile_for_forsaken")
    t.set_max_forsaken(1)
    url = q.declare_url("https://pretty-sure-this-is-not-a-valid-url.com")
    t.add_input(url, "infile_for_forsaken")
    q.submit(t)
    t = q.wait(wait_time)
    report_task(t, "forsaken", -1)

    # create a temporary output file, and then fetch its contents manually.
    t = vine.Task("echo howdy > output")
    temp = q.declare_temp()
    t.add_output(temp,"output")
    q.submit(t)
    t = q.wait(wait_time)
    report_task(t, "success", 0)

    got_file = False
    for i in range(3):
        data = q.fetch_file(temp)
        if data == "howdy\n":
            print("correct data returned from temp file")
            got_file = True
            break
        else:
            print("INCORRECT data returned from temp file: {}".format(data))
            print("Trying again...")
            time.sleep(1)
    if not got_file:
        error = True

    if error:
        sys.exit(1)

    sys.exit(0)
# vim: set sts=4 sw=4 ts=4 expandtab ft=python: