test for network backend added

This commit is contained in:
tmetsch 2012-10-18 13:30:24 +02:00
parent 31fbc78dd2
commit a2e1e8b398
2 changed files with 180 additions and 3 deletions

View File

@ -77,13 +77,17 @@ class NetworkInterfaceBackend(backend.KindBackend):
"""
As nova does not support creation of L2 networks we don't.
"""
# TODO: add all network info!
if link.target.identifier == '/network/public':
# public means floating IP in OS!
address = net.add_floating_ip_to_vm(link.source.attributes[
'occi.core.id'],
extras['nova_ctx'])
'occi.core.id'],
extras['nova_ctx'])
link.attributes['occi.networkinterface.interface'] = 'eth0'
link.attributes['occi.networkinterface.mac'] = 'aa:bb:cc:dd:ee:ff'
link.attributes['occi.networkinterface.state'] = 'active'
link.attributes['occi.networkinterface.address'] = address
link.attributes['occi.networkinterface.gateway'] = '0.0.0.0'
link.attributes['occi.networkinterface.allocation'] = 'static'
else:
raise AttributeError('Currently not supported.')

View File

@ -0,0 +1,173 @@
# coding=utf-8
# vim: tabstop=4 shiftwidth=4 softtabstop=4
#
# Copyright (c) 2012, Intel Performance Learning Solutions Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""
Test network resource backend.
"""
#pylint: disable=W0102,C0103,R0904
import mox
import unittest
from occi import core_model
from occi_os_api import nova_glue
from occi_os_api.backends import network
class TestNetworkInterfaceBackend(unittest.TestCase):
"""
Tests the network interface backend!
"""
def setUp(self):
"""
Setup the tests.
"""
self.backend = network.NetworkInterfaceBackend()
self.sec_obj = {'nova_ctx': None}
self.mox = mox.Mox()
def tearDown(self):
"""
Cleanup mocks.
"""
self.mox.UnsetStubs()
# Test for failure
def test_create_for_failure(self):
"""
Test create for failure!
"""
source = mox.MockObject(core_model.Resource)
source.attributes={'occi.core.id': 'bar'}
target = mox.MockObject(core_model.Resource)
target.identifier = '/network/admin'
link = core_model.Link('foo', None, [], source, target)
self.mox.ReplayAll()
self.assertRaises(AttributeError, self.backend.create, link,
self.sec_obj)
self.mox.VerifyAll()
def test_update_for_failure(self):
"""
No updates allowed!
"""
self.assertRaises(AttributeError, self.backend.update, None, None,
None)
# Test for sanity!
def test_create_for_sanity(self):
"""
Test create for sanity!
"""
source = mox.MockObject(core_model.Resource)
source.attributes={'occi.core.id': 'bar'}
target = mox.MockObject(core_model.Resource)
target.identifier = '/network/public'
link = core_model.Link('foo', None, [], source, target)
self.mox.StubOutWithMock(nova_glue.net, 'add_floating_ip_to_vm')
nova_glue.net.add_floating_ip_to_vm(mox.IsA(object),
mox.IsA(object)).AndReturn('10.0.0.1')
self.mox.ReplayAll()
self.backend.create(link, self.sec_obj)
# verify all attrs and mixins!
self.assertIn('occi.networkinterface.interface', link.attributes)
self.assertIn('occi.networkinterface.mac', link.attributes)
self.assertIn('occi.networkinterface.state', link.attributes)
self.assertIn('occi.networkinterface.address', link.attributes)
self.assertIn('occi.networkinterface.gateway', link.attributes)
self.assertIn('occi.networkinterface.allocation', link.attributes)
self.mox.VerifyAll()
def test_delete_for_sanity(self):
"""
Test create for sanity!
"""
source = mox.MockObject(core_model.Resource)
source.attributes={'occi.core.id': 'bar'}
target = mox.MockObject(core_model.Resource)
target.identifier = '/network/public'
link = core_model.Link('foo', None, [], source, target)
link.attributes = {'occi.networkinterface.address': '10.0.0.1'}
self.mox.StubOutWithMock(nova_glue.net, 'remove_floating_ip')
nova_glue.net.remove_floating_ip(mox.IsA(object), mox.IsA(object),
mox.IsA(object))
self.mox.ReplayAll()
self.backend.delete(link, self.sec_obj)
self.mox.VerifyAll()
class TestNetworkBackend(unittest.TestCase):
"""
Some tests for network resources.
"""
def setUp(self):
"""
Initialize test.
"""
self.backend = network.NetworkBackend()
def test_create_for_failure(self):
"""
Expecting an error!
"""
self.assertRaises(AttributeError, self.backend.create, None, None)
def test_action_for_failure(self):
"""
Expecting an error!
"""
self.assertRaises(AttributeError, self.backend.action, None,
None, None, None)
class TestIpNetworkBackend(unittest.TestCase):
"""
Some tests for network resources.
"""
def setUp(self):
"""
Initialize test.
"""
self.backend = network.IpNetworkBackend()
def test_create_for_failure(self):
"""
Expecting an error!
"""
self.assertRaises(AttributeError, self.backend.create, None, None)