gcontini
2020-04-25 36093433d67c2eb2c2a7b86ff4ffb9138e1f1869
improve tests
4个文件已修改
60 ■■■■ 已修改文件
.travis.yml 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/inspector/inspector.cpp 31 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/library/os/execution_environment.hpp 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
test/library/os/execution_environment_test.cpp 23 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
.travis.yml
@@ -98,7 +98,7 @@
        - 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"
src/inspector/inspector.cpp
@@ -23,10 +23,32 @@
                                                     {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 "},
@@ -68,15 +90,14 @@
    }
    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;
src/library/os/execution_environment.hpp
@@ -35,9 +35,9 @@
    ~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;
};
test/library/os/execution_environment_test.cpp
@@ -16,28 +16,33 @@
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");
        }