test-task-controller.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #include "../task-controller.h"
  19. #define HADOOP_CONF_DIR "/tmp"
  20. int write_config_file(char *file_name) {
  21. FILE *file;
  22. char const *str =
  23. "mapreduce.cluster.local.dir=/tmp/testing1,/tmp/testing2,/tmp/testing3,/tmp/testing4\n";
  24. file = fopen(file_name, "w");
  25. if (file == NULL) {
  26. printf("Failed to open %s.\n", file_name);
  27. return EXIT_FAILURE;
  28. }
  29. fwrite(str, 1, strlen(str), file);
  30. fclose(file);
  31. return 0;
  32. }
  33. void test_check_variable_against_config() {
  34. // A temporary configuration directory
  35. char *conf_dir_templ = "/tmp/test-task-controller-conf-dir-XXXXXX";
  36. // To accomodate "/conf/taskcontroller.cfg"
  37. char template[strlen(conf_dir_templ) + strlen("/conf/taskcontroller.cfg")];
  38. strcpy(template, conf_dir_templ);
  39. char *temp_dir = mkdtemp(template);
  40. if (temp_dir == NULL) {
  41. printf("Couldn't create a temporary dir for conf.\n");
  42. goto cleanup;
  43. }
  44. // Set the configuration directory
  45. hadoop_conf_dir = strdup(temp_dir);
  46. // create the configuration directory
  47. strcat(template, "/conf");
  48. char *conf_dir = strdup(template);
  49. mkdir(conf_dir, S_IRWXU);
  50. // create the configuration file
  51. strcat(template, "/taskcontroller.cfg");
  52. if (write_config_file(template) != 0) {
  53. printf("Couldn't write the configuration file.\n");
  54. goto cleanup;
  55. }
  56. // Test obtaining a value for a key from the config
  57. char *config_values[4] = { "/tmp/testing1", "/tmp/testing2",
  58. "/tmp/testing3", "/tmp/testing4" };
  59. char *value = (char *) get_value("mapreduce.cluster.local.dir");
  60. if (strcmp(value, "/tmp/testing1,/tmp/testing2,/tmp/testing3,/tmp/testing4")
  61. != 0) {
  62. printf("Obtaining a value for a key from the config failed.\n");
  63. goto cleanup;
  64. }
  65. // Test the parsing of a multiple valued key from the config
  66. char **values = (char **)get_values("mapreduce.cluster.local.dir");
  67. char **values_ptr = values;
  68. int i = 0;
  69. while (*values_ptr != NULL) {
  70. printf(" value : %s\n", *values_ptr);
  71. if (strcmp(*values_ptr, config_values[i++]) != 0) {
  72. printf("Configured values are not read out properly. Test failed!");
  73. goto cleanup;;
  74. }
  75. values_ptr++;
  76. }
  77. if (check_variable_against_config("mapreduce.cluster.local.dir", "/tmp/testing5") == 0) {
  78. printf("Configuration should not contain /tmp/testing5! \n");
  79. goto cleanup;
  80. }
  81. if (check_variable_against_config("mapreduce.cluster.local.dir", "/tmp/testing4") != 0) {
  82. printf("Configuration should contain /tmp/testing4! \n");
  83. goto cleanup;
  84. }
  85. cleanup: if (value != NULL) {
  86. free(value);
  87. }
  88. if (values != NULL) {
  89. free(values);
  90. }
  91. if (hadoop_conf_dir != NULL) {
  92. free(hadoop_conf_dir);
  93. }
  94. unlink(template);
  95. rmdir(conf_dir);
  96. rmdir(hadoop_conf_dir);
  97. }
  98. void test_get_user_directory() {
  99. char *user_dir = (char *) get_user_directory("/tmp", "user");
  100. printf("user_dir obtained is %s\n", user_dir);
  101. int ret = 0;
  102. if (strcmp(user_dir, "/tmp/taskTracker/user") != 0) {
  103. ret = -1;
  104. }
  105. free(user_dir);
  106. assert(ret == 0);
  107. }
  108. void test_get_job_directory() {
  109. char *job_dir = (char *) get_job_directory("/tmp", "user",
  110. "job_200906101234_0001");
  111. printf("job_dir obtained is %s\n", job_dir);
  112. int ret = 0;
  113. if (strcmp(job_dir, "/tmp/taskTracker/user/jobcache/job_200906101234_0001")
  114. != 0) {
  115. ret = -1;
  116. }
  117. free(job_dir);
  118. assert(ret == 0);
  119. }
  120. void test_get_attempt_directory() {
  121. char *job_dir = (char *) get_job_directory("/tmp", "user",
  122. "job_200906101234_0001");
  123. printf("job_dir obtained is %s\n", job_dir);
  124. char *attempt_dir = (char *) get_attempt_directory(job_dir,
  125. "attempt_200906101234_0001_m_000000_0");
  126. printf("attempt_dir obtained is %s\n", attempt_dir);
  127. int ret = 0;
  128. if (strcmp(
  129. attempt_dir,
  130. "/tmp/taskTracker/user/jobcache/job_200906101234_0001/attempt_200906101234_0001_m_000000_0")
  131. != 0) {
  132. ret = -1;
  133. }
  134. free(job_dir);
  135. free(attempt_dir);
  136. assert(ret == 0);
  137. }
  138. void test_get_task_launcher_file() {
  139. char *job_dir = (char *) get_job_directory("/tmp", "user",
  140. "job_200906101234_0001");
  141. char *task_file = (char *) get_task_launcher_file(job_dir,
  142. "attempt_200906112028_0001_m_000000_0");
  143. printf("task_file obtained is %s\n", task_file);
  144. int ret = 0;
  145. if (strcmp(
  146. task_file,
  147. "/tmp/taskTracker/user/jobcache/job_200906101234_0001/attempt_200906112028_0001_m_000000_0/taskjvm.sh")
  148. != 0) {
  149. ret = -1;
  150. }
  151. free(task_file);
  152. assert(ret == 0);
  153. }
  154. void test_get_job_log_dir() {
  155. char *logdir = (char *) get_job_log_dir("/tmp/testing",
  156. "job_200906101234_0001");
  157. printf("logdir obtained is %s\n", logdir);
  158. int ret = 0;
  159. if (strcmp(logdir, "/tmp/testing/userlogs/job_200906101234_0001") != 0) {
  160. ret = -1;
  161. }
  162. free(logdir);
  163. assert(ret == 0);
  164. }
  165. void test_get_job_acls_file() {
  166. char *job_acls_file = (char *) get_job_acls_file(
  167. "/tmp/testing/userlogs/job_200906101234_0001");
  168. printf("job acls file obtained is %s\n", job_acls_file);
  169. int ret = 0;
  170. if (strcmp(job_acls_file,
  171. "/tmp/testing/userlogs/job_200906101234_0001/job-acls.xml") != 0) {
  172. ret = -1;
  173. }
  174. free(job_acls_file);
  175. assert(ret == 0);
  176. }
  177. void test_get_task_log_dir() {
  178. char *logdir = (char *) get_task_log_dir("/tmp/testing",
  179. "job_200906101234_0001", "attempt_200906112028_0001_m_000000_0");
  180. printf("logdir obtained is %s\n", logdir);
  181. int ret = 0;
  182. if (strcmp(logdir,
  183. "/tmp/testing/userlogs/job_200906101234_0001/attempt_200906112028_0001_m_000000_0")
  184. != 0) {
  185. ret = -1;
  186. }
  187. free(logdir);
  188. assert(ret == 0);
  189. }
  190. int main(int argc, char **argv) {
  191. printf("\nStarting tests\n");
  192. LOGFILE = stdout;
  193. printf("\nTesting check_variable_against_config()\n");
  194. test_check_variable_against_config();
  195. printf("\nTesting get_user_directory()\n");
  196. test_get_user_directory();
  197. printf("\nTesting get_job_directory()\n");
  198. test_get_job_directory();
  199. printf("\nTesting get_attempt_directory()\n");
  200. test_get_attempt_directory();
  201. printf("\nTesting get_task_launcher_file()\n");
  202. test_get_task_launcher_file();
  203. printf("\nTesting get_job_log_dir()\n");
  204. test_get_job_log_dir();
  205. printf("\nTesting get_job_acls_file()\n");
  206. test_get_job_acls_file();
  207. printf("\nTesting get_task_log_dir()\n");
  208. test_get_task_log_dir();
  209. printf("\nFinished tests\n");
  210. return 0;
  211. }