From 704c26980e576b01caf27af751ca5ca08f8f355a Mon Sep 17 00:00:00 2001
From: Maxim Mamontov <faust.madf@gmail.com>
Date: Sat, 4 Oct 2014 23:13:57 +0300
Subject: [PATCH] Added functional test framework.

---
 functest/build.sh                        | 25 +++++++
 functest/clone.sh                        | 29 ++++++++
 functest/do.sh                           | 56 +++++++++++++++
 functest/stuff/OnChange                  |  6 ++
 functest/stuff/OnConnect                 | 18 +++++
 functest/stuff/OnDisconnect              | 18 +++++
 functest/stuff/db-stub/admins/admin.adm  |  8 +++
 functest/stuff/db-stub/tariffs/tariff.tf | 85 +++++++++++++++++++++++
 functest/stuff/db-stub/users/test/conf   | 27 ++++++++
 functest/stuff/db-stub/users/test/stat   | 26 +++++++
 functest/stuff/rules                     |  4 ++
 functest/stuff/stargazer-files.conf      | 57 ++++++++++++++++
 functest/test.sh                         | 86 ++++++++++++++++++++++++
 13 files changed, 445 insertions(+)
 create mode 100755 functest/build.sh
 create mode 100755 functest/clone.sh
 create mode 100755 functest/do.sh
 create mode 100755 functest/stuff/OnChange
 create mode 100755 functest/stuff/OnConnect
 create mode 100755 functest/stuff/OnDisconnect
 create mode 100644 functest/stuff/db-stub/admins/admin.adm
 create mode 100644 functest/stuff/db-stub/tariffs/tariff.tf
 create mode 100644 functest/stuff/db-stub/users/test/conf
 create mode 100644 functest/stuff/db-stub/users/test/stat
 create mode 100644 functest/stuff/rules
 create mode 100644 functest/stuff/stargazer-files.conf
 create mode 100755 functest/test.sh

diff --git a/functest/build.sh b/functest/build.sh
new file mode 100755
index 00000000..a84e9e0d
--- /dev/null
+++ b/functest/build.sh
@@ -0,0 +1,25 @@
+#!/bin/sh
+
+BASEPATH=$1
+
+if [ "$BASEPATH" == "" ]
+then
+    printf "Usage: $0 <path>\n"
+    exit -1
+fi
+
+if [ ! -d "$BASEPATH" ]
+then
+    printf "Path '$BASEPATH' does not exist or not a directory.\n"
+    exit -1
+fi
+
+cd "$BASEPATH/stg/projects/stargazer"
+./build debug
+make clean
+make
+
+cd "$BASEPATH/stg/projects/sgconf"
+./build debug
+make clean
+make
diff --git a/functest/clone.sh b/functest/clone.sh
new file mode 100755
index 00000000..f6c7b44b
--- /dev/null
+++ b/functest/clone.sh
@@ -0,0 +1,29 @@
+#!/bin/sh
+
+BASEPATH=$1
+ARCHIVE=$2
+CURPATH=`pwd`
+GIT=/usr/bin/git
+TAR=/bin/tar
+RM=/bin/rm
+
+if [ "$BASEPATH" == "" ]
+then
+    printf "Usage: $0 <path>\n"
+    exit -1
+fi
+
+if [ ! -d "$BASEPATH" ]
+then
+    printf "Path '$BASEPATH' does not exist or not a directory.\n"
+    exit -1
+fi
+
+$GIT clone "https://gitorious.org/stg/stg.git" "$BASEPATH/stg"
+
+if [ "$ARCHIVE" != "" ]
+then
+    $RM -rf "$BASEPATH/stg/.git"
+
+    $TAR -C "$BASEPATH" -jcf "$BASEPATH/stg.tar" "stg"
+fi
diff --git a/functest/do.sh b/functest/do.sh
new file mode 100755
index 00000000..77d928a5
--- /dev/null
+++ b/functest/do.sh
@@ -0,0 +1,56 @@
+#!/bin/sh
+
+MKTEMP=/usr/bin/mktemp
+RM=/bin/rm
+
+printf "Creating temporary dir... "
+DIR=`$MKTEMP -d`
+
+if [ "$DIR" == "" ]
+then
+    printf "Failed.\nTemporary dir is empty.\n"
+    exit -1
+fi
+
+if [ ! -d "$DIR" ]
+then
+    printf "Failed.\nTemporary dir '$DIR' does not exist or not a directory.\n"
+    exit -1
+fi
+
+LOGFILE=`date "+%Y-%m-%d-%H%M%S.log"`
+
+printf "Ok.\nCloning... "
+./clone.sh "$DIR" >> "$LOGFILE" 2>&1
+if [ "$?" != "0" ]
+then
+    printf "Failed.\n"
+    exit -1
+else
+    printf "Ok.\nBuilding... "
+fi
+./build.sh "$DIR" >> "$LOGFILE" 2>&1
+if [ "$?" != "0" ]
+then
+    printf "Failed.\n"
+    exit -1
+else
+    printf "Ok.\nTesting... "
+fi
+./test.sh "$DIR" # >> "$LOGFILE" 2>&1
+if [ "$?" != "0" ]
+then
+    printf "Failed.\n"
+    exit -1
+else
+    printf "Ok.\nCleaning up... "
+fi
+
+$RM -rf $DIR
+if [ "$?" != "0" ]
+then
+    printf "Failed.\n"
+    exit -1
+else
+    printf "Ok.\n"
+fi
diff --git a/functest/stuff/OnChange b/functest/stuff/OnChange
new file mode 100755
index 00000000..8feb93ca
--- /dev/null
+++ b/functest/stuff/OnChange
@@ -0,0 +1,6 @@
+#!/bin/sh
+
+login=$1
+param=$2
+oldValue=$3
+newValue=$4
diff --git a/functest/stuff/OnConnect b/functest/stuff/OnConnect
new file mode 100755
index 00000000..d0f5a496
--- /dev/null
+++ b/functest/stuff/OnConnect
@@ -0,0 +1,18 @@
+#! /bin/sh
+
+# Login
+LOGIN=$1
+
+# User IP
+IP=$2
+
+# Cash
+CASH=$3
+
+# User ID
+ID=$4
+
+# Selected dirs to connect
+DIRS=$5
+
+TARIFFNAME=$6
diff --git a/functest/stuff/OnDisconnect b/functest/stuff/OnDisconnect
new file mode 100755
index 00000000..7c80e5b2
--- /dev/null
+++ b/functest/stuff/OnDisconnect
@@ -0,0 +1,18 @@
+#! /bin/sh
+
+# Login
+LOGIN=$1
+
+# User IP
+IP=$2
+
+# Cash
+CASH=$3
+
+# User ID
+ID=$4
+
+# Selected dirs to disconnect
+DIRS=$5
+
+TARIFFNAME=$6
diff --git a/functest/stuff/db-stub/admins/admin.adm b/functest/stuff/db-stub/admins/admin.adm
new file mode 100644
index 00000000..bfc6cde1
--- /dev/null
+++ b/functest/stuff/db-stub/admins/admin.adm
@@ -0,0 +1,8 @@
+password=geahonjehjfofnhammefahbbbfbmpkmkmmefahbbbfbmpkmkmmefahbbbfbmpkmkaa
+ChgConf=1
+ChgPassword=1
+ChgStat=1
+ChgCash=1
+UsrAddDel=1
+ChgTariff=1
+ChgAdmin=1
diff --git a/functest/stuff/db-stub/tariffs/tariff.tf b/functest/stuff/db-stub/tariffs/tariff.tf
new file mode 100644
index 00000000..2a6971f5
--- /dev/null
+++ b/functest/stuff/db-stub/tariffs/tariff.tf
@@ -0,0 +1,85 @@
+Fee=1.000000
+Free=2.000000
+NoDiscount0=0
+NoDiscount1=0
+NoDiscount2=0
+NoDiscount3=0
+NoDiscount4=0
+NoDiscount5=0
+NoDiscount6=0
+NoDiscount7=0
+NoDiscount8=0
+NoDiscount9=0
+PassiveCost=3.000000
+Period=day
+PriceDayA0=5.000000
+PriceDayA1=0.000000
+PriceDayA2=0.000000
+PriceDayA3=0.000000
+PriceDayA4=0.000000
+PriceDayA5=0.000000
+PriceDayA6=0.000000
+PriceDayA7=0.000000
+PriceDayA8=0.000000
+PriceDayA9=0.000000
+PriceDayB0=3.500000
+PriceDayB1=0.000000
+PriceDayB2=0.000000
+PriceDayB3=0.000000
+PriceDayB4=0.000000
+PriceDayB5=0.000000
+PriceDayB6=0.000000
+PriceDayB7=0.000000
+PriceDayB8=0.000000
+PriceDayB9=0.000000
+PriceNightA0=4.000000
+PriceNightA1=0.000000
+PriceNightA2=0.000000
+PriceNightA3=0.000000
+PriceNightA4=0.000000
+PriceNightA5=0.000000
+PriceNightA6=0.000000
+PriceNightA7=0.000000
+PriceNightA8=0.000000
+PriceNightA9=0.000000
+PriceNightB0=2.500000
+PriceNightB1=0.000000
+PriceNightB2=0.000000
+PriceNightB3=0.000000
+PriceNightB4=0.000000
+PriceNightB5=0.000000
+PriceNightB6=0.000000
+PriceNightB7=0.000000
+PriceNightB8=0.000000
+PriceNightB9=0.000000
+SinglePrice0=0
+SinglePrice1=0
+SinglePrice2=0
+SinglePrice3=0
+SinglePrice4=0
+SinglePrice5=0
+SinglePrice6=0
+SinglePrice7=0
+SinglePrice8=0
+SinglePrice9=0
+Threshold0=1000
+Threshold1=0
+Threshold2=0
+Threshold3=0
+Threshold4=0
+Threshold5=0
+Threshold6=0
+Threshold7=0
+Threshold8=0
+Threshold9=0
+Time0=6:0-18:0
+Time1=0:0-0:0
+Time2=0:0-0:0
+Time3=0:0-0:0
+Time4=0:0-0:0
+Time5=0:0-0:0
+Time6=0:0-0:0
+Time7=0:0-0:0
+Time8=0:0-0:0
+Time9=0:0-0:0
+TraffType=up+down
diff --git a/functest/stuff/db-stub/users/test/conf b/functest/stuff/db-stub/users/test/conf
new file mode 100644
index 00000000..f19c6537
--- /dev/null
+++ b/functest/stuff/db-stub/users/test/conf
@@ -0,0 +1,27 @@
+Address=
+AlwaysOnline=1
+CreationTime=1123487395
+Credit=0.000000
+CreditExpire=0
+DisabledDetailStat=0
+Down=0
+Email=
+Group=
+IP=192.168.0.5
+Note=
+Passive=0
+Password=123456
+Phone=
+RealName=
+Tariff=tariff
+TariffChange=
+Userdata0=
+Userdata1=
+Userdata2=
+Userdata3=
+Userdata4=
+Userdata5=
+Userdata6=
+Userdata7=
+Userdata8=
+Userdata9=
diff --git a/functest/stuff/db-stub/users/test/stat b/functest/stuff/db-stub/users/test/stat
new file mode 100644
index 00000000..d69d07c6
--- /dev/null
+++ b/functest/stuff/db-stub/users/test/stat
@@ -0,0 +1,26 @@
+Cash=100
+D0=0
+D1=0
+D2=0
+D3=0
+D4=0
+D5=0
+D6=0
+D7=0
+D8=0
+D9=0
+FreeMb=0
+LastActivityTime=0
+LastCashAdd=0
+LastCashAddTime=0
+PassiveTime=0
+U0=0
+U1=0
+U2=0
+U3=0
+U4=0
+U5=0
+U6=0
+U7=0
+U8=0
+U9=0
diff --git a/functest/stuff/rules b/functest/stuff/rules
new file mode 100644
index 00000000..ad9faa18
--- /dev/null
+++ b/functest/stuff/rules
@@ -0,0 +1,4 @@
+ALL     192.168.0.0/16  DIR1
+ALL     172.16.0.0/12   DIR1
+ALL     10.0.0.0/8      DIR1
+ALL     0.0.0.0/0       DIR0
diff --git a/functest/stuff/stargazer-files.conf b/functest/stuff/stargazer-files.conf
new file mode 100644
index 00000000..c979a263
--- /dev/null
+++ b/functest/stuff/stargazer-files.conf
@@ -0,0 +1,57 @@
+PidFile = -STG-PATH-/stargazer.pid
+LogFile = -STG-PATH-/stargazer.log
+Rules = -STG-PATH-/rules
+ModulesPath = -STG-PATH-/modules
+DetailStatWritePeriod=1/6
+StatWritePeriod = 10
+DayFee = 1
+DayFeeIsLastDay = yes
+DayResetTraff = 1
+SpreadFee = no
+FreeMbAllowInet = no
+WriteFreeMbTraffCost = no
+FullFee=no
+ShowFeeInCash=yes
+ExecutersNum = 1
+ExecMsgKey = 5555
+# MonitorDir=/var/stargazer/monitor
+ScriptParams = tariffName
+
+<DirNames>
+    DirName0 = dir
+    DirName1 = dir2
+    DirName2 = dir3
+    DirName3 =
+    DirName4 =
+    DirName5 = dir5
+    DirName6 =
+    DirName7 =
+    DirName8 =
+    DirName9 =
+</DirNames>
+
+<StoreModule store_files>
+    WorkDir = -STG-PATH-/db
+
+    ConfOwner = root
+    ConfGroup = root
+    ConfMode = 600
+
+    StatOwner = root
+    StatGroup = root
+    StatMode = 640
+
+    UserLogOwner = root
+    UserLogGroup = root
+    UserLogMode = 640
+</StoreModule>
+
+<Modules>
+    <Module auth_ao>
+    </Module>
+
+    <Module conf_sg>
+        Port = 5555
+        BindAddress = localhost
+    </Module>
+</Modules>
diff --git a/functest/test.sh b/functest/test.sh
new file mode 100755
index 00000000..62a8a581
--- /dev/null
+++ b/functest/test.sh
@@ -0,0 +1,86 @@
+#!/bin/sh
+
+BASEPATH=$1
+
+if [ "$BASEPATH" == "" ]
+then
+    printf "Usage: $0 <path>\n"
+    exit -1
+fi
+
+if [ ! -d "$BASEPATH" ]
+then
+    printf "Path '$BASEPATH' does not exist or not a directory.\n"
+    exit -1
+fi
+
+STGPATH="$BASEPATH/stg/projects/stargazer"
+
+cp "stuff/stargazer-files.conf" "$STGPATH/stargazer.conf"
+cp "stuff/rules" "$STGPATH/"
+cp "stuff/OnConnect" "$STGPATH/"
+cp "stuff/OnDisconnect" "$STGPATH/"
+cp "stuff/OnChange" "$STGPATH/"
+cp -R "stuff/db-stub" "$STGPATH/db"
+
+sed -i "s|-STG-PATH-|$STGPATH|g" "$STGPATH/stargazer.conf"
+
+LOGFILE=`pwd`"/"`date "+%Y-%m-%d-%H%M%S.console.log"`
+
+cd "$STGPATH"
+
+"$STGPATH/stargazer" "$STGPATH" >> "$LOGFILE" 2>&1 &
+
+COUNT=""
+while true
+do
+    grep "Stg started successfully" "$STGPATH/stargazer.log" > /dev/null 2> /dev/null
+    if [ "$?" == "0" ]
+    then
+        break
+    fi
+    COUNT="$COUNT."
+    if [ "$COUNT" == "....." ]
+    then
+        printf "Failed to start stg in 5 sec.\n"
+        exit -1
+    fi
+    sleep 1
+done
+
+PID=`cat "$STGPATH/stargazer.pid"`
+printf "Started with pid $PID\n"
+
+SGCONFPATH="$BASEPATH/stg/projects/sgconf"
+
+RES=`"$SGCONFPATH/sgconf" -s localhost -p 5555 -u admin -w 123456 --get-admins`
+
+if [ "$?" != "0" ]
+then
+    printf "Failed to get admins list. Result:\n$RES\n"
+    exit 0
+fi
+
+printf "Got admins list:\n$RES\n"
+
+printf "Stopping...\n"
+kill $PID
+
+COUNT=""
+while true
+do
+    grep "Stg stopped successfully" "$STGPATH/stargazer.log" > /dev/null 2> /dev/null
+    if [ "$?" == "0" ]
+    then
+        break
+    fi
+    COUNT="$COUNT."
+    if [ "$COUNT" == "....." ]
+    then
+        printf "Failed to stop stg in 5 sec.\n"
+        exit -1
+    fi
+    sleep 1
+done
+
+printf "Stopped.\n"
-- 
2.44.2