main.lua

Thu, 18 Feb 2016 19:00:01 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Thu, 18 Feb 2016 19:00:01 +0000
changeset 80
6676218cc481
parent 78
54bb54fe9ed2
child 81
0ee626030acb
permissions
-rwxr-xr-x

main.lua: Add per-action timeout

54
09a754d5fcc1 main: Change shebang to LuaJIT
Kim Alvefur <zash@zash.se>
parents: 46
diff changeset
1 #!/usr/bin/env luajit
21
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
2
57
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
3 local json = require "cjson";
61
21871fb2db99 main: Switch to socket.gettime for higher accuracy timestamps in machine-readable logs
Matthew Wild <mwild1@gmail.com>
parents: 59
diff changeset
4 local time = require "socket".gettime;
67
a2a9dd606200 main: Add delay when reading server logs
Matthew Wild <mwild1@gmail.com>
parents: 65
diff changeset
5 local sleep = require "socket".sleep;
57
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
6
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
7 local result_log_filename = nil;
62
52a24deb0cc3 main: Support for --server-log/-s option, which reads the server's log file during a test and includes it in the JSON log
Matthew Wild <mwild1@gmail.com>
parents: 61
diff changeset
8 local server_log_reader = nil;
69
1de0ebd8832f main.lua: Support tagging metadata for test runs, and include it in JSON output
Matthew Wild <mwild1@gmail.com>
parents: 68
diff changeset
9 local test_metadata = {};
67
a2a9dd606200 main: Add delay when reading server logs
Matthew Wild <mwild1@gmail.com>
parents: 65
diff changeset
10 local server_log_wait_time = 0.2;
76
fecc1af937be main.lua: Add --skip-server-startup-log to discard server log output at startup
Matthew Wild <mwild1@gmail.com>
parents: 71
diff changeset
11 local skip_server_startup_log = false;
80
6676218cc481 main.lua: Add per-action timeout
Matthew Wild <mwild1@gmail.com>
parents: 78
diff changeset
12 local action_timeout = 1;
67
a2a9dd606200 main: Add delay when reading server logs
Matthew Wild <mwild1@gmail.com>
parents: 65
diff changeset
13
56
5eda634ea15b main.lua: Command-line processing, and a way to set connect_host/connect_port from the command-line
Matthew Wild <mwild1@gmail.com>
parents: 55
diff changeset
14 local property_rules = {};
5eda634ea15b main.lua: Command-line processing, and a way to set connect_host/connect_port from the command-line
Matthew Wild <mwild1@gmail.com>
parents: 55
diff changeset
15
5eda634ea15b main.lua: Command-line processing, and a way to set connect_host/connect_port from the command-line
Matthew Wild <mwild1@gmail.com>
parents: 55
diff changeset
16 local function apply_object_properties(class, name, object)
5eda634ea15b main.lua: Command-line processing, and a way to set connect_host/connect_port from the command-line
Matthew Wild <mwild1@gmail.com>
parents: 55
diff changeset
17 for _, rule in ipairs(property_rules) do
5eda634ea15b main.lua: Command-line processing, and a way to set connect_host/connect_port from the command-line
Matthew Wild <mwild1@gmail.com>
parents: 55
diff changeset
18 if (not(rule.class) or tostring(rule.class):lower() == tostring(class):lower())
5eda634ea15b main.lua: Command-line processing, and a way to set connect_host/connect_port from the command-line
Matthew Wild <mwild1@gmail.com>
parents: 55
diff changeset
19 and (not(rule.name) or tostring(rule.name):lower() == tostring(name):lower()) then
5eda634ea15b main.lua: Command-line processing, and a way to set connect_host/connect_port from the command-line
Matthew Wild <mwild1@gmail.com>
parents: 55
diff changeset
20 for prop_key, prop_value in pairs(rule.properties) do
5eda634ea15b main.lua: Command-line processing, and a way to set connect_host/connect_port from the command-line
Matthew Wild <mwild1@gmail.com>
parents: 55
diff changeset
21 object[prop_key] = prop_value;
5eda634ea15b main.lua: Command-line processing, and a way to set connect_host/connect_port from the command-line
Matthew Wild <mwild1@gmail.com>
parents: 55
diff changeset
22 end
5eda634ea15b main.lua: Command-line processing, and a way to set connect_host/connect_port from the command-line
Matthew Wild <mwild1@gmail.com>
parents: 55
diff changeset
23 end
5eda634ea15b main.lua: Command-line processing, and a way to set connect_host/connect_port from the command-line
Matthew Wild <mwild1@gmail.com>
parents: 55
diff changeset
24 end
5eda634ea15b main.lua: Command-line processing, and a way to set connect_host/connect_port from the command-line
Matthew Wild <mwild1@gmail.com>
parents: 55
diff changeset
25 end
5eda634ea15b main.lua: Command-line processing, and a way to set connect_host/connect_port from the command-line
Matthew Wild <mwild1@gmail.com>
parents: 55
diff changeset
26
5eda634ea15b main.lua: Command-line processing, and a way to set connect_host/connect_port from the command-line
Matthew Wild <mwild1@gmail.com>
parents: 55
diff changeset
27 function process_options()
5eda634ea15b main.lua: Command-line processing, and a way to set connect_host/connect_port from the command-line
Matthew Wild <mwild1@gmail.com>
parents: 55
diff changeset
28 local function get_value()
5eda634ea15b main.lua: Command-line processing, and a way to set connect_host/connect_port from the command-line
Matthew Wild <mwild1@gmail.com>
parents: 55
diff changeset
29 return (assert(table.remove(arg, 1), "unexpected end of command-line options"));
5eda634ea15b main.lua: Command-line processing, and a way to set connect_host/connect_port from the command-line
Matthew Wild <mwild1@gmail.com>
parents: 55
diff changeset
30 end
5eda634ea15b main.lua: Command-line processing, and a way to set connect_host/connect_port from the command-line
Matthew Wild <mwild1@gmail.com>
parents: 55
diff changeset
31 while arg[1] and arg[1]:sub(1,1) == "-" do
5eda634ea15b main.lua: Command-line processing, and a way to set connect_host/connect_port from the command-line
Matthew Wild <mwild1@gmail.com>
parents: 55
diff changeset
32 local opt = arg[1];
5eda634ea15b main.lua: Command-line processing, and a way to set connect_host/connect_port from the command-line
Matthew Wild <mwild1@gmail.com>
parents: 55
diff changeset
33 if opt == "--" then
5eda634ea15b main.lua: Command-line processing, and a way to set connect_host/connect_port from the command-line
Matthew Wild <mwild1@gmail.com>
parents: 55
diff changeset
34 table.remove(arg, 1);
5eda634ea15b main.lua: Command-line processing, and a way to set connect_host/connect_port from the command-line
Matthew Wild <mwild1@gmail.com>
parents: 55
diff changeset
35 break;
5eda634ea15b main.lua: Command-line processing, and a way to set connect_host/connect_port from the command-line
Matthew Wild <mwild1@gmail.com>
parents: 55
diff changeset
36 end
5eda634ea15b main.lua: Command-line processing, and a way to set connect_host/connect_port from the command-line
Matthew Wild <mwild1@gmail.com>
parents: 55
diff changeset
37 table.remove(arg, 1);
5eda634ea15b main.lua: Command-line processing, and a way to set connect_host/connect_port from the command-line
Matthew Wild <mwild1@gmail.com>
parents: 55
diff changeset
38 if opt == "--port" or opt == "-p" then
5eda634ea15b main.lua: Command-line processing, and a way to set connect_host/connect_port from the command-line
Matthew Wild <mwild1@gmail.com>
parents: 55
diff changeset
39 local port = assert(tonumber(get_value()), "port number must be a number");
5eda634ea15b main.lua: Command-line processing, and a way to set connect_host/connect_port from the command-line
Matthew Wild <mwild1@gmail.com>
parents: 55
diff changeset
40 table.insert(property_rules, { class = "client", properties = { connect_port = port } });
5eda634ea15b main.lua: Command-line processing, and a way to set connect_host/connect_port from the command-line
Matthew Wild <mwild1@gmail.com>
parents: 55
diff changeset
41 elseif opt == "--host" or opt == "-h" then
5eda634ea15b main.lua: Command-line processing, and a way to set connect_host/connect_port from the command-line
Matthew Wild <mwild1@gmail.com>
parents: 55
diff changeset
42 local host = get_value();
5eda634ea15b main.lua: Command-line processing, and a way to set connect_host/connect_port from the command-line
Matthew Wild <mwild1@gmail.com>
parents: 55
diff changeset
43 table.insert(property_rules, { class = "client", properties = { connect_host = host } });
57
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
44 elseif opt == "--out" or opt == "-o" then
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
45 result_log_filename = get_value();
62
52a24deb0cc3 main: Support for --server-log/-s option, which reads the server's log file during a test and includes it in the JSON log
Matthew Wild <mwild1@gmail.com>
parents: 61
diff changeset
46 elseif opt == "--server-log" or opt == "-s" then
52a24deb0cc3 main: Support for --server-log/-s option, which reads the server's log file during a test and includes it in the JSON log
Matthew Wild <mwild1@gmail.com>
parents: 61
diff changeset
47 local server_log = assert(io.open(get_value(), "r"));
52a24deb0cc3 main: Support for --server-log/-s option, which reads the server's log file during a test and includes it in the JSON log
Matthew Wild <mwild1@gmail.com>
parents: 61
diff changeset
48 function server_log_reader()
52a24deb0cc3 main: Support for --server-log/-s option, which reads the server's log file during a test and includes it in the JSON log
Matthew Wild <mwild1@gmail.com>
parents: 61
diff changeset
49 local new_lines = {};
67
a2a9dd606200 main: Add delay when reading server logs
Matthew Wild <mwild1@gmail.com>
parents: 65
diff changeset
50 local last_line_time = time();
a2a9dd606200 main: Add delay when reading server logs
Matthew Wild <mwild1@gmail.com>
parents: 65
diff changeset
51 while time() - last_line_time < server_log_wait_time do
a2a9dd606200 main: Add delay when reading server logs
Matthew Wild <mwild1@gmail.com>
parents: 65
diff changeset
52 sleep(0.05);
a2a9dd606200 main: Add delay when reading server logs
Matthew Wild <mwild1@gmail.com>
parents: 65
diff changeset
53 for line in server_log:lines() do
a2a9dd606200 main: Add delay when reading server logs
Matthew Wild <mwild1@gmail.com>
parents: 65
diff changeset
54 table.insert(new_lines, line);
a2a9dd606200 main: Add delay when reading server logs
Matthew Wild <mwild1@gmail.com>
parents: 65
diff changeset
55 last_line_time = time();
a2a9dd606200 main: Add delay when reading server logs
Matthew Wild <mwild1@gmail.com>
parents: 65
diff changeset
56 end
62
52a24deb0cc3 main: Support for --server-log/-s option, which reads the server's log file during a test and includes it in the JSON log
Matthew Wild <mwild1@gmail.com>
parents: 61
diff changeset
57 end
52a24deb0cc3 main: Support for --server-log/-s option, which reads the server's log file during a test and includes it in the JSON log
Matthew Wild <mwild1@gmail.com>
parents: 61
diff changeset
58 return new_lines;
52a24deb0cc3 main: Support for --server-log/-s option, which reads the server's log file during a test and includes it in the JSON log
Matthew Wild <mwild1@gmail.com>
parents: 61
diff changeset
59 end
76
fecc1af937be main.lua: Add --skip-server-startup-log to discard server log output at startup
Matthew Wild <mwild1@gmail.com>
parents: 71
diff changeset
60 elseif opt == "--skip-server-startup-log" then
fecc1af937be main.lua: Add --skip-server-startup-log to discard server log output at startup
Matthew Wild <mwild1@gmail.com>
parents: 71
diff changeset
61 skip_server_startup_log = true;
69
1de0ebd8832f main.lua: Support tagging metadata for test runs, and include it in JSON output
Matthew Wild <mwild1@gmail.com>
parents: 68
diff changeset
62 elseif opt == "--tag" then
1de0ebd8832f main.lua: Support tagging metadata for test runs, and include it in JSON output
Matthew Wild <mwild1@gmail.com>
parents: 68
diff changeset
63 local tag = get_value();
1de0ebd8832f main.lua: Support tagging metadata for test runs, and include it in JSON output
Matthew Wild <mwild1@gmail.com>
parents: 68
diff changeset
64 local key, value = tag:match("^([^=]+):(.+)$");
1de0ebd8832f main.lua: Support tagging metadata for test runs, and include it in JSON output
Matthew Wild <mwild1@gmail.com>
parents: 68
diff changeset
65 if key and value then
1de0ebd8832f main.lua: Support tagging metadata for test runs, and include it in JSON output
Matthew Wild <mwild1@gmail.com>
parents: 68
diff changeset
66 test_metadata[key] = value;
1de0ebd8832f main.lua: Support tagging metadata for test runs, and include it in JSON output
Matthew Wild <mwild1@gmail.com>
parents: 68
diff changeset
67 else
1de0ebd8832f main.lua: Support tagging metadata for test runs, and include it in JSON output
Matthew Wild <mwild1@gmail.com>
parents: 68
diff changeset
68 error("Unable to parse tag: "..tag);
1de0ebd8832f main.lua: Support tagging metadata for test runs, and include it in JSON output
Matthew Wild <mwild1@gmail.com>
parents: 68
diff changeset
69 end
58
8fa0a464c727 main.lua: Error on unknown command-line options
Matthew Wild <mwild1@gmail.com>
parents: 57
diff changeset
70 else
8fa0a464c727 main.lua: Error on unknown command-line options
Matthew Wild <mwild1@gmail.com>
parents: 57
diff changeset
71 error("Unhandled command-line option: "..opt);
56
5eda634ea15b main.lua: Command-line processing, and a way to set connect_host/connect_port from the command-line
Matthew Wild <mwild1@gmail.com>
parents: 55
diff changeset
72 end
5eda634ea15b main.lua: Command-line processing, and a way to set connect_host/connect_port from the command-line
Matthew Wild <mwild1@gmail.com>
parents: 55
diff changeset
73 end
5eda634ea15b main.lua: Command-line processing, and a way to set connect_host/connect_port from the command-line
Matthew Wild <mwild1@gmail.com>
parents: 55
diff changeset
74 assert(#arg > 0, "No test script provided");
5eda634ea15b main.lua: Command-line processing, and a way to set connect_host/connect_port from the command-line
Matthew Wild <mwild1@gmail.com>
parents: 55
diff changeset
75 assert(#arg < 2, "Too many parameters");
69
1de0ebd8832f main.lua: Support tagging metadata for test runs, and include it in JSON output
Matthew Wild <mwild1@gmail.com>
parents: 68
diff changeset
76 if not test_metadata["Name"] then
1de0ebd8832f main.lua: Support tagging metadata for test runs, and include it in JSON output
Matthew Wild <mwild1@gmail.com>
parents: 68
diff changeset
77 test_metadata["Name"] = arg[1];
1de0ebd8832f main.lua: Support tagging metadata for test runs, and include it in JSON output
Matthew Wild <mwild1@gmail.com>
parents: 68
diff changeset
78 end
56
5eda634ea15b main.lua: Command-line processing, and a way to set connect_host/connect_port from the command-line
Matthew Wild <mwild1@gmail.com>
parents: 55
diff changeset
79 end
0
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80
21
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
81 function read_script()
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
82 io.input(arg[1]);
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
83 return io.read("*a");
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
84
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
85 end
0
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86
21
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
87 function parse_script(data)
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
88 local parser = require "scansion.parser";
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
89 return assert(parser.parse(data));
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
90 end
0
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91
21
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
92 function initialize_script(script)
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
93 local c = 0;
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
94 for name, object in pairs(script.objects) do
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
95 local o = require("scansion.objects."..object.type);
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
96 object.handler = o;
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
97 object.script = script;
56
5eda634ea15b main.lua: Command-line processing, and a way to set connect_host/connect_port from the command-line
Matthew Wild <mwild1@gmail.com>
parents: 55
diff changeset
98 apply_object_properties(object.type, object.name, object);
21
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
99 o._validate(object);
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
100 c = c + 1;
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
101 end
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
102
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
103 --print("Script defines "..c.." objects, and "..#script.actions.." actions");
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
104 return script;
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
105 end
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
106
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
107 function initialize_verse(errcb)
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
108 local verse = require "verse";
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
109
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
110 verse.set_log_handler(verse._default_log_handler, { "debug", "info", "warn", "error" });
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
111
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
112 local function error_handler(err)
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
113 verse.log("error", "Error: %s", err);
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
114 verse.log("error", "Traceback: %s", debug.traceback());
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
115 errcb(err);
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
116 end
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
117 verse.set_error_handler(error_handler);
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
118 return verse;
0
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119 end
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120
57
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
121 function main(log_data)
56
5eda634ea15b main.lua: Command-line processing, and a way to set connect_host/connect_port from the command-line
Matthew Wild <mwild1@gmail.com>
parents: 55
diff changeset
122
21
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
123 local ok, err = true;
0
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124
21
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
125 local script = initialize_script(parse_script(read_script()));
71
f7dbb46b5770 main.lua: Log script title and summary
Matthew Wild <mwild1@gmail.com>
parents: 69
diff changeset
126
78
54bb54fe9ed2 main, scansion.parser: Allow scripts to include tags in comments at the beginning, which are included in JSON output
Matthew Wild <mwild1@gmail.com>
parents: 76
diff changeset
127 log_data("script", { title = script.title, summary = script.summary, tags = script.tags });
71
f7dbb46b5770 main.lua: Log script title and summary
Matthew Wild <mwild1@gmail.com>
parents: 69
diff changeset
128
62
52a24deb0cc3 main: Support for --server-log/-s option, which reads the server's log file during a test and includes it in the JSON log
Matthew Wild <mwild1@gmail.com>
parents: 61
diff changeset
129 if server_log_reader then
76
fecc1af937be main.lua: Add --skip-server-startup-log to discard server log output at startup
Matthew Wild <mwild1@gmail.com>
parents: 71
diff changeset
130 if skip_server_startup_log then
fecc1af937be main.lua: Add --skip-server-startup-log to discard server log output at startup
Matthew Wild <mwild1@gmail.com>
parents: 71
diff changeset
131 server_log_reader();
fecc1af937be main.lua: Add --skip-server-startup-log to discard server log output at startup
Matthew Wild <mwild1@gmail.com>
parents: 71
diff changeset
132 else
fecc1af937be main.lua: Add --skip-server-startup-log to discard server log output at startup
Matthew Wild <mwild1@gmail.com>
parents: 71
diff changeset
133 log_data("server", { lines = server_log_reader() });
fecc1af937be main.lua: Add --skip-server-startup-log to discard server log output at startup
Matthew Wild <mwild1@gmail.com>
parents: 71
diff changeset
134 end
62
52a24deb0cc3 main: Support for --server-log/-s option, which reads the server's log file during a test and includes it in the JSON log
Matthew Wild <mwild1@gmail.com>
parents: 61
diff changeset
135 end
52a24deb0cc3 main: Support for --server-log/-s option, which reads the server's log file during a test and includes it in the JSON log
Matthew Wild <mwild1@gmail.com>
parents: 61
diff changeset
136
21
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
137 local verse = initialize_verse(function (_err) ok, err = false, _err end);
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
138 local async = require "scansion.async";
7
ecac723bb6e1 main: Run actions in async runner
Matthew Wild <mwild1@gmail.com>
parents: 3
diff changeset
139
21
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
140 local runner = async.runner(function (d)
80
6676218cc481 main.lua: Add per-action timeout
Matthew Wild <mwild1@gmail.com>
parents: 78
diff changeset
141 for i, action in ipairs(script.actions) do
21
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
142 local object = script.objects[action.object_name];
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
143 local handler = object.handler;
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
144 assert(handler[action.action], "Objects of type '"..object.type.."' do not support action '"..action.action.."'");
55
996f30d43a88 main.lua: Print blank line between actions for easier reading
Matthew Wild <mwild1@gmail.com>
parents: 54
diff changeset
145 print("");
68
cd63f764d1bf main.lua: Restructure server log reading
Matthew Wild <mwild1@gmail.com>
parents: 67
diff changeset
146 if server_log_reader then
cd63f764d1bf main.lua: Restructure server log reading
Matthew Wild <mwild1@gmail.com>
parents: 67
diff changeset
147 log_data("server", { lines = server_log_reader() });
cd63f764d1bf main.lua: Restructure server log reading
Matthew Wild <mwild1@gmail.com>
parents: 67
diff changeset
148 end
57
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
149 log_data("action", {
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
150 action = action.action;
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
151 object = object.name;
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
152 object_type = object.type;
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
153 extra = action.extra;
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
154 annotation = action.annotation;
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
155 });
43
b37504fa3031 Add annotations to actions (by grabbing the preceding comment)
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
156 if action.annotation then
b37504fa3031 Add annotations to actions (by grabbing the preceding comment)
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
157 print(action.annotation);
b37504fa3031 Add annotations to actions (by grabbing the preceding comment)
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
158 end
68
cd63f764d1bf main.lua: Restructure server log reading
Matthew Wild <mwild1@gmail.com>
parents: 67
diff changeset
159 print(object.name, action.action.."...");
80
6676218cc481 main.lua: Add per-action timeout
Matthew Wild <mwild1@gmail.com>
parents: 78
diff changeset
160 if action_timeout and action_timeout > 0 then
6676218cc481 main.lua: Add per-action timeout
Matthew Wild <mwild1@gmail.com>
parents: 78
diff changeset
161 local action_number = i;
6676218cc481 main.lua: Add per-action timeout
Matthew Wild <mwild1@gmail.com>
parents: 78
diff changeset
162 verse.add_task(action_timeout, function ()
6676218cc481 main.lua: Add per-action timeout
Matthew Wild <mwild1@gmail.com>
parents: 78
diff changeset
163 if i == action_number then
6676218cc481 main.lua: Add per-action timeout
Matthew Wild <mwild1@gmail.com>
parents: 78
diff changeset
164 error("Timeout waiting for response from server");
6676218cc481 main.lua: Add per-action timeout
Matthew Wild <mwild1@gmail.com>
parents: 78
diff changeset
165 end
6676218cc481 main.lua: Add per-action timeout
Matthew Wild <mwild1@gmail.com>
parents: 78
diff changeset
166 end);
6676218cc481 main.lua: Add per-action timeout
Matthew Wild <mwild1@gmail.com>
parents: 78
diff changeset
167 end
68
cd63f764d1bf main.lua: Restructure server log reading
Matthew Wild <mwild1@gmail.com>
parents: 67
diff changeset
168 local ok, err = pcall(handler[action.action], object, action.extra);
62
52a24deb0cc3 main: Support for --server-log/-s option, which reads the server's log file during a test and includes it in the JSON log
Matthew Wild <mwild1@gmail.com>
parents: 61
diff changeset
169 if server_log_reader then
52a24deb0cc3 main: Support for --server-log/-s option, which reads the server's log file during a test and includes it in the JSON log
Matthew Wild <mwild1@gmail.com>
parents: 61
diff changeset
170 log_data("server", { lines = server_log_reader() });
52a24deb0cc3 main: Support for --server-log/-s option, which reads the server's log file during a test and includes it in the JSON log
Matthew Wild <mwild1@gmail.com>
parents: 61
diff changeset
171 end
21
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
172 if not ok then
57
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
173 log_data("error", { message = err });
21
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
174 error(err);
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
175 end
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
176 end
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
177 verse.log("info", "Completed script!");
30
3c2489e79074 main: Remove 5s delay on quitting (was a hack for debug purposes)
Matthew Wild <mwild1@gmail.com>
parents: 21
diff changeset
178 verse.quit();
21
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
179 end, {
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
180 error = function (runner, _err)
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
181 verse.log("error", "Runner caught error: %s", _err);
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
182 ok, err = false, _err;
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
183 verse.quit();
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
184 end;
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
185 });
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
186
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
187 runner:run(true);
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
188 verse.log("debug", "runner paused")
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
189
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
190 verse.loop();
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
191 return ok, err;
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
192 end
7
ecac723bb6e1 main: Run actions in async runner
Matthew Wild <mwild1@gmail.com>
parents: 3
diff changeset
193
57
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
194 -- Process command-line options
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
195 process_options();
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
196
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
197 -- Dummy logging function, used if no log file set
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
198 local log_data = function () end;
3
3cc860a893d2 main: verse.loop()
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
199
57
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
200 local result_log;
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
201 if result_log_filename then
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
202 result_log = assert(io.open(result_log_filename, "w+"));
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
203 result_log:write("[\n");
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
204 function log_data(type, data)
61
21871fb2db99 main: Switch to socket.gettime for higher accuracy timestamps in machine-readable logs
Matthew Wild <mwild1@gmail.com>
parents: 59
diff changeset
205 local entry = { type = type, data = data, time = time() };
57
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
206 result_log:write(" ", json.encode(entry), ",\n");
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
207 result_log:flush();
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
208 end
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
209 end
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
210
69
1de0ebd8832f main.lua: Support tagging metadata for test runs, and include it in JSON output
Matthew Wild <mwild1@gmail.com>
parents: 68
diff changeset
211 log_data("start", { metadata = test_metadata });
57
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
212 local ok, result, err = pcall(main, log_data);
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
213
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
214 local exit_code = 0;
21
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
215 if not ok then
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
216 print("TEST ERROR:", result);
57
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
217 exit_code = 2;
65
629162fc0681 main: Change JSON log format for clarity, error may be more than just a simple message now
Matthew Wild <mwild1@gmail.com>
parents: 63
diff changeset
218 log_data("test-error", { error = result });
21
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
219 elseif not result then
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
220 print("TEST FAILED", err);
57
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
221 exit_code = 1;
65
629162fc0681 main: Change JSON log format for clarity, error may be more than just a simple message now
Matthew Wild <mwild1@gmail.com>
parents: 63
diff changeset
222 log_data("test-failed", { error = err });
21
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
223 else
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
224 print("TEST PASSED");
57
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
225 log_data("test-passed");
21
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
226 end
57
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
227
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
228 if result_log then
63
0f19ffbe1519 main: Separate logging for the last event to ensure proper JSON syntax
Matthew Wild <mwild1@gmail.com>
parents: 62
diff changeset
229 result_log:write([[ {"type": "end", "time": ]]..time().."}\n]\n");
57
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
230 result_log:close();
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
231 end
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
232
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
233 os.exit(exit_code);

mercurial