Skip to content
This repository has been archived by the owner on Mar 3, 2023. It is now read-only.

Commit

Permalink
Fix some issues on unittests. Add dependencies for the test of packages.
Browse files Browse the repository at this point in the history
  • Loading branch information
xlz-jbleclere committed Sep 10, 2020
1 parent a8f0c3f commit 0313ce3
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 40 deletions.
3 changes: 3 additions & 0 deletions deployment/Dockerfile.j2
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ python3 -m pip install -U{% if osName in ("centos", "rhel") and osVersion == "7"
flake8 \
pytest \
requests \
flask \
python-dateutil \
pytest-flask \
tox \
wheel

Expand Down
2 changes: 1 addition & 1 deletion internal_inc/ws_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class CurlEasyPost {
template<class T>
void appendHeader( T&& header ) {
data.push_back( std::forward<T>(header) );
Debug2( "Add {} to CURL header", std::forward<T>(header) );
Debug2( "Add '{}' to CURL header", std::forward<T>(header) );
mHeaders_p = curl_slist_append( mHeaders_p, data.back().c_str() );
}

Expand Down
5 changes: 3 additions & 2 deletions source/csp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,9 @@ Json::Value Aws::get_metadata() {
tokenReq.appendHeader( "X-aws-ec2-metadata-token-ttl-seconds: 21600" );
token = tokenReq.perform_put( "http://169.254.169.254/latest/api/token", timeout_ms );

// Collect Alibaba information
mHTTPRequest.appendHeader( fmt::format("X-aws-ec2-metadata-token: {}", token) );
// Collect AWS information
std::string header = fmt::format("X-aws-ec2-metadata-token: {}", token);
mHTTPRequest.appendHeader( header );
std::string base_url("http://169.254.169.254/latest");
metadata["instance_id"] = mHTTPRequest.perform<std::string>( fmt::format( "{}/meta-data/instance-id", base_url ), timeout_ms );
metadata["instance_type"] = mHTTPRequest.perform<std::string>( fmt::format( "{}/meta-data/instance-type", base_url ), timeout_ms );
Expand Down
22 changes: 13 additions & 9 deletions source/drm_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -418,8 +418,10 @@ class DRM_LOCAL DrmManager::Impl {
}

void uninitLog() {
if ( sLogger )
if ( sLogger ) {
Debug( "Log messages are flushed now." );
sLogger->flush();
}
}

bool findXrtUtility() {
Expand Down Expand Up @@ -663,7 +665,6 @@ class DRM_LOCAL DrmManager::Impl {
Error( "Error in read register callback, errcode = {}: failed to read register {}", ret, regName );
return (uint32_t)(-1);
}
Debug2( "Read DRM register {} = 0x{:08x}", regName, value );
return 0;
}

Expand All @@ -684,7 +685,6 @@ class DRM_LOCAL DrmManager::Impl {
Error( "Error in write register callback, errcode = {}: failed to write {} to register {}", ret, value, regName );
return (uint32_t)(-1);
}
Debug2( "Wrote DRM register {} = 0x{:08x}", regName, value );
return 0;
}

Expand Down Expand Up @@ -992,18 +992,22 @@ class DRM_LOCAL DrmManager::Impl {
json_output["vlnvFile"][i_str]["version"] = std::string("x") + vlnvFile[i].substr(12, 4);
}

// Fulfill with Product information
// Fulfill with product information
if ( !mailboxReadOnly.empty() ) {
try {
Json::Value product_info = parseJsonString( mailboxReadOnly );
if ( product_info.isMember( "product_id" ) )
json_output["product"] = product_info["product_id"];
else
json_output["product"] = product_info;
if ( product_info.isMember( "pkg_version" ) )
if ( product_info.isMember( "pkg_version" ) ) {
json_output["pkg_version"] = product_info["pkg_version"];
if ( product_info.isMember( "dna_type" ) )
Debug( "HDK Generator version: {}", json_output["pkg_version"].asString() );
}
if ( product_info.isMember( "dna_type" ) ) {
json_output["dna_type"] = product_info["dna_type"];
Debug( "HDK DNA type: {}", json_output["dna_type"].asString() );
}
} catch( const Exception &e ) {
if ( e.getErrCode() == DRM_BadFormat )
Throw( DRM_BadFormat, "Failed to parse Read-Only Mailbox in DRM Controller: {}", e.what() );
Expand Down Expand Up @@ -1358,13 +1362,13 @@ class DRM_LOCAL DrmManager::Impl {
timeSpan = TClock::now() - timeStart;
mseconds = 1000.0 * double( timeSpan.count() ) * TClock::period::num / TClock::period::den;
if ( activationCodesTransmitted ) {
Debug( "License #{} transmitted after {} ms", mLicenseCounter, mseconds );
Debug( "License #{} transmitted after {:f} ms", mLicenseCounter, mseconds );
break;
}
Debug2( "License #{} not transmitted yet after {} ms", mLicenseCounter, mseconds );
Debug2( "License #{} not transmitted yet after {:f} ms", mLicenseCounter, mseconds );
}
if ( !activationCodesTransmitted ) {
Throw( DRM_CtlrError, "DRM Controller could not transmit Licence #{} to activators after {} ms. ", mLicenseCounter, mseconds ); //LCOV_EXCL_LINE
Throw( DRM_CtlrError, "DRM Controller could not transmit Licence #{} to activators after {:f} ms. ", mLicenseCounter, mseconds ); //LCOV_EXCL_LINE
}
mExpirationTime += std::chrono::seconds( mLicenseDuration );

Expand Down
4 changes: 3 additions & 1 deletion source/ws_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,9 @@ Json::Value DrmWSClient::requestMetering( const std::string url, const Json::Val
req.setURL( url );
req.appendHeader( "Accept: application/vnd.accelize.v1+json" );
req.appendHeader( "Content-Type: application/json" );
req.appendHeader( std::string("Authorization: Bearer ") + mOAuth2Token );
std::string token_header("Authorization: Bearer ");
token_header += mOAuth2Token;
req.appendHeader( token_header );
req.setPostFields( saveJsonToString( json_req ) );

// Send request and wait response
Expand Down
22 changes: 11 additions & 11 deletions tests/test_drm_license_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,11 @@ def test_header_error_on_licenseTimer(accelize_drm, conf_json, cred_json, async_
finally:
drm_manager.deactivate()
assert not drm_manager.get('license_status')
activators.autotest(is_activated=False)
assert async_cb.was_called
assert async_cb.message is not None
assert async_cb.errcode == accelize_drm.exceptions.DRMCtlrError.error_code
assert "License header check error" in async_cb.message
activators.autotest(is_activated=False)
assert async_cb.was_called
assert async_cb.message is not None
assert async_cb.errcode == accelize_drm.exceptions.DRMCtlrError.error_code
assert "License header check error" in async_cb.message


@pytest.mark.no_parallel
Expand Down Expand Up @@ -150,8 +150,8 @@ def test_session_id_error(accelize_drm, conf_json, cred_json, async_handler, liv
finally:
drm_manager.deactivate()
assert not drm_manager.get('license_status')
activators.autotest(is_activated=False)
async_cb.assert_NoError()
activators.autotest(is_activated=False)
async_cb.assert_NoError()

# Start session #2 to replay session #1
drm_manager.activate()
Expand All @@ -166,7 +166,7 @@ def test_session_id_error(accelize_drm, conf_json, cred_json, async_handler, liv
activators.autotest(is_activated=False)
finally:
drm_manager.deactivate()
assert async_cb.was_called
assert async_cb.message is not None
assert async_cb.errcode == accelize_drm.exceptions.DRMCtlrError.error_code
assert "License header check error" in async_cb.message
assert async_cb.was_called
assert async_cb.message is not None
assert async_cb.errcode == accelize_drm.exceptions.DRMCtlrError.error_code
assert "License header check error" in async_cb.message
6 changes: 2 additions & 4 deletions tests/test_frequency_detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,7 @@ def test_drm_manager_frequency_detection_method1(accelize_drm, conf_json, cred_j
with open(logpath, 'rt') as f:
log_content = f.read()
assert "Use dedicated counter to compute DRM frequency (method 1)" in log_content
if isfile(logpath):
remove(logpath)
remove(logpath)


def test_drm_manager_frequency_detection_method1_exception(accelize_drm, conf_json, cred_json, async_handler):
Expand Down Expand Up @@ -243,8 +242,7 @@ def test_drm_manager_frequency_detection_method2(accelize_drm, conf_json, cred_j
with open(logpath, 'rt') as f:
log_content = f.read()
assert "Use license timer counter to compute DRM frequency (method 2)" in log_content
if isfile(logpath):
remove(logpath)
remove(logpath)
finally:
# Reprogram FPGA with original image
driver.program_fpga(fpga_image_bkp)
Expand Down
7 changes: 3 additions & 4 deletions tests/test_function_arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
"""
Test DRM Library with bad arguments. Make sure errors are detected and reported as expected.
"""
from re import search

import pytest
from re import search


def test_drm_manager_constructor_with_bad_arguments(accelize_drm, conf_json, cred_json,
Expand Down Expand Up @@ -422,9 +421,9 @@ def test_drm_manager_with_bad_credential_file(accelize_drm, conf_json, cred_json

driver = accelize_drm.pytest_fpga_driver[0]
async_cb = async_handler.create()

# Test with an empty crendential file
async_cb.reset()

# Test with an empty crendential file
cred_json.reset()
cred_json._content = {}
cred_json.save()
Expand Down
3 changes: 2 additions & 1 deletion tests/test_hdk_compatibility.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def test_hdk_stability_on_programming(accelize_drm, conf_json, cred_json, async_
conf_json['settings']['log_file_verbosity'] = accelize_drm.create_log_level(0)
conf_json['settings']['log_file_type'] = 1
conf_json['settings']['log_file_path'] = logpath
conf_json['settings']['log_file_append'] = True
conf_json.save()

nb_reset = 10
Expand All @@ -37,8 +38,8 @@ def test_hdk_stability_on_programming(accelize_drm, conf_json, cred_json, async_
driver.write_register_callback,
async_cb.callback
)
assert not drm_manager.get('license_status')
try:
assert not drm_manager.get('license_status')
drm_manager.activate()
assert drm_manager.get('license_status')
finally:
Expand Down
11 changes: 6 additions & 5 deletions tests/test_metered_mode_on_hw.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ def test_metered_start_stop_in_raw(accelize_drm, conf_json, cred_json, async_han
driver.write_register_callback,
async_cb.callback
)
assert not drm_manager.get('license_status')
activators.autotest(is_activated=False)
activators[0].generate_coin(1000)
try:
assert not drm_manager.get('license_status')
activators.autotest(is_activated=False)
activators[0].generate_coin(1000)
drm_manager.activate()
assert drm_manager.get('metered_data') == 0
activators[0].check_coin(drm_manager.get('metered_data'))
Expand Down Expand Up @@ -446,7 +446,7 @@ def test_async_on_pause(accelize_drm, conf_json, cred_json, async_handler):
cred_json.set_user('accelize_accelerator_test_02')
conf_json.reset()
logpath = accelize_drm.create_log_path(whoami())
conf_json['settings']['log_file_verbosity'] = 1
conf_json['settings']['log_file_verbosity'] = 0
conf_json['settings']['log_file_type'] = 1
conf_json['settings']['log_file_path'] = logpath
conf_json.save()
Expand All @@ -465,6 +465,7 @@ def test_async_on_pause(accelize_drm, conf_json, cred_json, async_handler):
drm_manager.activate()
start = datetime.now()
if drm_manager.get('health_period') == 0:
remove(logpath)
pytest.skip('Health is not active: skip async test')
lic_duration = drm_manager.get('license_duration')
assert drm_manager.get('session_status')
Expand All @@ -483,9 +484,9 @@ def test_async_on_pause(accelize_drm, conf_json, cred_json, async_handler):
wait_func_true(lambda: isfile(logpath), 10)
with open(logpath, 'rt') as f:
log_content = f.read()
remove(logpath)
assert len(list(findall(r'"request"\s*:\s*"health"', log_content))) == 1
async_cb.assert_NoError()
remove(logpath)


@pytest.mark.minimum
Expand Down
3 changes: 1 addition & 2 deletions tests/test_retry_mechanism.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def test_long_to_short_retry_switch(accelize_drm, conf_json, cred_json, async_ha
@pytest.mark.no_parallel
def test_retry_on_no_connection(accelize_drm, conf_json, cred_json, async_handler, live_server):
"""
Test the number of expected retris and the gap between 2 retries are correct when the requests are lost
Test the number of expected retries and the gap between 2 retries are correct when the requests are lost
"""
driver = accelize_drm.pytest_fpga_driver[0]
async_cb = async_handler.create()
Expand Down Expand Up @@ -230,6 +230,5 @@ def test_retry_on_no_connection(accelize_drm, conf_json, cred_json, async_handle
attempts_list = [int(e) for e in findall(r'Attempt #(\d+) to obtain a new License failed with message', log_content)]
assert len(attempts_list) == nb_retry
assert sorted(list(attempts_list)) == list(range(1,nb_retry+1))
async_cb.assert_NoError()
remove(logpath)

0 comments on commit 0313ce3

Please sign in to comment.