From 85ca1c677a7db59c3d820084d9e3fa9cd3defb75 Mon Sep 17 00:00:00 2001
From: Sergey Yakubov <sergey.yakubov@desy.de>
Date: Wed, 15 Aug 2018 17:11:07 +0200
Subject: [PATCH] journal write concernt a mongo restart test for broker

---
 broker/src/asapo_broker/database/mongodb.go   |  1 +
 .../dummy_data_producer.cpp                   |  2 +-
 tests/automatic/broker/CMakeLists.txt         |  1 +
 .../broker/check_mongo_restart/CMakeLists.txt |  7 ++
 .../broker/check_mongo_restart/check_linux.sh | 97 +++++++++++++++++++
 .../check_mongo_restart/check_linux.sh        | 10 +-
 6 files changed, 114 insertions(+), 4 deletions(-)
 create mode 100644 tests/automatic/broker/check_mongo_restart/CMakeLists.txt
 create mode 100644 tests/automatic/broker/check_mongo_restart/check_linux.sh

diff --git a/broker/src/asapo_broker/database/mongodb.go b/broker/src/asapo_broker/database/mongodb.go
index de41e4f90..0155bc513 100644
--- a/broker/src/asapo_broker/database/mongodb.go
+++ b/broker/src/asapo_broker/database/mongodb.go
@@ -80,6 +80,7 @@ func (db *Mongodb) Connect(address string) (err error) {
 	if err != nil {
 		return err
 	}
+	db.session.SetSafe(&mgo.Safe{J: true})
 
 	if err := db.updateDatabaseList(); err != nil {
 		return err
diff --git a/examples/producer/dummy-data-producer/dummy_data_producer.cpp b/examples/producer/dummy-data-producer/dummy_data_producer.cpp
index 0e49d91c3..87ca3582c 100644
--- a/examples/producer/dummy-data-producer/dummy_data_producer.cpp
+++ b/examples/producer/dummy-data-producer/dummy_data_producer.cpp
@@ -94,7 +94,7 @@ std::unique_ptr<asapo::Producer> CreateProducer(const Args& args) {
     }
 
     producer->EnableLocalLog(true);
-    producer->SetLogLevel(asapo::LogLevel::Debug);
+    producer->SetLogLevel(asapo::LogLevel::Info);
     return producer;
 }
 
diff --git a/tests/automatic/broker/CMakeLists.txt b/tests/automatic/broker/CMakeLists.txt
index 6f1685c7d..24ec3cef4 100644
--- a/tests/automatic/broker/CMakeLists.txt
+++ b/tests/automatic/broker/CMakeLists.txt
@@ -3,4 +3,5 @@ add_subdirectory(read_config)
 
 if (UNIX)
 add_subdirectory(check_monitoring)
+add_subdirectory(check_mongo_restart)
 endif()
\ No newline at end of file
diff --git a/tests/automatic/broker/check_mongo_restart/CMakeLists.txt b/tests/automatic/broker/check_mongo_restart/CMakeLists.txt
new file mode 100644
index 000000000..a3d2838ce
--- /dev/null
+++ b/tests/automatic/broker/check_mongo_restart/CMakeLists.txt
@@ -0,0 +1,7 @@
+set(TARGET_NAME check_broker_mongo_restart)
+
+################################
+# Testing
+################################
+prepare_asapo()
+add_script_test("${TARGET_NAME}" "$<TARGET_FILE:dummy-data-producer> $<TARGET_FILE:getnext_broker> $<TARGET_PROPERTY:asapo,EXENAME>" nomem)
diff --git a/tests/automatic/broker/check_mongo_restart/check_linux.sh b/tests/automatic/broker/check_mongo_restart/check_linux.sh
new file mode 100644
index 000000000..026a573f5
--- /dev/null
+++ b/tests/automatic/broker/check_mongo_restart/check_linux.sh
@@ -0,0 +1,97 @@
+#!/usr/bin/env bash
+
+set -e
+
+trap Cleanup EXIT
+
+beamtime_id=asapo_test
+token=`$3 token -secret broker_secret.key $beamtime_id`
+
+monitor_database_name=db_test
+proxy_address=127.0.0.1:8400
+
+beamline=test
+receiver_root_folder=/tmp/asapo/receiver/files
+receiver_folder=${receiver_root_folder}/${beamline}/${beamtime_id}
+
+function wait_mongo {
+NEXT_WAIT_TIME=0
+until mongo --port 27016 --eval "db.version()" | tail -2 | grep version || [ $NEXT_WAIT_TIME -eq 30 ]; do
+  echo "Wait for mongo"
+  NEXT_WAIT_TIME=$(( NEXT_WAIT_TIME++ ))
+  sleep 1
+done
+if (( NEXT_WAIT_TIME == 30 )); then
+    echo "Timeout"
+    exit -1
+fi
+}
+
+
+function kill_mongo {
+    kill -9 `ps xa | grep mongod | grep 27016 | awk '{print $1;}'`
+}
+
+
+function start_mongo {
+    mongod --dbpath /tmp/mongo --port 27016 --logpath /tmp/mongolog --fork
+}
+
+
+Cleanup() {
+    echo cleanup
+    rm -rf ${receiver_root_folder}
+    nomad stop nginx
+    nomad stop receiver
+    nomad stop discovery
+    nomad stop broker
+    nomad stop authorizer
+#    kill $producerid
+    echo "db.dropDatabase()" | mongo --port 27016 ${beamtime_id}
+    influx -execute "drop database ${monitor_database_name}"
+    kill_mongo
+}
+
+influx -execute "create database ${monitor_database_name}"
+
+sed -i 's/27017/27016/g' receiver.json.tpl
+sed -i 's/"WriteToDisk":true/"WriteToDisk":false/g' receiver.json.tpl
+sed -i 's/27017/27016/g' broker.json.tpl
+sed -i 's/info/debug/g' broker.json.tpl
+
+start_mongo
+wait_mongo
+
+
+nomad run nginx.nmd
+nomad run authorizer.nmd
+nomad run receiver.nmd
+nomad run discovery.nmd
+nomad run broker.nmd
+
+sleep 1
+
+echo "db.${beamtime_id}.insert({dummy:1})" | mongo --port 27016 ${beamtime_id}
+
+
+
+#producer
+mkdir -p ${receiver_folder}
+$1 localhost:8400 ${beamtime_id} 100 1000 4 0 100 &
+#producerid=`echo $!`
+
+wait
+
+$2 ${proxy_address} ${beamtime_id} 2 $token  &> output.txt &
+
+sleep 2
+
+kill_mongo
+sleep 3
+start_mongo
+
+wait
+
+cat output.txt
+cat output.txt | grep "Processed 1000 file(s)"
+rm output.txt
\ No newline at end of file
diff --git a/tests/automatic/producer_receiver/check_mongo_restart/check_linux.sh b/tests/automatic/producer_receiver/check_mongo_restart/check_linux.sh
index 52e5f2a5c..6a60eeed6 100644
--- a/tests/automatic/producer_receiver/check_mongo_restart/check_linux.sh
+++ b/tests/automatic/producer_receiver/check_mongo_restart/check_linux.sh
@@ -23,6 +23,11 @@ function kill_mongo {
 }
 
 
+function start_mongo {
+    mongod --dbpath /tmp/mongo --port 27016 --logpath /tmp/mongolog --fork
+}
+
+
 database_name=db_test
 beamtime_id=asapo_test
 beamline=test
@@ -37,7 +42,7 @@ Cleanup() {
     kill_mongo
 }
 
-mongod --dbpath /tmp/mongo --port 27016 --logpath /tmp/mongolog --fork
+start_mongo
 wait_mongo
 
 
@@ -63,8 +68,7 @@ sleep 0.5
 
 kill_mongo
 sleep 3
-mongod --dbpath /tmp/mongo --port 27016 --logpath /tmp/mongolog --fork
-
+start_mongo
 
 wait
 
-- 
GitLab