summaryrefslogtreecommitdiff
path: root/tests/domainconftest.c
blob: f48c3ca682a60fdb93fd063ce118f96dce1b0346 (plain)
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
/*
 * Copyright (C) 2013 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see
 * <http://www.gnu.org/licenses/>.
 */

#include <config.h>

#include "testutils.h"
#include "virerror.h"
#include "viralloc.h"
#include "virlog.h"

#include "domain_conf.h"

#define VIR_FROM_THIS VIR_FROM_NONE

VIR_LOG_INIT("tests.domainconftest");

static virCapsPtr caps;
static virDomainXMLOptionPtr xmlopt;

struct testGetFilesystemData {
    const char *filename;
    const char *path;
    bool expectEntry;
};

static int testGetFilesystem(const void *opaque)
{
    int ret = -1;
    virDomainDefPtr def = NULL;
    char *filename = NULL;
    const struct testGetFilesystemData *data = opaque;
    virDomainFSDefPtr fsdef;

    filename = g_strdup_printf("%s/domainconfdata/%s.xml", abs_srcdir,
                               data->filename);

    if (!(def = virDomainDefParseFile(filename, caps, xmlopt, NULL, 0)))
        goto cleanup;

    fsdef = virDomainGetFilesystemForTarget(def,
                                            data->path);
    if (!fsdef) {
        if (data->expectEntry) {
            fprintf(stderr, "Expected FS for path '%s' in '%s'\n",
                    data->path, filename);
            goto cleanup;
        }
    } else {
        if (!data->expectEntry) {
            fprintf(stderr, "Unexpected FS for path '%s' in '%s'\n",
                    data->path, filename);
            goto cleanup;
        }
    }

    ret = 0;

 cleanup:
    virDomainDefFree(def);
    VIR_FREE(filename);
    return ret;
}

static int
mymain(void)
{
    int ret = 0;

    if ((caps = virTestGenericCapsInit()) == NULL)
        goto cleanup;

    if (!(xmlopt = virTestGenericDomainXMLConfInit()))
        goto cleanup;

#define DO_TEST_GET_FS(fspath, expect) \
    do { \
        struct testGetFilesystemData data = { \
            .filename = "getfilesystem", \
            .path = fspath, \
            .expectEntry = expect, \
        }; \
        if (virTestRun("Get FS " fspath, testGetFilesystem, &data) < 0) \
            ret = -1; \
    } while (0)

    DO_TEST_GET_FS("/", true);
    DO_TEST_GET_FS("/dev", true);
    DO_TEST_GET_FS("/dev/pts", false);
    DO_TEST_GET_FS("/doesnotexist", false);

    virObjectUnref(caps);
    virObjectUnref(xmlopt);

 cleanup:
    return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}

VIR_TEST_MAIN(mymain)