Commit cbc8008d authored by Alex Pyrgiotis's avatar Alex Pyrgiotis
Browse files

cyclades: Add tests for IP actions

Add a test for creating a floating IP and a test to check if the
`validate_ip_action` works as intended.
parent 9bd87a12
# flake8: noqa
from .networks import *
from .ips import *
from .servers import *
from .utils_tests import *
from .rapi_pool_tests import *
# vim: set fileencoding=utf-8 :
# Copyright (C) 2010-2014 GRNET S.A.
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <>.
# Provides automated tests for logic module
from django.test import TestCase
from django.core.exceptions import ObjectDoesNotExist
from snf_django.lib.api import faults
from snf_django.utils.testing import mocked_quotaholder
from synnefo.logic import ips
from synnefo.db import models_factory as mfactory
from synnefo.db.models import IPAddress
class IPTest(TestCase):
"""Test suite for actions on IP addresses."""
def setUp(self):
"""Common setup method for this suite.
This setUp method creates a simple IP Pool from which IPs can be
self.subnet = mfactory.IPv4SubnetFactory(network__floating_ip_pool=True) =
def test_create(self):
"""Test if a floating IP is created properly."""
with mocked_quotaholder():
ip = ips.create_floating_ip("1134",
self.assertEqual(len(, 1)
self.assertEqual([0], ip)
def test_delete(self):
"""Test if the delete action succeeds/fails properly."""
# Create a floating IP and force-attach it to a NIC instance.
vm = mfactory.VirtualMachineFactory()
nic = mfactory.NetworkInterfaceFactory(,
with mocked_quotaholder():
ip = ips.create_floating_ip("1134",
ip.nic = nic
# Test 1 - Check if we can delete an IP attached to a VM.
# The validate function and the action should both fail with the
# following message.
expected_msg = "IP '{}' is used by server '{}'".format(,
# Verify that the validate function fails in silent mode.
res, msg = ips.validate_ip_action(ip, "DELETE", silent=True)
self.assertEqual(msg, expected_msg)
# Verify that the validate function fails in non-silent mode.
with self.assertRaises(faults.Conflict) as cm:
ips.validate_ip_action(ip, "DELETE", silent=False)
self.assertEqual(cm.exception.message, expected_msg)
# Verify that the delete action fails with exception.
with mocked_quotaholder():
with self.assertRaises(faults.Conflict) as cm:
self.assertEqual(cm.exception.message, expected_msg)
# Test 2 - Check if we can delete a free IP.
# Force-detach IP from NIC.
ip.nic = None
# Verify that the validate function passes in silent mode.
res, _ = ips.validate_ip_action(ip, "DELETE", silent=True)
# Verify that the delete action succeeds.
with mocked_quotaholder():
with self.assertRaises(ObjectDoesNotExist):
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment