1 #include <config.h>
2 
3 #include "testutils.h"
4 
5 #ifdef WITH_OPENVZ
6 
7 # include <unistd.h>
8 
9 # include "internal.h"
10 # include "viralloc.h"
11 # include "openvz/openvz_conf.h"
12 # include "virstring.h"
13 
14 # define VIR_FROM_THIS VIR_FROM_OPENVZ
15 
16 static int
testLocateConfFile(int vpsid G_GNUC_UNUSED,char ** conffile,const char * ext G_GNUC_UNUSED)17 testLocateConfFile(int vpsid G_GNUC_UNUSED, char **conffile,
18                    const char *ext G_GNUC_UNUSED)
19 {
20     *conffile = g_strdup_printf("%s/openvzutilstest.conf", abs_srcdir);
21     return 0;
22 }
23 
24 struct testConfigParam {
25     const char *param;
26     const char *value;
27     int ret;
28 };
29 
30 static struct testConfigParam configParams[] = {
31     { "OSTEMPLATE", "rhel-5-lystor", 1 },
32     { "IP_ADDRESS", "194.44.18.88", 1 },
33     { "THIS_PARAM_IS_MISSING", NULL, 0 },
34 };
35 
36 static int
testReadConfigParam(const void * data G_GNUC_UNUSED)37 testReadConfigParam(const void *data G_GNUC_UNUSED)
38 {
39     size_t i;
40     g_autofree char *conf = NULL;
41     g_autofree char *value = NULL;
42 
43     conf = g_strdup_printf("%s/openvzutilstest.conf", abs_srcdir);
44 
45     for (i = 0; i < G_N_ELEMENTS(configParams); ++i) {
46         if (openvzReadConfigParam(conf, configParams[i].param,
47                                   &value) != configParams[i].ret) {
48             return -1;
49         }
50 
51         if (configParams[i].ret != 1)
52             continue;
53 
54         if (STRNEQ(configParams[i].value, value)) {
55             virTestDifference(stderr, configParams[i].value, value);
56             return -1;
57         }
58     }
59 
60     return 0;
61 }
62 
63 static int
testReadNetworkConf(const void * data G_GNUC_UNUSED)64 testReadNetworkConf(const void *data G_GNUC_UNUSED)
65 {
66     int result = -1;
67     g_autoptr(virDomainDef) def = NULL;
68     g_autofree char *actual = NULL;
69     const char *expected =
70         "<domain type='openvz'>\n"
71         "  <uuid>00000000-0000-0000-0000-000000000000</uuid>\n"
72         "  <memory unit='KiB'>0</memory>\n"
73         "  <currentMemory unit='KiB'>0</currentMemory>\n"
74         "  <vcpu placement='static'>0</vcpu>\n"
75         "  <os>\n"
76         "    <type>exe</type>\n"
77         "    <init>/sbin/init</init>\n"
78         "  </os>\n"
79         "  <clock offset='utc'/>\n"
80         "  <on_poweroff>destroy</on_poweroff>\n"
81         "  <on_reboot>destroy</on_reboot>\n"
82         "  <on_crash>destroy</on_crash>\n"
83         "  <devices>\n"
84         "    <interface type='ethernet'>\n"
85         "      <mac address='00:00:00:00:00:00'/>\n"
86         "      <ip address='194.44.18.88' family='ipv4'/>\n"
87         "    </interface>\n"
88         "    <interface type='bridge'>\n"
89         "      <mac address='00:18:51:c1:05:ee'/>\n"
90         "      <target dev='veth105.10'/>\n"
91         "    </interface>\n"
92         "  </devices>\n"
93         "</domain>\n";
94     struct openvz_driver driver = {
95         .xmlopt = openvzXMLOption(&driver),
96         .caps = openvzCapsInit(),
97     };
98 
99     if (!(def = virDomainDefNew(driver.xmlopt)))
100         goto cleanup;
101 
102     def->os.init = g_strdup("/sbin/init");
103 
104     def->virtType = VIR_DOMAIN_VIRT_OPENVZ;
105     def->os.type = VIR_DOMAIN_OSTYPE_EXE;
106 
107     if (openvzReadNetworkConf(def, 1) < 0) {
108         fprintf(stderr, "ERROR: %s\n", virGetLastErrorMessage());
109         goto cleanup;
110     }
111 
112     actual = virDomainDefFormat(def, driver.xmlopt, VIR_DOMAIN_DEF_FORMAT_INACTIVE);
113 
114     if (actual == NULL) {
115         fprintf(stderr, "ERROR: %s\n", virGetLastErrorMessage());
116         goto cleanup;
117     }
118 
119     if (STRNEQ(expected, actual)) {
120         virTestDifference(stderr, expected, actual);
121         goto cleanup;
122     }
123 
124     result = 0;
125 
126  cleanup:
127     virObjectUnref(driver.xmlopt);
128     virObjectUnref(driver.caps);
129 
130     return result;
131 }
132 
133 static int
mymain(void)134 mymain(void)
135 {
136     int result = 0;
137 
138     openvzLocateConfFile = testLocateConfFile;
139 
140 # define DO_TEST(_name) \
141         do { \
142             if (virTestRun("OpenVZ "#_name, test##_name, \
143                             NULL) < 0) { \
144                 result = -1; \
145             } \
146         } while (0)
147 
148     DO_TEST(ReadConfigParam);
149     DO_TEST(ReadNetworkConf);
150 
151     return result == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
152 }
153 
154 VIR_TEST_MAIN(mymain)
155 
156 #else
157 
158 int main(void)
159 {
160     return EXIT_AM_SKIP;
161 }
162 
163 #endif /* WITH_OPENVZ */
164