72
89
ConfSource *source;
90
ConfFile *conf_file, *ptr;
92
TEST_FUNCTION ("conf_file_get");
93
source = conf_source_new (NULL, "/tmp", CONF_JOB_DIR);
75
95
/* Check that we can request a new ConfFile structure, it should be
76
96
* allocated with nih_alloc and placed into the files hash table of
77
97
* the source, with the flag copied.
79
TEST_FUNCTION ("conf_file_new");
80
source = conf_source_new (NULL, "/tmp", CONF_JOB_DIR);
84
file = conf_file_new (NULL, source, "/tmp/foo");
86
if (test_alloc_failed) {
87
TEST_EQ_P (file, NULL);
91
TEST_ALLOC_SIZE (file, sizeof (ConfFile));
92
TEST_LIST_NOT_EMPTY (&file->entry);
93
TEST_ALLOC_PARENT (file->path, file);
94
TEST_EQ_STR (file->path, "/tmp/foo");
95
TEST_EQ (file->flag, TRUE);
96
TEST_NE_P (file->items, NULL);
98
TEST_EQ_P ((void *)nih_hash_lookup (source->files, "/tmp/foo"),
101
nih_list_free (&file->entry);
99
TEST_FEATURE ("with new path");
101
conf_file = conf_file_get (source, "/tmp/foo");
103
if (test_alloc_failed) {
104
TEST_EQ_P (conf_file, NULL);
108
TEST_ALLOC_SIZE (conf_file, sizeof (ConfFile));
109
TEST_ALLOC_PARENT (conf_file, source);
110
TEST_LIST_NOT_EMPTY (&conf_file->entry);
111
TEST_ALLOC_PARENT (conf_file->path, conf_file);
112
TEST_EQ_STR (conf_file->path, "/tmp/foo");
113
TEST_EQ (conf_file->flag, source->flag);
114
TEST_NE_P (conf_file->items, NULL);
116
TEST_EQ_P ((void *)nih_hash_lookup (source->files, "/tmp/foo"),
119
nih_list_free (&conf_file->entry);
123
/* Check that we can retrieve an existing ConfFile entry for a
124
* source, and that the flag is updated to the new value.
126
TEST_FEATURE ("with existing path");
127
conf_file = conf_file_get (source, "/tmp/foo");
128
source->flag = (! source->flag);
131
ptr = conf_file_get (source, "/tmp/foo");
133
if (test_alloc_failed) {
134
TEST_EQ_P (ptr, NULL);
138
TEST_EQ_P (ptr, conf_file);
140
TEST_ALLOC_SIZE (conf_file, sizeof (ConfFile));
141
TEST_ALLOC_PARENT (conf_file, source);
142
TEST_LIST_NOT_EMPTY (&conf_file->entry);
143
TEST_ALLOC_PARENT (conf_file->path, conf_file);
144
TEST_EQ_STR (conf_file->path, "/tmp/foo");
145
TEST_EQ (conf_file->flag, source->flag);
146
TEST_NE_P (conf_file->items, NULL);
148
TEST_EQ_P ((void *)nih_hash_lookup (source->files, "/tmp/foo"),
152
nih_list_free (&conf_file->entry);
104
153
nih_list_free (&source->entry);
110
159
ConfSource *source;
161
ConfItem *item, *ptr;
164
TEST_FUNCTION ("conf_item_set");
165
source = conf_source_new (NULL, "/tmp", CONF_JOB_DIR);
166
conf_file = conf_file_get (source, "/tmp/foo");
114
168
/* Check that we can request a new Confitem structure, it should be
115
169
* allocated with nih_alloc and placed into the items hash table of
116
170
* the file, with the flag copied.
118
TEST_FUNCTION ("conf_item_new");
119
source = conf_source_new (NULL, "/tmp", CONF_JOB_DIR);
122
file = conf_file_new (NULL, source, "/tmp/foo");
172
TEST_FEATURE ("with new item");
173
job1 = job_new (NULL, "foo");
124
175
TEST_ALLOC_FAIL {
125
item = conf_item_new (NULL, file, "foo");
176
item = conf_item_set (source, conf_file, "foo", job1);
127
178
if (test_alloc_failed) {
128
179
TEST_EQ_P (item, NULL);
132
183
TEST_ALLOC_SIZE (item, sizeof (ConfItem));
184
TEST_ALLOC_PARENT (item, conf_file);
133
185
TEST_LIST_NOT_EMPTY (&item->entry);
134
186
TEST_ALLOC_PARENT (item->name, item);
135
187
TEST_EQ_STR (item->name, "foo");
136
TEST_EQ (item->flag, TRUE);
137
TEST_EQ_P (item->data, NULL);
188
TEST_EQ (item->flag, conf_file->flag);
189
TEST_EQ_P (item->data, job1);
139
TEST_EQ_P ((void *)nih_hash_lookup (file->items, "foo"),
191
TEST_EQ_P ((void *)nih_hash_lookup (conf_file->items, "foo"),
142
194
nih_list_free (&item->entry);
145
nih_list_free (&file->entry);
197
/* Check that we can retrieve an existing ConfItem for a given name,
198
* and that the flag and data are both updated to the new value.
200
TEST_FEATURE ("with existing item");
201
item = conf_item_set (source, conf_file, "foo", job1);
202
source->flag = (! source->flag);
204
job2 = job_new (NULL, "foo");
207
ptr = conf_item_set (source, conf_file, "foo", job2);
209
if (test_alloc_failed) {
210
TEST_EQ_P (ptr, NULL);
214
TEST_EQ_P (ptr, item);
216
TEST_ALLOC_SIZE (item, sizeof (ConfItem));
217
TEST_ALLOC_PARENT (item, conf_file);
218
TEST_LIST_NOT_EMPTY (&item->entry);
219
TEST_ALLOC_PARENT (item->name, item);
220
TEST_EQ_STR (item->name, "foo");
221
TEST_EQ (item->flag, conf_file->flag);
222
TEST_EQ_P (item->data, job2);
224
TEST_EQ_P ((void *)nih_hash_lookup (conf_file->items, "foo"),
228
nih_list_free (&conf_file->entry);
146
229
nih_list_free (&source->entry);
234
test_source_reload (void)
238
int ret, fd[4096], i = 0;
239
char dirname[PATH_MAX], filename[PATH_MAX];
241
TEST_FUNCTION ("conf_source_reload");
242
TEST_FILENAME (dirname);
243
mkdir (dirname, 0755);
245
strcpy (filename, dirname);
246
strcat (filename, "/foo");
248
f = fopen (filename, "w");
249
fprintf (f, "exec /sbin/daemon\n");
250
fprintf (f, "respawn\n");
253
/* Make sure that we have inotify before performing some tests... */
254
if ((fd[0] = inotify_init ()) < 0) {
255
printf ("SKIP: inotify not available\n");
261
/* Check that we can reload a new file source. An inotify watch
262
* should be established on the parent directory, and its file
263
* descriptor set to be closed on exec.
265
TEST_FEATURE ("with new file source");
266
strcpy (filename, dirname);
267
strcat (filename, "/foo");
271
source = conf_source_new (NULL, filename, CONF_FILE);
274
ret = conf_source_reload (source);
277
TEST_ALLOC_SIZE (source->watch, sizeof (NihWatch));
278
TEST_EQ_STR (source->watch->path, dirname);
279
TEST_EQ_P (source->watch->data, source);
281
TEST_TRUE (fcntl (source->watch->fd, F_GETFD) & FD_CLOEXEC);
283
nih_list_free (&source->entry);
287
/* Consume all available inotify instances so that the following
288
* tests run without inotify.
290
for (i = 0; i < 4096; i++)
291
if ((fd[i] = inotify_init ()) < 0)
296
strcpy (filename, dirname);
297
strcat (filename, "/foo");
154
308
test_source_new ();
311
test_source_reload ();