there was an #include directive missing
[openmpi-llc.git] / test / util / orte_session_dir.c
blobf139fd359dcd442b222030f2e1753c2b10097d8f
1 /*
2 * Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
3 * University Research and Technology
4 * Corporation. All rights reserved.
5 * Copyright (c) 2004-2005 The University of Tennessee and The University
6 * of Tennessee Research Foundation. All rights
7 * reserved.
8 * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
9 * University of Stuttgart. All rights reserved.
10 * Copyright (c) 2004-2005 The Regents of the University of California.
11 * All rights reserved.
12 * $COPYRIGHT$
14 * Additional copyrights may follow
16 * $HEADER$
19 #include "orte_config.h"
21 #include <stdio.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #ifdef HAVE_UNISTD_H
25 #include <unistd.h>
26 #endif
27 #ifdef HAVE_LIBGEN_H
28 #include <libgen.h>
29 #endif
30 #ifdef HAVE_SYS_PARAM_H
31 #include <sys/param.h>
32 #endif
33 #include <sys/stat.h>
35 #include "support.h"
36 #include "orte/orte_constants.h"
37 #include "orte/mca/ns/ns_types.h"
38 #include "orte/util/proc_info.h"
39 #include "orte/util/sys_info.h"
40 #include "opal/util/os_path.h"
41 #include "opal/util/os_create_dirpath.h"
42 #include "orte/util/session_dir.h"
43 #include "orte/util/proc_info.h"
44 #include "orte/runtime/runtime.h"
47 static bool test1(void); /* given prefix, both one that works and one that fails */
48 static bool test2(void); /* no prefix given, ORTE_PREFIX_ENV set, one good and one bad */
49 static bool test3(void); /* no prefix given, TMPDIR set, one good and one bad */
50 static bool test4(void); /* no prefix given, TMP set, one good and one bad */
51 static bool test5(void); /* no prefix given, HOME set, one good and one bad */
52 static bool test6(void); /* no prefix given, nothing set, one good and one bad */
53 static bool test7(void); /* remove session directory tree */
54 static bool test8(void); /* attempt to remove tree when subdirs present */
56 void clear_proc_info(void);
58 static FILE *test_out=NULL;
60 int main(int argc, char* argv[])
62 orte_sys_info(); /* initialize system */
63 orte_proc_info(); /* initialize proc info structure */
64 orte_process_info.my_name = (orte_process_name_t*)malloc(sizeof(orte_process_name_t));
65 orte_process_info.my_name->cellid = 0;
66 orte_process_info.my_name->jobid = 0;
67 orte_process_info.my_name->vpid = 0;
69 test_init("orte_session_dir_t");
70 test_out = fopen( "test_session_dir_out", "w+" );
71 if( test_out == NULL ) {
72 test_failure("test_session_dir couldn't open test file failed");
73 test_finalize();
74 exit(1);
78 fprintf(test_out, "running test1\n");
79 if (test1()) {
80 test_success();
82 else {
83 test_failure("orte_session_dir_t test1 failed");
86 fprintf(test_out, "running test2\n");
87 if (test2()) {
88 test_success();
90 else {
91 test_failure("orte_session_dir_t test2 failed");
94 fprintf(test_out, "running test3\n");
95 if (test3()) {
96 test_success();
98 else {
99 test_failure("orte_session_dir_t test3 failed");
102 fprintf(test_out, "running test4\n");
103 if (test4()) {
104 test_success();
106 else {
107 test_failure("orte_session_dir_t test4 failed");
110 fprintf(test_out, "running test5\n");
111 if (test5()) {
112 test_success();
114 else {
115 test_failure("orte_session_dir_t test5 failed");
118 fprintf(test_out, "running test6\n");
119 if (test6()) {
120 test_success();
122 else {
123 test_failure("orte_session_dir_t test6 failed");
126 fprintf(test_out, "running test7\n");
127 if (test7()) {
128 test_success();
130 else {
131 test_failure("orte_session_dir_t test7 failed");
134 fprintf(test_out, "running test8\n");
135 if (test8()) {
136 test_success();
138 else {
139 test_failure("orte_session_dir_t test8 failed");
142 fprintf(test_out, "completed all tests\n");
144 fclose(test_out);
146 /* clean up */
147 orte_sys_info_finalize();
148 orte_proc_info_finalize();
150 test_finalize();
151 return 0;
155 static bool test1(void)
157 /* Test proper action when given a prefix */
159 char *prefix;
161 /* see if we can create a specified path */
163 clear_proc_info();
165 prefix = opal_os_path(false, "tmp", NULL);
166 if (ORTE_ERROR == orte_session_dir(true, prefix, orte_system_info.user, NULL, NULL, "test-universe", NULL, NULL)) {
167 fprintf(test_out, "test1 - couldn't create specified path\n");
168 free(prefix);
169 return(false);
171 /* see if it can access an existing path */
173 if (ORTE_ERROR == orte_session_dir(false, prefix, orte_system_info.user, NULL, NULL, "test-universe", NULL, NULL)) {
174 fprintf(test_out, "test1 - couldn't access existing path\n");
175 free(prefix);
176 return(false);
179 orte_session_dir_finalize(orte_process_info.my_name);
180 free(orte_process_info.universe_session_dir);
181 free(prefix);
183 return true;
187 static bool test2(void)
189 clear_proc_info();
191 /* use the ORTE_PREFIX_ENV variable */
193 setenv("OMPI_PREFIX_ENV", "/tmp/trythis", 1);
195 if (ORTE_ERROR == orte_session_dir(true, NULL, orte_system_info.user, NULL, NULL, "test-universe", NULL, NULL)) {
196 unsetenv("OMPI_PREFIX_ENV");
197 return(false);
200 orte_session_dir_finalize(orte_process_info.my_name);
202 unsetenv("OMPI_PREFIX_ENV");
204 return(true);
209 static bool test3(void)
211 /* use the TMPDIR enviro variable */
212 clear_proc_info();
214 setenv("TMPDIR", "/tmp/trythis", 1);
216 if (ORTE_ERROR == orte_session_dir(true, NULL, orte_system_info.user, NULL, NULL, "test-universe", NULL, NULL)) {
217 unsetenv("TMPDIR");
218 return(false);
221 orte_session_dir_finalize(orte_process_info.my_name);
223 unsetenv("TMPDIR");
225 return(true);
229 static bool test4(void)
231 /* use the TMP enviro variable */
233 clear_proc_info();
235 setenv("TMP", "/tmp/trythis", 1);
237 if (ORTE_ERROR == orte_session_dir(true, NULL, orte_system_info.user, NULL, NULL, "test-universe", NULL, NULL)) {
238 unsetenv("TMP");
239 return(false);
242 orte_session_dir_finalize(orte_process_info.my_name);
244 unsetenv("TMP");
246 return(true);
250 static bool test5(void)
252 /* use the HOME enviro variable */
254 clear_proc_info();
256 setenv("HOME", "/tmp/trythis", 1);
258 if (ORTE_ERROR == orte_session_dir(true, NULL, orte_system_info.user, NULL, NULL, "test-universe", NULL, NULL)) {
259 unsetenv("HOME");
260 return(false);
263 orte_session_dir_finalize(orte_process_info.my_name);
265 unsetenv("HOME");
267 return(true);
271 static bool test6(void)
274 clear_proc_info();
276 /* no enviro variables set, no prefix given
277 * Program should turn to default of /tmp (where "/" is whatever
278 * top-level directory is appropriate for given system)
280 if (ORTE_ERROR == orte_session_dir(true, NULL, orte_system_info.user, NULL, NULL, "test-universe", NULL, NULL)) {
281 return(false);
284 orte_session_dir_finalize(orte_process_info.my_name);
286 return(true);
289 static bool test7(void)
291 char *filenm[5];
292 FILE *fp;
293 int i;
295 clear_proc_info();
297 /* create test proc session directory tree */
298 if (ORTE_ERROR == orte_session_dir(true, NULL, orte_system_info.user, "localhost", NULL, "test-universe", "test-job", "test-proc")) {
299 return(false);
302 fprintf(test_out, "removing directories: %s\n\t%s\n\t%s\n",
303 orte_process_info.proc_session_dir,
304 orte_process_info.job_session_dir,
305 orte_process_info.universe_session_dir);
307 /* create some files */
309 filenm[0] = opal_os_path(false, orte_process_info.proc_session_dir, "dum1", NULL);
310 fp = fopen(filenm[0], "w");
311 fprintf(fp, "ss");
312 fclose(fp);
314 filenm[1] = opal_os_path(false, orte_process_info.job_session_dir, "dum2", NULL);
315 fp = fopen(filenm[1], "w");
316 fprintf(fp, "ss");
317 fclose(fp);
319 filenm[2] = opal_os_path(false, orte_process_info.universe_session_dir, "dum3", NULL);
320 fp = fopen(filenm[2], "w");
321 fprintf(fp, "ss");
322 fclose(fp);
324 if (ORTE_ERROR == orte_session_dir_finalize(orte_process_info.my_name)) {
325 return(false);
328 for (i=0; i < 3; i++) unlink(filenm[i]);
329 orte_session_dir_finalize(orte_process_info.my_name);
331 return true;
334 static bool test8(void)
336 char *filenm[5];
337 FILE *fp;
338 int i;
340 clear_proc_info();
342 /* create test proc session directory tree */
343 if (ORTE_ERROR == orte_session_dir(true, NULL, orte_system_info.user, "localhost", NULL, "test-universe2", "test-job2", "test-proc2")) {
344 return(false);
347 fprintf(test_out, "removing directories: %s\n\t%s\n\t%s\n",
348 orte_process_info.proc_session_dir,
349 orte_process_info.job_session_dir,
350 orte_process_info.universe_session_dir);
352 /* create some files */
354 filenm[0] = opal_os_path(false, orte_process_info.proc_session_dir, "dum1", NULL);
355 fp = fopen(filenm[0], "w");
356 fprintf(fp, "ss");
357 fclose(fp);
359 filenm[1] = opal_os_path(false, orte_process_info.job_session_dir, "dum2", NULL);
360 fp = fopen(filenm[1], "w");
361 fprintf(fp, "ss");
362 fclose(fp);
364 filenm[2] = opal_os_path(false, orte_process_info.universe_session_dir, "dum3", NULL);
365 fp = fopen(filenm[2], "w");
366 fprintf(fp, "ss");
367 fclose(fp);
370 if (ORTE_ERROR == orte_session_dir_finalize(orte_process_info.my_name)) {
371 return(false);
374 for (i=0; i < 3; i++) unlink(filenm[i]);
375 orte_session_dir_finalize(orte_process_info.my_name);
377 return true;
380 void clear_proc_info(void)
382 orte_process_info.tmpdir_base = NULL;
383 orte_process_info.top_session_dir = NULL;
384 orte_process_info.universe_session_dir = NULL;
385 orte_process_info.job_session_dir = NULL;
386 orte_process_info.proc_session_dir = NULL;