Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 51bcf47fa1
Fetching contributors…

Cannot retrieve contributors at this time

file 199 lines (168 sloc) 4.777 kb
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
#define _GNU_SOURCE
#include <check.h>
#include <fcntl.h>
#include <dirent.h>
#include <errno.h>
#include <unistd.h>
#include <wordexp.h>
#include <sys/stat.h>
#include <sys/types.h>

// libsolv
#include <solv/repo.h>
#include <solv/testcase.h>

// hawkey
#include "src/iutil.h"
#include "src/query.h"
#include "src/sack_internal.h"
#include "src/util.h"
#include "testsys.h"

/* define the global variable */
struct TestGlobals_s test_globals;

HyPackage
by_name(HySack sack, const char *name)
{
    HyQuery q = hy_query_create(sack);
    hy_query_filter(q, HY_PKG_NAME, HY_EQ, name);
    HyPackageList plist = hy_query_run(q);
    hy_query_free(q);
    HyPackage pkg = hy_packagelist_get_clone(plist, 0);
    hy_packagelist_free(plist);

    return pkg;
}

void
dump_packagelist(HyPackageList plist)
{
    for (int i = 0; i < hy_packagelist_count(plist); ++i) {
HyPackage pkg = hy_packagelist_get(plist, i);
char *nvra = hy_package_get_nvra(pkg);
printf("\t%s\n", nvra);
hy_free(nvra);
    }
}

HyRepo
glob_for_repofiles(Pool *pool, const char *repo_name, const char *path)
{
    HyRepo repo = hy_repo_create();
    const char *tmpl;
    wordexp_t word_vector;

    hy_repo_set_string(repo, HY_REPO_NAME, repo_name);

    tmpl = pool_tmpjoin(pool, path, "/repomd.xml", NULL);
    if (wordexp(tmpl, &word_vector, 0) || word_vector.we_wordc < 1)
goto fail;
    hy_repo_set_string(repo, HY_REPO_MD_FN, word_vector.we_wordv[0]);

    tmpl = pool_tmpjoin(pool, path, "/*primary.xml.gz", NULL);
    if (wordexp(tmpl, &word_vector, WRDE_REUSE) || word_vector.we_wordc < 1)
goto fail;
    hy_repo_set_string(repo, HY_REPO_PRIMARY_FN, word_vector.we_wordv[0]);

    tmpl = pool_tmpjoin(pool, path, "/*filelists.xml.gz", NULL);
    if (wordexp(tmpl, &word_vector, WRDE_REUSE) || word_vector.we_wordc < 1)
goto fail;
    hy_repo_set_string(repo, HY_REPO_FILELISTS_FN, word_vector.we_wordv[0]);

    tmpl = pool_tmpjoin(pool, path, "/*prestodelta.xml.gz", NULL);
    if (wordexp(tmpl, &word_vector, WRDE_REUSE) || word_vector.we_wordc < 1)
goto fail;
    hy_repo_set_string(repo, HY_REPO_PRESTO_FN, word_vector.we_wordv[0]);

    wordfree(&word_vector);
    return repo;

 fail:
    wordfree(&word_vector);
    hy_repo_free(repo);
    return NULL;
}

int
load_repo(Pool *pool, const char *name, const char *path, int installed)
{
    Repo *r = repo_create(pool, name);
    FILE *fp = fopen(path, "r");

    if (!fp)
return 1;
    testcase_add_testtags(r, fp, 0);
    if (installed)
pool_set_installed(pool, r);
    fclose(fp);
    return 0;
}

int
logfile_size(HySack sack)
{
    const int fd = fileno(sack->log_out);
    struct stat st;

    fstat(fd, &st);
    return st.st_size;
}

int
query_count_results(HyQuery query)
{
    HyPackageList plist = hy_query_run(query);
    int ret = hy_packagelist_count(plist);
    hy_packagelist_free(plist);
    return ret;
}

void
setup_empty_sack(void)
{
    HySack sack = hy_sack_create(test_globals.tmpdir, TEST_FIXED_ARCH);
    test_globals.sack = sack;
    HY_LOG_INFO("HySack for UT created: %p", sack);
}

void
setup(void)
{
    setup_empty_sack();
    Pool *pool = sack_pool(test_globals.sack);
    const char *path = pool_tmpjoin(pool, test_globals.repo_dir,
"system.repo", 0);

    fail_if(load_repo(pool, HY_SYSTEM_REPO_NAME, path, 1));
}

void
setup_with_main(void)
{
    setup();
    Pool *pool = sack_pool(test_globals.sack);
    const char *path = pool_tmpjoin(pool, test_globals.repo_dir,
"main.repo", 0);
    fail_if(load_repo(pool, "main", path, 0));
}

void
setup_with_updates(void)
{
    setup();
    Pool *pool = sack_pool(test_globals.sack);
    const char *path = pool_tmpjoin(pool, test_globals.repo_dir,
"updates.repo", 0);
    fail_if(load_repo(pool, "updates", path, 0));
}

void setup_all(void)
{
    setup_with_updates();
    Pool *pool = sack_pool(test_globals.sack);
    const char *path = pool_tmpjoin(pool, test_globals.repo_dir,
"main.repo", 0);

    fail_if(load_repo(pool, "main", path, 0));
}

void setup_yum_sack(HySack sack)
{
    Pool *pool = sack_pool(sack);
    const char *repo_path = pool_tmpjoin(pool, test_globals.repo_dir,
YUM_DIR_SUFFIX, NULL);
    fail_if(access(repo_path, X_OK));
    HyRepo repo = glob_for_repofiles(pool, "tfilenames", repo_path);

    hy_sack_load_yum_repo(sack, repo);
    fail_unless(pool->nsolvables ==
TEST_META_SOLVABLES_COUNT + TEST_EXPECT_YUM_NSOLVABLES);
    hy_repo_free(repo);
}

void setup_yum(void)
{
    setup_empty_sack();
    HySack sack = test_globals.sack;
    setup_yum_sack(sack);

    fail_if(hy_sack_write_all_repos(sack)); // the next setup_yum() will use the cache
    fail_if(hy_sack_load_filelists(sack));
}

void
teardown(void)
{
    hy_sack_free(test_globals.sack);
    test_globals.sack = NULL;
}
Something went wrong with that request. Please try again.