From d127464f5518c91b8a115383aca363e8e32a5ac5 Mon Sep 17 00:00:00 2001
From: MJB <mjb@it-innovation.soton.ac.uk>
Date: Wed, 21 Mar 2018 09:48:20 +0000
Subject: [PATCH] adding gitlab ci

---
 .gitlab-ci.yml                       | 15 ++++++
 Vagrantfile                          |  2 +-
 scripts/clmc-agent/write_config.sh   | 79 ----------------------------
 test/scripts/test_config_telegraf.py | 31 ++++++-----
 4 files changed, 34 insertions(+), 93 deletions(-)
 create mode 100644 .gitlab-ci.yml
 delete mode 100644 scripts/clmc-agent/write_config.sh

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
new file mode 100644
index 0000000..921cdb2
--- /dev/null
+++ b/.gitlab-ci.yml
@@ -0,0 +1,15 @@
+stages:  
+  - test
+
+test_scripts:
+  stage: test
+  script: 
+    - vagrant --fixture=scripts" -- up
+    - vagrant --fixture=scripts" -- ssh -- "cd /vagrant && pytest test/scripts/"
+    - vagrant --fixture=scripts" -- destroy --force
+  when: manual
+
+  
+
+  
+
diff --git a/Vagrantfile b/Vagrantfile
index 5ba469e..3ccfdcd 100644
--- a/Vagrantfile
+++ b/Vagrantfile
@@ -104,7 +104,7 @@ Vagrant.configure("2") do |config|
           instance_config.vm.provision :shell, inline: "cp /vagrant/test/services/#{host["service_name"]}/telegraf_#{host["service_name"]}.conf /etc/telegraf/telegraf.d/"     
 
           # CLMC agent general and output configuration
-          instance_config.vm.provision :shell, :path => "scripts/clmc-agent/write_config.sh"
+          instance_config.vm.provision :shell, :path => "scripts/clmc-agent/configure_template.sh"
 
           instance_config.vm.provision :shell, :path => "scripts/clmc-agent/configure.sh", :args => "#{host["location"]} #{host["sfc_id"]} #{host["sfc_id_instance"]} #{host["sf_id"]} #{host["sf_id_instance"]} #{host["ipendpoint_id"]} #{host["influxdb_url"]} #{host["database_name"]}"  
 
diff --git a/scripts/clmc-agent/write_config.sh b/scripts/clmc-agent/write_config.sh
deleted file mode 100644
index 04d1229..0000000
--- a/scripts/clmc-agent/write_config.sh
+++ /dev/null
@@ -1,79 +0,0 @@
-#!/bin/bash
-#/////////////////////////////////////////////////////////////////////////
-#//
-#// (c) University of Southampton IT Innovation Centre, 2017
-#//
-#// Copyright in this software belongs to University of Southampton
-#// IT Innovation Centre of Gamma House, Enterprise Road,
-#// Chilworth Science Park, Southampton, SO16 7NS, UK.
-#//
-#// This software may not be used, sold, licensed, transferred, copied
-#// or reproduced in whole or in part in any manner or form or in or
-#// on any media by any person other than in accordance with the terms
-#// of the Licence Agreement supplied with the software, or otherwise
-#// without the prior written consent of the copyright owners.
-#//
-#// This software is distributed WITHOUT ANY WARRANTY, without even the
-#// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
-#// PURPOSE, except where stated in the Licence Agreement supplied with
-#// the software.
-#//
-#//      Created By :            Michael Boniface
-#//      Created Date :          19/03/2018
-#//      Created for Project :   FLAME
-#//
-#/////////////////////////////////////////////////////////////////////////
-
-TELEGRAF_CONF_DIR="/etc/telegraf"
-TELEGRAF_CONF_FILE=$TELEGRAF_CONF_DIR"/telegraf.conf"
-TELEGRAF_INCLUDE_CONF_DIR=$TELEGRAF_CONF_DIR"/telegraf.d"
-TELEGRAF_OUTPUT_CONF_FILE=$TELEGRAF_INCLUDE_CONF_DIR"/telegraf_output.conf"
-
-echo "Checking Telegraf installation"
-
-# Check the target telegraf directory exists
-if [ ! -d "$TELEGRAF_CONF_DIR" ]; then
-    echo "Error: Telegraf conf directory does not exist on target machine. Check that telegraf is installed "$TELEGRAF_CONF_DIR
-    exit 1
-fi
-
-# Check the target telegraf directory exists
-if [ ! -d $TELEGRAF_INCLUDE_CONF_DIR ]; then
-    echo "Error: Telegraf conf include directory does not exist on target machine. Check that telegraf is installed "$TELEGRAF_INCLUDE_CONF_DIR
-    exit 1
-fi
-
-# Copy configuration
-echo "Telegraf general config file: " $TELEGRAF_CONF_FILE
-(cat <<'EOF'
-[global_tags]
-  location="$LOCATION"
-  sfc="$SFC_ID"
-  sfc_i="$SFC_ID_INSTANCE"
-  sf="$SF_ID"
-  sf_i="$SF_ID_INSTANCE"
-  ipendpoint="$IP_ENDPOINT_ID"
-[agent]
-  interval = "10s"
-  round_interval = true
-  metric_buffer_limit = 1000
-  flush_buffer_when_full = true
-  collection_jitter = "0s"
-  flush_interval = "10s"
-  flush_jitter = "0s"
-  debug = false
-  quiet = false
-  logfile = "/var/log/telegraf/telegraf.log"
-  hostname = ""
-EOF
-) > $TELEGRAF_CONF_FILE  
-
-echo "Telegraf output config file: " $TELEGRAF_OUTPUT_CONF_FILE
-(cat <<'EOF'
-[[outputs.influxdb]]
-  urls = ["$INFLUXDB_URL"]
-  database = "$DATABASE_NAME"
-  precision = "s"
-  timeout = "5s"
-EOF
-) > $TELEGRAF_OUTPUT_CONF_FILE
diff --git a/test/scripts/test_config_telegraf.py b/test/scripts/test_config_telegraf.py
index fa021f0..c1d9393 100644
--- a/test/scripts/test_config_telegraf.py
+++ b/test/scripts/test_config_telegraf.py
@@ -4,6 +4,8 @@ import pytest
 import subprocess
 
 def test_write_telegraf_conf():
+
+  # test telegraf monitoring configuration
   TELEGRAF_CONF_DIR="/etc/telegraf"
   LOCATION="DC1"
   SFC_ID="media_service_A"
@@ -15,38 +17,40 @@ def test_write_telegraf_conf():
   DATABASE_NAME="experimentation_database"  
 
   try:
-    # run with no telegraf conf directory
-    (out, err, code) = run_command('sudo /vagrant/scripts/clmc-agent/write_config.sh')
+    # run write config template script with no telegraf conf directory
+    cmd = 'sudo /vagrant/scripts/clmc-agent/configure_template.sh'
+    (out, err, code) = run_command(cmd)
     assert code == 1, "Failed to catch error of no telegraf configuration directory : " + str(code) + ", cmd=" + cmd
 
     # mk telegraf conf directory
     run_command("sudo mkdir -p /etc/telegraf")
 
-    # run with no telegraf.d directory
-    (out, err, code) = run_command('sudo /vagrant/scripts/clmc-agent/write_config.sh')
+    # run write config template  script with no telegraf.d directory
+    (out, err, code) = run_command(cmd)
     assert code == 1, "Failed to catch error of no telegraf include directory : " + str(code) + ", cmd=" + cmd
 
     # mk telegraf.d directory
     run_command("sudo mkdir -p /etc/telegraf/telegraf.d")    
 
-    # write configuration to file
-    (out, err, code) = run_command('sudo /vagrant/scripts/clmc-agent/write_config.sh')
+    # run write config template script and check that the script has exited correctly
+    (out, err, code) = run_command(cmd)
     assert code == 0, "Failed to write configuration files : " + str(code) + ", cmd=" + cmd
 
-    # run with incorrect arguments
+    # run template relacement script with incorrect arguments
     cmd = 'sudo /vagrant/scripts/clmc-agent/configure.sh' 
     (out, err, code) = run_command(cmd)
     assert code == 1, "Failed to return error on incorrect arguments : " + str(code) + ", cmd=" + cmd  
 
+    # run template relacement script with all arguments
     cmd = 'sudo /vagrant/scripts/clmc-agent/configure.sh ' + LOCATION + ' ' + SFC_ID + ' ' + SFC_ID_INSTANCE + ' ' + SF_ID + ' ' + SF_ID_INSTANCE + ' ' + IP_ENDPOINT_ID + ' ' + INFLUXDB_URL + ' ' + DATABASE_NAME
-    # run everything setup 
     (out, err, code) = run_command(cmd)
     assert code == 0, "Configure command returned error, output=" + str(out) + ", cmd=" + cmd
 
-    try:
-        TELEGRAF_GENERAL_CONF_FILE = TELEGRAF_CONF_DIR + "/telegraf.d/telegraf_output.conf"
+    # check that replacement was correct in telegraf.conf
+    try:        
+        TELEGRAF_GENERAL_CONF_FILE = TELEGRAF_CONF_DIR + "/telegraf.conf"
         with open(TELEGRAF_GENERAL_CONF_FILE) as general_conf:
-          lines = general_conf.read()
+          lines = general_conf.read()          
           assert lines.find(LOCATION), "Cannot find location" 
           assert lines.find(SFC_ID), "Cannot find sfc_id"
           assert lines.find(SFC_ID_INSTANCE), "Cannot find sfc_id_instance"  
@@ -56,8 +60,9 @@ def test_write_telegraf_conf():
     except FileNotFoundError:
         assert False, "Telegraf general conf file not found, " + TELEGRAF_GENERAL_CONF_FILE
 
+    # check that replacement was correct in telegraf_output.conf
     try:
-        TELEGRAF_OUTPUT_CONF_FILE = TELEGRAF_CONF_DIR + "/telegraf.conf"
+        TELEGRAF_OUTPUT_CONF_FILE = TELEGRAF_CONF_DIR + "/telegraf.d/telegraf_output.conf"
         with open(TELEGRAF_OUTPUT_CONF_FILE) as output_conf:
           lines = output_conf.read()
           assert lines.find(INFLUXDB_URL), "Cannot find influx_db" 
@@ -67,7 +72,7 @@ def test_write_telegraf_conf():
 
   finally:
       # clean up telegraf after test
-      #run_command("sudo rm -rf /etc/telegraf")
+      run_command("sudo rm -rf /etc/telegraf")
       print ("finally")
 
 # wrapper for executing commands on the cli, returning (std_out, std_error, process_return_code)
-- 
GitLab