From c48c46ca7727b4e353ddd3113e4dfac2db1c1829 Mon Sep 17 00:00:00 2001
From: Nikolay Stanchev <ns17@it-innovation.soton.ac.uk>
Date: Wed, 27 Jun 2018 14:34:18 +0100
Subject: [PATCH] Updated tests and install script for the clmc service

---
 scripts/clmc-service/install-clmc-service.sh | 31 ++++++++++++++++--
 scripts/clmc-service/install-tick-stack.sh   | 11 -------
 src/service/clmcservice/whoamiapi/tests.py   | 33 +++++++++++++++++++-
 3 files changed, 61 insertions(+), 14 deletions(-)

diff --git a/scripts/clmc-service/install-clmc-service.sh b/scripts/clmc-service/install-clmc-service.sh
index c1d93d9..e0df440 100755
--- a/scripts/clmc-service/install-clmc-service.sh
+++ b/scripts/clmc-service/install-clmc-service.sh
@@ -11,6 +11,25 @@ INFLUX_URL=$1
 DATABASE_NAME=$2
 REPORT_PERIOD=$3
 
+apt-get update
+
+# Create the database for the WHOAMI API
+apt-get install -y postgresql postgresql-contrib
+sudo -u postgres bash -c "psql -c \"CREATE USER clmc WITH PASSWORD 'clmc_service';\""
+sudo -u postgres bash -c "psql -c \"ALTER USER clmc CREATEDB;\""
+sudo -u postgres createdb whoamidb
+sudo -u postgres bash -c "psql -c \"GRANT ALL PRIVILEGES ON DATABASE \"whoamidb\" to clmc;\""
+
+# install virtualenvwrapper to manage python environments - and check
+echo "----> Installing Python3 and Pip3"
+apt-get install -y python3 python3-pip wget curl
+update-alternatives --install /usr/bin/python python /usr/bin/python3 10
+
+echo "----> Installing virtualenv and wrapper"
+apt-get install -y python3-virtualenv virtualenvwrapper
+pip3 install virtualenv
+pip3 install virtualenvwrapper
+
 ## CLMC-SERVICE
 ## ----------------------------------------------------------------------------------
 echo "----> Configuring virtualenvwrapper"
@@ -50,10 +69,10 @@ if [ $? -ne 0 ] ; then
 fi
 
 # navigate to the clmc-webservice - and check
-echo "----> Moving to CLMC webservice"
+echo "----> Moving to CLMC service"
 cd ${REPO_ROOT}/src/service
 if [ $? -ne 0 ] ; then
-        echo "Failed: could not find clmc-webservice"
+        echo "Failed: could not find clmc-service"
 		exit 1
 fi
 
@@ -80,6 +99,14 @@ fi
 echo "----> Creating CLMC web service log directory"
 mkdir -p /var/log/flame/clmc
 
+# initialise the CLMC service database with the model tables
+echo "----> Initialising CLMC database"
+initialize_clmcservice_db production.ini
+if [ $? -ne 0 ] ; then
+        echo "Failed: switching to CLMC python environment"
+		exit 1
+fi
+
 # Install minioclmc as systemctl service
 # -----------------------------------------------------------------------
 mkdir -p /opt/flame/clmc
diff --git a/scripts/clmc-service/install-tick-stack.sh b/scripts/clmc-service/install-tick-stack.sh
index f52fce9..d90405e 100755
--- a/scripts/clmc-service/install-tick-stack.sh
+++ b/scripts/clmc-service/install-tick-stack.sh
@@ -11,17 +11,6 @@ KAPACITOR_CHECKSUM=eea9b215f241906570eafe3857e1d4c5
 CHRONOGRAF_VERSION=1.4.4.2
 CHRONOGRAF_CHECKSUM=eea6915aa6db8f134fcd3b095e863b773bfb3a16a26e346dd65904a07df97963
 
-# install virtualenvwrapper to manage python environments - and check
-apt-get update
-echo "----> Installing Python3 and Pip3"
-apt-get install -y python3 python3-pip wget curl
-update-alternatives --install /usr/bin/python python /usr/bin/python3 10
-
-echo "----> Installing virtualenv and wrapper"
-apt-get install -y python3-virtualenv virtualenvwrapper
-pip3 install virtualenv
-pip3 install virtualenvwrapper
-
 # install influx
 echo "----> Installing InfluxDB"
 wget https://dl.influxdata.com/influxdb/releases/influxdb_${INFLUX_VERSION}_amd64.deb 2> /dev/null
diff --git a/src/service/clmcservice/whoamiapi/tests.py b/src/service/clmcservice/whoamiapi/tests.py
index 74e9379..4bf8960 100644
--- a/src/service/clmcservice/whoamiapi/tests.py
+++ b/src/service/clmcservice/whoamiapi/tests.py
@@ -204,7 +204,38 @@ class TestWhoamiAPI(object):
             WhoamiAPI(request).put()
         except HTTPConflict:
             error_raised = True
-        assert error_raised, "PUT request validates unique constraint"
+        assert error_raised, "PUT request breaks unique constraint"
+
+    @pytest.mark.parametrize("body, valid", [
+        ('{"location": "DC1", "sfc": "sfc1", "sfc_i": "sfc_i1", "sf": "sf1", "sf_i": "sf_i1", "sf_endpoint": "sf_endpoint1", "sr": "sr1"}', True),
+        ('{"location": "DC2", "sfc": "sfc2", "sfc_i": "sfc_i2", "sf": "sf2", "sf_i": "sf_i2", "sf_endpoint": "sf_endpoint2", "sr": "sr2"}', True),
+        ('{}', False),
+        ('{"location": "DC1", "sfc": "sfc1", "sfc_i": "sfc_i1", "sf": "sf1", "sf_i": "sf_i1"}', False),
+        ('{"place": "DC2", "sfc": "sfc2", "sfc_i": "sfc_i2", "sf": "sf2", "sf_i": "sf_i2", "sf_endpoint": "sf_endpoint2", "sr": "sr2"}', False),
+        ('{invalid json}', False),
+    ])
+    def test_put_body_validation(self, body, valid):
+        """
+        Tests the PUT request validation of the body content.
+
+        :param body: The request body to be validated
+        :param valid: True if body is valid, False otherwise
+        """
+
+        sf_e = ServiceFunctionEndpoint(location="DC1", sfc="sfc1", sfc_i="sfc_i1", sf="sf1", sf_i="sf_i1", sf_endpoint="sf_endpoint1", sr="sr1")
+        ServiceFunctionEndpoint.add(sf_e)  # adds the new instance of the model to the database
+
+        request = testing.DummyRequest()
+        request.params["sf_endpoint"] = "sf_endpoint1"
+        request.params["sf_i"] = "sf_i1"
+        request.params["sr"] = "sr1"
+        request.body = body.encode(request.charset)
+        error_raised = False
+        try:
+            WhoamiAPI(request).put()
+        except HTTPBadRequest:
+            error_raised = True
+        assert error_raised == (not valid), "An error must be raised in case of an invalid request body"
 
     def test_delete(self):
         """
-- 
GitLab