src/test-driver.c
author Kristian H?gsberg <krh@redhat.com>
Mon Jun 30 13:28:59 2008 -0400 (2008-06-30)
changeset 306 cd3954499086
parent 300 455eaa569767
child 307 95b6bcadd6c4
permissions -rw-r--r--
Get rid of razor_set_get_package().

This was always a silly little helper function, not general enough for
real world applications. Use an iterator to search through the set to
find the package of interest.
     1 /*
     2  * Copyright (C) 2008  Kristian Høgsberg <krh@redhat.com>
     3  * Copyright (C) 2008  Red Hat, Inc
     4  *
     5  * This program is free software; you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation; either version 2 of the License, or
     8  * (at your option) any later version.
     9  *
    10  * This program is distributed in the hope that it will be useful,
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  * GNU General Public License for more details.
    14  *
    15  * You should have received a copy of the GNU General Public License along
    16  * with this program; if not, write to the Free Software Foundation, Inc.,
    17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
    18  */
    19 
    20 #include <stdio.h>
    21 #include <string.h>
    22 #include <stdarg.h>
    23 #include <unistd.h>
    24 #include <fcntl.h>
    25 #include <errno.h>
    26 #include <expat.h>
    27 
    28 #include "razor.h"
    29 
    30 #define XML_BUFFER_SIZE 4096
    31 
    32 static void
    33 parse_xml_file(const char *filename,
    34 	       XML_StartElementHandler start,
    35 	       XML_EndElementHandler end,
    36 	       void *data)
    37 {
    38 	XML_Parser parser;
    39 	char *buffer;
    40 	int fd, len, err;
    41 
    42 	parser = XML_ParserCreate(NULL);
    43 	XML_SetElementHandler(parser, start, end);
    44 	XML_SetUserData(parser, data);
    45 
    46 	fd = open(filename, O_RDONLY);
    47 	if (fd < 0) {
    48 		fprintf(stderr, "failed to open %s: %m\n", filename);
    49 		exit(-1);
    50 	}
    51 
    52 	while (1) {
    53 		buffer = XML_GetBuffer(parser, XML_BUFFER_SIZE);
    54 		len = read(fd, buffer, XML_BUFFER_SIZE);
    55 		if (len == 0)
    56 			break;
    57 		err = XML_ParseBuffer(parser, len, len == 0);
    58 		if (err == XML_STATUS_ERROR) {
    59 			fprintf(stderr, "parse error at line %lu:\n%s\n",
    60 				XML_GetCurrentLineNumber(parser),
    61 				XML_ErrorString(XML_GetErrorCode(parser)));
    62 			exit(-1);
    63 		}
    64 	}
    65 
    66 	if (fd < 0) {
    67 		fprintf(stderr, "read: %m\n");
    68 		exit(-1);
    69 	}
    70 
    71 	close(fd);
    72 }
    73 
    74 struct test_context {
    75 	struct razor_set *system_set, *repo_set, *result_set;
    76 
    77 	struct razor_importer *importer;
    78 	struct razor_set **importer_set;
    79 
    80 	struct razor_transaction *trans;
    81 
    82 	char *install_pkgs[3], *remove_pkgs[3];
    83 	int n_install_pkgs, n_remove_pkgs;
    84 
    85 	int unsat;
    86 	int in_result;
    87 
    88 	int debug, errors;
    89 };
    90 
    91 static void
    92 get_atts(const char **atts, ...)
    93 {
    94 	va_list ap;
    95 	const char *name, **ptr;
    96 	int i;
    97 
    98 	va_start(ap, atts);
    99 	while (name = va_arg(ap, const char *), name != NULL) {
   100 		ptr = va_arg(ap, const char **);
   101 		*ptr = NULL;
   102 		for (i = 0; atts[i]; i += 2) {
   103 			if (strcmp(atts[i], name) == 0)
   104 				*ptr = atts[i + 1];
   105 		}
   106 	}
   107 	va_end(ap);
   108 }
   109 
   110 static enum razor_property_flags
   111 parse_relation (const char *rel_str)
   112 {
   113 	if (!rel_str)
   114 		return -1;
   115 	if (rel_str[0] == 'L')
   116 		return rel_str[1] == 'E' ? RAZOR_PROPERTY_LESS | RAZOR_PROPERTY_EQUAL : RAZOR_PROPERTY_LESS;
   117 	else if (rel_str[0] == 'G')
   118 		return rel_str[1] == 'E' ? RAZOR_PROPERTY_GREATER | RAZOR_PROPERTY_EQUAL : RAZOR_PROPERTY_GREATER;
   119 	else if (rel_str[0] == 'E' || rel_str[1] == 'Q')
   120 		return RAZOR_PROPERTY_EQUAL;
   121 	else
   122 		return -1;
   123 }
   124 
   125 static void
   126 start_test(struct test_context *ctx, const char **atts)
   127 {
   128 	const char *name = NULL;
   129 
   130 	get_atts(atts, "name", &name, NULL);
   131 	if (!name) {
   132 		fprintf(stderr, "Test with no name\n");
   133 		exit(1);
   134 	}
   135 	printf("%s\n", name);
   136 }
   137 
   138 static void
   139 end_test(struct test_context *ctx)
   140 {
   141 	if (ctx->system_set) {
   142 		razor_set_destroy(ctx->system_set);
   143 		ctx->system_set = NULL;
   144 	}
   145 	if (ctx->repo_set) {
   146 		razor_set_destroy(ctx->repo_set);
   147 		ctx->repo_set = NULL;
   148 	}
   149 	if (ctx->result_set) {
   150 		razor_set_destroy(ctx->result_set);
   151 		ctx->result_set = NULL;
   152 	}
   153 	if (ctx->trans) {
   154 		razor_transaction_destroy(ctx->trans);
   155 		ctx->trans = NULL;
   156 	}
   157 }
   158 
   159 static void
   160 start_set(struct test_context *ctx, const char **atts)
   161 {
   162 	const char *name = NULL;
   163 
   164 	ctx->importer = razor_importer_create();
   165 	get_atts(atts, "name", &name, NULL);
   166 	if (!name)
   167 		ctx->importer_set = &ctx->result_set;
   168 	else if (!strcmp(name, "system"))
   169 		ctx->importer_set = &ctx->system_set;
   170 	else if (!strcmp(name, "repo"))
   171 		ctx->importer_set = &ctx->repo_set;
   172 	else {
   173 		fprintf(stderr, "  bad set name '%s'\n", name);
   174 		exit(1);
   175 	}
   176 }
   177 
   178 static void
   179 end_set(struct test_context *ctx)
   180 {
   181 	*ctx->importer_set = razor_importer_finish(ctx->importer);
   182 	ctx->importer = NULL;
   183 }
   184 
   185 static void
   186 start_package(struct test_context *ctx, const char **atts)
   187 {
   188 	const char *name = NULL, *version = NULL, *arch = NULL;
   189 
   190 	get_atts(atts, "name", &name,
   191 		 "version", &version,
   192 		 "arch", &arch,
   193 		 NULL);
   194 
   195 	if (!name) {
   196 		fprintf(stderr, "  package with no name\n");
   197 		exit(1);
   198 	}
   199 
   200 	razor_importer_begin_package(ctx->importer, name, version, arch);
   201 	razor_importer_add_property(ctx->importer, name,
   202 				    RAZOR_PROPERTY_EQUAL | RAZOR_PROPERTY_PROVIDES,
   203 				    version);
   204 }
   205 
   206 static void
   207 end_package(struct test_context *ctx)
   208 {
   209 	razor_importer_finish_package(ctx->importer);
   210 }
   211 
   212 static void
   213 add_property(struct test_context *ctx, enum razor_property_flags type, const char *name, enum razor_property_flags rel, const char *version)
   214 {
   215 	razor_importer_add_property(ctx->importer, name,
   216 				    rel | type, version);
   217 }
   218 
   219 static const char*
   220 razor_property_flags_relation_to_string(enum razor_property_flags rel)
   221 {
   222 	if (rel == RAZOR_PROPERTY_LESS)
   223 		return "<";
   224 	if (rel == (RAZOR_PROPERTY_EQUAL | RAZOR_PROPERTY_LESS))
   225 		return "<=";
   226 	if (rel == RAZOR_PROPERTY_EQUAL)
   227 		return "=";
   228 	if (rel == (RAZOR_PROPERTY_EQUAL | RAZOR_PROPERTY_GREATER))
   229 		return ">=";
   230 	if (rel == RAZOR_PROPERTY_GREATER)
   231 		return ">";
   232 
   233 	return "";
   234 }
   235 
   236 static void
   237 check_unsatisfiable_property(struct test_context *ctx,
   238 			     enum razor_property_flags type,
   239 			     const char *name,
   240 			     enum razor_property_flags rel,
   241 			     const char *version)
   242 {
   243 	if (!version)
   244 		version = "";
   245 
   246 	if (razor_transaction_unsatisfied_property(ctx->trans,
   247 						   name, rel | type, version))
   248 		return;
   249 
   250 	fprintf(stderr, "  didn't get unsatisfiable '%s %s %s'\n",
   251 		name, razor_property_flags_relation_to_string(rel), version);
   252 	ctx->errors++;
   253 }
   254 
   255 static void
   256 start_property(struct test_context *ctx, enum razor_property_flags type, const char **atts)
   257 {
   258 	const char *name = NULL, *rel_str = NULL, *version = NULL;
   259 	enum razor_property_flags rel;
   260 
   261 	get_atts(atts, "name", &name, "relation", &rel_str, "version", &version, NULL);
   262 	if (name == NULL) {
   263 		fprintf(stderr, "  no name specified for property\n");
   264 		exit(1);
   265 	}
   266 	if (version) {
   267 		rel = parse_relation(rel_str);
   268 		if (rel == -1) {
   269 			fprintf(stderr, "  bad or missing version relation for property %s\n", name);
   270 			exit(1);
   271 		}
   272 	} else
   273 		rel = RAZOR_PROPERTY_EQUAL;
   274 
   275 	if (ctx->unsat)
   276 		check_unsatisfiable_property(ctx, type, name, rel, version);
   277 	else
   278 		add_property(ctx, type, name, rel, version);
   279 }
   280 
   281 static void
   282 start_transaction(struct test_context *ctx, const char **atts)
   283 {
   284 	ctx->n_install_pkgs = 0;
   285 	ctx->n_remove_pkgs = 0;
   286 }
   287 
   288 static struct razor_package *
   289 get_package(struct razor_set *set, const char *package)
   290 {
   291 	struct razor_package_iterator *pi;
   292 	struct razor_package *p;
   293 	const char *name, *version, *arch;
   294 
   295 	pi = razor_package_iterator_create(set);
   296 	while (razor_package_iterator_next(pi, &p, &name, &version, &arch)) {
   297 		if (strcmp(package, name) == 0)
   298 			break;
   299 	}
   300 	razor_package_iterator_destroy(pi);
   301 
   302 	return p;
   303 }
   304 
   305 static void
   306 end_transaction(struct test_context *ctx)
   307 {
   308 	struct razor_package *pkg;
   309 	int errors, i;
   310 
   311 	ctx->trans = razor_transaction_create(ctx->system_set, ctx->repo_set);
   312 	for (i = 0; i < ctx->n_install_pkgs; i++) {
   313 		pkg = get_package(ctx->repo_set, ctx->install_pkgs[i]);
   314 		razor_transaction_install_package(ctx->trans, pkg);
   315 	}
   316 	for (i = 0; i < ctx->n_remove_pkgs; i++) {
   317 		pkg = get_package(ctx->system_set, ctx->remove_pkgs[i]);
   318 		if (!pkg)
   319 			pkg = get_package(ctx->repo_set, ctx->remove_pkgs[i]);
   320 
   321 		razor_transaction_remove_package(ctx->trans, pkg);
   322 	}
   323 
   324 	razor_transaction_resolve(ctx->trans);
   325 	errors = razor_transaction_describe(ctx->trans);
   326 	printf("\n");
   327 
   328 	while (ctx->n_install_pkgs--)
   329 		free(ctx->install_pkgs[ctx->n_install_pkgs]);
   330 	while (ctx->n_remove_pkgs--)
   331 		free(ctx->remove_pkgs[ctx->n_remove_pkgs]);
   332 
   333 	if (!errors) {
   334 		struct razor_set *new;
   335 		new = razor_transaction_finish(ctx->trans);
   336 		ctx->trans = NULL;
   337 		ctx->system_set = new;
   338 	}
   339 }
   340 
   341 static void
   342 start_install_or_update(struct test_context *ctx, const char **atts)
   343 {
   344 	const char *name = NULL;
   345 
   346 	get_atts(atts, "name", &name, NULL);
   347 	if (!name) {
   348 		fprintf(stderr, "  install/update with no name\n");
   349 		exit(1);
   350 	}
   351 
   352 	ctx->install_pkgs[ctx->n_install_pkgs++] = strdup(name);
   353 }
   354 
   355 static void
   356 start_remove(struct test_context *ctx, const char **atts)
   357 {
   358 	const char *name = NULL;
   359 
   360 	get_atts(atts, "name", &name, NULL);
   361 	if (!name) {
   362 		fprintf(stderr, "  remove with no name\n");
   363 		exit(1);
   364 	}
   365 
   366 	ctx->remove_pkgs[ctx->n_remove_pkgs++] = strdup(name);
   367 }
   368 
   369 static void
   370 start_result(struct test_context *ctx, const char **atts)
   371 {
   372 	ctx->in_result = 1;
   373 }
   374 
   375 static void
   376 diff_callback(enum razor_diff_action action,
   377 	      struct razor_package *package,
   378 	      const char *name,
   379 	      const char *version,
   380 	      const char *arch,
   381 	      void *data)
   382 {
   383 	struct test_context *ctx = data;
   384 
   385 	ctx->errors++;
   386 	if (action == RAZOR_DIFF_ACTION_REMOVE) {
   387 		fprintf(stderr, "  result set should not contain %s %s\n",
   388 			name, version);
   389 	} else {
   390 		fprintf(stderr, "  result set should contain %s %s\n",
   391 			name, version);
   392 	}
   393 }
   394 
   395 static void
   396 end_result(struct test_context *ctx)
   397 {
   398 	ctx->in_result = 0;
   399 
   400 	if (ctx->result_set) {
   401 		if (!ctx->system_set)
   402 			ctx->system_set = razor_set_create();
   403 		razor_set_diff(ctx->system_set, ctx->result_set,
   404 			       diff_callback, ctx);
   405 	}
   406 }
   407 
   408 static void
   409 start_unsatisfiable(struct test_context *ctx, const char **atts)
   410 {
   411 	if (ctx->result_set) {
   412 		fprintf(stderr, "Expected to fail, but didn't\n");
   413 		exit(1);
   414 	}
   415 
   416 	ctx->unsat = 1;
   417 }
   418 
   419 static void
   420 end_unsatisfiable(struct test_context *ctx)
   421 {
   422 	ctx->unsat = 0;
   423 }
   424 
   425 static void
   426 start_test_element(void *data, const char *element, const char **atts)
   427 {
   428 	struct test_context *ctx = data;
   429 
   430 	if (strcmp(element, "tests") == 0) {
   431 		;
   432 	} else if (strcmp(element, "test") == 0) {
   433 		start_test(ctx, atts);
   434 	} else if (strcmp(element, "set") == 0) {
   435 		start_set(ctx, atts);
   436 	} else if (strcmp(element, "transaction") == 0) {
   437 		start_transaction(ctx, atts);
   438 	} else if (strcmp(element, "install") == 0) {
   439 		start_install_or_update(ctx, atts);
   440 	} else if (strcmp(element, "install") == 0) {
   441 		start_install_or_update(ctx, atts);
   442 	} else if (strcmp(element, "remove") == 0) {
   443 		start_remove(ctx, atts);
   444 	} else if (strcmp(element, "result") == 0) {
   445 		start_result(ctx, atts);
   446 	} else if (strcmp(element, "unsatisfiable") == 0) {
   447 		start_unsatisfiable(ctx, atts);
   448 	} else if (strcmp(element, "package") == 0) {
   449 		start_package(ctx, atts);
   450 	} else if (strcmp(element, "requires") == 0) {
   451 		start_property(ctx, RAZOR_PROPERTY_REQUIRES, atts);
   452 	} else if (strcmp(element, "provides") == 0) {
   453 		start_property(ctx, RAZOR_PROPERTY_PROVIDES, atts);
   454 	} else if (strcmp(element, "conflicts") == 0) {
   455 		start_property(ctx, RAZOR_PROPERTY_CONFLICTS, atts);
   456 	} else if (strcmp(element, "obsoletes") == 0) {
   457 		start_property(ctx, RAZOR_PROPERTY_OBSOLETES, atts);
   458 	} else {
   459 		fprintf(stderr, "Unrecognized element '%s'\n", element);
   460 		exit(1);
   461 	}
   462 }
   463 
   464 static void
   465 end_test_element (void *data, const char *element)
   466 {
   467 	struct test_context *ctx = data;
   468 
   469 	if (strcmp(element, "test") == 0) {
   470 		end_test(ctx);
   471 	} else if (strcmp(element, "set") == 0) {
   472 		end_set(ctx);
   473 	} else if (strcmp(element, "package") == 0) {
   474 		end_package(ctx);
   475 	} else if (strcmp(element, "transaction") == 0) {
   476 		end_transaction(ctx);
   477 	} else if (strcmp(element, "result") == 0) {
   478 		end_result(ctx);
   479 	} else if (strcmp(element, "unsatisfiable") == 0) {
   480 		end_unsatisfiable(ctx);
   481 	}
   482 }
   483 
   484 int main(int argc, char *argv[])
   485 {
   486 	struct test_context ctx;
   487 	const char *test_file;
   488 
   489 	memset(&ctx, 0, sizeof ctx);
   490 
   491 	if (argc > 3) {
   492 		fprintf(stderr, "usage: %s [-d] [TESTS-FILE]\n", argv[0]);
   493 		exit(-1);
   494 	}
   495 
   496 	if (argc >= 2 && !strcmp (argv[1], "-d")) {
   497 		ctx.debug = 1;
   498 		argc--;
   499 		argv++;
   500 	}
   501 	if (argc == 2)
   502 		test_file = argv[1];
   503 	else
   504 		test_file = "test.xml";
   505 
   506 	parse_xml_file(test_file, start_test_element, end_test_element, &ctx);
   507 
   508 	if (ctx.errors) {
   509 		fprintf(stderr, "\n%d errors\n", ctx.errors);
   510 		return 1;
   511 	} else
   512 		return 0;
   513 }