Commit 64e5a644 authored by Timo Teräs's avatar Timo Teräs

test: rewrite the testing framework to use the real applets

also merge the expected output to the *.test files.
parent 082ffbd3
src/apk
src/apk_test
src/apk-test
src/apk.static
test/*.got
*.o
......
......@@ -27,16 +27,18 @@ libapk.so-objs := common.o database.o package.o archive.o \
solver.o
ifeq ($(TEST),y)
progs-y += apk_test
apk_test-objs := apk.o test.o $(libapk.so-objs)
progs-y += apk-test
apk-test-objs := apk-test.o $(filter-out apk.o, $(apk-objs))
endif
ifeq ($(SHARED_LIBAPK),)
apk-objs += $(libapk.so-objs)
apk-test-objs += $(libapk.so-objs)
apk.so-objs += $(libapk.so-objs)
else
LIBAPK := YesPlease
LIBS_apk := -lapk
LIBS_apk-test := -lapk
LIBS_apk.so := -L$(obj) -lapk
endif
......@@ -50,12 +52,13 @@ endif
CFLAGS_ALL += -D_ATFILE_SOURCE
CFLAGS_apk.o := -DAPK_VERSION=\"$(FULL_VERSION)\"
CFLAGS_apk-static.o := -DAPK_VERSION=\"$(FULL_VERSION)\" -DOPENSSL_NO_ENGINE
CFLAGS_apk-test.o := -DAPK_VERSION=\"$(FULL_VERSION)\" -DOPENSSL_NO_ENGINE -DTEST_MODE
progs-$(STATIC) += apk.static
apk.static-objs := $(filter-out apk.o,$(apk-objs)) apk-static.o
LDFLAGS_apk.static := -static
LDFLAGS_apk += -nopie -L$(obj)
LDFLAGS_apk_test += -nopie -L$(obj)
LDFLAGS_apk-test += -nopie -L$(obj)
CFLAGS_ALL += $(shell $(PKG_CONFIG) --cflags $(PKGDEPS))
LIBS := -Wl,--as-needed \
......
apk.c
\ No newline at end of file
......@@ -31,6 +31,7 @@
#include "apk_applet.h"
#include "apk_blob.h"
#include "apk_print.h"
#include "apk_io.h"
char **apk_argv;
......@@ -67,11 +68,19 @@ static struct apk_option generic_options[] = {
{ 0x111, "overlay-from-stdin", "Read list of overlay files from stdin" },
{ 0x112, "arch", "Use architecture with --root",
required_argument, "ARCH" },
#ifdef TEST_MODE
{ 0x200, "test-repo", "Repository", required_argument, "REPO" },
{ 0x201, "test-instdb", "Installed db", required_argument, "INSTALLED" },
{ 0x202, "test-world", "World", required_argument, "WORLD DEPS" },
#endif
};
static int version(void)
{
printf("apk-tools " APK_VERSION ", compiled for " APK_DEFAULT_ARCH ".\n");
#ifdef TEST_MODE
printf("TEST MODE BUILD. NOT FOR PRODUCTION USE.\n");
#endif
return 0;
}
......@@ -273,6 +282,14 @@ int main(int argc, char **argv)
struct apk_repository_list *repo = NULL;
struct apk_database db;
struct apk_db_options dbopts;
#ifdef TEST_MODE
const char *test_installed_db = NULL;
const char *test_world = NULL;
struct apk_string_array *test_repos;
int i;
apk_string_array_init(&test_repos);
#endif
apk_argv = malloc(sizeof(char*[argc+2]));
memcpy(apk_argv, argv, sizeof(char*[argc]));
......@@ -383,6 +400,17 @@ int main(int argc, char **argv)
case 0x112:
dbopts.arch = optarg;
break;
#ifdef TEST_MODE
case 0x200:
*apk_string_array_add(&test_repos) = (char*) optarg;
break;
case 0x201:
test_installed_db = optarg;
break;
case 0x202:
test_world = optarg;
break;
#endif
default:
if (applet == NULL || applet->parse == NULL ||
applet->parse(ctx, &dbopts, r,
......@@ -407,6 +435,12 @@ int main(int argc, char **argv)
argv++;
}
#ifdef TEST_MODE
dbopts.open_flags &= ~(APK_OPENF_WRITE | APK_OPENF_CACHE_WRITE | APK_OPENF_CREATE);
dbopts.open_flags |= APK_OPENF_READ | APK_OPENF_NO_STATE | APK_OPENF_NO_REPOS;
apk_flags |= APK_SIMULATE;
apk_flags &= ~APK_INTERACTIVE;
#endif
r = apk_db_open(&db, &dbopts);
if (r != 0) {
apk_error("Failed to open apk database: %s",
......@@ -414,6 +448,35 @@ int main(int argc, char **argv)
goto err;
}
#ifdef TEST_MODE
if (test_world != NULL) {
apk_blob_t b = APK_BLOB_STR(test_world);
apk_blob_pull_deps(&b, &db, &db.world);
}
if (test_installed_db != NULL) {
struct apk_bstream *bs = apk_bstream_from_file(AT_FDCWD, test_installed_db);
if (bs != NULL) {
apk_db_index_read(&db, bs, -1);
bs->close(bs, NULL);
}
}
for (i = 0; i < test_repos->num; i++) {
struct apk_bstream *bs;
char *fn = test_repos->item[i];
int repo_tag = 0;
if (fn[0] == '+') {
repo_tag = apk_db_get_tag_id(&db, APK_BLOB_STR("testing"));
fn++;
}
bs = apk_bstream_from_file(AT_FDCWD, fn);
if (bs != NULL) {
apk_db_index_read(&db, bs, i);
db.repo_tags[repo_tag].allowed_repos |= BIT(i);
bs->close(bs, NULL);
}
}
#endif
r = applet->main(ctx, &db, argc, argv);
apk_db_close(&db);
......
/* test.c - Alpine Package Keeper (APK)
*
* Copyright (C) 2008-2011 Timo Teräs <timo.teras@iki.fi>
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published
* by the Free Software Foundation. See http://www.gnu.org/ for details.
*/
#include <errno.h>
#include <fcntl.h>
#include "apk_applet.h"
#include "apk_database.h"
#include "apk_solver.h"
#include "apk_io.h"
#include "apk_print.h"
struct test_ctx {
const char *installed_db;
struct apk_string_array *repos;
unsigned short solver_flags;
};
static int test_parse(void *pctx, struct apk_db_options *dbopts,
int optch, int optindex, const char *optarg)
{
struct test_ctx *ctx = (struct test_ctx *) pctx;
switch (optch) {
case 0x10000:
ctx->installed_db = optarg;
break;
case 0x10001:
if (ctx->repos == NULL)
apk_string_array_init(&ctx->repos);
*apk_string_array_add(&ctx->repos) = (char*) optarg;
break;
case 'u':
ctx->solver_flags |= APK_SOLVERF_UPGRADE;
break;
case 'a':
ctx->solver_flags |= APK_SOLVERF_AVAILABLE;
break;
default:
return -1;
}
return 0;
}
static inline void print_change(struct apk_package *oldpkg,
struct apk_package *newpkg)
{
const char *msg = NULL;
struct apk_name *name;
int r;
if (oldpkg != NULL)
name = oldpkg->name;
else
name = newpkg->name;
if (oldpkg == NULL) {
apk_message("Installing %s (" BLOB_FMT ")",
name->name,
BLOB_PRINTF(*newpkg->version));
} else if (newpkg == NULL) {
apk_message("Purging %s (" BLOB_FMT ")",
name->name,
BLOB_PRINTF(*oldpkg->version));
} else {
r = apk_pkg_version_compare(newpkg, oldpkg);
switch (r) {
case APK_VERSION_LESS:
msg = "Downgrading";
break;
case APK_VERSION_EQUAL:
if (newpkg == oldpkg)
msg = "Re-installing";
else
msg = "Replacing";
break;
case APK_VERSION_GREATER:
msg = "Upgrading";
break;
}
apk_message("%s %s (" BLOB_FMT " -> " BLOB_FMT ")",
msg, name->name,
BLOB_PRINTF(*oldpkg->version),
BLOB_PRINTF(*newpkg->version));
}
}
static void print_dep_errors(struct apk_database *db, char *label, struct apk_dependency_array *deps)
{
int i, print_label = 1;
char buf[256];
apk_blob_t p = APK_BLOB_BUF(buf);
for (i = 0; i < deps->num; i++) {
struct apk_dependency *dep = &deps->item[i];
struct apk_package *pkg = dep->name->state_ptr;
if (apk_dep_is_satisfied(dep, pkg))
continue;
if (print_label) {
print_label = 0;
printf("%s: ", label);
} else {
printf(" ");
}
apk_blob_push_dep(&p, db, dep);
p = apk_blob_pushed(APK_BLOB_BUF(buf), p);
fwrite(p.ptr, p.len, 1, stdout);
}
if (!print_label)
printf("\n");
}
static void print_errors_in_solution(struct apk_database *db, int unsatisfiable,
struct apk_solution_array *solution)
{
int i;
printf("%d unsatisfiable dependencies (solution with %zu names)\n",
unsatisfiable, solution->num);
for (i = 0; i < solution->num; i++) {
struct apk_package *pkg = solution->item[i].pkg;
pkg->name->state_ptr = pkg;
}
print_dep_errors(db, "world", db->world);
for (i = 0; i < solution->num; i++) {
struct apk_package *pkg = solution->item[i].pkg;
char pkgtext[256];
snprintf(pkgtext, sizeof(pkgtext), PKG_VER_FMT, PKG_VER_PRINTF(pkg));
print_dep_errors(db, pkgtext, pkg->depends);
}
}
static int test_main(void *pctx, struct apk_database *db, int argc, char **argv)
{
struct test_ctx *ctx = (struct test_ctx *) pctx;
struct apk_bstream *bs;
struct apk_solution_array *solution = NULL;
struct apk_changeset changeset = {};
apk_blob_t b;
int i, r;
if (argc != 1)
return -EINVAL;
apk_db_get_tag_id(db, APK_BLOB_STR("testing"));
/* load installed db */
if (ctx->installed_db != NULL) {
bs = apk_bstream_from_file(AT_FDCWD, ctx->installed_db);
if (bs != NULL) {
apk_db_index_read(db, bs, -1);
bs->close(bs, NULL);
}
}
/* load additional indexes */
if (ctx->repos) {
for (i = 0; i < ctx->repos->num; i++) {
char *fn = ctx->repos->item[i];
int repo = 0;
if (fn[0] == '+') {
fn++;
repo = 1;
}
bs = apk_bstream_from_file(AT_FDCWD, fn);
if (bs != NULL) {
apk_db_index_read(db, bs, i);
db->repo_tags[repo].allowed_repos |= BIT(i);
bs->close(bs, NULL);
}
}
}
/* construct new world */
b = APK_BLOB_STR(argv[0]);
apk_blob_pull_deps(&b, db, &db->world);
/* run solver */
r = apk_solver_solve(db, ctx->solver_flags, db->world, &solution, &changeset);
if (r == 0) {
/* dump changeset */
for (i = 0; i < changeset.changes->num; i++) {
struct apk_change *c = &changeset.changes->item[i];
print_change(c->oldpkg, c->newpkg);
}
} else { /* r >= 1*/
print_errors_in_solution(db, r, solution);
}
return 0;
}
static struct apk_option test_options[] = {
{ 0x10000, "installed", "Installed database",
required_argument, "DB" },
{ 0x10001, "raw-repository", "Add unsigned test repository index",
required_argument, "INDEX" },
{ 'u', "upgrade", "Prefer to upgrade package" },
{ 'a', "available",
"Re-install or downgrade if currently installed package is not "
"currently available from any repository" },
};
static struct apk_applet test_applet = {
.name = "test",
.help = "Test dependency graph solver (uses simple repository and installed db)",
.arguments = "'WORLD'",
.open_flags = APK_OPENF_READ | APK_OPENF_NO_STATE | APK_OPENF_NO_REPOS,
.context_size = sizeof(struct test_ctx),
.num_options = ARRAY_SIZE(test_options),
.options = test_options,
.parse = test_parse,
.main = test_main,
};
APK_DEFINE_APPLET(test_applet);
Installing b (2)
Installing a (2)
--raw-repository basic.repo
a
@ARGS
--test-repo basic.repo
add a
@EXPECT
(1/2) Installing b (2)
(2/2) Installing a (2)
OK: 0 MiB in 0 packages
--raw-repository basic.repo --installed basic.installed
a
@ARGS
--test-repo basic.repo
--test-instdb basic.installed
--test-world a
add
@EXPECT
OK: 0 MiB in 2 packages
Upgrading b (1 -> 2)
Upgrading a (1 -> 2)
--raw-repository basic.repo --installed basic.installed -u
a
@ARGS
--test-repo basic.repo
--test-instdb basic.installed
--test-world a
upgrade
@EXPECT
(1/2) Upgrading b (1 -> 2)
(2/2) Upgrading a (1 -> 2)
OK: 0 MiB in 2 packages
--raw-repository basic.repo --installed basic.installed
b
@ARGS
--test-repo basic.repo
--test-instdb basic.installed
--test-world "a b"
del a
@EXPECT
(1/1) Purging a (1)
OK: 0 MiB in 2 packages
Replacing b (2 -> 2)
Replacing a (2 -> 2)
--raw-repository basic.repo --installed basic.installed2 -a
a
@ARGS
--test-repo basic.repo
--test-instdb basic.installed2
--test-world a
upgrade -a
@EXPECT
(1/2) Replacing b (2 -> 2)
(2/2) Replacing a (2 -> 2)
OK: 0 MiB in 2 packages
--raw-repository basic.repo --installed basic.installed2
a
@ARGS
--test-repo basic.repo
--test-instdb basic.installed2
--test-world a
upgrade
@EXPECT
OK: 0 MiB in 2 packages
--no-network --raw-repository basic.repo --installed basic.installed -u
a
@ARGS
--no-network
--test-repo basic.repo
--test-instdb basic.installed
--test-world a
upgrade
@EXPECT
OK: 0 MiB in 2 packages
Installing d (1.5)
Installing c (1)
Installing b (1)
Installing a (2)
--raw-repository complicated1.repo
a
@ARGS
--test-repo complicated1.repo
add a
@EXPECT
(1/4) Installing d (1.5)
(2/4) Installing c (1)
(3/4) Installing b (1)
(4/4) Installing a (2)
OK: 0 MiB in 0 packages
Installing d (1.5)
Installing c (1)
Installing b (1)
--raw-repository complicated1.repo
b
@ARGS
--test-repo complicated1.repo
add b
@EXPECT
(1/3) Installing d (1.5)
(2/3) Installing c (1)
(3/3) Installing b (1)
OK: 0 MiB in 0 packages
Installing d (2.0)
Installing c (1)
--raw-repository complicated1.repo
c
@ARGS
--test-repo complicated1.repo
add c
@EXPECT
(1/2) Installing d (2.0)
(2/2) Installing c (1)
OK: 0 MiB in 0 packages
Upgrading d (1.0 -> 1.5)
Installing c (1)
Installing b (1)
Installing a (2)
--raw-repository complicated1.repo --installed complicated1.installed
a
@ARGS
--test-repo complicated1.repo
--test-instdb complicated1.installed
--test-world d
add a
@EXPECT
(1/4) Upgrading d (1.0 -> 1.5)
(2/4) Installing c (1)
(3/4) Installing b (1)
(4/4) Installing a (2)
OK: 0 MiB in 1 packages
......@@ -3,7 +3,7 @@ P:a
V:1
S:1
I:1
D:!b<2
D:!b>1
C:Q1C4uoV7SdMdDhYg4OCVmI71D8HIA=
P:b
......
Installing b (2)
Installing a (1)
@ARGS
--test-repo conflict.repo
add a b
@EXPECT
(1/2) Installing b (1)
(2/2) Installing a (1)
OK: 0 MiB in 0 packages
1 unsatisfiable dependencies (solution with 2 names)
world: b<2
@ARGS
--test-repo conflict.repo
add a b>1
@EXPECT
ERROR: 1 unsatisfiable dependencies:
a-1: !b>1
1 unsatisfiable dependencies (solution with 4 names)
b-1: d<2.0
--raw-repository complicated1.repo
a d>1.5
@ARGS
--test-repo complicated1.repo
add a d>1.5
@EXPECT
ERROR: 1 unsatisfiable dependencies:
b-1: d<2.0
1 unsatisfiable dependencies (solution with 4 names)
world: d<1.5
--raw-repository complicated1.repo
a d<1.5
@ARGS
--test-repo complicated1.repo
add a d<1.5
@EXPECT
ERROR: 1 unsatisfiable dependencies:
world: d<1.5
1 unsatisfiable dependencies (solution with 3 names)
a-3: b
--raw-repository complicated1.repo
a !b
@ARGS
--test-repo complicated1.repo
add a !b
@EXPECT
ERROR: 1 unsatisfiable dependencies:
a-3: b
1 unsatisfiable dependencies (solution with 4 names)
world: nonexistant
--raw-repository complicated1.repo
a nonexistant
@ARGS
--test-repo complicated1.repo
add a nonexistant
@EXPECT
ERROR: 1 unsatisfiable dependencies:
world: nonexistant
1 unsatisfiable dependencies (solution with 4 names)
b-1: d<2.0
--raw-repository complicated1.repo
a>2
@ARGS
--test-repo complicated1.repo
add a>2
@EXPECT
ERROR: 1 unsatisfiable dependencies:
b-1: d<2.0
Installing foo (1)
Installing lib (1)
Installing app (1)
Installing appiif1 (1)
--raw-repository installif1.repo
foo app
@ARGS
--test-repo installif1.repo
add foo app
@EXPECT
(1/4) Installing foo (1)
(2/4) Installing lib (1)
(3/4) Installing app (1)
(4/4) Installing appiif1 (1)
OK: 0 MiB in 0 packages
Installing foo (1)
Installing lib (1)
Installing app (1)
Installing appiif1 (1)
Installing bar (1)
Installing appiif2 (1)
--raw-repository installif1.repo
foo app bar
@ARGS
--test-repo installif1.repo
add foo app bar
@EXPECT
(1/6) Installing foo (1)
(2/6) Installing lib (1)
(3/6) Installing app (1)
(4/6) Installing appiif1 (1)
(5/6) Installing bar (1)
(6/6) Installing appiif2 (1)
OK: 0 MiB in 0 packages
Installing foo (1)
Installing lib (1)
Installing app (1)
Installing appiif1 (1)
--raw-repository installif1.repo --raw-repository installif2.repo
foo app
@ARGS
--test-repo installif1.repo
--test-repo installif2.repo
add foo app
@EXPECT
(1/4) Installing foo (1)
(2/4) Installing lib (1)
(3/4) Installing app (1)
(4/4) Installing appiif1 (1)
OK: 0 MiB in 0 packages
Installing b (2)
Installing a (2)
--raw-repository basic.repo --raw-repository +pinning.repo
a
@ARGS
--test-repo basic.repo
--test-repo +pinning.repo
add a
@EXPECT
(1/2) Installing b (2)
(2/2) Installing a (2)
OK: 0 MiB in 0 packages
Installing b (2)
Installing a (3)
--raw-repository basic.repo --raw-repository +pinning.repo
a@testing
@ARGS
--test-repo basic.repo
--test-repo +pinning.repo
add a@testing
@EXPECT
(1/2) Installing b (2)
(2/2) Installing a@testing (3)
OK: 0 MiB in 0 packages