Skip to content

Commit

Permalink
improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gcontini committed Apr 25, 2020
1 parent 352744a commit 3609343
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ jobs:
- docker commit centos7_toconfigure centos7_configured
script:
- docker run --name centos7_make -v `pwd`:/root/open-license-manager centos7_configured /bin/bash -c
"cd /root/open-license-manager/build && make && make install && VIRT_ENV=CONTAINER make test"
"cd /root/open-license-manager/build && make && make install && VIRTUAL_ENV=CONTAINER make test"
- os: linux
dist: bionic
name: "CentOS-8 Docker"
Expand Down
31 changes: 26 additions & 5 deletions src/inspector/inspector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,33 @@ const unordered_map<int, string> descByVirtDetail = {{BARE_TO_METAL, "No virtual
{HV, "Microsoft Hypervisor"},
{V_OTHER, "Other type of vm"}};

const unordered_map<LCC_API_VIRTUALIZATION_SUMMARY, string> descByVirt = {
const unordered_map<int, string> descByVirt = {
{LCC_API_VIRTUALIZATION_SUMMARY::NONE, "No virtualization"},
{LCC_API_VIRTUALIZATION_SUMMARY::VM, "VM"},
{LCC_API_VIRTUALIZATION_SUMMARY::VM, "Virtual machine"},
{LCC_API_VIRTUALIZATION_SUMMARY::CONTAINER, "Container"}};

typedef enum {
PROV_UNKNOWN = 0,
ON_PREMISE = 1,
GOOGLE_CLOUD = 2,
AZURE_CLOUD = 3,
AWS = 4,
/**
* "/sys/class/dmi/id/bios_vendor" SeaBIOS
* "/sys/class/dmi/id/sys_vendor" Alibaba Cloud
* modalias
* "dmi:bvnSeaBIOS:bvrrel-1.7.5-0-ge51488c-20140602_164612-nilsson.home.kraxel.org:bd04/01/2014:svnAlibabaCloud:pnAlibabaCloudECS:pvrpc-i440fx-2.1:cvnAlibabaCloud:ct1:cvrpc-i440fx-2.1:"
*/
ALI_CLOUD = 5
} LCC_API_CLOUD_PROVIDER;

const unordered_map<int, string> descByCloudProvider = {{PROV_UNKNOWN, "Provider unknown"},
{ON_PREMISE, "On premise hardware (no cloud)"},
{GOOGLE_CLOUD, "Google Cloud"},
{AZURE_CLOUD, "Microsoft Azure"},
{AWS, "Amazon AWS"},
{ALI_CLOUD, "Alibaba Cloud (Chinese cloud provider)"}};

const unordered_map<int, string> stringByEventType = {
{LICENSE_OK, "OK "},
{LICENSE_FILE_NOT_FOUND, "license file not found "},
Expand Down Expand Up @@ -68,15 +90,14 @@ int main(int argc, char* argv[]) {
}
cout << "Virtualiz. class :" << descByVirt.find(exec_env_info.virtualization)->second << endl;
cout << "Virtualiz. detail:" << descByVirtDetail.find(exec_env_info.virtualization_detail)->second << endl;
cout << "Cloud provider :" << exec_env_info.cloud_provider << endl << "=============" << endl;
;
cout << "Cloud provider :" << descByCloudProvider.find(exec_env_info.cloud_provider) << endl
<< "=============" << endl;
license::os::CpuInfo cpu;
cout << "Cpu Vendor :" << cpu.vendor() << endl;
cout << "Cpu Brand :" << cpu.brand() << endl;
cout << "Cpu hypervisor :" << cpu.is_hypervisor_set() << endl;
cout << "Cpu model :0x" << std::hex << ((long)cpu.model()) << std::dec << endl;
license::os::DmiInfo dmi_info;

cout << "Bios vendor :" << dmi_info.bios_vendor() << endl;
cout << "Bios description:" << dmi_info.bios_description() << endl;
cout << "System vendor :" << dmi_info.sys_vendor() << endl << endl;
Expand Down
4 changes: 2 additions & 2 deletions src/library/os/execution_environment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ class ExecutionEnvironment {
~ExecutionEnvironment(){};
LCC_API_VIRTUALIZATION_SUMMARY virtualization() const;
bool is_cloud() const;
bool is_docker() const { return m_container_type != CONTAINER_TYPE::DOCKER; }
bool is_docker() const { return m_container_type == CONTAINER_TYPE::DOCKER; }
// detect if it's a kind of container technology (docker or lxc)
bool is_container() const { return m_container_type != NONE; }
bool is_container() const { return m_container_type != CONTAINER_TYPE::NONE; }
LCC_API_CLOUD_PROVIDER cloud_provider() const;
LCC_API_VIRTUALIZATION_DETAIL virtualization_detail() const;
};
Expand Down
23 changes: 14 additions & 9 deletions test/library/os/execution_environment_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,33 @@ namespace test {
using namespace license::os;
using namespace std;

// To test if virtualization is detected correctly define an env variable VIRT_ENV
// To test if virtualization is detected correctly define an env variable VIRTUAL_ENV
// otherwise the test is skipped
BOOST_AUTO_TEST_CASE(test_virtualization) {
const char *env = getenv("VIRTUAL_ENV");
os::ExecutionEnvironment exec_env;
bool docker = false;
if (env != nullptr) {
LCC_API_VIRTUALIZATION_SUMMARY virt = exec_env.virtualization();
if (strcmp(env, "CONTAINER") == 0 || (docker = (strcmp(env, "DOCKER") == 0))) {
BOOST_CHECK_MESSAGE(virt == LCC_API_VIRTUALIZATION_SUMMARY::CONTAINER, "container detected");
string required_virtualization(env);
os::ExecutionEnvironment exec_env;
LCC_API_VIRTUALIZATION_SUMMARY detected_virt = exec_env.virtualization();
if (required_virtualization == "CONTAINER" || (docker = (required_virtualization == "DOCKER"))) {
BOOST_CHECK_MESSAGE(detected_virt == LCC_API_VIRTUALIZATION_SUMMARY::CONTAINER, "container detected");
BOOST_CHECK_MESSAGE(exec_env.is_container(), "container detected");
if (docker) {
BOOST_CHECK_MESSAGE(exec_env.is_docker(), "docker detected");
}
} else if (strcmp(env, "VM") == 0) {
BOOST_CHECK_MESSAGE(virt == LCC_API_VIRTUALIZATION_SUMMARY::VM, "VM detected");
} else if (required_virtualization == "VM") {
BOOST_CHECK_MESSAGE(detected_virt == LCC_API_VIRTUALIZATION_SUMMARY::VM, "VM detected");
BOOST_CHECK_MESSAGE(!exec_env.is_container(), "VM is not a container");
BOOST_CHECK_MESSAGE(!exec_env.is_docker(), "VM is not a docker");
} else if (strcmp(env, "NONE") == 0) {
BOOST_CHECK_EQUAL(virt, LCC_API_VIRTUALIZATION_SUMMARY::NONE);
BOOST_CHECK_MESSAGE(exec_env.virtualization_detail() != LCC_API_VIRTUALIZATION_DETAIL::BARE_TO_METAL,
"It is not run bare to metal.");
} else if (required_virtualization == "NONE") {
BOOST_CHECK_EQUAL(detected_virt, LCC_API_VIRTUALIZATION_SUMMARY::NONE);
BOOST_CHECK_MESSAGE(!exec_env.is_container(), "not a container");
BOOST_CHECK_MESSAGE(!exec_env.is_docker(), "not a docker");
BOOST_CHECK_MESSAGE(exec_env.virtualization_detail() == LCC_API_VIRTUALIZATION_DETAIL::BARE_TO_METAL,
"running bare to metal.");
} else {
BOOST_FAIL(string("value ") + env + " not supported: VM,DOCKER,CONTAINER,NONE");
}
Expand Down

0 comments on commit 3609343

Please sign in to comment.