From 78f8e4e6e2de79d8a7d122f443f04bb5a03f68f3 Mon Sep 17 00:00:00 2001
From: flavien peyre <peyre.flavien@gmail.com>
Date: Thu, 2 Jul 2015 13:19:27 -0400
Subject: [PATCH] Integration test: update host + metrics name

Change-Id: I2f369300562882f3db880241cb6463b8fbe6c9ea
---
 requirements.txt                              |  1 +
 .../backend/{docker.py => docker_backend.py}  | 14 +++
 .../tests/integration/backend/integration.yml |  2 +-
 surveil/tests/integration/integration_test.py |  7 +-
 surveil/tests/integration/test_surveil.py     | 93 +++++++++++++++----
 5 files changed, 97 insertions(+), 20 deletions(-)
 rename surveil/tests/integration/backend/{docker.py => docker_backend.py} (81%)

diff --git a/requirements.txt b/requirements.txt
index 10f74dd..0fc3947 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -12,3 +12,4 @@ influxdb==2.6.0
 pika
 python-surveilclient==0.10.0
 six
+docker-py
diff --git a/surveil/tests/integration/backend/docker.py b/surveil/tests/integration/backend/docker_backend.py
similarity index 81%
rename from surveil/tests/integration/backend/docker.py
rename to surveil/tests/integration/backend/docker_backend.py
index d844abb..cd33859 100644
--- a/surveil/tests/integration/backend/docker.py
+++ b/surveil/tests/integration/backend/docker_backend.py
@@ -18,6 +18,7 @@ import time
 from compose.cli import docker_client
 from compose import config as compose_config
 from compose import project as compose_project
+import docker
 from surveilclient import client as sclient
 
 
@@ -83,6 +84,19 @@ class DockerBackend():
             else:
                 raise Exception("Surveil could not start")
 
+    def execute_command(self, commands, container_name):
+        dclient = docker.Client()
+        if container_name is not None:
+            containers = dclient.containers()
+            for container in containers:
+                count = container.get('Image').count(container_name)
+                if ((count == 1 and container_name != 'surveil') or
+                        (count == 2 and container_name == 'surveil')):
+                    for command in commands:
+                        id = dclient.exec_create(container.get('Id')[:12],
+                                                 command)
+                        dclient.exec_start(id['Id'])
+
     def tearDownClass(self):
         self.project.kill()
         self.project.remove_stopped()
diff --git a/surveil/tests/integration/backend/integration.yml b/surveil/tests/integration/backend/integration.yml
index f0d75ae..4cce24d 100644
--- a/surveil/tests/integration/backend/integration.yml
+++ b/surveil/tests/integration/backend/integration.yml
@@ -5,7 +5,7 @@ surveil:
    - influxdb
    - alignak
   ports:
-   - "8999:8080"
+   - "8999:5311"
   command: bash -c "cd /opt/surveil && ./setup.sh && /opt/surveil/env/bin/python setup.py develop && ((sleep 40 && surveil-init --influxdb --packs --mongodb --demo) &) && sleep 20 && surveil-api --reload"
 
 alignak:
diff --git a/surveil/tests/integration/integration_test.py b/surveil/tests/integration/integration_test.py
index 57bc28f..3e63adc 100644
--- a/surveil/tests/integration/integration_test.py
+++ b/surveil/tests/integration/integration_test.py
@@ -18,7 +18,7 @@ import os
 import unittest
 
 from surveil.tests import base
-from surveil.tests.integration.backend import docker
+from surveil.tests.integration.backend import docker_backend
 
 
 @unittest.skipIf(os.environ.get('SURVEIL_INTEGRATION_TESTS', None) != 'True',
@@ -33,7 +33,7 @@ class MergedIntegrationTest(base.BaseTestCase):
         )
 
         if test_backend == 'DOCKER':
-            MergedIntegrationTest.backend = docker.DockerBackend()
+            MergedIntegrationTest.backend = docker_backend.DockerBackend()
         else:
             raise Exception(
                 "Could not identify tests backend: '%s'" % test_backend
@@ -47,6 +47,9 @@ class MergedIntegrationTest(base.BaseTestCase):
     def get_surveil_client(self):
         return MergedIntegrationTest.backend.get_surveil_client()
 
+    def execute_command(self, command, container):
+        MergedIntegrationTest.backend.execute_command(command, container)
+
 
 class SeparatedIntegrationTests(MergedIntegrationTest):
 
diff --git a/surveil/tests/integration/test_surveil.py b/surveil/tests/integration/test_surveil.py
index 21fe141..22f855f 100644
--- a/surveil/tests/integration/test_surveil.py
+++ b/surveil/tests/integration/test_surveil.py
@@ -65,6 +65,7 @@ class TestSeparatedIntegrationSurveil(
         )
 
     def test_delete_host(self):
+        """Delete a host and asserts that is is not monitored by Alignak."""
         self.test_create_host()
 
         self.get_surveil_client().config.hosts.delete(
@@ -90,16 +91,41 @@ class TestSeparatedIntegrationSurveil(
             )
         )
 
-    def test_passive_check(self):
-        self.get_surveil_client().config.hosts.create(
+    def test_update_host(self):
+        """Update a host and asserts that is is monitored by Alignak."""
+        self.test_create_host()
+
+        self.get_surveil_client().config.hosts.update(
             host_name='integrationhosttest',
-            address='127.0.0.1',
-            use='generic-host',
+            host={'host_name': 'host_name_up',
+                  'address': '127.0.1.1'}
         )
-        self.get_surveil_client().config.commands.create(
-            command_name='check_integrationhosttest',
-            command_line='/usr/lib/monitoring/plugins/sfl/check_example'
+
+        self.get_surveil_client().config.reload_config()
+
+        def function():
+            status_host = (self.get_surveil_client().
+                           config.hosts.get(host_name='host_name_up'))
+            self.assertTrue(
+                status_host['host_name'].decode() == 'host_name_up' and
+                status_host['address'].decode() == '127.0.1.1' and
+                status_host['use'].decode() == 'generic-host'
+            )
+
+        self.assertTrue(
+            self.try_for_x_seconds(
+                function,
+                time_to_wait=180,
+                cooldown=10,
+                exception=AssertionError,
+                message="Host is not updated."
+            )
         )
+
+    def test_passive_check(self):
+        """Test  monitoring a host with passive checks."""
+        self.test_create_host()
+
         self.get_surveil_client().config.services.create(
             check_command="check_integrationhosttest",
             check_interval="5",
@@ -145,14 +171,24 @@ class TestSeparatedIntegrationSurveil(
         )
 
     def test_custom_plugins(self):
-        self.get_surveil_client().config.hosts.create(
-            host_name='integrationhosttest',
-            address='127.0.0.1',
-            use='generic-host',
-        )
+        """Test documentation tutorial monitoring with your custom plugins."""
+        commands = [
+            "mkdir /usr/lib/monitoring/plugins/custom/",
+            "echo -e '#!/bin/bash\necho " +
+            "DISK $1 OK - free space: / 3326 MB (56%);"
+            " | /=2643MB;5948;5958;0;5968" +
+            "' | sudo tee /usr/lib/monitoring/plugins/"
+            "custom/check_example",
+            'chmod +x /usr/lib/monitoring/plugins/custom/'
+            'check_example'
+        ]
+
+        self.execute_command(commands, 'alignak')
+
+        self.test_create_host()
         self.get_surveil_client().config.commands.create(
             command_name='check_integrationhosttest',
-            command_line='/usr/lib/monitoring/plugins/sfl/check_example'
+            command_line='$CUSTOMPLUGINSDIR$/check_example $HOSTADDRESS$'
         )
         self.get_surveil_client().config.services.create(
             check_command="check_integrationhosttest",
@@ -165,8 +201,7 @@ class TestSeparatedIntegrationSurveil(
             notification_interval="30",
             notification_period="24x7",
             retry_interval="3",
-            service_description="check_integrationhosttest",
-            passive_checks_enabled="1"
+            service_description="check_integrationhosttest"
         )
 
         self.get_surveil_client().config.reload_config()
@@ -178,8 +213,8 @@ class TestSeparatedIntegrationSurveil(
                     service['service_description'].decode() ==
                     'check_integrationhosttest' and
                     service['plugin_output'].decode() ==
-                    "DISK OK - free space: / 3326 MB (56%);"
-                    " | /=2643MB;5948;5958;0;5968"
+                    "DISK 127.0.1.1 OK - free space: / 3326 MB (56%);"
+                    "| /=2643MB;5948;5958;0;5968"
                     for service in status_services)
             )
 
@@ -191,4 +226,28 @@ class TestSeparatedIntegrationSurveil(
                 exception=AssertionError,
                 message="Custom Plugins is not used"
             )
+        )
+
+    def test_list_metrics_name_host(self):
+        """Test if a host with use=generic-host have the good metrics name."""
+        self.test_create_host()
+
+        def function():
+            metrics_name_hosts = (
+                self.get_surveil_client().status.hosts.metrics.get(
+                    host_name='integrationhosttest')
+            )
+            self.assertTrue(
+                any(metric_name['metric_name'].decode() == 'rtmin'
+                    for metric_name in metrics_name_hosts)
+            )
+
+        self.assertTrue(
+            self.try_for_x_seconds(
+                function,
+                time_to_wait=360,
+                cooldown=10,
+                exception=AssertionError,
+                message="No metric name for host created"
+            )
         )
\ No newline at end of file