2 * test-save.c: test various aspects of saving
4 * Copyright (C) 2009-2011 Red Hat Inc.
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.
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.
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
20 * Author: David Lutterkort <lutter@redhat.com>
29 #include <sys/types.h>
32 const char *abs_top_srcdir;
33 const char *abs_top_builddir;
34 char *root = NULL, *src_root = NULL;
35 struct augeas *aug = NULL;
39 fprintf(stderr, "%s:%d: Fatal error: %s\n", __FILE__, __LINE__, msg); \
43 static void setup(CuTest *tc) {
46 if (asprintf(&root, "%s/build/test-save/%s",
47 abs_top_builddir, tc->name) < 0) {
48 CuFail(tc, "failed to set root");
51 if (asprintf(&lensdir, "%s/lenses", abs_top_srcdir) < 0)
52 CuFail(tc, "asprintf lensdir failed");
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);
60 aug = aug_init(root, lensdir, AUG_NO_STDINC);
61 CuAssertPtrNotNull(tc, aug);
64 static void teardown(ATTRIBUTE_UNUSED CuTest *tc) {
71 static void testSaveNewFile(CuTest *tc) {
74 r = aug_match(aug, "/augeas/files/etc/yum.repos.d/new.repo/path", NULL);
75 CuAssertIntEquals(tc, 0, r);
77 r = aug_set(aug, "/files/etc/yum.repos.d/new.repo/newrepo/baseurl",
79 CuAssertIntEquals(tc, 0, r);
82 CuAssertIntEquals(tc, 0, r);
84 r = aug_match(aug, "/augeas/files/etc/yum.repos.d/new.repo/path", NULL);
85 CuAssertIntEquals(tc, 1, r);
88 static void testNonExistentLens(CuTest *tc) {
91 r = aug_rm(aug, "/augeas/load/*");
92 CuAssertTrue(tc, r >= 0);
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);
102 CuAssertIntEquals(tc, -1, r);
104 CuAssertIntEquals(tc, AUG_ENOLENS, r);
107 static void testMultipleXfm(CuTest *tc) {
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);
115 r = aug_set(aug, "/files/etc/yum.repos.d/fedora.repo/fedora/enabled", "0");
116 CuAssertIntEquals(tc, 0, r);
119 CuAssertIntEquals(tc, -1, r);
121 CuAssertIntEquals(tc, AUG_EMXFM, r);
124 static void testMtime(CuTest *tc) {
125 const char *s, *mtime2;
129 r = aug_set(aug, "/files/etc/hosts/1/alias[last() + 1]", "new");
130 CuAssertIntEquals(tc, 0, r);
132 r = aug_get(aug, "/augeas/files/etc/hosts/mtime", &s);
133 CuAssertIntEquals(tc, 1, r);
135 CuAssertPtrNotNull(tc, mtime1);
139 CuAssertIntEquals(tc, 0, r);
141 r = aug_get(aug, "/augeas/files/etc/hosts/mtime", &mtime2);
142 CuAssertIntEquals(tc, 1, r);
144 CuAssertStrNotEqual(tc, mtime1, mtime2);
145 CuAssertStrNotEqual(tc, "0", mtime2);
148 /* Check that loading and saving a file given with a relative path
151 static void testRelPath(CuTest *tc) {
154 r = aug_rm(aug, "/augeas/load/*");
155 CuAssertPositive(tc, r);
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);
162 CuAssertRetSuccess(tc, r);
164 r = aug_match(aug, "/files/etc/hosts/1/alias[ . = 'new']", NULL);
165 CuAssertIntEquals(tc, 0, r);
167 r = aug_set(aug, "/files/etc/hosts/1/alias[last() + 1]", "new");
168 CuAssertRetSuccess(tc, r);
171 CuAssertRetSuccess(tc, r);
172 r = aug_match(aug, "/augeas//error", NULL);
173 CuAssertIntEquals(tc, 0, r);
175 /* Force reloading the file */
176 r = aug_rm(aug, "/augeas/files//mtime");
177 CuAssertPositive(tc, r);
180 CuAssertRetSuccess(tc, r);
182 r = aug_match(aug, "/files/etc/hosts/1/alias[. = 'new']", NULL);
183 CuAssertIntEquals(tc, 1, r);
188 CuSuite* suite = CuSuiteNew();
190 abs_top_srcdir = getenv("abs_top_srcdir");
191 if (abs_top_srcdir == NULL)
192 die("env var abs_top_srcdir must be set");
194 abs_top_builddir = getenv("abs_top_builddir");
195 if (abs_top_builddir == NULL)
196 die("env var abs_top_builddir must be set");
198 if (asprintf(&src_root, "%s/tests/root", abs_top_srcdir) < 0) {
199 die("failed to set src_root");
202 CuSuiteSetup(suite, setup, teardown);
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);
211 CuSuiteSummary(suite, &output);
212 CuSuiteDetails(suite, &output);
213 printf("%s\n", output);
215 return suite->failCount;
220 * indent-tabs-mode: nil