Skip to content
Snippets Groups Projects
Commit e57b02d2 authored by Rafael S Castilho's avatar Rafael S Castilho
Browse files

SCRUM#72: testing with .run


Implemented a .run, testing to see if it works with the rest of the code

Signed-off-by: default avatarRafael S Castilho <rsc15@inf.ufpr.br>
parent 2f6345ca
No related branches found
No related tags found
2 merge requests!51Merge development to master,!47Issue/72
...@@ -25,14 +25,18 @@ compile: ...@@ -25,14 +25,18 @@ compile:
- debian - debian
script: script:
- echo "Estágio 'build'" - echo "Estágio 'build'"
- apt-get update && apt-get install -y build-essential cmake git - apt-get update && apt-get install -y build-essential cmake git makeself
libudev-dev libudev-dev
- git submodule init - git submodule init
- git submodule update --init --recursive - git submodule update --init --recursive
- mkdir build - mkdir build
- cd build - cd build
- cmake -DCURL_STATICLIB=ON -DPRINT_JSON=ON .. - cmake -DCURL_STATICLIB=ON ..
- make - make
- cd ..
- makeself --notemp --noprogress bin/ agent.run "" ./agent-v0.0 --once --print
- chmod +x agent.run
- mv agent.run bin/
testDebian: testDebian:
stage: test stage: test
...@@ -71,7 +75,7 @@ testOpensuse: ...@@ -71,7 +75,7 @@ testOpensuse:
- opensuse - opensuse
script: script:
- zypper -n update - zypper -n update
- zypper -n install bats jq libfaketime expect - zypper -n install bats jq libfaketime expect tar
- touch /var/log/wtmp - touch /var/log/wtmp
- chown root.tty /var/log/wtmp - chown root.tty /var/log/wtmp
- chmod 664 /var/log/wtmp - chmod 664 /var/log/wtmp
......
...@@ -20,9 +20,9 @@ ...@@ -20,9 +20,9 @@
*/ */
#include <agent/linux/inventory.h> #include <agent/linux/inventory.h>
#include <list> #include <list>
#include <string>
Json::Value get_inventory() { Json::Value get_inventory() {
Json::Value inv; Json::Value inv;
// os info // os info
...@@ -30,69 +30,68 @@ Json::Value get_inventory() { ...@@ -30,69 +30,68 @@ Json::Value get_inventory() {
struct utsname sysinfo; struct utsname sysinfo;
uname(&sysinfo); uname(&sysinfo);
inv["data"]["os_type"] = "Linux"; inv["data_inventory"]["os_type"] = "Linux";
try { try {
inv["data"]["os_distro"] = get_distro(); inv["data_inventory"]["os_distro"] = get_distro();
} }
catch ( std::string err ) { catch ( std::string err ) {
inv["data"]["os_distro"] = Json::nullValue; inv["data_inventory"]["os_distro"] = Json::nullValue;
inv["error"]["os_distro"] = err; inv["error_inventory"]["os_distro"] = err;
} }
inv["data"]["os_kernel"] = sysinfo.release; inv["data_inventory"]["os_kernel"] = sysinfo.release;
inv["data"]["mirror_timestamp"] = sysinfo.version; inv["data_inventory"]["mirror_timestamp"] = sysinfo.version;
try { try {
inv["data"]["processor"] = get_processor_model(); inv["data_inventory"]["processor"] = get_processor_model();
} }
catch ( std::string err ) { catch ( std::string err ) {
inv["data"]["processor"] = Json::nullValue; inv["data_inventory"]["processor"] = Json::nullValue;
inv["error"]["processor"] = err; inv["error_inventory"]["processor"] = err;
} }
try { try {
inv["data"]["memory"] = get_memory_size(); inv["data_inventory"]["memory"] = get_memory_size();
} }
catch ( std::string err ) { catch ( std::string err ) {
inv["data"]["memory"] = Json::nullValue; inv["data_inventory"]["memory"] = Json::nullValue;
inv["error"]["memory"] = err; inv["error_inventory"]["memory"] = err;
} }
try { try {
std::list<disk_t> disks; std::list<disk_t> disks;
get_disks_info(disks); get_disks_info(disks);
disk_t curr = disks.front(); disk_t curr = disks.front();
inv["data"]["disk1_model"] = curr.model; inv["data_inventory"]["disk1_model"] = curr.model;
inv["data"]["disk1_size"] = curr.size; inv["data_inventory"]["disk1_size"] = curr.size;
disks.pop_front(); disks.pop_front();
if (disks.empty()) { if (disks.empty()) {
inv["data"]["disk2_model"] = Json::nullValue; inv["data_inventory"]["disk2_model"] = Json::nullValue;
inv["data"]["disk2_size"] = Json::nullValue; inv["data_inventory"]["disk2_size"] = Json::nullValue;
} else { } else {
curr = disks.front(); curr = disks.front();
inv["data"]["disk2_model"] = curr.model; inv["data_inventory"]["disk2_model"] = curr.model;
inv["data"]["disk2_size"] = curr.size; inv["data_inventory"]["disk2_size"] = curr.size;
disks.pop_front(); disks.pop_front();
} }
Json::UInt64 disks_size = disks.size(); Json::UInt64 disks_size = disks.size();
inv["data"]["extra_hds"] = disks_size; inv["data_inventory"]["extra_hds"] = disks_size;
} }
catch ( std::string err ) { catch ( std::string err ) {
inv["data"]["disk1_model"] = Json::nullValue; inv["data_inventory"]["disk1_model"] = Json::nullValue;
inv["data"]["disk1_size"] = Json::nullValue; inv["data_inventory"]["disk1_size"] = Json::nullValue;
inv["data"]["disk2_model"] = Json::nullValue; inv["data_inventory"]["disk2_model"] = Json::nullValue;
inv["data"]["disk2_size"] = Json::nullValue; inv["data_inventory"]["disk2_size"] = Json::nullValue;
inv["data"]["extra_hds"] = Json::nullValue; inv["data_inventory"]["extra_hds"] = Json::nullValue;
inv["error"]["disk1_model"] = err; inv["error_inventory"]["disk1_model"] = err;
inv["error"]["disk1_size"] = err; inv["error_inventory"]["disk1_size"] = err;
inv["error"]["disk2_model"] = err; inv["error_inventory"]["disk2_model"] = err;
inv["error"]["disk2_size"] = err; inv["error_inventory"]["disk2_size"] = err;
inv["error"]["extra_hds"] = err; inv["error_inventory"]["extra_hds"] = err;
} }
// hw info // hw info
...@@ -107,33 +106,33 @@ Json::Value get_inventory() { ...@@ -107,33 +106,33 @@ Json::Value get_inventory() {
inv["contact_date"] = inv["contact_date"] =
*/ */
if (inv["data"]["os_distro"].asString().find("Linux Comunicações") if (inv["data_inventory"]["os_distro"].asString().find("Linux Comunicações")
!= std::string::npos) { != std::string::npos) {
try { try {
inv["data"]["machine_type"] = get_machine_type(); inv["data_inventory"]["machine_type"] = get_machine_type();
} }
catch ( std::string err ) { catch ( std::string err ) {
inv["data"]["machine_type"] = Json::nullValue; inv["data_inventory"]["machine_type"] = Json::nullValue;
inv["error"]["machine_type"] = err; inv["error_inventory"]["machine_type"] = err;
} }
} else { } else {
inv["data"]["machine_type"] = Json::nullValue; inv["data_inventory"]["machine_type"] = Json::nullValue;
} }
try { try {
inv["data"]["amount_users"] = get_user_count(); inv["data_inventory"]["amount_users"] = get_user_count();
} }
catch ( std::string err ) { catch ( std::string err ) {
inv["data"]["amount_users"] = Json::nullValue; inv["data_inventory"]["amount_users"] = Json::nullValue;
inv["error"]["amount_users"] = err; inv["error_inventory"]["amount_users"] = err;
} }
try { try {
inv["data"]["mac_address"] = get_macaddr(); inv["data_inventory"]["mac_address"] = get_macaddr();
} }
catch ( std::string err ) { catch ( std::string err ) {
inv["data"]["mac_address"] = Json::nullValue; inv["data_inventory"]["mac_address"] = Json::nullValue;
inv["error"]["mac_address"] = err; inv["error_inventory"]["mac_address"] = err;
} }
return inv; return inv;
......
...@@ -43,13 +43,14 @@ Json::Value get_net_bandwidth() { ...@@ -43,13 +43,14 @@ Json::Value get_net_bandwidth() {
b_tr_allt = 0; b_tr_allt = 0;
pack_tr_allt = 0; pack_tr_allt = 0;
} else { } else {
b_rec_allt = net_band_all_time["data"]["bytes_recived"].asLargestUInt(); b_rec_allt =
net_band_all_time["data_net"]["bytes_recived"].asLargestUInt();
pack_rec_allt = pack_rec_allt =
net_band_all_time["data"]["packets_recived"].asLargestUInt(); net_band_all_time["data_net"]["packets_recived"].asLargestUInt();
b_tr_allt = b_tr_allt =
net_band_all_time["data"]["bytes_transmited"].asLargestUInt(); net_band_all_time["data_net"]["bytes_transmited"].asLargestUInt();
pack_tr_allt = pack_tr_allt =
net_band_all_time["data"]["packets_tranmited"].asLargestUInt(); net_band_all_time["data_net"]["packets_tranmited"].asLargestUInt();
} }
...@@ -61,22 +62,39 @@ Json::Value get_net_bandwidth() { ...@@ -61,22 +62,39 @@ Json::Value get_net_bandwidth() {
getline(myfile, t); getline(myfile, t);
int i = 0; int i = 0;
int j = 0;
int len = 0; int len = 0;
char tmp[20];
std::string stmp;
while (t[i] == ' ') { while (t[i] == ' ') {
++i; ++i;
} }
while (t[i] != ' ') { while (t[i] != ' ') {
tmp[j] = t[i];
tmp[j+1] = '\0';
stmp = tmp;
++i; ++i;
++j;
} // skips " eth0:" } // skips " eth0:"
if (stmp != "eth0:") {
i = 0;
getline(myfile, t);
while (t[i] == ' ') {
++i;
}
while (t[i] != ' ') {
++i;
}
}
while (t[i] == ' ') { while (t[i] == ' ') {
++i; ++i;
} }
int j = i; j = i;
while (t[i] != ' ') { while (t[i] != ' ') {
++i; ++i;
...@@ -164,10 +182,10 @@ Json::Value get_net_bandwidth() { ...@@ -164,10 +182,10 @@ Json::Value get_net_bandwidth() {
pack_tr = atoll(t.substr(j, len).c_str()); pack_tr = atoll(t.substr(j, len).c_str());
ofs.open("net_band.json", std::ios::trunc); ofs.open("net_band.json", std::ios::trunc);
net_band_all_time["data"]["bytes_recived"] = b_rec; net_band_all_time["data_net"]["bytes_recived"] = b_rec;
net_band_all_time["data"]["packets_recived"] = pack_rec; net_band_all_time["data_net"]["packets_recived"] = pack_rec;
net_band_all_time["data"]["bytes_transmited"] = b_tr; net_band_all_time["data_net"]["bytes_transmited"] = b_tr;
net_band_all_time["data"]["packets_tranmited"] = pack_tr; net_band_all_time["data_net"]["packets_tranmited"] = pack_tr;
ofs << net_band_all_time; ofs << net_band_all_time;
ofs.close(); ofs.close();
//////////////////////////////////////////////////// ////////////////////////////////////////////////////
...@@ -178,11 +196,11 @@ Json::Value get_net_bandwidth() { ...@@ -178,11 +196,11 @@ Json::Value get_net_bandwidth() {
getline(time_file, str); getline(time_file, str);
time_file.close(); time_file.close();
////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////
net_band["data"]["bytes_recived"] = b_rec - b_rec_allt; net_band["data_net"]["bytes_recived"] = b_rec - b_rec_allt;
net_band["data"]["packets_recived"] = pack_rec - pack_rec_allt; net_band["data_net"]["packets_recived"] = pack_rec - pack_rec_allt;
net_band["data"]["bytes_transmited"] = b_tr - b_tr_allt; net_band["data_net"]["bytes_transmited"] = b_tr - b_tr_allt;
net_band["data"]["packets_tranmited"] = pack_tr - pack_tr_allt; net_band["data_net"]["packets_tranmited"] = pack_tr - pack_tr_allt;
net_band["data"]["collect_time"] = str; net_band["data_net"]["collect_time"] = str;
myfile.close(); myfile.close();
return (net_band); return (net_band);
} }
...@@ -71,7 +71,7 @@ int parse_command_line(int ac, char* av[], int *increment, int *lim, ...@@ -71,7 +71,7 @@ int parse_command_line(int ac, char* av[], int *increment, int *lim,
desc.add_options() desc.add_options()
("help", "produce help message") ("help", "produce help message")
("once", "run agent only once") ("once", "run agent only once")
("print", "set print JSON to true"); ("print", "set print JSON to true")
("uninstall", "uninstall agent and all associated files"); ("uninstall", "uninstall agent and all associated files");
po::variables_map vm; po::variables_map vm;
...@@ -90,7 +90,6 @@ int parse_command_line(int ac, char* av[], int *increment, int *lim, ...@@ -90,7 +90,6 @@ int parse_command_line(int ac, char* av[], int *increment, int *lim,
} else { } else {
*print = false; *print = false;
} }
return (0);
if (vm.count("uninstall")) { if (vm.count("uninstall")) {
fs::remove_all("../../agent"); fs::remove_all("../../agent");
......
...@@ -29,7 +29,6 @@ int send_inventory(bool print) { ...@@ -29,7 +29,6 @@ int send_inventory(bool print) {
Json::Value inventory = get_inventory(); Json::Value inventory = get_inventory();
Json::FastWriter fastWriter; Json::FastWriter fastWriter;
// #ifdef PRINT_JSON
if (print == true) { if (print == true) {
Json::StyledWriter styledWriter; Json::StyledWriter styledWriter;
if (!inventory.isNull()) if (!inventory.isNull())
...@@ -37,7 +36,6 @@ int send_inventory(bool print) { ...@@ -37,7 +36,6 @@ int send_inventory(bool print) {
else else
std::cout << "Empty inventory object" << std::endl; std::cout << "Empty inventory object" << std::endl;
} }
// #endif
auto p = cpr::Post(cpr::Url{base_url + "/api/v1/collect"}, auto p = cpr::Post(cpr::Url{base_url + "/api/v1/collect"},
cpr::Parameters{{"type", "inventory"}}, cpr::Parameters{{"type", "inventory"}},
...@@ -59,7 +57,6 @@ int send_net_bandwidth(bool print) { ...@@ -59,7 +57,6 @@ int send_net_bandwidth(bool print) {
Json::Value net = get_net_bandwidth(); Json::Value net = get_net_bandwidth();
Json::FastWriter fastWriter; Json::FastWriter fastWriter;
// #ifdef PRINT_JSON
if (print == true) { if (print == true) {
Json::StyledWriter styledWriter; Json::StyledWriter styledWriter;
if (!net.isNull()) if (!net.isNull())
...@@ -67,7 +64,6 @@ int send_net_bandwidth(bool print) { ...@@ -67,7 +64,6 @@ int send_net_bandwidth(bool print) {
else else
std::cout << "Empty network_bandwidth object" << std::endl; std::cout << "Empty network_bandwidth object" << std::endl;
} }
// #endif
auto p = cpr::Post(cpr::Url{base_url + "/api/v1/collect"}, auto p = cpr::Post(cpr::Url{base_url + "/api/v1/collect"},
cpr::Parameters{{"type", "network_bandwidth"}}, cpr::Parameters{{"type", "network_bandwidth"}},
......
...@@ -2,55 +2,63 @@ ...@@ -2,55 +2,63 @@
function amount_users() { function amount_users() {
expected_number_of_users=3 expected_number_of_users=3
obj=$(cat export.json | jq -r '.data.amount_users') obj=$(cat inventory.json | jq -r '.data_inventory.amount_users')
[[ "$obj" -eq "$expected_number_of_users" ]] && [[ "$obj" != "null" ]] [[ "$obj" -eq "$expected_number_of_users" ]] && [[ "$obj" != "null" ]]
} }
function mac_address() { function mac_address() {
obj=$(cat export.json | jq -r '.data.mac_address') obj=$(cat inventory.json | jq -r '.data_inventory.mac_address')
[[ "$obj" =~ ^(..:){5}..$ ]] && [[ "$obj" != "null" ]] [[ "$obj" =~ ^(..:){5}..$ ]] && [[ "$obj" != "null" ]]
} }
function machine_type() { function machine_type() {
os=$(cat export.json | jq -r '.data.os_distro') os=$(cat inventory.json | jq -r '.data_inventory.os_distro')
obj=$(cat export.json | jq -r '.data.machine_type') obj=$(cat inventory.json | jq -r '.data_inventory.machine_type')
! [[ "$os" =~ .*Linux\ Comunicações.* ]] || ( [[ "$os" =~ .*Linux\ Comunicações.* ]] && [[ "$obj" != "null" ]] ) ! [[ "$os" =~ .*Linux\ Comunicações.* ]] || ( [[ "$os" =~ .*Linux\ Comunicações.* ]] && [[ "$obj" != "null" ]] )
} }
function memory() { function memory() {
obj=$(cat export.json | jq -r '.data.memory') obj=$(cat inventory.json | jq -r '.data_inventory.memory')
[[ "$obj" -eq "$obj" ]] && [[ "$obj" != "null" ]] [[ "$obj" -eq "$obj" ]] && [[ "$obj" != "null" ]]
} }
function os_distro() { function os_distro() {
obj=$(cat export.json | jq -r '.data.os_distro') obj=$(cat inventory.json | jq -r '.data_inventory.os_distro')
[[ "$obj" != "null" ]] [[ "$obj" != "null" ]]
} }
function os_kernel() { function os_kernel() {
obj=$(cat export.json | jq -r '.data.os_kernel') obj=$(cat inventory.json | jq -r '.data_inventory.os_kernel')
[[ "$obj" != "null" ]] [[ "$obj" != "null" ]]
} }
function os_type() { function os_type() {
obj=$(cat export.json | jq -r '.data.os_type') obj=$(cat inventory.json | jq -r '.data_inventory.os_type')
[[ "$obj" != "null" ]] [[ "$obj" != "null" ]]
} }
function processor() { function processor() {
obj=$(cat export.json | jq -r '.data.processor') obj=$(cat inventory.json | jq -r '.data_inventory.processor')
[[ "$obj" != "null" ]] [[ "$obj" != "null" ]]
} }
setup() { setup() {
./../bin/agent-v0.0 --once --print | head -n -2 > export.json ./../bin/agent.run --quiet | head -n -1 > export.json
pt1=$(cat export.json | head -n -9)
echo $pt1 > inventory.json
pt2=$(cat export.json | tail -n 9)
echo $pt2 > net.json
[ -s export.json ] [ -s export.json ]
[ -s inventory.json ]
[ -s net.json ]
cat export.json cat export.json
} }
teardown() { teardown() {
rm -f export.json rm -f export.json
rm -f inventory.json
rm -f net.json
} }
@test "Amount Users" { @test "Amount Users" {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment