From 04fa4be2d528c11aa2caff79a3d3e25b6e045b09 Mon Sep 17 00:00:00 2001
From: Lais Frigerio <laaisfrigerio@gmail.com>
Date: Fri, 22 Sep 2017 08:34:54 -0300
Subject: [PATCH] SCRUM#142 - Repair messages errors from windows test

Signed-off-by: Lais Frigerio <laaisfrigerio@gmail.com>
---
 .gitlab-ci.yml        |  6 ------
 generate_agent.iss    |  2 +-
 include/agent/agent.h |  3 +++
 include/agent/main.h  |  6 +++---
 src/agent/agent.cpp   |  5 +++++
 src/agent/main.cpp    | 25 +++++++++++++++++--------
 test/testWin.bat      |  9 +++++++--
 7 files changed, 36 insertions(+), 20 deletions(-)

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index bfc6597c..31849d76 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -72,7 +72,6 @@ testDebian:
         - cd ../test
         - ./create_users.sh
         - last -F
-        - cat /opt/agentC3SL/conf/datasid-conf.json
         - ./returnTest.bats
     dependencies:
         - compileUnix
@@ -88,7 +87,6 @@ testUbuntu:
         - cd test
         - ./create_users.sh
         - last -F
-        - cat /opt/agentC3SL/conf/datasid-conf.json
         - ./returnTest.bats
     dependencies:
         - compileUnix
@@ -107,7 +105,6 @@ testOpensuse:
         - cd test
         - ./create_users.sh
         - last -F
-        - cat /opt/agentC3SL/conf/datasid-conf.json
         - ./returnTest.bats
     dependencies:
         - compileUnix
@@ -117,12 +114,9 @@ testWin7:
     tags:
         - windows
     script:
-        - DIR C:\
         - if exist C:\agentC3SL rmdir C:\agentC3SL /s /q
         - agent-1234.exe /SILENT /VERYSILENT
         - if not exist C:\agentC3SL exit 1;
-        - DIR C:\agentC3SL
-        - DIR C:\agentC3SL\bin
         - copy C:\jq-win32.exe .\test
         - cd .\test
         - C:\agentC3SL\bin\agent-v0.0.exe --once --print > test.json
diff --git a/generate_agent.iss b/generate_agent.iss
index df7e1e7f..a8cf5287 100644
--- a/generate_agent.iss
+++ b/generate_agent.iss
@@ -56,7 +56,7 @@
 #endif
 
 #ifndef prj
-  #define prj 'simmc-agent'
+  #define prj 'simmc'
 #endif
 
 #ifndef outputName
diff --git a/include/agent/agent.h b/include/agent/agent.h
index e370ba63..91a6bccf 100644
--- a/include/agent/agent.h
+++ b/include/agent/agent.h
@@ -25,6 +25,8 @@
  * <br><br>
 */
 
+extern std::string path_to_agent;
+
 class Connection; // forward declarations
 class Conf; // forward declarations
 class Proxy; // forward declarations
@@ -51,6 +53,7 @@ class Agent{
         void setPathLogDir(std::string);
         void setPathLogFile(std::string);
         void setPrefix(std::string, int);
+        void setPrefix();
         void setInstallerName(std::string);
         void setConnection(Connection*);
         void setConf(Conf*);
diff --git a/include/agent/main.h b/include/agent/main.h
index 41d4938f..ac6b3c06 100644
--- a/include/agent/main.h
+++ b/include/agent/main.h
@@ -29,7 +29,7 @@ namespace fs = boost::filesystem;
 #ifdef __linux__
     std::string path_to_agent_tmp =  // NOLINT(runtime/string)
         "/opt/agentC3SL/tmp/";  // NOLINT(runtime/string)
-    std::string path_to_agent = "/opt/";  // NOLINT(runtime/string)
+    std::string path_to_agent = "/opt/agentC3SL/";  // NOLINT(runtime/string)
     //std::string path_to_agent_conf =  // NOLINT(runtime/string)
         //"/opt/agentC3SL/conf/";  // NOLINT(runtime/string)
     std::string path_to_agent_conf = "/home/teste/Imagens/agent/conf/";
@@ -38,7 +38,7 @@ namespace fs = boost::filesystem;
     std::string path_to_agent_tmp =  "C:\\agentC3SL\\tmp\\";
     std::string path_to_agent = "C:\\agentC3SL\\";
     std::string path_to_agent_conf =  // NOLINT(runtime/string)
-        "C:\\agentC3SL\\conf\\";  // NOLINT(runtime/string)
+        "C:\\agentC3SL\\teste\\";  // NOLINT(runtime/string)
 #endif
 
 bool agent_send_inventory = false;
@@ -65,7 +65,7 @@ int main(int, char**);
  *      - print - set print JSON to true - show inventory
  *      - uninstall - uninstall agent and all associated files
  */
-int parse_command_line(int, char*, int*, int*, bool*);
+int parse_command_line(int, char*, int*, int*, bool*, bool*);
 
 bool read_config_files(Agent*);
 
diff --git a/src/agent/agent.cpp b/src/agent/agent.cpp
index 4028ab0a..68ff02d9 100644
--- a/src/agent/agent.cpp
+++ b/src/agent/agent.cpp
@@ -60,6 +60,11 @@ void Agent::setPrefix(std::string p, int tot) {
     this->prefix = p.substr(0, i);
 }
 
+/** Set the absolute path from agent */
+void Agent::setPrefix() {
+    this->prefix = path_to_agent;
+}
+
 /** Set the name to agent installer (.run (linux) or .exe (windows)) file */
 void Agent::setInstallerName(std::string i) {
     this->installerName = i;
diff --git a/src/agent/main.cpp b/src/agent/main.cpp
index bd26082b..76e0a0bc 100644
--- a/src/agent/main.cpp
+++ b/src/agent/main.cpp
@@ -108,13 +108,14 @@ void config_agent() {
  *      - uninstall - uninstall agent and all associated files
  */
 int parse_command_line(int ac, char* av[], int *increment, int *lim,
-     bool *print) {
+     bool *print, bool *dev) {
     po::options_description desc("Allowed options");
     desc.add_options()
         ("help", "produce help message")
         ("once", "run agent only once")
         ("print", "set print JSON to true")
-        ("uninstall", "uninstall agent and all associated files");
+        ("uninstall", "uninstall agent and all associated files")
+        ("dev", "development environment");
 
     po::variables_map vm;
     po::store(po::parse_command_line(ac, av, desc), vm);
@@ -132,6 +133,8 @@ int parse_command_line(int ac, char* av[], int *increment, int *lim,
     } else {
         *print = false;
     }
+    if (vm.count("dev"))
+        *dev = true;
 
     #ifdef __unix__
         if (vm.count("uninstall")) {
@@ -181,19 +184,25 @@ int main(int argc, char* argv[]) {
     agent.setConf(&conf);
     agent.setProxy(&proxy);
 
-    /* Set agent prefix (agent folder) */
-    agent.setPrefix(__FILE__, 3);
-    agent.setPathLogFile("/log/datasid-agent.log");
-    agent.logFile.open(agent.getPathLogFile().c_str(), std::fstream::app);
-
     int increment = 0;
     int lim = 1;
     bool print;
+    bool dev = false;
     pt::ptime begin;
 
-    if (parse_command_line(argc, argv, &increment, &lim, &print)) {
+    if (parse_command_line(argc, argv, &increment, &lim, &print, &dev)) {
         return (1);
     }
+
+    /* Set agent prefix (agent folder) */
+    if (dev)  // __FILE__ macro get the path on compiler time
+        agent.setPrefix(__FILE__, 3);
+    else
+        agent.setPrefix();
+
+    agent.setPathLogFile("/log/datasid-agent.log");
+    agent.logFile.open(agent.getPathLogFile().c_str(), std::fstream::app);
+
     config_agent();
     set_begin(begin);
 
diff --git a/test/testWin.bat b/test/testWin.bat
index 83b70c85..e9cd599a 100644
--- a/test/testWin.bat
+++ b/test/testWin.bat
@@ -74,6 +74,7 @@ FOR /f "delims=" %%a IN (test.json) DO (
 
 REM Verificando o data_inventory json ...
 ECHO Verificando o data_inventory json ...
+type result.json
 
 REM Check agent version
 jq-win32.exe ".data_inventory.agent_version==\"1.0.0\"" result.json > result.txt
@@ -117,7 +118,7 @@ set /p var=<result.txt
 if %var%==false ( exit 1 ) else ( echo extra hds ok )
 
 REM Check id point
-jq-win32.exe ".data_inventory.id_point==\"1234\"" result.json > result.txt
+jq-win32.exe ".data_inventory.id_point==1234" result.json > result.txt
 set /p var=<result.txt
 if %var%==false ( exit 1 ) else ( echo id point ok )
 
@@ -225,4 +226,8 @@ if %var%==false ( exit 1 ) else ( echo packets tranmitted ok )
 
 del result.txt
 del result.json
-del test.json
\ No newline at end of file
+del test.json
+
+REM uninstall agent
+start C:\agentC3SL\uninstall_agent.exe /SILENT /VERYSILENT
+dir /b /a "C:\agentC3SL\*" | >nul findstr "^" && (echo Agent has been uninstalled) || (exit 1);
\ No newline at end of file
-- 
GitLab