Imported Upstream version 0.10.0
[platform/upstream/augeas.git] / tests / test-save.c
1 /*
2  * test-save.c: test various aspects of saving
3  *
4  * Copyright (C) 2009-2011 Red Hat Inc.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
19  *
20  * Author: David Lutterkort <lutter@redhat.com>
21  */
22
23 #include <config.h>
24 #include "augeas.h"
25 #include "internal.h"
26 #include "cutest.h"
27
28 #include <stdio.h>
29 #include <sys/types.h>
30 #include <sys/wait.h>
31
32 const char *abs_top_srcdir;
33 const char *abs_top_builddir;
34 char *root = NULL, *src_root = NULL;
35 struct augeas *aug = NULL;
36
37 #define die(msg)                                                    \
38     do {                                                            \
39         fprintf(stderr, "%s:%d: Fatal error: %s\n", __FILE__, __LINE__, msg); \
40         exit(EXIT_FAILURE);                                         \
41     } while(0)
42
43 static void setup(CuTest *tc) {
44     char *lensdir;
45
46     if (asprintf(&root, "%s/build/test-save/%s",
47                  abs_top_builddir, tc->name) < 0) {
48         CuFail(tc, "failed to set root");
49     }
50
51     if (asprintf(&lensdir, "%s/lenses", abs_top_srcdir) < 0)
52         CuFail(tc, "asprintf lensdir failed");
53
54     run(tc, "test -d %s && chmod -R u+w %s || :", root, root);
55     run(tc, "rm -rf %s", root);
56     run(tc, "mkdir -p %s", root);
57     run(tc, "cp -pr %s/* %s", src_root, root);
58     run(tc, "chmod -R u+w %s", root);
59
60     aug = aug_init(root, lensdir, AUG_NO_STDINC);
61     CuAssertPtrNotNull(tc, aug);
62 }
63
64 static void teardown(ATTRIBUTE_UNUSED CuTest *tc) {
65     aug_close(aug);
66     aug = NULL;
67     free(root);
68     root = NULL;
69 }
70
71 static void testSaveNewFile(CuTest *tc) {
72     int r;
73
74     r = aug_match(aug, "/augeas/files/etc/yum.repos.d/new.repo/path", NULL);
75     CuAssertIntEquals(tc, 0, r);
76
77     r = aug_set(aug, "/files/etc/yum.repos.d/new.repo/newrepo/baseurl",
78                 "http://foo.com/");
79     CuAssertIntEquals(tc, 0, r);
80
81     r = aug_save(aug);
82     CuAssertIntEquals(tc, 0, r);
83
84     r = aug_match(aug, "/augeas/files/etc/yum.repos.d/new.repo/path", NULL);
85     CuAssertIntEquals(tc, 1, r);
86 }
87
88 static void testNonExistentLens(CuTest *tc) {
89     int r;
90
91     r = aug_rm(aug, "/augeas/load/*");
92     CuAssertTrue(tc, r >= 0);
93
94     r = aug_set(aug, "/augeas/load/Fake/lens", "Fake.lns");
95     CuAssertIntEquals(tc, 0, r);
96     r = aug_set(aug, "/augeas/load/Fake/incl", "/fake");
97     CuAssertIntEquals(tc, 0, r);
98     r = aug_set(aug, "/files/fake/entry", "value");
99     CuAssertIntEquals(tc, 0, r);
100
101     r = aug_save(aug);
102     CuAssertIntEquals(tc, -1, r);
103     r = aug_error(aug);
104     CuAssertIntEquals(tc, AUG_ENOLENS, r);
105 }
106
107 static void testMultipleXfm(CuTest *tc) {
108     int r;
109
110     r = aug_set(aug, "/augeas/load/Yum2/lens", "Yum.lns");
111     CuAssertIntEquals(tc, 0, r);
112     r = aug_set(aug, "/augeas/load/Yum2/incl", "/etc/yum.repos.d/*");
113     CuAssertIntEquals(tc, 0, r);
114
115     r = aug_set(aug, "/files/etc/yum.repos.d/fedora.repo/fedora/enabled", "0");
116     CuAssertIntEquals(tc, 0, r);
117
118     r = aug_save(aug);
119     CuAssertIntEquals(tc, -1, r);
120     r = aug_error(aug);
121     CuAssertIntEquals(tc, AUG_EMXFM, r);
122 }
123
124 static void testMtime(CuTest *tc) {
125     const char *s, *mtime2;
126     char *mtime1;
127     int r;
128
129     r = aug_set(aug, "/files/etc/hosts/1/alias[last() + 1]", "new");
130     CuAssertIntEquals(tc, 0, r);
131
132     r = aug_get(aug, "/augeas/files/etc/hosts/mtime", &s);
133     CuAssertIntEquals(tc, 1, r);
134     mtime1 = strdup(s);
135     CuAssertPtrNotNull(tc, mtime1);
136
137
138     r = aug_save(aug);
139     CuAssertIntEquals(tc, 0, r);
140
141     r = aug_get(aug, "/augeas/files/etc/hosts/mtime", &mtime2);
142     CuAssertIntEquals(tc, 1, r);
143
144     CuAssertStrNotEqual(tc, mtime1, mtime2);
145     CuAssertStrNotEqual(tc, "0", mtime2);
146 }
147
148 /* Check that loading and saving a file given with a relative path
149  * works. Bug #238
150  */
151 static void testRelPath(CuTest *tc) {
152     int r;
153
154     r = aug_rm(aug, "/augeas/load/*");
155     CuAssertPositive(tc, r);
156
157     r = aug_set(aug, "/augeas/load/Hosts/lens", "Hosts.lns");
158     CuAssertRetSuccess(tc, r);
159     r = aug_set(aug, "/augeas/load/Hosts/incl", "etc/hosts");
160     CuAssertRetSuccess(tc, r);
161     r = aug_load(aug);
162     CuAssertRetSuccess(tc, r);
163
164     r = aug_match(aug, "/files/etc/hosts/1/alias[ . = 'new']", NULL);
165     CuAssertIntEquals(tc, 0, r);
166
167     r = aug_set(aug, "/files/etc/hosts/1/alias[last() + 1]", "new");
168     CuAssertRetSuccess(tc, r);
169
170     r = aug_save(aug);
171     CuAssertRetSuccess(tc, r);
172     r = aug_match(aug, "/augeas//error", NULL);
173     CuAssertIntEquals(tc, 0, r);
174
175     /* Force reloading the file */
176     r = aug_rm(aug, "/augeas/files//mtime");
177     CuAssertPositive(tc, r);
178
179     r = aug_load(aug);
180     CuAssertRetSuccess(tc, r);
181
182     r = aug_match(aug, "/files/etc/hosts/1/alias[. = 'new']", NULL);
183     CuAssertIntEquals(tc, 1, r);
184 }
185
186 int main(void) {
187     char *output = NULL;
188     CuSuite* suite = CuSuiteNew();
189
190     abs_top_srcdir = getenv("abs_top_srcdir");
191     if (abs_top_srcdir == NULL)
192         die("env var abs_top_srcdir must be set");
193
194     abs_top_builddir = getenv("abs_top_builddir");
195     if (abs_top_builddir == NULL)
196         die("env var abs_top_builddir must be set");
197
198     if (asprintf(&src_root, "%s/tests/root", abs_top_srcdir) < 0) {
199         die("failed to set src_root");
200     }
201
202     CuSuiteSetup(suite, setup, teardown);
203
204     SUITE_ADD_TEST(suite, testSaveNewFile);
205     SUITE_ADD_TEST(suite, testNonExistentLens);
206     SUITE_ADD_TEST(suite, testMultipleXfm);
207     SUITE_ADD_TEST(suite, testMtime);
208     SUITE_ADD_TEST(suite, testRelPath);
209
210     CuSuiteRun(suite);
211     CuSuiteSummary(suite, &output);
212     CuSuiteDetails(suite, &output);
213     printf("%s\n", output);
214     free(output);
215     return suite->failCount;
216 }
217
218 /*
219  * Local variables:
220  *  indent-tabs-mode: nil
221  *  c-indent-level: 4
222  *  c-basic-offset: 4
223  *  tab-width: 4
224  * End:
225  */