4596e5df9014aacff632be7a522b1ec09e899777
[collectd.git] / src / virt_test.c
1 /**
2  * collectd - src/virt_test.c
3  * Copyright (C) 2016 Francesco Romani <fromani at redhat.com>
4  * Based on
5  * collectd - src/ceph_test.c
6  * Copyright (C) 2015      Florian octo Forster
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by the
10  * Free Software Foundation; only version 2 of the License is applicable.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
20  *
21  * Authors:
22  *   Florian octo Forster <octo at collectd.org>
23  **/
24
25 #include "testing.h"
26 #include "virt.c" /* sic */
27
28 virDomainPtr *domains;
29
30 static int setup(void) {
31   if (virInitialize() != 0) {
32     printf("ERROR: virInitialize() != 0\n");
33     return -1;
34   }
35
36   conn = virConnectOpen("test:///default");
37   if (conn == NULL) {
38     printf("ERROR: virConnectOpen == NULL\n");
39     return -1;
40   }
41
42   return 0;
43 }
44
45 static int teardown(void) {
46   sfree(domains);
47   if (conn != NULL)
48     virConnectClose(conn);
49
50   return 0;
51 }
52
53 #ifdef HAVE_LIST_ALL_DOMAINS
54 DEF_TEST(get_domain_state_notify) {
55   if (setup() == 0) {
56     int n_domains = virConnectListAllDomains(
57         conn, &domains, VIR_CONNECT_GET_ALL_DOMAINS_STATS_PERSISTENT);
58     if (n_domains <= 0) {
59       printf("ERROR: virConnectListAllDomains: n_domains <= 0\n");
60       return -1;
61     }
62
63     int ret = get_domain_state_notify(domains[0]);
64     EXPECT_EQ_INT(0, ret);
65   }
66   teardown();
67
68   return 0;
69 }
70 #endif
71
72 DEF_TEST(persistent_domains_state_notification) {
73   if (setup() == 0) {
74     int ret = persistent_domains_state_notification();
75     EXPECT_EQ_INT(0, ret);
76   }
77   teardown();
78
79   return 0;
80 }
81
82 int main(void) {
83 #ifdef HAVE_LIST_ALL_DOMAINS
84   RUN_TEST(get_domain_state_notify);
85 #endif
86   RUN_TEST(persistent_domains_state_notification);
87
88   END_TEST;
89 }