Commit 48a0cb71 authored by Christos Stavrakakis's avatar Christos Stavrakakis
Browse files

Fixes to tests

parent 362e8c15
......@@ -42,8 +42,15 @@ from synnefo.db.models_factory import (FloatingIPFactory, NetworkFactory,
from mock import patch, Mock
from functools import partial
from synnefo.cyclades_settings import cyclades_services
from synnefo.lib.services import get_service_path
from synnefo.lib import join_urls
URL = "/api/v1.1/os-floating-ips"
compute_path = get_service_path(cyclades_services, "compute", version="v2.0")
URL = join_urls(compute_path, "os-floating-ips")
NETWORKS_URL = join_urls(compute_path, "networks")
SERVERS_URL = join_urls(compute_path, "servers")
FloatingIPPoolFactory = partial(NetworkFactory, public=True, deleted=False,
floating_ip_pool=True)
......@@ -195,7 +202,7 @@ class FloatingIPAPITest(BaseAPITest):
# Also send a notification to remove the NIC and assert that FIP is in
# use until notification from ganeti arrives
request = {"removeFloatingIp": {"address": ip.ipv4}}
url = "/api/v1.1/servers/%s/action" % vm.id
url = SERVERS_URL + "/%s/action" % vm.id
with patch('synnefo.logic.rapi_pool.GanetiRapiClient') as c:
c().ModifyInstance.return_value = 10
response = self.post(url, vm.userid, json.dumps(request),
......@@ -219,19 +226,19 @@ class FloatingIPAPITest(BaseAPITest):
net = ip.network
# Can not remove network with floating IPs
with mocked_quotaholder():
response = self.delete("/api/v1.1/networks/%s" % net.id,
response = self.delete(NETWORKS_URL + "/%s" % net.id,
net.userid)
self.assertFault(response, 421, "networkInUse")
# But we can with only deleted Floating Ips
ip.deleted = True
ip.save()
with mocked_quotaholder():
response = self.delete("/api/v1.1/networks/%s" % net.id,
response = self.delete(NETWORKS_URL + "/%s" % net.id,
net.userid)
self.assertSuccess(response)
POOLS_URL = "/api/v1.1/os-floating-ip-pools"
POOLS_URL = join_urls(compute_path, "os-floating-ip-pools")
class FloatingIPPoolsAPITest(BaseAPITest):
......@@ -259,7 +266,7 @@ class FloatingIPActionsTest(BaseAPITest):
self.vm = vm
def test_bad_request(self):
url = "/api/v1.1/servers/%s/action" % self.vm.id
url = SERVERS_URL + "/%s/action" % self.vm.id
response = self.post(url, self.vm.userid, json.dumps({}), "json")
self.assertBadRequest(response)
response = self.post(url, self.vm.userid,
......@@ -270,7 +277,7 @@ class FloatingIPActionsTest(BaseAPITest):
@patch('synnefo.logic.rapi_pool.GanetiRapiClient')
def test_add_floating_ip(self, mock):
# Not exists
url = "/api/v1.1/servers/%s/action" % self.vm.id
url = SERVERS_URL + "/%s/action" % self.vm.id
request = {"addFloatingIp": {"address": "10.0.0.1"}}
response = self.post(url, self.vm.userid, json.dumps(request), "json")
self.assertItemNotFound(response)
......@@ -297,7 +304,7 @@ class FloatingIPActionsTest(BaseAPITest):
@patch('synnefo.logic.rapi_pool.GanetiRapiClient')
def test_remove_floating_ip(self, mock):
# Not exists
url = "/api/v1.1/servers/%s/action" % self.vm.id
url = SERVERS_URL + "/%s/action" % self.vm.id
request = {"removeFloatingIp": {"address": "10.0.0.1"}}
response = self.post(url, self.vm.userid, json.dumps(request), "json")
self.assertItemNotFound(response)
......
......@@ -43,10 +43,11 @@ from synnefo.lib import join_urls
class ComputeAPITest(BaseAPITest):
def setUp(self, *args, **kwargs):
super(ComputeAPITest, self).setUp(*args, **kwargs)
def __init__(self, *args, **kwargs):
super(ComputeAPITest, self).__init__(*args, **kwargs)
self.compute_path = get_service_path(cyclades_services, 'compute',
version='v2.0')
def myget(self, path, *args, **kwargs):
path = join_urls(self.compute_path, path)
return self.get(path, *args, **kwargs)
......@@ -372,7 +373,7 @@ class NetworkAPITest(ComputeAPITest):
net = mfactory.NetworkFactory(state='PENDING', subnet='10.0.0.0/31',
userid=user)
request = {'add': {'serverRef': vm.id}}
response = self.mypost('/api/v1.1/networks/%d/action' % net.id,
response = self.mypost('networks/%d/action' % net.id,
net.userid, json.dumps(request), 'json')
# Test that returns BuildInProgress
self.assertEqual(response.status_code, 409)
......@@ -400,7 +401,8 @@ class NetworkAPITest(ComputeAPITest):
def test_remove_nic(self, mrapi):
user = 'userr'
vm = mfactory.VirtualMachineFactory(name='yo', userid=user)
vm = mfactory.VirtualMachineFactory(name='yo', userid=user,
operstate="ACTIVE")
net = mfactory.NetworkFactory(state='ACTIVE', userid=user)
nic = mfactory.NetworkInterfaceFactory(machine=vm, network=net)
mrapi().ModifyInstance.return_value = 1
......
......@@ -49,8 +49,8 @@ from mock import patch, Mock
class ComputeAPITest(BaseAPITest):
def setUp(self, *args, **kwargs):
super(ComputeAPITest, self).setUp(*args, **kwargs)
def __init__(self, *args, **kwargs):
super(ComputeAPITest, self).__init__(*args, **kwargs)
self.compute_path = get_service_path(cyclades_services, 'compute',
version='v2.0')
......@@ -80,7 +80,6 @@ class ServerAPITest(ComputeAPITest):
self.vm3 = mfactory.VirtualMachineFactory(deleted=True,
userid=self.user1)
self.vm4 = mfactory.VirtualMachineFactory(userid=self.user2)
super(ServerAPITest, self).setUp()
def test_server_list_1(self):
"""Test if the expected list of servers is returned."""
......@@ -275,7 +274,7 @@ class ServerCreateAPITest(ComputeAPITest):
mrapi().CreateInstance.return_value = 12
with override_settings(settings, DEFAULT_INSTANCE_NETWORKS=[]):
with mocked_quotaholder():
response = self.mypost('/api/v1.1/servers', 'test_user',
response = self.mypost('servers', 'test_user',
json.dumps(self.request), 'json')
self.assertEqual(response.status_code, 202)
mrapi().CreateInstance.assert_called_once()
......@@ -318,7 +317,7 @@ class ServerCreateAPITest(ComputeAPITest):
DEFAULT_INSTANCE_NETWORKS=["public", bnet1.network.id,
bnet2.network.id]):
with mocked_quotaholder():
response = self.mypost('/api/v1.1/servers', 'test_user',
response = self.mypost('servers', 'test_user',
json.dumps(request), 'json')
self.assertEqual(response.status_code, 202)
name, args, kwargs = mrapi().CreateInstance.mock_calls[0]
......@@ -338,7 +337,7 @@ class ServerCreateAPITest(ComputeAPITest):
with override_settings(settings,
DEFAULT_INSTANCE_NETWORKS=[bnet2.network.id]):
with mocked_quotaholder():
response = self.mypost('/api/v1.1/servers', 'test_user',
response = self.mypost('servers', 'test_user',
json.dumps(request), 'json')
self.assertEqual(response.status_code, 202)
name, args, kwargs = mrapi().CreateInstance.mock_calls[1]
......@@ -352,7 +351,7 @@ class ServerCreateAPITest(ComputeAPITest):
# test invalid network in DEFAULT_INSTANCE_NETWORKS
with override_settings(settings, DEFAULT_INSTANCE_NETWORKS=[42]):
response = self.mypost('/api/v1.1/servers', 'test_user',
response = self.mypost('servers', 'test_user',
json.dumps(request), 'json')
self.assertFault(response, 500, "internalServerError")
......@@ -360,7 +359,7 @@ class ServerCreateAPITest(ComputeAPITest):
request = deepcopy(self.request)
request["server"]["networks"] = [self.network.id]
with override_settings(settings, DEFAULT_INSTANCE_NETWORKS=["public"]):
response = self.mypost('/api/v1.1/servers', 'test_user',
response = self.mypost('servers', 'test_user',
json.dumps(request), 'json')
self.assertFault(response, 403, "forbidden")
# test wrong user
......@@ -368,7 +367,7 @@ class ServerCreateAPITest(ComputeAPITest):
request["server"]["networks"] = [bnet3.network.id]
with override_settings(settings, DEFAULT_INSTANCE_NETWORKS=["public"]):
with mocked_quotaholder():
response = self.mypost('/api/v1.1/servers', 'dummy_user',
response = self.mypost('servers', 'dummy_user',
json.dumps(request), 'json')
self.assertItemNotFound(response)
......@@ -389,7 +388,7 @@ class ServerCreateAPITest(ComputeAPITest):
with override_settings(settings,
DEFAULT_INSTANCE_NETWORKS=[bnet3.network.id]):
with mocked_quotaholder():
response = self.post('/api/v1.1/servers', 'test_user',
response = self.mypost('servers', 'test_user',
json.dumps(request), 'json')
self.assertEqual(response.status_code, 202)
api_server = json.loads(response.content)['server']
......@@ -413,7 +412,7 @@ class ServerCreateAPITest(ComputeAPITest):
request = deepcopy(self.request)
request["server"]["flavorRef"] = 42
with mocked_quotaholder():
response = self.mypost('/api/v1.1/servers', 'test_user',
response = self.mypost('servers', 'test_user',
json.dumps(request), 'json')
self.assertItemNotFound(response)
......@@ -423,7 +422,7 @@ class ServerDestroyAPITest(ComputeAPITest):
def test_delete_server(self, mrapi):
vm = mfactory.VirtualMachineFactory()
mrapi().DeleteInstance.return_value = 12
response = self.mydelete('/api/v1.1/servers/%d' % vm.id, vm.userid)
response = self.mydelete('servers/%d' % vm.id, vm.userid)
self.assertEqual(response.status_code, 204)
mrapi().DeleteInstance.assert_called_once()
......@@ -539,7 +538,7 @@ class ServerActionAPITest(ComputeAPITest):
vm = mfactory.VirtualMachineFactory(operstate="BUILD")
request = {'start': {}}
with mocked_quotaholder():
response = self.mypost('/api/v1.1/servers/%d/action' % vm.id,
response = self.mypost('servers/%d/action' % vm.id,
vm.userid, json.dumps(request), 'json')
self.assertEqual(response.status_code, 409)
self.assertFalse(mrapi.mock_calls)
......@@ -548,7 +547,7 @@ class ServerActionAPITest(ComputeAPITest):
"""Test building in progress"""
vm = mfactory.VirtualMachineFactory()
mrapi().DeleteInstance.return_value = 2
response = self.mydelete('/api/v1.1/servers/%d' % vm.id,
response = self.mydelete('servers/%d' % vm.id,
vm.userid)
self.assertSuccess(response)
mrapi().RemoveInstance.assert_called_once()
......@@ -578,13 +577,13 @@ class ServerActionAPITest(ComputeAPITest):
# Check building VM
vm = self.get_vm(flavor=flavor, operstate="BUILD")
request = {'resize': {'flavorRef': flavor.id}}
response = self.post('/api/v1.1/servers/%d/action' % vm.id,
response = self.mypost('servers/%d/action' % vm.id,
vm.userid, json.dumps(request), 'json')
self.assertFault(response, 409, "buildInProgress")
# Check same Flavor
vm = self.get_vm(flavor=flavor, operstate="STOPPED")
request = {'resize': {'flavorRef': flavor.id}}
response = self.post('/api/v1.1/servers/%d/action' % vm.id,
response = self.mypost('servers/%d/action' % vm.id,
vm.userid, json.dumps(request), 'json')
self.assertBadRequest(response)
# Check flavor with different disk
......@@ -592,14 +591,14 @@ class ServerActionAPITest(ComputeAPITest):
flavor3 = mfactory.FlavorFactory(disk=2048)
vm = self.get_vm(flavor=flavor2, operstate="STOPPED")
request = {'resize': {'flavorRef': flavor3.id}}
response = self.post('/api/v1.1/servers/%d/action' % vm.id,
response = self.mypost('servers/%d/action' % vm.id,
vm.userid, json.dumps(request), 'json')
self.assertBadRequest(response)
flavor2 = mfactory.FlavorFactory(disk_template="foo")
flavor3 = mfactory.FlavorFactory(disk_template="baz")
vm = self.get_vm(flavor=flavor2, operstate="STOPPED")
request = {'resize': {'flavorRef': flavor3.id}}
response = self.post('/api/v1.1/servers/%d/action' % vm.id,
response = self.mypost('servers/%d/action' % vm.id,
vm.userid, json.dumps(request), 'json')
self.assertBadRequest(response)
# Check success
......@@ -609,7 +608,7 @@ class ServerActionAPITest(ComputeAPITest):
cpu=4, ram=2048)
request = {'resize': {'flavorRef': flavor4.id}}
mrapi().ModifyInstance.return_value = 42
response = self.post('/api/v1.1/servers/%d/action' % vm.id,
response = self.mypost('servers/%d/action' % vm.id,
vm.userid, json.dumps(request), 'json')
self.assertEqual(response.status_code, 202)
vm = VirtualMachine.objects.get(id=vm.id)
......@@ -625,13 +624,13 @@ class ServerActionAPITest(ComputeAPITest):
vm.save()
for action in VirtualMachine.ACTIONS:
request = {action[0]: ""}
response = self.post('/api/v1.1/servers/%d/action' % vm.id,
response = self.mypost('servers/%d/action' % vm.id,
vm.userid, json.dumps(request), 'json')
self.assertBadRequest(response)
# however you can destroy
mrapi().DeleteInstance.return_value = 42
response = self.delete('/api/v1.1/servers/%d' % vm.id,
vm.userid)
response = self.mydelete('servers/%d' % vm.id,
vm.userid)
self.assertSuccess(response)
def get_vm(self, flavor, operstate):
......@@ -659,7 +658,7 @@ class ServerVNCConsole(ComputeAPITest):
data = json.dumps({'console': {'type': 'vnc'}})
with override_settings(settings, TEST=True):
response = self.mypost('/api/v1.1/servers/%d/action' % vm.id,
response = self.mypost('servers/%d/action' % vm.id,
vm.userid, data, 'json')
self.assertEqual(response.status_code, 200)
reply = json.loads(response.content)
......
......@@ -357,6 +357,8 @@ class UpdateDBTest(TestCase):
db_vm = VirtualMachine.objects.get(id=vm.id)
self.assertEqual(db_vm.operstate, vm.operstate)
# Test intermediate states
vm.operstate = "STOPPED"
vm.save()
for status in ["queued", "waiting", "running"]:
msg = self.create_msg(operation='OP_INSTANCE_SET_PARAMS',
instance=vm.backend_vm_id,
......@@ -774,11 +776,11 @@ class ReconciliationTest(TestCase):
def test_get_servers_from_db(self):
"""Test getting a dictionary from each server to its operstate"""
backend = 30000
backends = Backend.objects.all()
vm1 = self.get_vm('STARTED')
vm2 = self.get_vm('DESTROYED', deleted=True)
vm3 = self.get_vm('STOPPED')
self.assertEquals(reconciliation.get_servers_from_db(),
self.assertEquals(reconciliation.get_servers_from_db(backends),
{vm1.id: VMState(state='STARTED', cpu=2, ram=1024, nics=[]),
vm3.id: VMState(state='STOPPED', cpu=2, ram=1024, nics=[])}
)
......
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