summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias Bolte <matthias.bolte@googlemail.com>2011-05-31 14:58:58 +0200
committerMatthias Bolte <matthias.bolte@googlemail.com>2011-06-01 11:58:15 +0200
commitd6caacd1e439e0ee1f7aa5b3039fdb84f1164700 (patch)
treed23bbc97e345790525d0aefadee433e33061f8f4
parentopenvz: Set virtType to openvz in openvzLoadDomains (diff)
downloadlibvirt-d6caacd1e439e0ee1f7aa5b3039fdb84f1164700.tar.gz
libvirt-d6caacd1e439e0ee1f7aa5b3039fdb84f1164700.tar.bz2
libvirt-d6caacd1e439e0ee1f7aa5b3039fdb84f1164700.zip
openvz: Add simple test for openvzReadNetworkConf
Convert openvzLocateConfFile to a replaceable function pointer to allow testing the config file parsing without rewriting the whole OpenVZ config parsing to a more testable structure.
-rw-r--r--src/openvz/openvz_conf.c19
-rw-r--r--src/openvz/openvz_conf.h6
-rw-r--r--tests/openvzutilstest.c78
-rw-r--r--tests/openvzutilstest.conf1
4 files changed, 95 insertions, 9 deletions
diff --git a/src/openvz/openvz_conf.c b/src/openvz/openvz_conf.c
index 9ce92dc69..189279dbe 100644
--- a/src/openvz/openvz_conf.c
+++ b/src/openvz/openvz_conf.c
@@ -58,8 +58,10 @@
static char *openvzLocateConfDir(void);
static int openvzGetVPSUUID(int vpsid, char *uuidstr, size_t len);
-static int openvzLocateConfFile(int vpsid, char **conffile, const char *ext);
static int openvzAssignUUIDs(void);
+static int openvzLocateConfFileDefault(int vpsid, char **conffile, const char *ext);
+
+openvzLocateConfFileFunc openvzLocateConfFile = openvzLocateConfFileDefault;
int
strtoI(const char *str)
@@ -171,7 +173,7 @@ no_memory:
}
-static int
+int
openvzReadNetworkConf(virDomainDefPtr def,
int veid) {
int ret;
@@ -801,13 +803,12 @@ cleanup:
}
/* Locate config file of container
-* return -1 - error
-* 0 - OK
-*/
+ * return -1 - error
+ * 0 - OK */
static int
-openvzLocateConfFile(int vpsid, char **conffile, const char *ext)
+openvzLocateConfFileDefault(int vpsid, char **conffile, const char *ext)
{
- char * confdir;
+ char *confdir;
int ret = 0;
confdir = openvzLocateConfDir();
@@ -824,8 +825,8 @@ openvzLocateConfFile(int vpsid, char **conffile, const char *ext)
return ret;
}
-static char
-*openvzLocateConfDir(void)
+static char *
+openvzLocateConfDir(void)
{
const char *conf_dir_list[] = {"/etc/vz/conf", "/usr/local/etc/conf", NULL};
int i=0;
diff --git a/src/openvz/openvz_conf.h b/src/openvz/openvz_conf.h
index d5a57a616..9f2245f5c 100644
--- a/src/openvz/openvz_conf.h
+++ b/src/openvz/openvz_conf.h
@@ -53,6 +53,11 @@ struct openvz_driver {
int version;
};
+typedef int (*openvzLocateConfFileFunc)(int vpsid, char **conffile, const char *ext);
+
+/* this allows the testsuite to replace the conf file locator function */
+extern openvzLocateConfFileFunc openvzLocateConfFile;
+
int openvz_readline(int fd, char *ptr, int maxlen);
int openvzExtractVersion(struct openvz_driver *driver);
int openvzReadVPSConfigParam(int vpsid, const char *param, char **value);
@@ -66,5 +71,6 @@ int strtoI(const char *str);
int openvzSetDefinedUUID(int vpsid, unsigned char *uuid);
unsigned int openvzGetNodeCPUs(void);
int openvzGetVEID(const char *name);
+int openvzReadNetworkConf(virDomainDefPtr def, int veid);
#endif /* OPENVZ_CONF_H */
diff --git a/tests/openvzutilstest.c b/tests/openvzutilstest.c
index fe6a2ea7f..f04ddbbce 100644
--- a/tests/openvzutilstest.c
+++ b/tests/openvzutilstest.c
@@ -12,6 +12,13 @@
# include "util.h"
# include "openvz/openvz_conf.h"
+static int
+testLocateConfFile(int vpsid ATTRIBUTE_UNUSED, char **conffile,
+ const char *ext ATTRIBUTE_UNUSED)
+{
+ return virAsprintf(conffile, "%s/openvzutilstest.conf", abs_srcdir);
+}
+
struct testConfigParam {
const char *param;
const char *value;
@@ -62,10 +69,80 @@ cleanup:
}
static int
+testReadNetworkConf(const void *data ATTRIBUTE_UNUSED)
+{
+ int result = -1;
+ virDomainDefPtr def = NULL;
+ char *actual = NULL;
+ virErrorPtr err = NULL;
+ const char *expected =
+ "<domain type='openvz'>\n"
+ " <uuid>00000000-0000-0000-0000-000000000000</uuid>\n"
+ " <memory>0</memory>\n"
+ " <currentMemory>0</currentMemory>\n"
+ " <vcpu>0</vcpu>\n"
+ " <os>\n"
+ " <type>exe</type>\n"
+ " <init>/sbin/init</init>\n"
+ " </os>\n"
+ " <clock offset='utc'/>\n"
+ " <on_poweroff>destroy</on_poweroff>\n"
+ " <on_reboot>destroy</on_reboot>\n"
+ " <on_crash>destroy</on_crash>\n"
+ " <devices>\n"
+ " <interface type='ethernet'>\n"
+ " <mac address='00:00:00:00:00:00'/>\n"
+ " <ip address='194.44.18.88'/>\n"
+ " </interface>\n"
+ " <interface type='bridge'>\n"
+ " <mac address='00:18:51:c1:05:ee'/>\n"
+ " <target dev='veth105.10'/>\n"
+ " </interface>\n"
+ " </devices>\n"
+ "</domain>\n";
+
+ if (VIR_ALLOC(def) < 0 ||
+ !(def->os.type = strdup("exe")) ||
+ !(def->os.init = strdup("/sbin/init")))
+ goto cleanup;
+
+ def->virtType = VIR_DOMAIN_VIRT_OPENVZ;
+
+ if (openvzReadNetworkConf(def, 1) < 0) {
+ err = virGetLastError();
+ fprintf(stderr, "ERROR: %s\n", err != NULL ? err->message : "<unknown>");
+ goto cleanup;
+ }
+
+ actual = virDomainDefFormat(def, VIR_DOMAIN_XML_INACTIVE);
+
+ if (actual == NULL) {
+ err = virGetLastError();
+ fprintf(stderr, "ERROR: %s\n", err != NULL ? err->message : "<unknown>");
+ goto cleanup;
+ }
+
+ if (STRNEQ(expected, actual)) {
+ virtTestDifference(stderr, expected, actual);
+ goto cleanup;
+ }
+
+ result = 0;
+
+cleanup:
+ VIR_FREE(actual);
+ virDomainDefFree(def);
+
+ return result;
+}
+
+static int
mymain(void)
{
int result = 0;
+ openvzLocateConfFile = testLocateConfFile;
+
# define DO_TEST(_name) \
do { \
if (virtTestRun("OpenVZ "#_name, 1, test##_name, \
@@ -75,6 +152,7 @@ mymain(void)
} while (0)
DO_TEST(ReadConfigParam);
+ DO_TEST(ReadNetworkConf);
return result == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}
diff --git a/tests/openvzutilstest.conf b/tests/openvzutilstest.conf
index a1b93b7c8..c5d048c5f 100644
--- a/tests/openvzutilstest.conf
+++ b/tests/openvzutilstest.conf
@@ -39,3 +39,4 @@ QUOTATIME=""
DISK_QUOTA=no
OSTEMPLATE="rhel-5-lystor"
IP_ADDRESS="194.44.18.88"
+NETIF="ifname=eth10,mac=00:18:51:C1:05:EE,host_ifname=veth105.10,host_mac=00:18:51:8F:D9:F3