libmpm

morphux C package management library
Log | Files | Refs | Submodules | README | LICENSE | git clone https://git.ne02ptzero.me/git/libmpm

commit 222d4b92bf2422fd799df7dab58ff355ed2d247f
parent 5d0e65da7e7cf99c653ef6b9bc2e4c43e3aee3e3
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Sun, 28 May 2017 18:50:41 +0200

Add(Tests): Add tests for compilation

Diffstat:
Minc/compile.h | 14++++++++++++--
Msrc/compile.c | 52+++++++++++++++++++++++++++++++++++++++++++++++++---
Msrc/packer.c | 6++++++
Mtests/test_compile.c | 221+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
4 files changed, 284 insertions(+), 9 deletions(-)

diff --git a/inc/compile.h b/inc/compile.h @@ -22,9 +22,10 @@ # define COMP_BEFORE_SCRIPT PACKER_SCRIPT_DIR "before.sh" # define COMP_AFTER_SCRIPT PACKER_SCRIPT_DIR "after.sh" -# define PATCH_CMD "patch -Ni" +# define PATCH_CMD "patch -Ni" +# define CONFIGURE_CMD "./configure" -# define CONFIGURE_CMD "./configure" +# define DEFAULT_EXTRACT_DIR "/var/tmp/mpm_private" typedef enum { INST_STATE_NONE = 0, @@ -125,4 +126,13 @@ bool install_package(compile_t *ctx); */ bool after_package(compile_t *ctx); +/*! + * \brief Extract and Install an .mpx archive + * + * \param[in] ctx Archive to install + * + * \return true on success, false on failure + */ +bool install_archive(packer_t *ctx); + #endif /* COMPILE_H */ diff --git a/src/compile.c b/src/compile.c @@ -91,7 +91,11 @@ bool patch_package(compile_t *ctx) { if (dinfo->d_type != DT_DIR) { asprintf(&cmd, "%s ../%s%s", PATCH_CMD, PACKER_PATCH_DIR, dinfo->d_name); - exec_line(cmd); + if (exec_line(cmd) != 0) + { + free(cmd); + goto end; + } free(cmd); } } @@ -114,7 +118,10 @@ bool configure_package(compile_t *ctx) { /* Nothing to configure, we're good */ if (ctx->package->header->compilation->configure == NULL) + { + ret = true; goto end; + } if (chdir(PACKER_SRC_DIR) == -1) goto end; @@ -135,8 +142,7 @@ bool configure_package(compile_t *ctx) { list_free(cmd, NULL); - if (chdir("..") == -1) - goto end; + chdir(".."); end: ctx->state = INST_STATE_CONFIGURATION; @@ -194,3 +200,43 @@ end: ctx->state = INST_STATE_AFTER; return true; } + +bool install_archive(packer_t *ctx) { + bool ret = false; + compile_t *ptr = NULL; + + if (packer_extract_archive(ctx, DEFAULT_EXTRACT_DIR) == false) + { + packer_free(ctx); + return ret; + } + + ptr = package_install_init(ctx); + if (ptr == NULL) + goto end; + + if (before_package(ptr) == false) + goto end; + + if (patch_package(ptr) == false) + goto end; + + if (configure_package(ptr) == false) + goto end; + + if (make_package(ptr) == false) + goto end; + + if (install_package(ptr) == false) + goto end; + + if (after_package(ptr) == false) + goto end; + + ret = true; + +end: + recursive_delete(ctx->out_dir); + package_install_cleanup(ptr); + return ret; +} diff --git a/src/packer.c b/src/packer.c @@ -853,6 +853,12 @@ bool packer_extract_archive(packer_t *ctx, const char *dir) { ctr = t_ctr; + DIR *p_dir = opendir(dir); + if (p_dir == NULL) + mkdir(dir, S_IRWXU | S_IRWXG | S_IRWXO); + else + closedir(p_dir); + if (chdir(dir) == -1) goto cleanup; diff --git a/tests/test_compile.c b/tests/test_compile.c @@ -2,11 +2,13 @@ #define PACKAGE_OUTPUT_FN "package_compile" PACKER_DEF_EXT +# define OUTPUT_DIR "/tmp/test-2.0/" + TEST(init_compile) { packer_t *ptr; set_mkdir_fail(-1); - recursive_delete("/tmp/test-2.0"); + recursive_delete(OUTPUT_DIR); ptr = packer_init_dir("packer/compilation/"); TEST_ASSERT(packer_read_dir(ptr) == true, "An error happened"); TEST_ASSERT(packer_create_archive(ptr, PACKAGE_OUTPUT_FN) == true, @@ -16,20 +18,222 @@ TEST(init_compile) { return TEST_SUCCESS; } -TEST(configure_package) { +TEST(before_package) { packer_t *ctx = packer_init_archive(PACKAGE_OUTPUT_FN); compile_t *ptr = NULL; TEST_ASSERT(packer_extract_archive(ctx, "/tmp") == true, "An error happened"); ptr = package_install_init(ctx); TEST_ASSERT(ptr != NULL, "An error happened"); + set_fork_fail(0); + TEST_ASSERT(before_package(ptr) == false, "Error did not raise"); + chdir("/"); TEST_ASSERT(before_package(ptr) == true, "An error happened"); - TEST_ASSERT(patch_package(ptr) == true, "An error happened"); - TEST_ASSERT(configure_package(ptr) == true, "An error happened"); + TEST_ASSERT(package_install_cleanup(ptr) == true, "An error happened"); + recursive_delete(OUTPUT_DIR); + + return TEST_SUCCESS; +} + +TEST(patch_package) { + packer_t *ctx = packer_init_archive(PACKAGE_OUTPUT_FN); + compile_t *ptr = NULL; + + TEST_ASSERT(packer_extract_archive(ctx, "/tmp") == true, "An error happened"); + ptr = package_install_init(ctx); + TEST_ASSERT(ptr != NULL, "An error happened"); + + set_chdir_fail(0); + TEST_ASSERT(patch_package(ptr) == false, "Error did not raise"); + + set_chdir_fail(1); + TEST_ASSERT(patch_package(ptr) == false, "Error did not raise"); + + chdir("/"); + TEST_ASSERT(patch_package(ptr) == true, "Error did not raise"); + + TEST_ASSERT(package_install_cleanup(ptr) == true, "An error happened"); + recursive_delete(OUTPUT_DIR); + + return TEST_SUCCESS; +} + +TEST(configure_package) { + packer_t *ctx = packer_init_archive(PACKAGE_OUTPUT_FN); + compile_t *ptr = NULL; + mlist_t *tmp; + + TEST_ASSERT(packer_extract_archive(ctx, "/tmp") == true, "An error happened"); + ptr = package_install_init(ctx); + TEST_ASSERT(ptr != NULL, "An error happened"); + + tmp = ptr->package->header->compilation->configure; + ptr->package->header->compilation->configure = NULL; + TEST_ASSERT(configure_package(ptr) == true, "Return is wrong"); + ptr->package->header->compilation->configure = tmp; + + set_chdir_fail(0); + TEST_ASSERT(configure_package(ptr) == false, "Error did not raise"); + + TEST_ASSERT(package_install_cleanup(ptr) == true, "An error happened"); + recursive_delete(OUTPUT_DIR); + return TEST_SUCCESS; +} + +TEST(make_package) { + packer_t *ctx = packer_init_archive(PACKAGE_OUTPUT_FN); + compile_t *ptr = NULL; + char *tmp; + + TEST_ASSERT(packer_extract_archive(ctx, "/tmp") == true, "An error happened"); + ptr = package_install_init(ctx); + TEST_ASSERT(ptr != NULL, "An error happened"); + + tmp = ptr->package->header->compilation->make; + ptr->package->header->compilation->make = NULL; TEST_ASSERT(make_package(ptr) == true, "An error happened"); + ptr->package->header->compilation->make = tmp; + + set_chdir_fail(0); + TEST_ASSERT(make_package(ptr) == false, "Error did not raise"); + + set_fork_fail(0); + TEST_ASSERT(make_package(ptr) == false, "Error did not raise"); + + TEST_ASSERT(package_install_cleanup(ptr) == true, "An error happened"); + recursive_delete(OUTPUT_DIR); + return TEST_SUCCESS; +} + +TEST(install_package) { + packer_t *ctx = packer_init_archive(PACKAGE_OUTPUT_FN); + compile_t *ptr = NULL; + char *tmp; + + TEST_ASSERT(packer_extract_archive(ctx, "/tmp") == true, "An error happened"); + ptr = package_install_init(ctx); + TEST_ASSERT(ptr != NULL, "An error happened"); + + tmp = ptr->package->header->compilation->install; + ptr->package->header->compilation->install = NULL; TEST_ASSERT(install_package(ptr) == true, "An error happened"); + ptr->package->header->compilation->install = tmp; + + set_chdir_fail(0); + TEST_ASSERT(install_package(ptr) == false, "Error did not raise"); + + set_fork_fail(0); + TEST_ASSERT(install_package(ptr) == false, "Error did not raise"); + + TEST_ASSERT(package_install_cleanup(ptr) == true, "An error happened"); + recursive_delete(OUTPUT_DIR); + return TEST_SUCCESS; +} + +TEST(after_package) { + packer_t *ctx = packer_init_archive(PACKAGE_OUTPUT_FN); + compile_t *ptr = NULL; + + TEST_ASSERT(packer_extract_archive(ctx, "/tmp") == true, "An error happened"); + ptr = package_install_init(ctx); + TEST_ASSERT(ptr != NULL, "An error happened"); + set_fork_fail(0); + TEST_ASSERT(after_package(ptr) == false, "Error did not raise"); + chdir("/"); TEST_ASSERT(after_package(ptr) == true, "An error happened"); TEST_ASSERT(package_install_cleanup(ptr) == true, "An error happened"); + recursive_delete(OUTPUT_DIR); + + return TEST_SUCCESS; +} + +TEST(package_install_init) { + packer_t *ctx = packer_init_archive(PACKAGE_OUTPUT_FN); + compile_t *ptr = NULL; + mlist_t *tmp; + + TEST_ASSERT(packer_extract_archive(ctx, "/tmp") == true, "An error happened"); + set_malloc_fail(0); + TEST_ASSERT(package_install_init(ctx) == NULL, "Error did not raise"); + + set_chdir_fail(0); + TEST_ASSERT(package_install_init(ctx) == NULL, "Error did not raise"); + + tmp = ctx->header->compilation->env; + ctx->header->compilation->env = NULL; + + ptr = package_install_init(ctx); + TEST_ASSERT(ptr != NULL, "Error did not raise"); + ctx->header->compilation->env = tmp; + + TEST_ASSERT(package_install_cleanup(ptr) == true, "An error happened"); + recursive_delete(OUTPUT_DIR); + + return TEST_SUCCESS; + +} + +TEST(package_install_cleanup) { + packer_t *ctx = packer_init_archive(PACKAGE_OUTPUT_FN); + compile_t *ptr = NULL; + + TEST_ASSERT(packer_extract_archive(ctx, "/tmp") == true, "An error happened"); + ptr = package_install_init(ctx); + TEST_ASSERT(ptr != NULL, "An error happened"); + + set_chdir_fail(0); + TEST_ASSERT(package_install_cleanup(ptr) == false, "Error did not raise"); + TEST_ASSERT(package_install_cleanup(ptr) == true, "An error happened"); + recursive_delete(OUTPUT_DIR); + + return TEST_SUCCESS; +} + +TEST(full_install) { + packer_t *ctx = packer_init_archive(PACKAGE_OUTPUT_FN); + + TEST_ASSERT(install_archive(ctx) == true, "An error happened") + return TEST_SUCCESS; +} + +TEST(full_install_2) { + packer_t *ctx; + + ctx = packer_init_archive(PACKAGE_OUTPUT_FN); + set_malloc_fail(0); + TEST_ASSERT(install_archive(ctx) == false, "Error did not raise"); + + ctx = packer_init_archive(PACKAGE_OUTPUT_FN); + set_malloc_fail(9); + TEST_ASSERT(install_archive(ctx) == false, "Error did not raise"); + + ctx = packer_init_archive(PACKAGE_OUTPUT_FN); + set_fork_fail(0); + TEST_ASSERT(install_archive(ctx) == false, "Error did not raise"); + + ctx = packer_init_archive(PACKAGE_OUTPUT_FN); + set_fork_fail(1); + TEST_ASSERT(install_archive(ctx) == false, "Error did not raise"); + + ctx = packer_init_archive(PACKAGE_OUTPUT_FN); + set_fork_fail(2); + TEST_ASSERT(install_archive(ctx) == false, "Error did not raise"); + recursive_delete(ctx->out_dir); + + ctx = packer_init_archive(PACKAGE_OUTPUT_FN); + set_fork_fail(3); + TEST_ASSERT(install_archive(ctx) == false, "Error did not raise"); + recursive_delete(ctx->out_dir); + + ctx = packer_init_archive(PACKAGE_OUTPUT_FN); + set_fork_fail(4); + TEST_ASSERT(install_archive(ctx) == false, "Error did not raise"); + recursive_delete(ctx->out_dir); + + ctx = packer_init_archive(PACKAGE_OUTPUT_FN); + set_fork_fail(5); + TEST_ASSERT(install_archive(ctx) == false, "Error did not raise"); + recursive_delete(ctx->out_dir); return TEST_SUCCESS; } @@ -41,6 +245,15 @@ TEST(compile_cleanup) { void register_test_compile(void) { reg_test("compile", init_compile) + reg_test("compile", before_package); + reg_test("compile", patch_package); reg_test("compile", configure_package); + reg_test("compile", make_package); + reg_test("compile", install_package); + reg_test("compile", after_package); + reg_test("compile", full_install); + reg_test("compile", package_install_init); + reg_test("compile", package_install_cleanup); + reg_test("compile", full_install_2); reg_test("compile", compile_cleanup); }