From 9b8c16fcc6d784246eacda9545042f68556eb519 Mon Sep 17 00:00:00 2001
From: Jakob Olsson <jakob.olsson@iopsys.eu>
Date: Fri, 16 Oct 2020 16:58:06 +0200
Subject: [PATCH] add coding style check and functional test to pipeline

---
 .gitlab-ci.yml                              | 42 +++++++++++++++++++++
 gitlab-ci/coding-style.sh                   | 17 +++++++++
 gitlab-ci/functional-test.sh                | 15 ++++++--
 gitlab-ci/install-dependencies.sh           |  9 ++---
 gitlab-ci/iopsys-supervisord.conf           |  4 +-
 gitlab-ci/setup.sh                          |  3 ++
 hooks/pre-commit                            |  1 -
 src/core/cntlr_map_debug.h                  |  2 +-
 test/api/json/mapcontroller.validation.json |  4 --
 9 files changed, 80 insertions(+), 17 deletions(-)
 create mode 100644 .gitlab-ci.yml
 create mode 100755 gitlab-ci/coding-style.sh

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
new file mode 100644
index 00000000..9ba0acbb
--- /dev/null
+++ b/.gitlab-ci.yml
@@ -0,0 +1,42 @@
+include:
+  - project: 'iopsys/gitlab-ci-pipeline'
+    file: '/static-code-analysis.yml'
+
+stages:
+    - static_code_analysis
+    - checkpatch
+    - functional_test
+
+variables:
+  DEBUG: 'TRUE'
+  SOURCE_FOLDER: "."
+
+run_functional_test:
+    stage: functional_test
+    image: iopsys/code-analysis-dev
+    allow_failure: false
+    script:
+        - "./gitlab-ci/install-dependencies.sh"
+        - "./gitlab-ci/setup.sh"
+        - "./gitlab-ci/functional-test.sh"
+
+    artifacts:
+        when: always
+        paths:
+            - functional-test-coverage.xml
+            - timestamp.log
+            - coverage.tar.gz
+
+run_checkpatch:
+    stage: checkpatch
+    image: iopsys/code-analysis-dev
+    allow_failure: false
+    script:
+        - "./gitlab-ci/coding-style.sh"
+
+    artifacts:
+        when: always
+        paths:
+            - functional-test-coverage.xml
+            - timestamp.log
+            - coverage.tar.gz
diff --git a/gitlab-ci/coding-style.sh b/gitlab-ci/coding-style.sh
new file mode 100755
index 00000000..3b45734b
--- /dev/null
+++ b/gitlab-ci/coding-style.sh
@@ -0,0 +1,17 @@
+#!/bin/bash
+
+DIRS="src/core src/include src/ipc src/utils "
+
+rm -rf coding_style.log
+touch coding_style.log
+for dir in $DIRS; do
+	scripts/checkpatch.pl -f --no-tree --no-signoff --ignore CONST_STRUCT,SPDX_LICENSE_TAG,LINE_CONTINUATIONS $dir/*.h $dir/*.c >> coding_style.log 2>1
+done
+
+cat coding_style.log
+
+cat coding_style.log | grep -q "ERROR: "
+
+[ "$?" == 0 ] && exit 1
+
+exit 0
diff --git a/gitlab-ci/functional-test.sh b/gitlab-ci/functional-test.sh
index 7b7ead97..6f98250a 100755
--- a/gitlab-ci/functional-test.sh
+++ b/gitlab-ci/functional-test.sh
@@ -24,9 +24,6 @@ supervisorctl status all
 echo "Running the unit test cases, pwd ${LIB_DIR}"
 #ret=$?
 
-supervisorctl stop ubusd ieee1905d wifimngr topologyd mapagent mapcontroller
-supervisorctl status all
-
 tap_validate_md5sum() {
     sha1=$(md5sum "$1" | cut -d' ' -f1)
     sha2=$(md5sum "$2" | cut -d' ' -f1)
@@ -40,6 +37,7 @@ ubus_invoke() {
     args=$3
 
     ubus call $object $method $args
+    echo "$?"
 }
 
 json_load "$(cat test/api/json/mapcontroller.validation.json)"
@@ -53,7 +51,12 @@ for i in $methods; do
     json_get_var method method
     json_get_var args args
 
-    ubus_invoke $object $method $args
+    rv=$(ubus_invoke $object $method $args)
+    if [ "$rv" != 0 ]; then
+        tap_is_str "return code: 0" "return code: $rv" "ubus call $object $method $args"
+        continue
+    fi
+    sleep 1
     tap_validate_md5sum "$agentlog" "$cntlrlog" "$object" "$method" "$args"
 
     echo "" > "$cntlrlog"
@@ -64,6 +67,10 @@ done
 tap_done_testing
 tap_finish
 
+supervisorctl stop ubusd ieee1905d wifimngr topologyd mapagent mapcontroller
+supervisorctl status all
+
+
 #report part
 #GitLab-CI output
 gcovr -r .
diff --git a/gitlab-ci/install-dependencies.sh b/gitlab-ci/install-dependencies.sh
index 74e46d7f..eaf28997 100755
--- a/gitlab-ci/install-dependencies.sh
+++ b/gitlab-ci/install-dependencies.sh
@@ -66,10 +66,10 @@ rm -fr map-1905
 git clone -b devel https://dev.iopsys.eu/iopsys/map-1905.git
 cd map-1905
 exec_cmd make
-cp -r libmaputils.so /usr/lib
+mkdir -p /usr/lib/ieee1905
+cp -r libmaputils.so /usr/lib/
 mkdir -p /usr/include/map1905
 cp include/*.h /usr/include/map1905/
-mkdir -p /usr/lib/ieee1905
 cp -r libwifimap2.so /usr/lib/ieee1905
 #cd ${home}
 #mkdir -p /usr/lib/ieee1905
@@ -94,11 +94,10 @@ cd /opt/dev
 rm -fr map-agent
 exec_cmd git clone -b devel https://dev.iopsys.eu/iopsys/map-agent.git
 cd map-agent/src
-git checkout ap_capabilities
 exec_cmd make
 exec_cmd cp mapagent /usr/sbin/
 
-# install mapagent
+# install tap.sh library 
 cd /opt/dev
 rm -fr tap
 exec_cmd git clone https://github.com/andrewgregory/tap.sh.git tap
@@ -162,4 +161,4 @@ echo "
 config wificntlr
 	option enabled '1'
 	option debug '6'
-" > /etc/config/controller
\ No newline at end of file
+" > /etc/config/controller
diff --git a/gitlab-ci/iopsys-supervisord.conf b/gitlab-ci/iopsys-supervisord.conf
index bed0b42d..3f80af5e 100644
--- a/gitlab-ci/iopsys-supervisord.conf
+++ b/gitlab-ci/iopsys-supervisord.conf
@@ -11,7 +11,7 @@ command=/bin/bash -c "/usr/sbin/ieee1905d"
 command=/bin/bash -c "/usr/sbin/topologyd"
 
 [program:mapagent]
-command=/bin/bash -c "/usr/sbin/mapagent -d"
+command=/bin/bash -c "/usr/sbin/mapagent -vvvvvv -d"
 
 [program:mapcontroller]
-command=/bin/bash -c "/usr/bin/valgrind --xml=yes --xml-file=memory-report.xml --leak-check=full --show-reachable=yes --show-leak-kinds=all --errors-for-leak-kinds=all --error-exitcode=1 --track-origins=yes /builds/iopsys/map-controller/src/mapcontroller -d"
+command=/bin/bash -c "/usr/bin/valgrind --xml=yes --xml-file=memory-report.xml --leak-check=full --show-reachable=yes --show-leak-kinds=all --errors-for-leak-kinds=all --error-exitcode=1 --track-origins=yes /builds/iopsys/map-controller/src/mapcontroller -vvvvvv -d"
diff --git a/gitlab-ci/setup.sh b/gitlab-ci/setup.sh
index 3fd902b1..8f565815 100755
--- a/gitlab-ci/setup.sh
+++ b/gitlab-ci/setup.sh
@@ -4,6 +4,9 @@ echo "preparation script"
 
 pwd
 
+uci set ieee1905.ieee1905.map_plugin="1"
+uci commit ieee1905
+
 cp -r ./test/files/etc/* /etc/
 cp -r ./schemas/ubus/* /usr/share/rpcd/schemas
 cp ./gitlab-ci/iopsys-supervisord.conf /etc/supervisor/conf.d/
diff --git a/hooks/pre-commit b/hooks/pre-commit
index 417684ed..891321c6 100755
--- a/hooks/pre-commit
+++ b/hooks/pre-commit
@@ -1,3 +1,2 @@
 #!/bin/bash
 exec git diff --cached | scripts/checkpatch.pl --no-tree --no-signoff --ignore CONST_STRUCT,SPDX_LICENSE_TAG,LINE_CONTINUATIONS
-
diff --git a/src/core/cntlr_map_debug.h b/src/core/cntlr_map_debug.h
index 384886c4..a1780ea0 100644
--- a/src/core/cntlr_map_debug.h
+++ b/src/core/cntlr_map_debug.h
@@ -24,7 +24,7 @@ int debug_sta_caps_report(void *cntlr, struct cmdu_cstruct *cmdu);
 int debug_ap_metrics_response(void *cntlr, struct cmdu_cstruct *cmdu);
 int debug_sta_link_metrics_response(void *cntlr, struct cmdu_cstruct *cmdu);
 int debug_unassoc_sta_link_metrics_response(void *cntlr,
-        struct cmdu_cstruct *cmdu);
+	struct cmdu_cstruct *cmdu);
 int debug_beacon_metrics_response(void *cntlr, struct cmdu_cstruct *cmdu);
 int debug_sta_steer_btm_report(void *cntlr, struct cmdu_cstruct *cmdu);
 int debug_sta_steer_complete(void *cntlr, struct cmdu_cstruct *cmdu);
diff --git a/test/api/json/mapcontroller.validation.json b/test/api/json/mapcontroller.validation.json
index 91584e83..26374026 100644
--- a/test/api/json/mapcontroller.validation.json
+++ b/test/api/json/mapcontroller.validation.json
@@ -4,10 +4,6 @@
         {
             "method": "ap_caps",
             "rc": 0
-        },
-        {
-            "method": "sta_caps",
-            "rc": 0
         }
     ]
 }
-- 
GitLab