Skip to content

Commit

Permalink
Fix error when tests run in parallel.
Browse files Browse the repository at this point in the history
  • Loading branch information
graebm committed Dec 29, 2023
1 parent baeffa7 commit 9107251
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 22 deletions.
58 changes: 44 additions & 14 deletions tests/credentials_provider_sso_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -326,17 +326,15 @@ static int s_credentials_provider_sso_connect_failure(struct aws_allocator *allo
}
AWS_TEST_CASE(credentials_provider_sso_connect_failure, s_credentials_provider_sso_connect_failure);

AWS_STATIC_STRING_FROM_LITERAL(s_home_env_var, "HOME");
AWS_STATIC_STRING_FROM_LITERAL(s_home_env_current_directory, ".");

static int s_credentials_provider_sso_failure_token_missing(struct aws_allocator *allocator, void *ctx) {
(void)ctx;

aws_credentials_provider_http_mock_tester_init(allocator);
credentials_provider_http_mock_tester.is_request_successful = false;

/* redirect $HOME */
ASSERT_SUCCESS(aws_set_environment_value(s_home_env_var, s_home_env_current_directory));
struct aws_string *tmp_home;
ASSERT_SUCCESS(aws_create_random_home_directory(allocator, &tmp_home));

s_aws_credentials_provider_sso_test_init_config_profile(allocator, s_sso_session_config_contents);

Expand Down Expand Up @@ -366,6 +364,8 @@ static int s_credentials_provider_sso_failure_token_missing(struct aws_allocator
aws_credentials_provider_http_mock_wait_for_shutdown_callback();

aws_credentials_provider_http_mock_tester_cleanup();
aws_directory_delete(tmp_home, true);
aws_string_destroy(tmp_home);

return 0;
}
Expand All @@ -383,7 +383,8 @@ static int s_credentials_provider_sso_failure_token_expired(struct aws_allocator
credentials_provider_http_mock_tester.is_request_successful = false;

/* redirect $HOME */
ASSERT_SUCCESS(aws_set_environment_value(s_home_env_var, s_home_env_current_directory));
struct aws_string *tmp_home;
ASSERT_SUCCESS(aws_create_random_home_directory(allocator, &tmp_home));

/* create token file */
struct aws_string *token_path = aws_construct_sso_token_path(allocator, s_sso_session_name);
Expand Down Expand Up @@ -423,6 +424,8 @@ static int s_credentials_provider_sso_failure_token_expired(struct aws_allocator
aws_credentials_provider_http_mock_wait_for_shutdown_callback();

aws_credentials_provider_http_mock_tester_cleanup();
aws_directory_delete(tmp_home, true);
aws_string_destroy(tmp_home);
aws_string_destroy(token_path);
return 0;
}
Expand All @@ -436,7 +439,8 @@ static int s_credentials_provider_sso_failure_token_empty(struct aws_allocator *
credentials_provider_http_mock_tester.is_request_successful = false;

/* redirect $HOME */
ASSERT_SUCCESS(aws_set_environment_value(s_home_env_var, s_home_env_current_directory));
struct aws_string *tmp_home;
ASSERT_SUCCESS(aws_create_random_home_directory(allocator, &tmp_home));

/* create token file */
struct aws_string *token_path = aws_construct_sso_token_path(allocator, s_sso_session_name);
Expand Down Expand Up @@ -474,6 +478,8 @@ static int s_credentials_provider_sso_failure_token_empty(struct aws_allocator *
aws_credentials_provider_http_mock_wait_for_shutdown_callback();

aws_credentials_provider_http_mock_tester_cleanup();
aws_directory_delete(tmp_home, true);
aws_string_destroy(tmp_home);
aws_string_destroy(token_path);
return 0;
}
Expand All @@ -487,7 +493,8 @@ static int s_credentials_provider_sso_request_failure(struct aws_allocator *allo
credentials_provider_http_mock_tester.response_code = AWS_HTTP_STATUS_CODE_400_BAD_REQUEST;

/* redirect $HOME */
ASSERT_SUCCESS(aws_set_environment_value(s_home_env_var, s_home_env_current_directory));
struct aws_string *tmp_home;
ASSERT_SUCCESS(aws_create_random_home_directory(allocator, &tmp_home));

/* create token file */
struct aws_string *token_path = aws_construct_sso_token_path(allocator, s_sso_session_name);
Expand Down Expand Up @@ -526,6 +533,8 @@ static int s_credentials_provider_sso_request_failure(struct aws_allocator *allo

aws_credentials_provider_http_mock_tester_cleanup();

aws_directory_delete(tmp_home, true);
aws_string_destroy(tmp_home);
aws_string_destroy(token_path);
return 0;
}
Expand All @@ -538,7 +547,8 @@ static int s_credentials_provider_sso_bad_response(struct aws_allocator *allocat
aws_credentials_provider_http_mock_tester_init(allocator);

/* redirect $HOME */
ASSERT_SUCCESS(aws_set_environment_value(s_home_env_var, s_home_env_current_directory));
struct aws_string *tmp_home;
ASSERT_SUCCESS(aws_create_random_home_directory(allocator, &tmp_home));

/* create token file */
struct aws_string *token_path = aws_construct_sso_token_path(allocator, s_sso_session_name);
Expand Down Expand Up @@ -581,6 +591,8 @@ static int s_credentials_provider_sso_bad_response(struct aws_allocator *allocat

aws_credentials_provider_http_mock_tester_cleanup();

aws_directory_delete(tmp_home, true);
aws_string_destroy(tmp_home);
aws_string_destroy(token_path);
return 0;
}
Expand All @@ -593,7 +605,8 @@ static int s_credentials_provider_sso_retryable_error(struct aws_allocator *allo
credentials_provider_http_mock_tester.response_code = AWS_HTTP_STATUS_CODE_500_INTERNAL_SERVER_ERROR;

/* redirect $HOME */
ASSERT_SUCCESS(aws_set_environment_value(s_home_env_var, s_home_env_current_directory));
struct aws_string *tmp_home;
ASSERT_SUCCESS(aws_create_random_home_directory(allocator, &tmp_home));

/* create token file */
struct aws_string *token_path = aws_construct_sso_token_path(allocator, s_sso_session_name);
Expand Down Expand Up @@ -636,6 +649,8 @@ static int s_credentials_provider_sso_retryable_error(struct aws_allocator *allo

aws_credentials_provider_http_mock_tester_cleanup();

aws_directory_delete(tmp_home, true);
aws_string_destroy(tmp_home);
aws_string_destroy(token_path);
return 0;
}
Expand All @@ -647,7 +662,8 @@ static int s_credentials_provider_sso_basic_success(struct aws_allocator *alloca
aws_credentials_provider_http_mock_tester_init(allocator);

/* redirect $HOME */
ASSERT_SUCCESS(aws_set_environment_value(s_home_env_var, s_home_env_current_directory));
struct aws_string *tmp_home;
ASSERT_SUCCESS(aws_create_random_home_directory(allocator, &tmp_home));

/* create token file */
struct aws_string *token_path = aws_construct_sso_token_path(allocator, s_sso_session_name);
Expand Down Expand Up @@ -691,6 +707,8 @@ static int s_credentials_provider_sso_basic_success(struct aws_allocator *alloca

aws_credentials_provider_http_mock_tester_cleanup();

aws_directory_delete(tmp_home, true);
aws_string_destroy(tmp_home);
aws_string_destroy(token_path);
return 0;
}
Expand All @@ -702,7 +720,8 @@ static int s_credentials_provider_sso_basic_success_cached_config_file(struct aw
aws_credentials_provider_http_mock_tester_init(allocator);

/* redirect $HOME */
ASSERT_SUCCESS(aws_set_environment_value(s_home_env_var, s_home_env_current_directory));
struct aws_string *tmp_home;
ASSERT_SUCCESS(aws_create_random_home_directory(allocator, &tmp_home));

/* create token file */
struct aws_string *token_path = aws_construct_sso_token_path(allocator, s_sso_session_name);
Expand Down Expand Up @@ -751,6 +770,8 @@ static int s_credentials_provider_sso_basic_success_cached_config_file(struct aw

aws_credentials_provider_http_mock_tester_cleanup();

aws_directory_delete(tmp_home, true);
aws_string_destroy(tmp_home);
aws_string_destroy(token_path);
aws_profile_collection_release(config_collection);

Expand All @@ -766,7 +787,8 @@ static int s_credentials_provider_sso_basic_success_profile(struct aws_allocator
aws_credentials_provider_http_mock_tester_init(allocator);

/* redirect $HOME */
ASSERT_SUCCESS(aws_set_environment_value(s_home_env_var, s_home_env_current_directory));
struct aws_string *tmp_home;
ASSERT_SUCCESS(aws_create_random_home_directory(allocator, &tmp_home));

/* create token file */
struct aws_string *token_path = aws_construct_sso_token_path(allocator, s_sso_profile_start_url);
Expand Down Expand Up @@ -810,6 +832,8 @@ static int s_credentials_provider_sso_basic_success_profile(struct aws_allocator

aws_credentials_provider_http_mock_tester_cleanup();

aws_directory_delete(tmp_home, true);
aws_string_destroy(tmp_home);
aws_string_destroy(token_path);
return 0;
}
Expand All @@ -823,7 +847,8 @@ static int s_credentials_provider_sso_basic_success_profile_cached_config_file(
aws_credentials_provider_http_mock_tester_init(allocator);

/* redirect $HOME */
ASSERT_SUCCESS(aws_set_environment_value(s_home_env_var, s_home_env_current_directory));
struct aws_string *tmp_home;
ASSERT_SUCCESS(aws_create_random_home_directory(allocator, &tmp_home));

/* create token file */
struct aws_string *token_path = aws_construct_sso_token_path(allocator, s_sso_profile_start_url);
Expand Down Expand Up @@ -870,6 +895,8 @@ static int s_credentials_provider_sso_basic_success_profile_cached_config_file(
aws_credentials_provider_http_mock_wait_for_shutdown_callback();
aws_credentials_provider_http_mock_tester_cleanup();
aws_profile_collection_release(config_collection);
aws_directory_delete(tmp_home, true);
aws_string_destroy(tmp_home);
aws_string_destroy(token_path);

return 0;
Expand All @@ -885,7 +912,8 @@ static int s_credentials_provider_sso_basic_success_after_failure(struct aws_all
credentials_provider_http_mock_tester.failure_count = 2;
credentials_provider_http_mock_tester.failure_response_code = AWS_HTTP_STATUS_CODE_500_INTERNAL_SERVER_ERROR;
/* redirect $HOME */
ASSERT_SUCCESS(aws_set_environment_value(s_home_env_var, s_home_env_current_directory));
struct aws_string *tmp_home;
ASSERT_SUCCESS(aws_create_random_home_directory(allocator, &tmp_home));

/* create token file */
struct aws_string *token_path = aws_construct_sso_token_path(allocator, s_sso_session_name);
Expand Down Expand Up @@ -929,6 +957,8 @@ static int s_credentials_provider_sso_basic_success_after_failure(struct aws_all

aws_credentials_provider_http_mock_tester_cleanup();

aws_directory_delete(tmp_home, true);
aws_string_destroy(tmp_home);
aws_string_destroy(token_path);
return 0;
}
Expand Down
27 changes: 27 additions & 0 deletions tests/credentials_provider_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@

#include <aws/auth/credentials.h>
#include <aws/auth/private/credentials_utils.h>
#include <aws/common/environment.h>
#include <aws/common/string.h>
#include <aws/common/task_scheduler.h>
#include <aws/common/uuid.h>
#include <aws/http/status_code.h>
#include <aws/io/channel_bootstrap.h>
#include <aws/io/event_loop.h>
Expand All @@ -19,6 +21,8 @@

#include <errno.h>

AWS_STATIC_STRING_FROM_LITERAL(s_home_env_var, "HOME");

/*
* Support for async get testing
*/
Expand Down Expand Up @@ -491,6 +495,29 @@ int aws_create_directory_components(struct aws_allocator *allocator, const struc
return AWS_OP_SUCCESS;
}

int aws_create_random_home_directory(struct aws_allocator *allocator, struct aws_string **out_path) {
struct aws_byte_buf path_buf;
ASSERT_SUCCESS(aws_byte_buf_init(&path_buf, allocator, 256));

struct aws_byte_cursor prefix = aws_byte_cursor_from_c_str("./home-");
ASSERT_SUCCESS(aws_byte_buf_append(&path_buf, &prefix));

struct aws_uuid uuid;
ASSERT_SUCCESS(aws_uuid_init(&uuid));
ASSERT_SUCCESS(aws_uuid_to_str(&uuid, &path_buf));

ASSERT_SUCCESS(aws_byte_buf_append_byte_dynamic(&path_buf, '/'));

struct aws_string *path_str = aws_string_new_from_buf(allocator, &path_buf);
ASSERT_SUCCESS(aws_create_directory_components(allocator, path_str));

ASSERT_SUCCESS(aws_set_environment_value(s_home_env_var, path_str));

aws_byte_buf_clean_up(&path_buf);
*out_path = path_str;
return AWS_OP_SUCCESS;
}

/*
* Mocked HTTP connection manager for tests
*/
Expand Down
5 changes: 5 additions & 0 deletions tests/credentials_provider_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ struct aws_credentials_provider *aws_credentials_provider_new_null(
*/
int aws_create_directory_components(struct aws_allocator *allocator, const struct aws_string *path);

/**
* Create a new directory (under current working dir) and set $HOME env variable.
*/
int aws_create_random_home_directory(struct aws_allocator *allocator, struct aws_string **out_path);

/**
* Mocked HTTP connection manager for tests
*/
Expand Down
Loading

0 comments on commit 9107251

Please sign in to comment.