From f433b9d216e8dcbf82e496fbb14d63a33eb778d1 Mon Sep 17 00:00:00 2001
From: Benjamin Bertrand <benjamin.bertrand@esss.se>
Date: Tue, 13 Mar 2018 23:25:29 +0100
Subject: [PATCH] Fix failing tests

Factory boy doesn't reset the sequence between tests.
We use a sequence to generate unique IP addresses in the InterfaceFactory.
When the sequence number gets too high, the IP is not in the network
range anymore.

To fix the issue, we reset the sequence number when initializing a new
session.
---
 tests/functional/conftest.py  | 1 +
 tests/functional/factories.py | 5 ++++-
 2 files changed, 5 insertions(+), 1 deletion(-)

diff --git a/tests/functional/conftest.py b/tests/functional/conftest.py
index d9166d2..518b78c 100644
--- a/tests/functional/conftest.py
+++ b/tests/functional/conftest.py
@@ -82,6 +82,7 @@ def session(db, request):
 
     Rollback any transaction to always leave the database clean
     """
+    factories.InterfaceFactory.reset_sequence()
     connection = db.engine.connect()
     transaction = connection.begin()
     session = common.Session
diff --git a/tests/functional/factories.py b/tests/functional/factories.py
index 523103e..b347923 100644
--- a/tests/functional/factories.py
+++ b/tests/functional/factories.py
@@ -139,7 +139,10 @@ class NetworkFactory(factory.alchemy.SQLAlchemyModelFactory):
     def last_ip(self):
         net = ipaddress.ip_network(self.address)
         hosts = list(net.hosts())
-        return str(hosts[-5])
+        try:
+            return str(hosts[-5])
+        except IndexError:
+            return str(hosts[-1])
 
 
 class InterfaceFactory(factory.alchemy.SQLAlchemyModelFactory):
-- 
GitLab