From bbba27959c69e9895ff47434cb93073dbbedfc30 Mon Sep 17 00:00:00 2001
From: Kevin Duret <duret.kevin@gmail.com>
Date: Mon, 19 Mar 2018 15:37:58 +0100
Subject: [PATCH] test(acceptance): fix random fails on flexible downtime
 acceptance test (#6148)

---
 features/DowntimeStartAndStop.feature         |  2 +-
 .../bootstrap/DowntimeStartAndStopContext.php | 30 +++++++++++++++++++
 2 files changed, 31 insertions(+), 1 deletion(-)

diff --git a/features/DowntimeStartAndStop.feature b/features/DowntimeStartAndStop.feature
index ada73d7142..ea9b060677 100644
--- a/features/DowntimeStartAndStop.feature
+++ b/features/DowntimeStartAndStop.feature
@@ -37,7 +37,7 @@ Feature: Downtime start and stop
     Given a flexible downtime on a monitored element
     And the flexible downtime is started
     When the downtime duration is finished
-    Then the downtime is stopped
+    Then the flexible downtime is stopped
 
   Scenario: Configure recurrent downtime
     Given a recurrent downtime on an other timezone service
diff --git a/features/bootstrap/DowntimeStartAndStopContext.php b/features/bootstrap/DowntimeStartAndStopContext.php
index 58549f7b22..38020b6315 100644
--- a/features/bootstrap/DowntimeStartAndStopContext.php
+++ b/features/bootstrap/DowntimeStartAndStopContext.php
@@ -198,6 +198,36 @@ class DowntimeStartAndStopContext extends CentreonContext
         );
     }
 
+    /**
+     * @Then the flexible downtime is stopped
+     */
+    public function theFlexibleDowntimeIsStopped()
+    {
+        $this->spin(
+            function ($context) {
+                $finished = false;
+
+                $storageDb = $context->getStorageDatabase();
+                $res = $storageDb->query(
+                    'SELECT d.downtime_id, d.actual_end_time ' .
+                    'FROM downtimes d, hosts h, services s ' .
+                    'WHERE h.host_id = d.host_id ' .
+                    'AND s.service_id = d.service_id ' .
+                    'AND h.name = "' . $context->host . '" ' .
+                    'AND s.description = "' . $context->service . '" ' .
+                    'AND d.actual_end_time IS NOT NULL ' .
+                    'AND d.actual_end_time < ' . time()
+                );
+                if ($row = $res->fetch()) {
+                    $finished = true;
+                }
+                return $finished;
+            },
+            'FLexible downtime is still running.',
+            30
+        );
+    }
+
     /**
      * @Given a downtime in configuration of a user in other timezone
      */
-- 
GitLab