From 37b7dcc183bf0c01f054de14c993b4840bbfe02e Mon Sep 17 00:00:00 2001
From: Gabriele Contini <contini.mailing@gmail.com>
Date: 周六, 25 4月 2020 22:22:00 +0800
Subject: [PATCH] improve tests

---
 src/library/os/execution_environment.hpp       |    4 ++--
 .travis.yml                                    |    2 +-
 src/inspector/inspector.cpp                    |   31 ++++++++++++++++++++++++++-----
 test/library/os/execution_environment_test.cpp |   23 ++++++++++++++---------
 4 files changed, 43 insertions(+), 17 deletions(-)

diff --git a/.travis.yml b/.travis.yml
index aa174d4..88a7b73 100644
--- a/.travis.yml
+++ b/.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"
diff --git a/src/inspector/inspector.cpp b/src/inspector/inspector.cpp
index dd44f76..0c0819e 100644
--- a/src/inspector/inspector.cpp
+++ b/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;
diff --git a/src/library/os/execution_environment.hpp b/src/library/os/execution_environment.hpp
index 4aeb86d..296d403 100644
--- a/src/library/os/execution_environment.hpp
+++ b/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;
 };
diff --git a/test/library/os/execution_environment_test.cpp b/test/library/os/execution_environment_test.cpp
index dba290f..a058cd0 100644
--- a/test/library/os/execution_environment_test.cpp
+++ b/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");
 		}

--
Gitblit v1.9.1