summaryrefslogtreecommitdiff
path: root/lib/get-test-job.c
blob: a5245b7d6ce73f6a6b8cf03357dfa98f638cca71 (plain)
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#define _GNU_SOURCE

#include <ctype.h>
#include <getopt.h>
#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>

#define HAVE_STATEMENT_EXPR 1
#include "darray/darray.h"

static char *outdir = NULL;
static char *branches_to_test = NULL;
static bool verbose = false;

#define die(msg, ...)					\
do {							\
	fprintf(stderr, msg "\n", ##__VA_ARGS__);	\
	exit(EXIT_FAILURE);				\
} while (0)

static char *mprintf(const char *fmt, ...)
{
	va_list args;
	char *str;
	int ret;

	va_start(args, fmt);
	ret = vasprintf(&str, fmt, args);
	va_end(args);

	if (ret < 0)
		die("insufficient memory");

	return str;
}

static void strim(char *line)
{
	char *p = line;

	while (isalnum(*p))
		p++;
	*p = 0;
}

static char *test_basename(const char *str)
{
	char *p = strrchr(str, '/');
	char *ret = strdup(p ? p + 1 : str);

	p = strstr(ret, ".ktest");
	if (p)
		*p = 0;
	return ret;
}

typedef darray(char *) strings;

static void strings_free(strings *strs)
{
	char **s;

	darray_foreach(s, *strs)
		free(*s);
	darray_free(*strs);

	memset(strs, 0, sizeof(*strs));
}

typedef struct {
	char		*branch;
	char		*commit;
	unsigned	age;
	char		*test;
	strings		subtests;
} test_job;

static void test_job_free(test_job *job)
{
	free(job->branch);
	free(job->commit);
	free(job->test);
	strings_free(&job->subtests);
	memset(job, 0, sizeof(*job));
}

static void test_job_print(test_job job)
{
	fprintf(stderr, "%s %s %s age %u subtests",
		job.branch, job.commit, job.test, job.age);

	char **subtest;
	darray_foreach(subtest, job.subtests)
		fprintf(stderr, " %s", *subtest);
	fprintf(stderr, "\n");
}

static strings get_subtests(char *test_path)
{
	darray_char output;
	strings ret;
	size_t bytes_read;

	darray_init(output);
	darray_init(ret);

	if (verbose)
		fprintf(stderr, "Getting subtests for %s\n", test_path);

	char *cmd = mprintf("%s list-tests", test_path);
	FILE *f = popen(cmd, "r");
	free(cmd);

	if (!f)
		die("error executing %s", test_path);

	do {
		darray_make_room(output, 4096);

		bytes_read = fread(output.item + output.size,
				   1, 4095, f);
		output.size += bytes_read;
	} while (bytes_read);

	pclose(f);

	output.item[output.size] = '\0';

	char *subtest, *p = output.item;
	while ((subtest = strtok(p, " \t\n"))) {
		darray_push(ret, strdup(subtest));
		p = NULL;
	}

	darray_free(output);

	if (darray_empty(ret))
		die("error getting subtests from %s", test_path);

	return ret;
}

static char *slashes_to_dots(const char *str)
{
	char *p, *ret = strdup(str);

	while ((p = strchr(ret, '/')))
		*p = '.';

	return ret;
}

static bool __lockfile_exists(const char *commitdir,
			      const char *testdir,
			      const char *lockfile,
			      bool create)
{
	if (!create) {
		return access(lockfile, F_OK) == 0;
	} else {
		bool exists;

		if (mkdir(commitdir, 0755) < 0 && errno != EEXIST)
			die("error creating %s", commitdir);

		if (mkdir(testdir, 0755) < 0 && errno != EEXIST)
			die("error creating %s", testdir);

		int fd = open(lockfile, O_RDWR|O_CREAT|O_EXCL, 0644);
		exists = fd < 0;
		if (!exists)
			close(fd);

		return exists;
	}
}

static bool lockfile_exists(const char *commit,
			    const char *test_path,
			    const char *subtest,
			    bool create)
{
	char *test_name = test_basename(test_path);
	char *subtest_mangled = slashes_to_dots(subtest);
	char *commitdir = mprintf("%s/%s", outdir, commit);
	char *testdir = mprintf("%s/%s.%s", commitdir, test_name, subtest_mangled);
	char *lockfile = mprintf("%s/status", testdir);
	struct timeval now;
	struct stat statbuf;
	bool exists;

	gettimeofday(&now, NULL);

	exists = __lockfile_exists(commitdir, testdir, lockfile, create);

	if (exists &&
	    !stat(lockfile, &statbuf) &&
	    !statbuf.st_size &&
	    S_ISREG(statbuf.st_mode) &&
	    statbuf.st_ctime + 60 * 60 < now.tv_sec &&
	    !unlink(lockfile)) {
		fprintf(stderr, "Deleting stale test job %s %s %s (%lu minutes old)\n",
			commit, test_name, subtest,
			(now.tv_sec - statbuf.st_ctime) / 60);
		exists = false;
	}

	free(lockfile);
	free(testdir);
	free(commitdir);
	free(subtest_mangled);
	free(test_name);

	return exists;
}

static test_job branch_get_next_test_job(char *branch,
					 char *test_path,
					 strings subtests)
{
	char *cmd = mprintf("git log --pretty=format:%H %s", branch);
	FILE *commits = popen(cmd, "r");
	char *commit = NULL;
	size_t n = 0;
	ssize_t len;
	test_job ret;

	memset(&ret, 0, sizeof(ret));

	while ((len = getline(&commit, &n, commits)) >= 0) {
		strim(commit);

		char **subtest;
		darray_foreach(subtest, subtests)
			if (!lockfile_exists(commit, test_path, *subtest, false)) {
				darray_push(ret.subtests, strdup(*subtest));
				if (darray_size(ret.subtests) > 20)
					break;
			}

		if (!darray_empty(ret.subtests)) {
			ret.branch	= strdup(branch);
			ret.commit	= strdup(commit);
			ret.test	= strdup(test_path);
			goto success;
		}

		ret.age++;
	}
	fprintf(stderr, "error looking up commits on branch %s\n", branch);
success:
	pclose(commits);
	free(commit);
	free(cmd);
	return ret;
}

static test_job get_best_test_job()
{
	FILE *branches = fopen(branches_to_test, "r");
	char *line = NULL;
	size_t n = 0;
	ssize_t len;
	test_job best;

	memset(&best, 0, sizeof(best));

	if (!branches)
		die("error opening %s: %m", branches_to_test);

	while ((len = getline(&line, &n, branches)) >= 0) {
		char *branch	= strtok(line, " \t\n");
		char *test_path	= strtok(NULL, " \t\n");

		if (!branch || !test_path)
			continue;

		if (verbose)
			fprintf(stderr, "get_best_test_job: checking branch %s test %s\n",
				branch, test_path);

		strings subtests = get_subtests(test_path);

		test_job job = branch_get_next_test_job(branch, test_path, subtests);

		strings_free(&subtests);

		if (!best.branch || job.age < best.age) {
			test_job_free(&best);
			best = job;
		} else {
			test_job_free(&job);
		}
	}

	if (!best.branch)
		die("Nothing found");

	if (verbose) {
		fprintf(stderr, "get_best_test_job: best ");
		test_job_print(best);
	}

	fclose(branches);
	free(line);
	return best;
}

void usage(void)
{
	puts("get-test-job: get a test job and create lockfile\n"
	     "Usage: get-test-job [OPTIONS]\n"
	     "\n"
	     "Options\n"
	     "      -b file         List of branches to test and tests to run\n"
	     "      -o dir          Directory for tests results\n"
	     "      -v              Verbose\n"
	     "      -h              Display this help and exit");
	exit(EXIT_SUCCESS);
}

int main(int argc, char *argv[])
{
	int opt;
	test_job job;
	strings subtests;
	char **subtest;

	darray_init(subtests);
	memset(&job, 0, sizeof(job));

	while ((opt = getopt(argc, argv, "b:o:vh")) != -1) {
		switch (opt) {
		case 'b':
			branches_to_test = strdup(optarg);
			break;
		case 'o':
			outdir = strdup(optarg);
			break;
		case 'v':
			verbose = true;
			break;
		case 'h':
			usage();
			exit(EXIT_SUCCESS);
		case '?':
			usage();
			exit(EXIT_FAILURE);
		}
	}

	if (!branches_to_test || !outdir)
		die("required argument missing");

	do {
		test_job_free(&job);
		job = get_best_test_job();

		darray_free(subtests);
		darray_init(subtests);

		darray_foreach(subtest, job.subtests)
			if (!lockfile_exists(job.commit, job.test, *subtest, true))
				darray_push(subtests, *subtest);
	} while (darray_empty(subtests));

	printf("%s %s %s", job.branch, job.commit, job.test);
	darray_foreach(subtest, subtests)
		printf(" %s", *subtest);
	printf("\n");

	test_job_free(&job);
	darray_free(subtests);
	free(outdir);
	free(branches_to_test);
}