YARN-5717. Add tests for container-executor is_feature_enabled. Contributed by Sidharta Seethana

(cherry picked from commit cf3f43e95b)
This commit is contained in:
Chris Douglas 2016-10-13 20:47:49 -07:00
parent ad69baf6a9
commit f0f2681b4f
4 changed files with 79 additions and 29 deletions

View File

@ -415,9 +415,9 @@ int change_user(uid_t user, gid_t group) {
return 0; return 0;
} }
int is_feature_enabled(const char* feature_key, int default_value,
static int is_feature_enabled(const char* feature_key, int default_value) { struct configuration *cfg) {
char *enabled_str = get_value(feature_key, &executor_cfg); char *enabled_str = get_value(feature_key, cfg);
int enabled = default_value; int enabled = default_value;
if (enabled_str != NULL) { if (enabled_str != NULL) {
@ -441,15 +441,14 @@ static int is_feature_enabled(const char* feature_key, int default_value) {
} }
} }
int is_docker_support_enabled() { int is_docker_support_enabled() {
return is_feature_enabled(DOCKER_SUPPORT_ENABLED_KEY, return is_feature_enabled(DOCKER_SUPPORT_ENABLED_KEY,
DEFAULT_DOCKER_SUPPORT_ENABLED); DEFAULT_DOCKER_SUPPORT_ENABLED, &executor_cfg);
} }
int is_tc_support_enabled() { int is_tc_support_enabled() {
return is_feature_enabled(TC_SUPPORT_ENABLED_KEY, return is_feature_enabled(TC_SUPPORT_ENABLED_KEY,
DEFAULT_TC_SUPPORT_ENABLED); DEFAULT_TC_SUPPORT_ENABLED, &executor_cfg);
} }
char* check_docker_binary(char *docker_binary) { char* check_docker_binary(char *docker_binary) {

View File

@ -258,6 +258,10 @@ int check_dir(const char* npath, mode_t st_mode, mode_t desired,
int create_validate_dir(const char* npath, mode_t perm, const char* path, int create_validate_dir(const char* npath, mode_t perm, const char* path,
int finalComponent); int finalComponent);
/** Check if a feature is enabled in the specified configuration. */
int is_feature_enabled(const char* feature_key, int default_value,
struct configuration *cfg);
/** Check if tc (traffic control) support is enabled in configuration. */ /** Check if tc (traffic control) support is enabled in configuration. */
int is_tc_support_enabled(); int is_tc_support_enabled();

View File

@ -43,73 +43,69 @@
#endif #endif
static void display_usage(FILE *stream) { static void display_usage(FILE *stream) {
char usage_template[4096]; fprintf(stream,
usage_template[0] = '\0';
strcat(usage_template,
"Usage: container-executor --checksetup\n" "Usage: container-executor --checksetup\n"
" container-executor --mount-cgroups <hierarchy> " " container-executor --mount-cgroups <hierarchy> "
"<controller=path>...\n" ); "<controller=path>...\n" );
if(is_tc_support_enabled()) { if(is_tc_support_enabled()) {
strcat(usage_template, fprintf(stream,
" container-executor --tc-modify-state <command-file>\n" " container-executor --tc-modify-state <command-file>\n"
" container-executor --tc-read-state <command-file>\n" " container-executor --tc-read-state <command-file>\n"
" container-executor --tc-read-stats <command-file>\n" ); " container-executor --tc-read-stats <command-file>\n" );
} else { } else {
strcat(usage_template, fprintf(stream,
"[DISABLED] container-executor --tc-modify-state <command-file>\n" "[DISABLED] container-executor --tc-modify-state <command-file>\n"
"[DISABLED] container-executor --tc-read-state <command-file>\n" "[DISABLED] container-executor --tc-read-state <command-file>\n"
"[DISABLED] container-executor --tc-read-stats <command-file>\n"); "[DISABLED] container-executor --tc-read-stats <command-file>\n");
} }
if(is_docker_support_enabled()) { if(is_docker_support_enabled()) {
strcat(usage_template, fprintf(stream,
" container-executor --run-docker <command-file>\n"); " container-executor --run-docker <command-file>\n");
} else { } else {
strcat(usage_template, fprintf(stream,
"[DISABLED] container-executor --run-docker <command-file>\n"); "[DISABLED] container-executor --run-docker <command-file>\n");
} }
strcat(usage_template, fprintf(stream,
" container-executor <user> <yarn-user> <command> <command-args>\n" " container-executor <user> <yarn-user> <command> <command-args>\n"
" where command and command-args: \n" \ " where command and command-args: \n" \
" initialize container: %2d appid tokens nm-local-dirs " " initialize container: %2d appid tokens nm-local-dirs "
"nm-log-dirs cmd app...\n" "nm-log-dirs cmd app...\n"
" launch container: %2d appid containerid workdir " " launch container: %2d appid containerid workdir "
"container-script tokens pidfile nm-local-dirs nm-log-dirs resources "); "container-script tokens pidfile nm-local-dirs nm-log-dirs resources ",
INITIALIZE_CONTAINER, LAUNCH_CONTAINER);
if(is_tc_support_enabled()) { if(is_tc_support_enabled()) {
strcat(usage_template, "optional-tc-command-file\n"); fprintf(stream, "optional-tc-command-file\n");
} else { } else {
strcat(usage_template, "\n"); fprintf(stream, "\n");
} }
if(is_docker_support_enabled()) { if(is_docker_support_enabled()) {
strcat(usage_template, fprintf(stream,
" launch docker container: %2d appid containerid workdir " " launch docker container: %2d appid containerid workdir "
"container-script tokens pidfile nm-local-dirs nm-log-dirs " "container-script tokens pidfile nm-local-dirs nm-log-dirs "
"docker-command-file resources "); "docker-command-file resources ", LAUNCH_DOCKER_CONTAINER);
} else { } else {
strcat(usage_template, fprintf(stream,
"[DISABLED] launch docker container: %2d appid containerid workdir " "[DISABLED] launch docker container: %2d appid containerid workdir "
"container-script tokens pidfile nm-local-dirs nm-log-dirs " "container-script tokens pidfile nm-local-dirs nm-log-dirs "
"docker-command-file resources "); "docker-command-file resources ", LAUNCH_DOCKER_CONTAINER);
} }
if(is_tc_support_enabled()) { if(is_tc_support_enabled()) {
strcat(usage_template, "optional-tc-command-file\n"); fprintf(stream, "optional-tc-command-file\n");
} else { } else {
strcat(usage_template, "\n"); fprintf(stream, "\n");
} }
strcat(usage_template, fprintf(stream,
" signal container: %2d container-pid signal\n" " signal container: %2d container-pid signal\n"
" delete as user: %2d relative-path\n" " delete as user: %2d relative-path\n"
" list as user: %2d relative-path\n"); " list as user: %2d relative-path\n",
SIGNAL_CONTAINER, DELETE_AS_USER, LIST_AS_USER);
fprintf(stream, usage_template, INITIALIZE_CONTAINER, LAUNCH_CONTAINER,
LAUNCH_DOCKER_CONTAINER, SIGNAL_CONTAINER, DELETE_AS_USER, LIST_AS_USER);
} }
/* Sets up log files for normal/error logging */ /* Sets up log files for normal/error logging */

View File

@ -383,6 +383,54 @@ void test_delete_app() {
free(dont_touch); free(dont_touch);
} }
void validate_feature_enabled_value(int expected_value, const char* key,
int default_value, struct configuration *cfg) {
int value = is_feature_enabled(key, default_value, cfg);
if (value != expected_value) {
printf("FAIL: expected value %d for key %s but found %d\n",
expected_value, key, value);
exit(1);
}
}
void test_is_feature_enabled() {
char* filename = TEST_ROOT "/feature_flag_test.cfg";
FILE *file = fopen(filename, "w");
int disabled = 0;
int enabled = 1;
struct configuration cfg = {.size=0, .confdetails=NULL};
if (file == NULL) {
printf("FAIL: Could not open configuration file: %s\n", filename);
exit(1);
}
fprintf(file, "feature.name1.enabled=0\n");
fprintf(file, "feature.name2.enabled=1\n");
fprintf(file, "feature.name3.enabled=1klajdflkajdsflk\n");
fprintf(file, "feature.name4.enabled=asdkjfasdkljfklsdjf0\n");
fprintf(file, "feature.name5.enabled=-1\n");
fprintf(file, "feature.name6.enabled=2\n");
fclose(file);
read_config(filename, &cfg);
validate_feature_enabled_value(disabled, "feature.name1.enabled",
disabled, &cfg);
validate_feature_enabled_value(enabled, "feature.name2.enabled",
disabled, &cfg);
validate_feature_enabled_value(disabled, "feature.name3.enabled",
disabled, &cfg);
validate_feature_enabled_value(disabled, "feature.name4.enabled",
disabled, &cfg);
validate_feature_enabled_value(enabled, "feature.name5.enabled",
enabled, &cfg);
validate_feature_enabled_value(disabled, "feature.name6.enabled",
disabled, &cfg);
free_configurations(&cfg);
}
void test_delete_user() { void test_delete_user() {
printf("\nTesting delete_user\n"); printf("\nTesting delete_user\n");
@ -1074,6 +1122,9 @@ int main(int argc, char **argv) {
printf("\nTesting delete_app()\n"); printf("\nTesting delete_app()\n");
test_delete_app(); test_delete_app();
printf("\nTesting is_feature_enabled()\n");
test_is_feature_enabled();
test_check_user(0); test_check_user(0);
// the tests that change user need to be run in a subshell, so that // the tests that change user need to be run in a subshell, so that