test-driver.c
changeset 125 e56c83bda295
parent 109 313b0a615c14
child 129 d221757574c1
     1.1 --- a/test-driver.c	Mon Feb 04 14:25:45 2008 -0500
     1.2 +++ b/test-driver.c	Fri Feb 15 15:09:37 2008 -0500
     1.3 @@ -50,17 +50,16 @@
     1.4  	close(fd);
     1.5  }
     1.6  
     1.7 -struct test_set {
     1.8 -	char *name;
     1.9 -	struct razor_set *set;
    1.10 -	struct test_set *next;
    1.11 -};
    1.12 +struct test_context {
    1.13 +	struct razor_set *system_set, *repo_set, *result_set;
    1.14  
    1.15 -struct test_context {
    1.16  	struct razor_importer *importer;
    1.17 -	struct test_set *sets;
    1.18 -	struct razor_package_iterator *package_iterator;
    1.19 -	struct razor_property_iterator *property_iterator;
    1.20 +	struct razor_set **importer_set;
    1.21 +
    1.22 +	char *install_pkgs[3], *remove_pkgs[3];
    1.23 +	int n_install_pkgs, n_remove_pkgs;
    1.24 +
    1.25 +	int in_result, result_errors;
    1.26  };
    1.27  
    1.28  static void
    1.29 @@ -82,234 +81,267 @@
    1.30  	va_end(ap);
    1.31  }
    1.32  
    1.33 -static void
    1.34 -parse_property(struct test_context *ctx, const char **atts,
    1.35 -	       enum razor_property_type type)
    1.36 +static enum razor_version_relation
    1.37 +parse_relation (const char *rel_str)
    1.38  {
    1.39 -	const char *name = NULL, *version = NULL;
    1.40 -
    1.41 -	get_atts(atts, "name", &name, "eq", &version, NULL);
    1.42 -
    1.43 -	if (name == NULL) {
    1.44 -		fprintf(stderr, "no name specified for property\n");
    1.45 -		exit(-1);
    1.46 -	}
    1.47 -	
    1.48 -	razor_importer_add_property(ctx->importer, name,
    1.49 -				    RAZOR_VERSION_EQUAL, version, type);
    1.50 +	if (!rel_str)
    1.51 +		return -1;
    1.52 +	if (rel_str[0] == 'l')
    1.53 +		return rel_str[1] == 'e' ? RAZOR_VERSION_LESS_OR_EQUAL : RAZOR_VERSION_LESS;
    1.54 +	else if (rel_str[0] == 'g')
    1.55 +		return rel_str[1] == 'e' ? RAZOR_VERSION_GREATER_OR_EQUAL : RAZOR_VERSION_GREATER;
    1.56 +	else if (rel_str[0] == 'e' || rel_str[1] == 'q')
    1.57 +		return RAZOR_VERSION_EQUAL;
    1.58 +	else
    1.59 +		return -1;
    1.60  }
    1.61  
    1.62  static void
    1.63 -start_set_element(void *data, const char *element, const char **atts)
    1.64 +start_test(struct test_context *ctx, const char **atts)
    1.65  {
    1.66 -	struct test_context *ctx = data;
    1.67 -	struct test_set *set;
    1.68 -	const char *name, *version;
    1.69 +	const char *name = NULL;
    1.70  
    1.71 -	if (strcmp(element, "set") == 0) {
    1.72 -		get_atts(atts, "name", &name, NULL);
    1.73 -		ctx->importer = razor_importer_new();	
    1.74 -		set = malloc(sizeof *set);
    1.75 -		set->name = strdup(name);
    1.76 -		set->next = ctx->sets;
    1.77 -		ctx->sets = set;
    1.78 -	} else if (strcmp(element, "package") == 0) {
    1.79 -		get_atts(atts, "name", &name, "version", &version, NULL);
    1.80 -		razor_importer_begin_package(ctx->importer, name, version);
    1.81 -	} else if (strcmp(element, "requires") == 0) {
    1.82 -		parse_property(ctx, atts, RAZOR_PROPERTY_REQUIRES);
    1.83 -	} else if (strcmp(element, "provides") == 0) {
    1.84 -		parse_property(ctx, atts, RAZOR_PROPERTY_PROVIDES);
    1.85 -	} else if (strcmp(element, "obsoletes") == 0) {
    1.86 -		parse_property(ctx, atts, RAZOR_PROPERTY_OBSOLETES);
    1.87 -	} else if (strcmp(element, "conflicts") == 0) {
    1.88 -		parse_property(ctx, atts, RAZOR_PROPERTY_CONFLICTS);
    1.89 -	} else if (strcmp(element, "file") == 0) {
    1.90 -		get_atts(atts, "name", &name, NULL);
    1.91 -		razor_importer_add_file(ctx->importer, name);		
    1.92 -	} else if (strcmp(element, "dir") == 0) {
    1.93 -		get_atts(atts, "name", &name, NULL);
    1.94 -		razor_importer_add_file(ctx->importer, name);		
    1.95 +	get_atts(atts, "name", &name, NULL);
    1.96 +	if (!name) {
    1.97 +		fprintf(stderr, "Test with no name\n");
    1.98 +		exit(1);
    1.99 +	}
   1.100 +	printf("%s\n", name);
   1.101 +}
   1.102 +
   1.103 +static void
   1.104 +end_test(struct test_context *ctx)
   1.105 +{
   1.106 +	if (ctx->system_set) {
   1.107 +		razor_set_destroy(ctx->system_set);
   1.108 +		ctx->system_set = NULL;
   1.109 +	}
   1.110 +	if (ctx->repo_set) {
   1.111 +		razor_set_destroy(ctx->repo_set);
   1.112 +		ctx->repo_set = NULL;
   1.113 +	}
   1.114 +	if (ctx->result_set) {
   1.115 +		razor_set_destroy(ctx->result_set);
   1.116 +		ctx->result_set = NULL;
   1.117  	}
   1.118  }
   1.119  
   1.120  static void
   1.121 -end_set_element (void *data, const char *name)
   1.122 +start_set(struct test_context *ctx, const char **atts)
   1.123  {
   1.124 -	struct test_context *ctx = data;
   1.125 +	const char *name = NULL;
   1.126  
   1.127 -	if (strcmp(name, "set") == 0) {
   1.128 -		ctx->sets->set = razor_importer_finish(ctx->importer);
   1.129 -	} else if (strcmp(name, "package") == 0) {
   1.130 -		razor_importer_finish_package(ctx->importer);
   1.131 -	}
   1.132 -}
   1.133 -
   1.134 -static struct razor_set *
   1.135 -lookup_set(struct test_context *ctx, const char *name)
   1.136 -{
   1.137 -	struct test_set *set;
   1.138 -
   1.139 -	for (set = ctx->sets; set != NULL; set = set->next) {
   1.140 -		if (strcmp(set->name, name) == 0)
   1.141 -			return set->set;
   1.142 -	}
   1.143 -
   1.144 -	return NULL;
   1.145 -}
   1.146 -
   1.147 -static void
   1.148 -verify_begin(struct test_context *ctx, const char **atts)
   1.149 -{
   1.150 -	struct razor_set *set;
   1.151 -	const char *type, *name;
   1.152 -
   1.153 -	get_atts(atts, "type", &type, "set", &name, NULL);
   1.154 -	set = lookup_set(ctx, name);
   1.155 -	if (set == NULL) {
   1.156 -		fprintf(stderr, "set %s not found\n", name);
   1.157 -		exit(-1);
   1.158 -	}
   1.159 -
   1.160 -	if (strcmp(type, "packages") == 0) {
   1.161 -		ctx->package_iterator =
   1.162 -			razor_package_iterator_create(set);
   1.163 -	} else if (strcmp(type, "properties") == 0) {
   1.164 -		ctx->property_iterator =
   1.165 -			razor_property_iterator_create(set, NULL);
   1.166 -	} else {
   1.167 -		fprintf(stderr,
   1.168 -			"unknown compare type \"%s\"\n", type);
   1.169 -		exit(-1);
   1.170 +	ctx->importer = razor_importer_new();
   1.171 +	get_atts(atts, "name", &name, NULL);
   1.172 +	if (!name)
   1.173 +		ctx->importer_set = &ctx->result_set;
   1.174 +	else if (!strcmp(name, "system"))
   1.175 +		ctx->importer_set = &ctx->system_set;
   1.176 +	else if (!strcmp(name, "repo"))
   1.177 +		ctx->importer_set = &ctx->repo_set;
   1.178 +	else {
   1.179 +		fprintf(stderr, "  bad set name '%s'\n", name);
   1.180 +		exit(1);
   1.181  	}
   1.182  }
   1.183  
   1.184  static void
   1.185 -verify_end(struct test_context *ctx)
   1.186 +end_set(struct test_context *ctx)
   1.187  {
   1.188 -	struct razor_package *package;
   1.189 -	struct razor_property *property;
   1.190 -	const char *name, *version;
   1.191 -	enum razor_property_type type;
   1.192 -	enum razor_version_relation relation;
   1.193 +	*ctx->importer_set = razor_importer_finish(ctx->importer);
   1.194 +	ctx->importer = NULL;
   1.195 +}
   1.196  
   1.197 -	if (ctx->package_iterator != NULL) {
   1.198 -		if (razor_package_iterator_next(ctx->package_iterator,
   1.199 -						&package,
   1.200 -						&name, &version)) {
   1.201 -			fprintf(stderr, "too few packages in set\n");
   1.202 -			exit(-1);
   1.203 -		}
   1.204 -				
   1.205 -		razor_package_iterator_destroy(ctx->package_iterator);
   1.206 -		ctx->package_iterator = NULL;
   1.207 +static void
   1.208 +start_package(struct test_context *ctx, const char **atts)
   1.209 +{
   1.210 +	const char *name = NULL, *version = NULL, *arch = NULL;
   1.211 +
   1.212 +	get_atts(atts, "name", &name, "version", &version, "arch", &arch, NULL);
   1.213 +	if (!name) {
   1.214 +		fprintf(stderr, "  package with no name\n");
   1.215 +		exit(1);
   1.216  	}
   1.217  
   1.218 -	if (ctx->property_iterator != NULL) {
   1.219 -		if (razor_property_iterator_next(ctx->property_iterator,
   1.220 -						 &property,
   1.221 -						 &name, &relation, &version,
   1.222 -						 &type)) {
   1.223 -			fprintf(stderr, "too few properties in set\n");
   1.224 -			exit(-1);
   1.225 +	razor_importer_begin_package(ctx->importer, name, version);
   1.226 +	razor_importer_add_property(ctx->importer, name,
   1.227 +				    RAZOR_VERSION_EQUAL, version,
   1.228 +				    RAZOR_PROPERTY_PROVIDES);
   1.229 +}
   1.230 +
   1.231 +static void
   1.232 +end_package(struct test_context *ctx)
   1.233 +{
   1.234 +	razor_importer_finish_package(ctx->importer);
   1.235 +}
   1.236 +
   1.237 +static void
   1.238 +start_property(struct test_context *ctx, enum razor_property_type type, const char **atts)
   1.239 +{
   1.240 +	const char *name = NULL, *rel_str = NULL, *version = NULL;
   1.241 +	enum razor_version_relation rel;
   1.242 +
   1.243 +	get_atts(atts, "name", &name, "rel", &rel_str, "version", &version, NULL);
   1.244 +	if (name == NULL) {
   1.245 +		fprintf(stderr, "  no name specified for property\n");
   1.246 +		exit(1);
   1.247 +	}
   1.248 +	if (version) {
   1.249 +		rel = parse_relation(rel_str);
   1.250 +		if (rel == -1) {
   1.251 +			fprintf(stderr, "  bad or missing version relation for property %s\n", name);
   1.252 +			exit(1);
   1.253  		}
   1.254 +	} else
   1.255 +		rel = RAZOR_VERSION_EQUAL;
   1.256 +	
   1.257 +	razor_importer_add_property(ctx->importer, name,
   1.258 +				    rel, version, type);
   1.259 +}
   1.260  
   1.261 -		razor_property_iterator_destroy(ctx->property_iterator);
   1.262 -		ctx->property_iterator = NULL;
   1.263 +static void
   1.264 +start_transaction(struct test_context *ctx, const char **atts)
   1.265 +{
   1.266 +	ctx->n_install_pkgs = 0;
   1.267 +	ctx->n_remove_pkgs = 0;
   1.268 +}
   1.269 +
   1.270 +static void
   1.271 +end_transaction(struct test_context *ctx)
   1.272 +{
   1.273 +	/* FIXME: removes */
   1.274 +	ctx->system_set = razor_set_update(ctx->system_set,
   1.275 +					   ctx->repo_set,
   1.276 +					   ctx->n_install_pkgs,
   1.277 +					   (const char **)ctx->install_pkgs);
   1.278 +
   1.279 +	while (ctx->n_install_pkgs--)
   1.280 +		free(ctx->install_pkgs[ctx->n_install_pkgs]);
   1.281 +	while (ctx->n_remove_pkgs--)
   1.282 +		free(ctx->remove_pkgs[ctx->n_remove_pkgs]);
   1.283 +}
   1.284 +
   1.285 +static void
   1.286 +start_install_or_update(struct test_context *ctx, const char **atts)
   1.287 +{
   1.288 +	const char *name = NULL;
   1.289 +
   1.290 +	get_atts(atts, "name", &name, NULL);
   1.291 +	if (!name) {
   1.292 +		fprintf(stderr, "  install/update with no name\n");
   1.293 +		exit(1);
   1.294 +	}
   1.295 +
   1.296 +	ctx->install_pkgs[ctx->n_install_pkgs++] = strdup(name);
   1.297 +}
   1.298 +
   1.299 +static void
   1.300 +start_remove(struct test_context *ctx, const char **atts)
   1.301 +{
   1.302 +	const char *name = NULL;
   1.303 +
   1.304 +	get_atts(atts, "name", &name, NULL);
   1.305 +	if (!name) {
   1.306 +		fprintf(stderr, "  remove with no name\n");
   1.307 +		exit(1);
   1.308 +	}
   1.309 +
   1.310 +	ctx->remove_pkgs[ctx->n_remove_pkgs++] = strdup(name);
   1.311 +}
   1.312 +
   1.313 +static void
   1.314 +start_result(struct test_context *ctx, const char **atts)
   1.315 +{
   1.316 +	ctx->in_result = 1;
   1.317 +}
   1.318 +
   1.319 +static void
   1.320 +diff_callback(const char *name,
   1.321 +	      const char *old_version, const char *new_version,
   1.322 +	      void *data)
   1.323 +{
   1.324 +	struct test_context *ctx = data;
   1.325 +
   1.326 +	ctx->result_errors++;
   1.327 +	if (old_version) {
   1.328 +		fprintf(stderr, "  result set should not contain %s %s\n",
   1.329 +			name, old_version);
   1.330 +	} else {
   1.331 +		fprintf(stderr, "  result set should contain %s %s\n",
   1.332 +			name, new_version);
   1.333  	}
   1.334  }
   1.335  
   1.336  static void
   1.337 -verify_package(struct test_context *ctx, const char **atts)
   1.338 +end_result(struct test_context *ctx)
   1.339  {
   1.340 -	struct razor_package *package;
   1.341 -	const char *name, *version, *ref_name, *ref_version;
   1.342 +	ctx->in_result = 0;
   1.343  
   1.344 -	if (ctx->package_iterator == NULL) {
   1.345 -		fprintf(stderr,
   1.346 -			"\"package\" element seen, "
   1.347 -			"but not in package verify mode\n");
   1.348 -		exit(-1);
   1.349 +	/* FIXME */
   1.350 +	if (ctx->n_remove_pkgs) {
   1.351 +		printf ("  (ignoring because of unimplemented remove)\n");
   1.352 +		return;
   1.353  	}
   1.354  
   1.355 -	get_atts(atts, "name", &ref_name, "version", &ref_version, NULL);
   1.356 -	if (!razor_package_iterator_next(ctx->package_iterator,
   1.357 -					 &package, &name, &version)) {
   1.358 -		fprintf(stderr, "too many packages in set\n");
   1.359 -		exit(-1);
   1.360 +	if (ctx->system_set && ctx->result_set) {
   1.361 +		ctx->result_errors = 0;
   1.362 +		razor_set_diff(ctx->system_set, ctx->result_set,
   1.363 +			       diff_callback, ctx);
   1.364 +		if (ctx->result_errors)
   1.365 +			exit(1);
   1.366  	}
   1.367 -			
   1.368 -	if (strcmp(name, ref_name) != 0 || strcmp(version, ref_version) != 0) {
   1.369 -		fprintf(stderr,
   1.370 -			"package mismatch; expected %s-%s, got %s-%s\n",
   1.371 -			ref_name, ref_version, name, version);
   1.372 -		exit(-1);
   1.373 -	}
   1.374 +
   1.375  }
   1.376  
   1.377  static void
   1.378 -verify_property(struct test_context *ctx,
   1.379 -		enum razor_property_type ref_type, const char **atts)
   1.380 +start_unsatisfied(struct test_context *ctx, const char **atts)
   1.381  {
   1.382 -	struct razor_property *property;
   1.383 -	const char *name, *version, *ref_name, *ref_version;
   1.384 -	enum razor_property_type type;
   1.385 -	enum razor_version_relation relation;
   1.386 -	int same_version;
   1.387 +	/* FIXME */
   1.388 +	fprintf(stderr, "Can't handle <unsatisfied>\n");
   1.389 +	exit(1);
   1.390 +}
   1.391  
   1.392 -	if (ctx->property_iterator == NULL) {
   1.393 -		fprintf(stderr,
   1.394 -			"\"requires/provides\" element seen, "
   1.395 -			"but not in property verify mode\n");
   1.396 -		exit(-1);
   1.397 -	}
   1.398 -
   1.399 -	get_atts(atts, "name", &ref_name, "eq", &ref_version, NULL);
   1.400 -	if (!razor_property_iterator_next(ctx->property_iterator, &property,
   1.401 -					  &name, &relation, &version, &type)) {
   1.402 -		fprintf(stderr, "too many properties in set\n");
   1.403 -		exit(-1);
   1.404 -	}
   1.405 -			
   1.406 -	if (version != NULL && ref_version != NULL)
   1.407 -		same_version = strcmp(version, ref_version) == 0;
   1.408 -	else if (version == NULL && ref_version == NULL)
   1.409 -		same_version = 1;
   1.410 -	else
   1.411 -		same_version = 0;
   1.412 -
   1.413 -	if (strcmp(name, ref_name) != 0 || !same_version || type != ref_type) {
   1.414 -		fprintf(stderr,
   1.415 -			"property mismatch; expected %s-%s/%d, got %s-%s/%d\n",
   1.416 -			ref_name, ref_version, ref_type,
   1.417 -			name, version, type);
   1.418 -		exit(-1);
   1.419 -	}
   1.420 +static void
   1.421 +end_unsatisfied(struct test_context *ctx)
   1.422 +{
   1.423  }
   1.424  
   1.425  static void
   1.426  start_test_element(void *data, const char *element, const char **atts)
   1.427  {
   1.428  	struct test_context *ctx = data;
   1.429 -	const char *name;
   1.430  
   1.431 -	if (strcmp(element, "import") == 0) {
   1.432 -		get_atts(atts, "file", &name, NULL);
   1.433 -		parse_xml_file(name, start_set_element, end_set_element, ctx);
   1.434 -	} else if (strcmp(element, "update") == 0) {
   1.435 -		/* run update to create new set */
   1.436 -	} else if (strcmp(element, "verify") == 0) {
   1.437 -		verify_begin(ctx, atts);
   1.438 +	if (strcmp(element, "tests") == 0) {
   1.439 +		;
   1.440 +	} else if (strcmp(element, "test") == 0) {
   1.441 +		start_test(ctx, atts);
   1.442 +	} else if (strcmp(element, "set") == 0) {
   1.443 +		start_set(ctx, atts);
   1.444 +	} else if (strcmp(element, "transaction") == 0) {
   1.445 +		start_transaction(ctx, atts);
   1.446 +	} else if (strcmp(element, "install") == 0) {
   1.447 +		start_install_or_update(ctx, atts);
   1.448 +	} else if (strcmp(element, "install") == 0) {
   1.449 +		start_install_or_update(ctx, atts);
   1.450 +	} else if (strcmp(element, "remove") == 0) {
   1.451 +		start_remove(ctx, atts);
   1.452 +	} else if (strcmp(element, "result") == 0) {
   1.453 +		start_result(ctx, atts);
   1.454 +	} else if (strcmp(element, "unsatisfied") == 0) {
   1.455 +		start_unsatisfied(ctx, atts);
   1.456  	} else if (strcmp(element, "package") == 0) {
   1.457 -		verify_package(ctx, atts);
   1.458 +		start_package(ctx, atts);
   1.459  	} else if (strcmp(element, "requires") == 0) {
   1.460 -		verify_property(ctx, RAZOR_PROPERTY_REQUIRES, atts);
   1.461 +		start_property(ctx, RAZOR_PROPERTY_REQUIRES, atts);
   1.462  	} else if (strcmp(element, "provides") == 0) {
   1.463 -		verify_property(ctx, RAZOR_PROPERTY_PROVIDES, atts);
   1.464 +		start_property(ctx, RAZOR_PROPERTY_PROVIDES, atts);
   1.465  	} else if (strcmp(element, "conflicts") == 0) {
   1.466 -		verify_property(ctx, RAZOR_PROPERTY_CONFLICTS, atts);
   1.467 +		start_property(ctx, RAZOR_PROPERTY_CONFLICTS, atts);
   1.468  	} else if (strcmp(element, "obsoletes") == 0) {
   1.469 -		verify_property(ctx, RAZOR_PROPERTY_OBSOLETES, atts);
   1.470 +		start_property(ctx, RAZOR_PROPERTY_OBSOLETES, atts);
   1.471 +	} else {
   1.472 +		fprintf(stderr, "Unrecognized element '%s'\n", element);
   1.473 +		exit(1);
   1.474  	}
   1.475  }
   1.476  
   1.477 @@ -318,8 +350,19 @@
   1.478  {
   1.479  	struct test_context *ctx = data;
   1.480  
   1.481 -	if (strcmp(element, "verify") == 0)
   1.482 -		verify_end(ctx);
   1.483 +	if (strcmp(element, "test") == 0) {
   1.484 +		end_test(ctx);
   1.485 +	} else if (strcmp(element, "set") == 0) {
   1.486 +		end_set(ctx);
   1.487 +	} else if (strcmp(element, "package") == 0) {
   1.488 +		end_package(ctx);
   1.489 +	} else if (strcmp(element, "transaction") == 0) {
   1.490 +		end_transaction(ctx);
   1.491 +	} else if (strcmp(element, "result") == 0) {
   1.492 +		end_result(ctx);
   1.493 +	} else if (strcmp(element, "unsatisfied") == 0) {
   1.494 +		end_unsatisfied(ctx);
   1.495 +	}
   1.496  }
   1.497  
   1.498  int main(int argc, char *argv[])