From af4226cc48a9ed72ecf99b4de75d3bd53bad8256 Mon Sep 17 00:00:00 2001 From: CIPop Date: Tue, 1 Mar 2022 00:20:58 +0000 Subject: [PATCH 1/2] Adding retries to the DPS test. --- .../tests/common_prov_e2e/common_prov_e2e.c | 30 +++++++++++++++++-- .../tests/common_prov_e2e/common_prov_e2e.h | 4 ++- .../prov_x509_client_e2e.c | 12 ++++---- .../prov_x509_client_openssl_engine_e2e.c | 18 +++++++---- 4 files changed, 48 insertions(+), 16 deletions(-) diff --git a/provisioning_client/tests/common_prov_e2e/common_prov_e2e.c b/provisioning_client/tests/common_prov_e2e/common_prov_e2e.c index d1c976c906..65a0c8c76f 100644 --- a/provisioning_client/tests/common_prov_e2e/common_prov_e2e.c +++ b/provisioning_client/tests/common_prov_e2e/common_prov_e2e.c @@ -34,6 +34,8 @@ #include "common_prov_e2e.h" +const int TEST_PROV_RANDOMIZED_BACK_OFF_SEC = 60; + #define MAX_CLOUD_TRAVEL_TIME 60.0 #define DEVICE_GUID_SIZE 37 @@ -304,8 +306,9 @@ void remove_enrollment_device(const char* g_prov_conn_string) } -void send_dps_test_registration(const char* global_uri, const char* scope_id, PROV_DEVICE_TRANSPORT_PROVIDER_FUNCTION protocol, bool g_enable_tracing) +static REGISTRATION_RESULT send_dps_test_registration(const char* global_uri, const char* scope_id, PROV_DEVICE_TRANSPORT_PROVIDER_FUNCTION protocol, bool g_enable_tracing) { + REGISTRATION_RESULT result; PROV_CLIENT_E2E_INFO prov_info; memset(&prov_info, 0, sizeof(PROV_CLIENT_E2E_INFO)); @@ -341,11 +344,32 @@ void send_dps_test_registration(const char* global_uri, const char* scope_id, PR wait_for_dps_result(handle, &prov_info); - // Assert - ASSERT_ARE_EQUAL(int, REG_RESULT_COMPLETE, prov_info.reg_result, "Failure calling registering device x509 http"); + result = prov_info.reg_result; free(prov_info.iothub_uri); free(prov_info.device_id); Prov_Device_LL_Destroy(handle); + + return result; +} + +void send_dps_test_registration_with_retry(const char* global_uri, const char* scope_id, PROV_DEVICE_TRANSPORT_PROVIDER_FUNCTION protocol, bool g_enable_tracing) +{ + if (send_dps_test_registration(global_uri, scope_id, protocol, g_enable_tracing) != REG_RESULT_COMPLETE) + { + // DPS fails when having multiple enrollments at the same time: + // {"errorCode":409203,"trackingId":"e5490c1e-2528-4eb5-9cf6-e72e80c20268","message":"Precondition failed.","timestampUtc":"2022-02-28T10:11:31.1373215Z"} + // Since we are running these tests on multiple machines we retry with a randomized back-off timer. + srand(time(0)); + int random_back_off_sec = rand() % TEST_PROV_RANDOMIZED_BACK_OFF_SEC; + LogInfo("prov_x509_client_e2e failed: Random back-off = %ds", random_back_off_sec); + ThreadAPI_Sleep(random_back_off_sec * 1000); + + ASSERT_ARE_EQUAL( + int, + REG_RESULT_COMPLETE, + send_dps_test_registration(global_uri, scope_id, protocol, g_enable_tracing), + "Failure during device provisioning"); + } } diff --git a/provisioning_client/tests/common_prov_e2e/common_prov_e2e.h b/provisioning_client/tests/common_prov_e2e/common_prov_e2e.h index d8a40fc146..0c41522e98 100644 --- a/provisioning_client/tests/common_prov_e2e/common_prov_e2e.h +++ b/provisioning_client/tests/common_prov_e2e/common_prov_e2e.h @@ -57,9 +57,11 @@ extern void remove_enrollment_device(); extern void wait_for_dps_result(PROV_DEVICE_LL_HANDLE handle, PROV_CLIENT_E2E_INFO* prov_info); extern int construct_device_id(const char* prefix, char** device_name); -extern void send_dps_test_registration(const char* global_uri, const char* scope_id, PROV_DEVICE_TRANSPORT_PROVIDER_FUNCTION protocol, bool use_tracing); +extern void send_dps_test_registration_with_retry(const char* global_uri, const char* scope_id, PROV_DEVICE_TRANSPORT_PROVIDER_FUNCTION protocol, bool use_tracing); extern char* convert_base64_to_string(const char* base64_cert); +extern const int TEST_PROV_RANDOMIZED_BACK_OFF_SEC; + #ifdef __cplusplus } #endif diff --git a/provisioning_client/tests/prov_x509_client_e2e/prov_x509_client_e2e.c b/provisioning_client/tests/prov_x509_client_e2e/prov_x509_client_e2e.c index 6cfeaafa73..cd5547a842 100644 --- a/provisioning_client/tests/prov_x509_client_e2e/prov_x509_client_e2e.c +++ b/provisioning_client/tests/prov_x509_client_e2e/prov_x509_client_e2e.c @@ -32,8 +32,6 @@ char* g_dps_x509_key_individual = NULL; char* g_dps_regid_individual = NULL; const bool g_enable_tracing = true; -static const int TEST_PROV_RANDOMIZED_BACK_OFF_SEC = 60; - BEGIN_TEST_SUITE(prov_x509_client_e2e) TEST_SUITE_INITIALIZE(TestClassInitialize) @@ -99,31 +97,31 @@ BEGIN_TEST_SUITE(prov_x509_client_e2e) #if USE_HTTP TEST_FUNCTION(dps_register_x509_device_http_success) { - send_dps_test_registration(g_dps_uri, g_dps_scope_id, Prov_Device_HTTP_Protocol, g_enable_tracing); + send_dps_test_registration_with_retry(g_dps_uri, g_dps_scope_id, Prov_Device_HTTP_Protocol, g_enable_tracing); } #endif #if USE_AMQP TEST_FUNCTION(dps_register_x509_device_amqp_success) { - send_dps_test_registration(g_dps_uri, g_dps_scope_id, Prov_Device_AMQP_Protocol, g_enable_tracing); + send_dps_test_registration_with_retry(g_dps_uri, g_dps_scope_id, Prov_Device_AMQP_Protocol, g_enable_tracing); } TEST_FUNCTION(dps_register_x509_device_amqp_ws_success) { - send_dps_test_registration(g_dps_uri, g_dps_scope_id, Prov_Device_AMQP_WS_Protocol, g_enable_tracing); + send_dps_test_registration_with_retry(g_dps_uri, g_dps_scope_id, Prov_Device_AMQP_WS_Protocol, g_enable_tracing); } #endif #if USE_MQTT TEST_FUNCTION(dps_register_x509_device_mqtt_success) { - send_dps_test_registration(g_dps_uri, g_dps_scope_id, Prov_Device_MQTT_Protocol, g_enable_tracing); + send_dps_test_registration_with_retry(g_dps_uri, g_dps_scope_id, Prov_Device_MQTT_Protocol, g_enable_tracing); } TEST_FUNCTION(dps_register_x509_device_mqtt_ws_success) { - send_dps_test_registration(g_dps_uri, g_dps_scope_id, Prov_Device_MQTT_WS_Protocol, g_enable_tracing); + send_dps_test_registration_with_retry(g_dps_uri, g_dps_scope_id, Prov_Device_MQTT_WS_Protocol, g_enable_tracing); } #endif END_TEST_SUITE(prov_x509_client_e2e) diff --git a/provisioning_client/tests/prov_x509_client_openssl_engine_e2e/prov_x509_client_openssl_engine_e2e.c b/provisioning_client/tests/prov_x509_client_openssl_engine_e2e/prov_x509_client_openssl_engine_e2e.c index 75b58204a5..5218153c4a 100644 --- a/provisioning_client/tests/prov_x509_client_openssl_engine_e2e/prov_x509_client_openssl_engine_e2e.c +++ b/provisioning_client/tests/prov_x509_client_openssl_engine_e2e/prov_x509_client_openssl_engine_e2e.c @@ -57,6 +57,14 @@ BEGIN_TEST_SUITE(prov_x509_client_openssl_engine_e2e) g_dps_regid_individual = getenv(DPS_X509_INDIVIDUAL_REGISTRATION_ID); ASSERT_IS_NOT_NULL(g_dps_regid_individual, "DPS_X509_INDIVIDUAL_REGISTRATION_ID is NULL"); + // DPS fails when having multiple enrollments at the same time. + // Since we are running these tests on multiple machines with each test taking about 1 second, + // we randomize their start time to avoid collisions. + srand(time(0)); + int random_back_off_sec = rand() % TEST_PROV_RANDOMIZED_BACK_OFF_SEC; + LogInfo("prov_x509_client_e2e: Random back-off = %ds", random_back_off_sec); + ThreadAPI_Sleep(random_back_off_sec * 1000); + // Register device create_x509_individual_enrollment_device(); } @@ -81,31 +89,31 @@ BEGIN_TEST_SUITE(prov_x509_client_openssl_engine_e2e) #if USE_HTTP TEST_FUNCTION(dps_register_x509_openssl_engine_device_http_success) { - send_dps_test_registration(g_dps_uri, g_dps_scope_id, Prov_Device_HTTP_Protocol, g_enable_tracing); + send_dps_test_registration_with_retry(g_dps_uri, g_dps_scope_id, Prov_Device_HTTP_Protocol, g_enable_tracing); } #endif #if USE_AMQP TEST_FUNCTION(dps_register_x509_openssl_engine_device_amqp_success) { - send_dps_test_registration(g_dps_uri, g_dps_scope_id, Prov_Device_AMQP_Protocol, g_enable_tracing); + send_dps_test_registration_with_retry(g_dps_uri, g_dps_scope_id, Prov_Device_AMQP_Protocol, g_enable_tracing); } TEST_FUNCTION(dps_register_x509_openssl_engine_device_amqp_ws_success) { - send_dps_test_registration(g_dps_uri, g_dps_scope_id, Prov_Device_AMQP_WS_Protocol, g_enable_tracing); + send_dps_test_registration_with_retry(g_dps_uri, g_dps_scope_id, Prov_Device_AMQP_WS_Protocol, g_enable_tracing); } #endif #if USE_MQTT TEST_FUNCTION(dps_register_x509_openssl_engine_device_mqtt_success) { - send_dps_test_registration(g_dps_uri, g_dps_scope_id, Prov_Device_MQTT_Protocol, g_enable_tracing); + send_dps_test_registration_with_retry(g_dps_uri, g_dps_scope_id, Prov_Device_MQTT_Protocol, g_enable_tracing); } TEST_FUNCTION(dps_register_x509_openssl_engine_device_mqtt_ws_success) { - send_dps_test_registration(g_dps_uri, g_dps_scope_id, Prov_Device_MQTT_WS_Protocol, g_enable_tracing); + send_dps_test_registration_with_retry(g_dps_uri, g_dps_scope_id, Prov_Device_MQTT_WS_Protocol, g_enable_tracing); } #endif END_TEST_SUITE(prov_x509_client_openssl_engine_e2e) From 5f61de4d4f70ddd80b63f619af9af4f4b580f1f9 Mon Sep 17 00:00:00 2001 From: Cristian Pop Date: Tue, 1 Mar 2022 10:00:20 -0800 Subject: [PATCH 2/2] Apply suggestions from code review Co-authored-by: Eric Wolz --- provisioning_client/tests/common_prov_e2e/common_prov_e2e.c | 2 +- .../prov_x509_client_openssl_engine_e2e.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/provisioning_client/tests/common_prov_e2e/common_prov_e2e.c b/provisioning_client/tests/common_prov_e2e/common_prov_e2e.c index 65a0c8c76f..b07ec66c0f 100644 --- a/provisioning_client/tests/common_prov_e2e/common_prov_e2e.c +++ b/provisioning_client/tests/common_prov_e2e/common_prov_e2e.c @@ -358,7 +358,7 @@ void send_dps_test_registration_with_retry(const char* global_uri, const char* s { if (send_dps_test_registration(global_uri, scope_id, protocol, g_enable_tracing) != REG_RESULT_COMPLETE) { - // DPS fails when having multiple enrollments at the same time: + // DPS fails when having multiple enrollments of the same device ID at the same time: // {"errorCode":409203,"trackingId":"e5490c1e-2528-4eb5-9cf6-e72e80c20268","message":"Precondition failed.","timestampUtc":"2022-02-28T10:11:31.1373215Z"} // Since we are running these tests on multiple machines we retry with a randomized back-off timer. srand(time(0)); diff --git a/provisioning_client/tests/prov_x509_client_openssl_engine_e2e/prov_x509_client_openssl_engine_e2e.c b/provisioning_client/tests/prov_x509_client_openssl_engine_e2e/prov_x509_client_openssl_engine_e2e.c index 5218153c4a..ac73e9dd7c 100644 --- a/provisioning_client/tests/prov_x509_client_openssl_engine_e2e/prov_x509_client_openssl_engine_e2e.c +++ b/provisioning_client/tests/prov_x509_client_openssl_engine_e2e/prov_x509_client_openssl_engine_e2e.c @@ -57,7 +57,7 @@ BEGIN_TEST_SUITE(prov_x509_client_openssl_engine_e2e) g_dps_regid_individual = getenv(DPS_X509_INDIVIDUAL_REGISTRATION_ID); ASSERT_IS_NOT_NULL(g_dps_regid_individual, "DPS_X509_INDIVIDUAL_REGISTRATION_ID is NULL"); - // DPS fails when having multiple enrollments at the same time. + // DPS fails when having multiple enrollments of the same device ID at the same time: // Since we are running these tests on multiple machines with each test taking about 1 second, // we randomize their start time to avoid collisions. srand(time(0));