From 9398df980a7be19b231e239e0c35cb47e7c7573c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=B6rn=20K=C3=BCnsem=C3=B6ller?=
 <jkuensem@physik.uni-bielefeld.de>
Date: Mon, 11 Jun 2018 16:41:58 +0000
Subject: [PATCH] Task LSMR-8: Added simple functional test case and unit test
 stub

---
 .gitattributes                                |  8 +++-
 CMake/LofarPackageList.cmake                  |  2 +-
 SAS/LSMR/bin/lsmr                             | 17 +++++++-
 SAS/LSMR/requirements.txt                     |  4 --
 SAS/LSMR/src/CMakeLists.txt                   |  2 +
 .../lsmr/lsmrapp/migrations/0001_initial.py   | 14 +++++++
 SAS/LSMR/test/CMakeLists.txt                  |  9 +++++
 SAS/LSMR/test/t_functional.py                 | 40 +++++++++++++++++++
 SAS/LSMR/test/t_functional.run                | 20 ++++++++++
 SAS/LSMR/test/t_functional.sh                 |  5 +++
 SAS/LSMR/test/t_lsmrapp_models.py             | 40 +++++++++++++++++++
 SAS/LSMR/test/t_lsmrapp_models.run            |  3 ++
 SAS/LSMR/test/t_lsmrapp_models.sh             |  3 ++
 13 files changed, 160 insertions(+), 7 deletions(-)
 delete mode 100644 SAS/LSMR/requirements.txt
 create mode 100644 SAS/LSMR/src/lsmr/lsmrapp/migrations/0001_initial.py
 create mode 100644 SAS/LSMR/test/t_functional.py
 create mode 100644 SAS/LSMR/test/t_functional.run
 create mode 100644 SAS/LSMR/test/t_functional.sh
 create mode 100644 SAS/LSMR/test/t_lsmrapp_models.py
 create mode 100755 SAS/LSMR/test/t_lsmrapp_models.run
 create mode 100755 SAS/LSMR/test/t_lsmrapp_models.sh

diff --git a/.gitattributes b/.gitattributes
index 9225f153e54..d9143fe3ea1 100644
--- a/.gitattributes
+++ b/.gitattributes
@@ -4234,7 +4234,6 @@ SAS/Feedback_Service/src/fb_data_44883.txt -text
 SAS/LSMR/CMakeLists.txt -text
 SAS/LSMR/bin/CMakeLists.txt -text
 SAS/LSMR/bin/lsmr -text
-SAS/LSMR/requirements.txt -text
 SAS/LSMR/src/CMakeLists.txt -text
 SAS/LSMR/src/lsmr/CMakeLists.txt -text
 SAS/LSMR/src/lsmr/__init__.py -text
@@ -4242,6 +4241,7 @@ SAS/LSMR/src/lsmr/lsmrapp/CMakeLists.txt -text
 SAS/LSMR/src/lsmr/lsmrapp/__init__.py -text
 SAS/LSMR/src/lsmr/lsmrapp/admin.py -text
 SAS/LSMR/src/lsmr/lsmrapp/apps.py -text
+SAS/LSMR/src/lsmr/lsmrapp/migrations/0001_initial.py -text
 SAS/LSMR/src/lsmr/lsmrapp/migrations/__init__.py -text
 SAS/LSMR/src/lsmr/lsmrapp/models.py -text
 SAS/LSMR/src/lsmr/lsmrapp/tests.py -text
@@ -4251,6 +4251,12 @@ SAS/LSMR/src/lsmr/urls.py -text
 SAS/LSMR/src/lsmr/wsgi.py -text
 SAS/LSMR/src/manage.py -text
 SAS/LSMR/test/CMakeLists.txt -text
+SAS/LSMR/test/t_functional.py -text
+SAS/LSMR/test/t_functional.run -text
+SAS/LSMR/test/t_functional.sh -text
+SAS/LSMR/test/t_lsmrapp_models.py -text
+SAS/LSMR/test/t_lsmrapp_models.run -text
+SAS/LSMR/test/t_lsmrapp_models.sh -text
 SAS/MoM/CMakeLists.txt -text
 SAS/MoM/MoMQueryService/CMakeLists.txt -text
 SAS/MoM/MoMQueryService/MoMQueryServiceClient/CMakeLists.txt -text
diff --git a/CMake/LofarPackageList.cmake b/CMake/LofarPackageList.cmake
index ce394cfeda3..9097d8c7bbc 100644
--- a/CMake/LofarPackageList.cmake
+++ b/CMake/LofarPackageList.cmake
@@ -1,7 +1,7 @@
 # - Create for each LOFAR package a variable containing the absolute path to
 # its source directory. 
 #
-# Generated by gen_LofarPackageList_cmake.sh at Fr 8. Jun 15:16:51 CEST 2018
+# Generated by gen_LofarPackageList_cmake.sh at Mo 11. Jun 16:21:43 CEST 2018
 #
 #                      ---- DO NOT EDIT ----
 #
diff --git a/SAS/LSMR/bin/lsmr b/SAS/LSMR/bin/lsmr
index 6ee38285d30..9a173d903d9 100644
--- a/SAS/LSMR/bin/lsmr
+++ b/SAS/LSMR/bin/lsmr
@@ -23,7 +23,22 @@
 echo "---"
 echo "! This is for testing only, properly deploy in Nginx or Apache for production use!"
 echo "---"
+
+
+# Parse args:
+
+PORT=8000
+while getopts p: option
+do
+case "${option}"
+in
+p) PORT=${OPTARG};;
+esac
+done
+
+echo "Using port $PORT"
+
 DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
 /usr/bin/python $DIR/../lib*/python3.4/site-packages/lofar/sas/lsmr/manage.py migrate 
-/usr/bin/python $DIR/../lib*/python3.4/site-packages/lofar/sas/lsmr/manage.py runserver 0.0.0.0:8000
+/usr/bin/python $DIR/../lib*/python3.4/site-packages/lofar/sas/lsmr/manage.py runserver 0.0.0.0:$PORT
 
diff --git a/SAS/LSMR/requirements.txt b/SAS/LSMR/requirements.txt
deleted file mode 100644
index fc272c36bb9..00000000000
--- a/SAS/LSMR/requirements.txt
+++ /dev/null
@@ -1,4 +0,0 @@
-Django 2.0.5
-djangorestframework
-markdown
-django-filter
diff --git a/SAS/LSMR/src/CMakeLists.txt b/SAS/LSMR/src/CMakeLists.txt
index b294cfc6e56..2a8ba7ee5f8 100644
--- a/SAS/LSMR/src/CMakeLists.txt
+++ b/SAS/LSMR/src/CMakeLists.txt
@@ -4,6 +4,8 @@ include(FindPythonModule)
 find_python_module(django REQUIRED)
 find_python_module(rest_framework REQUIRED)     # pip install djangorestframework
 find_python_module(ldap REQUIRED)
+find_python_module(markdown REQUIRED)
+# find_python_module(django-filter REQUIRED) # FIXME: not detected
 
 set(_py_files
     manage.py
diff --git a/SAS/LSMR/src/lsmr/lsmrapp/migrations/0001_initial.py b/SAS/LSMR/src/lsmr/lsmrapp/migrations/0001_initial.py
new file mode 100644
index 00000000000..9983921379f
--- /dev/null
+++ b/SAS/LSMR/src/lsmr/lsmrapp/migrations/0001_initial.py
@@ -0,0 +1,14 @@
+# Generated by Django 2.0.6 on 2018-06-11 08:56
+
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+    ]
+
+    operations = [
+        # Add things like triggers to the database here:
+	# migrations.RunSQL('RAW SQL CODE')
+    ]
diff --git a/SAS/LSMR/test/CMakeLists.txt b/SAS/LSMR/test/CMakeLists.txt
index e69de29bb2d..fe52fb69fed 100644
--- a/SAS/LSMR/test/CMakeLists.txt
+++ b/SAS/LSMR/test/CMakeLists.txt
@@ -0,0 +1,9 @@
+include(LofarCTest)
+include(FindPythonModule)
+
+find_python_module(mock REQUIRED)
+find_python_module(requests REQUIRED)
+find_python_module(selenium REQUIRED)
+
+lofar_add_test(t_lsmrapp_models)
+lofar_add_test(t_functional)
diff --git a/SAS/LSMR/test/t_functional.py b/SAS/LSMR/test/t_functional.py
new file mode 100644
index 00000000000..389c1512150
--- /dev/null
+++ b/SAS/LSMR/test/t_functional.py
@@ -0,0 +1,40 @@
+#!/usr/bin/python
+
+# Copyright (C) 2018    ASTRON (Netherlands Institute for Radio Astronomy)
+# P.O. Box 2, 7990 AA Dwingeloo, The Netherlands
+#
+# This file is part of the LOFAR software suite.
+# The LOFAR software suite is free software: you can redistribute it and/or
+# modify it under the terms of the GNU General Public License as published
+# by the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# The LOFAR software suite is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.    See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with the LOFAR software suite. If not, see <http://www.gnu.org/licenses/>.
+
+# $Id:  $
+
+import unittest
+import requests
+
+BASE_URL = 'http://localhost:8000'
+
+class BasicFunctionTestCase(unittest.TestCase):
+
+    def setUp(self):
+        pass
+
+    def test_welcome_page(self):
+        r = requests.get(BASE_URL)
+        self.assertEqual(r.status_code, 200)
+        self.assertTrue("The install worked successfully! Congratulations!" in r.content.decode('utf8'))
+
+
+if __name__ == "__main__":
+    unittest.main()
+
diff --git a/SAS/LSMR/test/t_functional.run b/SAS/LSMR/test/t_functional.run
new file mode 100644
index 00000000000..18b24ca1b44
--- /dev/null
+++ b/SAS/LSMR/test/t_functional.run
@@ -0,0 +1,20 @@
+#!/bin/sh
+
+
+# Run Django test instance
+PORT=8000
+lsmr -p $PORT &
+PID=$!
+echo "Started server with PID: " $PID
+sleep 3
+
+# Run test
+./t_functional.py
+EXITCODE=$?
+
+# Kill Django test instance
+pkill -f "lofar/sas/lsmr/manage.py runserver 0.0.0.0:$PORT"
+kill $PID
+
+# Return with success or failure of actual test
+exit $EXITCODE
diff --git a/SAS/LSMR/test/t_functional.sh b/SAS/LSMR/test/t_functional.sh
new file mode 100644
index 00000000000..d668e09aaa7
--- /dev/null
+++ b/SAS/LSMR/test/t_functional.sh
@@ -0,0 +1,5 @@
+#!/bin/sh
+
+# Run Test
+./runctest.sh t_functional
+
diff --git a/SAS/LSMR/test/t_lsmrapp_models.py b/SAS/LSMR/test/t_lsmrapp_models.py
new file mode 100644
index 00000000000..3047dfea392
--- /dev/null
+++ b/SAS/LSMR/test/t_lsmrapp_models.py
@@ -0,0 +1,40 @@
+#!/usr/bin/python
+
+# Copyright (C) 2018    ASTRON (Netherlands Institute for Radio Astronomy)
+# P.O. Box 2, 7990 AA Dwingeloo, The Netherlands
+#
+# This file is part of the LOFAR software suite.
+# The LOFAR software suite is free software: you can redistribute it and/or
+# modify it under the terms of the GNU General Public License as published
+# by the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# The LOFAR software suite is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.    See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with the LOFAR software suite. If not, see <http://www.gnu.org/licenses/>.
+
+# $Id:  $
+
+import rest_framework.test
+from lofar.sas.lsmr.lsmr.lsmrapp.models import *
+
+client = rest_framework.test.APIClient()
+
+
+class MyModelTestTest(rest_framework.test.APITestCase):
+
+    # todo: populate db
+    def setUp(self):
+        # mymodel.objects.create(...)
+        pass
+
+    # todo: add bunch of tests
+    def test_GET_something(self):
+        response = client.get('/get/something/', format='json', follow=True)
+        self.assertEqual(response.status_code, 404)
+        #self.assertEqual(response.data, {'id': 42, 'some':'thing'})
+
diff --git a/SAS/LSMR/test/t_lsmrapp_models.run b/SAS/LSMR/test/t_lsmrapp_models.run
new file mode 100755
index 00000000000..98e04b3e8fe
--- /dev/null
+++ b/SAS/LSMR/test/t_lsmrapp_models.run
@@ -0,0 +1,3 @@
+#!/bin/sh
+
+$LOFARROOT/lib/python3.4/site-packages/lofar/sas/lsmr/manage.py test --keepdb --pattern="t_lsmrapp_models.py"
diff --git a/SAS/LSMR/test/t_lsmrapp_models.sh b/SAS/LSMR/test/t_lsmrapp_models.sh
new file mode 100755
index 00000000000..213c159c963
--- /dev/null
+++ b/SAS/LSMR/test/t_lsmrapp_models.sh
@@ -0,0 +1,3 @@
+#!/bin/sh
+
+./runctest.sh t_lsmrapp_models
-- 
GitLab