示例#1
0
    def setUp(self):
        self.kind1 = Kind('http://www.example.com#', 'foo')
        self.kind2 = Kind('http://www.example.com#', 'bar')
        self.mixin = Mixin('http://www.example.com#', 'mixin')

        self.registry.set_backend(self.kind1, KindBackend())
        self.registry.set_backend(self.kind2, KindBackend())
示例#2
0
    def setUp(self):
        self.kind1 = Kind('http://example.com#', '1')
        self.kind2 = Kind('http://example.com#', '2')
        self.action = Action('http://example.com#', 'action')
        self.mixin = Mixin('http://example.com#', 'mixin')

        self.registry.set_backend(self.kind1, KindBackend())
        self.registry.set_backend(self.kind2, DummyBackend())
        self.registry.set_backend(self.action, ActionBackend())
        self.registry.set_backend(self.mixin, MixinBackend())

        self.entity = Resource('foo', self.kind1, [self.kind2])
示例#3
0
 def setUp(self):
     self.back = KindBackend()
     self.action = Action('foo', 'action')
     self.kind = Kind('foo',
                      'bar',
                      actions=[self.action],
                      attributes={
                          'foo': 'mutable',
                          'bar': 'required'
                      })
     self.link_kind = Kind('foo', 'bar', related=self.kind)
     self.resource = Resource('/foo/1', self.kind, [])
     self.resource.actions = [self.action]
     self.link = Link('/link/1', self.link_kind, [], None, self.resource)
示例#4
0
    def setUp(self):
        self.test_kind = Kind('http://example.com#', 'test')
        self.link_kind = Kind('http://example.com#link', 'link')
        mixins = []
        self.action = Action('http://example.com#', 'action')
        self.src_entity = Resource(None, self.test_kind, mixins, [])
        self.trg_entity = Resource('/foo/trg', self.test_kind, mixins, [])
        self.link1 = Link('/link/1', self.link_kind, [], self.src_entity,
                          self.trg_entity)
        self.src_entity.links = [self.link1]

        self.registry.add_resource(self.trg_entity.identifier, self.trg_entity)
        self.registry.set_backend(self.test_kind, KindBackend())
        self.registry.set_backend(self.link_kind, KindBackend())
        self.registry.set_backend(self.action, ActionBackend())
示例#5
0
    def test_init_so(self):
        # initialization
        kind = Kind('http://schemas.mobile-cloud-networking.eu/occi/sm#',
                    'myservice',
                    title='Test Service',
                    attributes={'mcn.test.attribute1': 'immutable'},
                    related=[Resource.kind],
                    actions=[])
        entity = Resource('my-id', kind, None)
        entity.extras = {}
        host = 'localhost:8888/myservice'
        entity.extras['host'] = host
        extras = {}
        extras['token'] = 'test_token'
        extras['tenant_name'] = 'test_tenantname'
        httpretty.register_uri(httpretty.POST,
                               'http://' + host + '/action=init',
                               status=200,
                               content_type='text/occi')

        # Test method
        self.som._SOManager__init_so(entity, extras)

        # Asserts
        sent_headers = httpretty.last_request().headers
        self.assertEqual(sent_headers['X-Auth-Token'], 'test_token')
        self.assertEqual(sent_headers['X-Tenant-Name'], 'test_tenantname')
示例#6
0
    def test_deploy_so_sanity(self):
        # initialization
        kind = Kind('http://schemas.mobile-cloud-networking.eu/occi/sm#',
                    'myservice',
                    title='Test Service',
                    attributes={'mcn.test.attribute1': 'immutable'},
                    related=[Resource.kind],
                    actions=[])
        entity = Resource('my-id', kind, None)
        entity.extras = {}
        host = 'test.mcn.org:8888/myservice'
        entity.extras['host'] = host
        extras = {}
        extras['token'] = 'test_token'
        extras['tenant_name'] = 'test_tenantname'
        body_content = '123456789'
        httpretty.register_uri(httpretty.POST,
                               'http://' + host + '/action=deploy',
                               status=200,
                               body=body_content,
                               content_type='text/occi')

        # Test method
        self.som._SOManager__deploy_so(entity, extras)

        # Asserts
        self.assertEqual(entity.extras['stack_id'], body_content)
示例#7
0
    def test_create_app(self):
        # Initialization
        # Tentative Kind/Resource Creation
        kind = Kind('http://schemas.mobile-cloud-networking.eu/occi/sm#',
                    'myservice',
                    title='Test Service',
                    attributes={'mcn.test.attribute1': 'immutable'},
                    related=[Resource.kind],
                    actions=[])
        entity = Resource('my-id', kind, None)
        httpretty.register_uri(
            httpretty.POST,
            'http://*****:*****@git.mcn.eu:myserv.git"'
            })

        # Test method
        repo_uri = self.som._SOManager__create_app(entity, None)

        # Asserts
        self.assertEqual(repo_uri, '[email protected]:myserv.git')
示例#8
0
    def test_deploy_so_failure(self):
        # Initialization
        kind = Kind('http://schemas.mobile-cloud-networking.eu/occi/sm#',
                    'myservice',
                    title='Test Service',
                    attributes={'mcn.test.attribute1': 'immutable'},
                    related=[Resource.kind],
                    actions=[])
        entity = Resource('my-id', kind, None)
        entity.extras = {}
        host = 'test.mcn.org:8888/myservice'
        entity.extras['host'] = host
        extras = {}
        extras['token'] = 'test_token'
        extras['tenant_name'] = 'test_tenantname'
        body_content = 'Please initialize SO with token and tenant first.'
        httpretty.register_uri(httpretty.POST,
                               'http://' + host + '/action=deploy',
                               status=200,
                               body=body_content,
                               content_type='text/occi')

        # Test method and Assert exception
        self.assertRaises(Exception, self.som._SOManager__deploy_so,
                          (entity, extras))
示例#9
0
    def get_category(self, svc_kind):

        keystone = client.Client(token=self.token,
                                 tenant_name=self.tenant_name,
                                 auth_url=self.design_uri)

        try:
            svc = keystone.services.find(type=svc_kind.keys()[0])
            svc_ep = keystone.endpoints.find(service_id=svc.id)
        except Exception as e:
            LOG.error('Cannot find the service endpoint of: ' +
                      svc_kind.__repr__())
            raise e

        u = urlparse(svc_ep.publicurl)

        # sort out the OCCI QI path
        if u.path == '/':
            svc_ep.publicurl += '-/'
        elif u.path == '':
            svc_ep.publicurl += '/-/'
        else:
            LOG.warn('Service endpoint URL does not look like it will work: ' +
                     svc_ep.publicurl.__repr__())
            svc_ep.publicurl = u.scheme + '://' + u.netloc + '/-/'
            LOG.warn('Trying with the scheme and net location: ' +
                     svc_ep.publicurl.__repr__())

        heads = {
            'X-Auth-Token': self.token,
            'X-Tenant-Name': self.tenant_name,
            'Accept': 'application/occi+json'
        }

        try:
            r = requests.get(svc_ep.publicurl, headers=heads)
            r.raise_for_status()
        except requests.HTTPError as err:
            LOG.error('HTTP Error: should do something more here!' +
                      err.message)
            raise err

        registry = json.loads(r.content)

        category = None
        for cat in registry:
            if 'related' in cat:
                category = cat

        return Kind(scheme=category['scheme'],
                    term=category['term'],
                    related=category['related'],
                    title=category['title'],
                    attributes=category['attributes'],
                    location=category['location'])
示例#10
0
    def setUp(self):
        self.kind = Kind('http://example.com/foo#', 'bar')
        self.link_kind = Kind('http://example.com/foo#', 'link')
        self.mixin = Mixin('http://example.com/foo#', 'mixin')

        target = Resource('/foo/target', self.kind, [], [])

        source = Resource('/foo/src', self.kind, [self.mixin], [])
        source.attributes = {'foo': 'bar'}

        link = Link('/link/foo', self.link_kind, [], source, target)
        link.attributes = {'foo': 'bar'}
        source.links = [link]

        self.resources = [source, target, link]
        for item in self.resources:
            self.registry.add_resource(item.identifier, item)

        self.registry.set_backend(self.kind, KindBackend())
        self.registry.set_backend(self.mixin, MixinBackend())
示例#11
0
    def create_service_type(self):

        required_occi_kinds = self.get_dependencies()

        svc_scheme = self.stg['service_type'].split('#')[0] + '#'
        svc_term = self.stg['service_type'].split('#')[1]

        return Kind(scheme=svc_scheme,
                    term=svc_term,
                    related=required_occi_kinds,
                    title=self.stg['service_description'],
                    attributes=self.stg['service_attributes'],
                    location='/' + svc_term + '/')
 def setUp(self):
     action = Action('http://example.com/foo#', 'action')
     self.kind = Kind('http://example.com/foo#', 'bar',
                      'http://schemeas.ogf.org/occi/core#',
                       [action], 'Some bla bla',
                       {'foo': 'required', 'foo2': 'immutable', 'bar': ''},
                       '/foo/')
     mixin = Mixin('http://example.com/foo#', 'mixin')
     action = Action('http://example.com/foo#', 'action')
     self.target = Resource('/foo/target', self.kind, [], [])
     self.source = Resource('/foo/src', self.kind, [mixin], [])
     self.link = Link('/link/foo', self.kind, [], self.source, self.target)
     self.source.links = [self.link]
     self.source.actions = [action]
示例#13
0
 def setUp(self):
     kind = Kind('http://schemas.mobile-cloud-networking.eu/occi/sm#',
                 'myservice',
                 title='Test Service',
                 attributes={'mcn.test.attribute1': 'immutable'},
                 related=[Resource.kind],
                 actions=[])
     self.test_entity = Resource('my-id', kind, None)
     self.patcher_system = patch('os.system', return_value=0)
     self.patcher_system.start()
     self.patcher_config = patch('mcn.sm.so_manager.CONFIG')
     self.patcher_config.start()
     self.patcher_log = patch('mcn.sm.so_manager.LOG')
     self.patcher_log.start()
示例#14
0
    def setUp(self):
        self.rendering = TextOcciRendering(self.registry)
        # type system...
        self.kind = Kind('http://example.com#', 'foo', related=[Resource.kind])
        self.invalid_kind = Kind('http://example.com#', 'invalid')
        self.link = Kind('http://example.com#', 'link', related=[Link.kind])
        self.mixin = Mixin('http://example.com#', 'mixin')
        self.action = Action('http://example.com#', 'action')

        self.registry.set_backend(self.kind, KindBackend())
        self.registry.set_backend(self.invalid_kind, KindBackend())
        self.registry.set_backend(self.link, KindBackend())
        self.registry.set_backend(self.mixin, MixinBackend())
        self.registry.set_backend(self.action, ActionBackend())

        # 2 linked entities
        self.entity = Resource('/foo/1', self.kind, [self.mixin])
        trg = Resource('/foo/2', self.kind, [], [])
        self.link1 = Link('/link/1', self.link, [], self.entity, trg)
        self.entity.links = [self.link1]

        self.registry.add_resource('/foo/2', trg)
        self.registry.add_resource('/link/1', self.link1)
        self.registry.add_resource('/foo/1', self.entity)
示例#15
0
 def setUp(self):
     self.patcher_config = patch('mcn.sm.Service.CONFIG')
     self.patcher_config.start()
     self.patcher_config_get = patch('mcn.sm.Service.CONFIG.get', side_effect=self.side_effect_config)
     self.patcher_config_get.start()
     self.patcher_log = patch('mcn.sm.Service.LOG')
     self.patcher_log.start()
     self.patcher_system = patch('os.system', return_value=0)
     self.patcher_system.start()
     self.epc_svc_type = Kind('http://schemas.mobile-cloud-networking.eu/occi/sm#',
                              'epc',
                              title='This is an example EPC service type',
                              attributes={'mcn.endpoint.enodeb': 'immutable',
                                          'mcn.endpoint.mme': 'immutable',
                                          'mcn.endpoint.hss': 'immutable',
                                          'mcn.endpoint.srv-gw': 'immutable',
                                          'mcn.endpoint.pdn-gw': 'immutable',
                              },
                              related=[Resource.kind],
                              actions=[])
示例#16
0
    def test_resource_for_sanity(self):
        '''
        Test is a resource can be rendered and retrieved.
        '''
        # basic check
        headers, body = self.rendering.from_entity(self.entity)
        new = self.rendering.to_entity(headers, body, None)
        self.assertEqual(self.entity.kind, new.kind)
        self.assertEqual(len(self.entity.links), len(new.links))

        # verify that provided kind is taken
        kind = Kind('foo', 'bar', related=[Resource.kind])
        headers, body = self.rendering.from_entity(self.entity)
        new = self.rendering.to_entity(headers, body, kind)
        self.assertEqual(new.kind, kind)
        self.assertEqual(len(self.entity.links), len(new.links))

        # verify that actions get added
        self.entity.actions = [self.action]
        headers, body = self.rendering.from_entity(self.entity)
        self.assertTrue('?action' in headers['Link'])
示例#17
0
    def test_deploy(self, mock_deploy_so, mock_init_so, mock_deploy_app,
                    mock_create_app, mock_ensure_ssh, mcn_log):
        # Initialization
        kind = Kind('http://schemas.mobile-cloud-networking.eu/occi/sm#',
                    'myservice',
                    title='Test Service',
                    attributes={'mcn.test.attribute1': 'immutable'},
                    related=[Resource.kind],
                    actions=[])
        entity = Resource('my-id', kind, None)

        # repo uri is weird
        repo = 'http://[email protected]:myserv.git'
        mock_create_app.return_value = repo

        # Test method
        self.som.deploy(entity, extras={})

        # Asserts
        mock_deploy_app.assert_called_with(repo)
        self.assertEqual(entity.extras['host'], 'git.mcn.eu:myserv.git')
示例#18
0
    def test_type_system_for_sanity(self):
        '''
        Test category, Action, Kind and Mixin.
        '''
        cat1 = Category('http://example.com#', 'foo', '', {}, '')
        cat2 = Category('http://example.com#', 'foo', '', {}, '')
        kind = Kind('http://example.com#', 'bar')
        action = Action('http://example.com#', 'action')
        mixin = Mixin('http://example.com#', 'mixin')

        # test eq
        self.assertEquals(cat1, cat2)
        self.assertFalse(cat1 == str)
        self.assertFalse(cat1 == kind)

        # test str
        self.assertEqual(str(cat1), 'http://example.com#foo')

        # test repr
        self.assertEqual(repr(kind), 'kind')
        self.assertEqual(repr(action), 'action')
        self.assertEqual(repr(mixin), 'mixin')
示例#19
0
    def setUp(self):
        self.kind1 = Kind('http://example.com#', '1')
        self.kind2 = Kind('http://example.com#', '2', location='/foo/')

        self.registry.set_backend(self.kind1, KindBackend())
        self.registry.set_backend(self.kind2, DummyBackend())
示例#20
0
 def test_get_backend_for_failure(self):
     '''
     Test if backend can be retrieved...
     '''
     self.assertRaises(AttributeError, self.registry.get_backend,
                       Kind('foo', 'bar'))
class TestParser(unittest.TestCase):
    '''
    Test for the parser.
    '''

    start_action = Action('http://schemas.ogf.org/occi/infrastructure#',
                          'start')
    compute = Kind('http://schemas.ogf.org/occi/infrastructure#',
                   'compute',
                   title='A OCCI compute...',
                   attributes={
                       'occi.compute.cores': '',
                       'occi.compute.state': 'immutable',
                       'occi.compute.memory': 'required'
                   },
                   related=[Resource.kind],
                   actions=[start_action])
    network_link = Kind('http://schemas.ogf.org/occi/infrastructure#',
                        'networkinterface',
                        related=[Link.kind])

    source = Resource('/1', compute, [])
    target = Resource('/2', compute, [])
    link1 = Link('/link/1', network_link, [], source, target)
    link2 = Link(None, network_link, [], source, target)

    registry = NonePersistentRegistry()

    def setUp(self):
        self.registry.add_resource(self.source.identifier, self.source)
        self.registry.add_resource(self.target.identifier, self.target)

        self.registry.set_backend(self.start_action, None)
        self.registry.set_backend(self.compute, None)
        self.registry.set_backend(self.network_link, None)

        self.link1.attributes = {'foo': 'bar'}

    def tearDown(self):
        for item in self.registry.get_categories():
            self.registry.delete_mixin(item)

    #==========================================================================
    # Success
    #==========================================================================

    def test_get_category_for_success(self):
        '''
        Tests if a mixin can be created.
        '''

        # disabling 'Method could be func' pylint check (pyunit fault :-))
        # pylint: disable=R0201

        # mixin check...
        tmp = 'foo; scheme="http://example.com#"; location="/foo_bar/"'
        parser.get_category(tmp, self.registry.get_categories(), is_mixin=True)

    #==========================================================================
    # Failure
    #==========================================================================

    def test_get_category_for_failure(self):
        '''
        Test with faulty category.
        '''
        self.assertRaises(AttributeError, parser.get_category, 'some crap',
                          self.registry.get_categories())
        self.assertRaises(AttributeError, parser.get_category,
                          'foo; scheme="bar"', self.registry.get_categories())

        # mixin with msg location check...
        tmp = 'foo; scheme="http://example.com#"'
        self.assertRaises(AttributeError, parser.get_category, tmp,
                          self.registry.get_categories(), True)

        # mixin with faulty location check...
        tmp = 'foo; scheme="http://example.com#"; location="sdf"'
        self.assertRaises(AttributeError, parser.get_category, tmp,
                          self.registry.get_categories(), True)

    def test_get_link_for_failure(self):
        '''
        Test with msg category.
        '''
        # no valid string...
        self.assertRaises(AttributeError, parser.get_link, 'some crap', None,
                          self.registry.get_categories())

        # no target...
        link_string = parser.get_link_str(self.link1)
        self.registry.delete_resource(self.target.identifier)
        self.assertRaises(AttributeError, parser.get_link, link_string, None,
                          self.registry)

    def test_get_attributes_for_failure(self):
        '''
        Verifies the parsing of attributes.
        '''
        self.assertRaises(AttributeError, parser.get_attributes, 'bla blub')

    #==========================================================================
    # Sanity
    #==========================================================================

    def test_get_category_for_sanity(self):
        '''
        Simple sanity check...
        '''
        res = parser.get_category(parser.get_category_str(self.compute),
                                  self.registry.get_categories())
        self.assertEqual(res, self.compute)

    def test_get_link_for_sanity(self):
        '''
        Verifies that source and target are set...
        '''
        link_string = parser.get_link_str(self.link1)
        link = parser.get_link(link_string, self.source, self.registry)
        self.assertEquals(link.kind, self.network_link)
        self.assertEquals(link.source, self.source)
        self.assertEquals(link.target, self.target)
        # 4 = 1 attr + core.id + core.src + core.target
        self.assertTrue(len(link.attributes) == 4)

        # identifier checks...
        link_string = parser.get_link_str(self.link1)
        link = parser.get_link(link_string, self.source, self.registry)
        self.assertEquals(link.identifier, '/link/1')

        tmp = link_string.split('; ')
        tmp.pop(2)
        link_string = '; '.join(tmp)
        link = parser.get_link(link_string, self.source, self.registry)
        self.assertEquals(link.identifier, None)

    def test_strip_all_for_sanity(self):
        '''
        Tests if information get's stripped correctly.
        '''
        self.assertEqual('bla', parser._strip_all('bla'))
        self.assertEqual('bla', parser._strip_all('"bla"'))
        self.assertEqual('bla', parser._strip_all(' bla '))
        self.assertEqual('bla', parser._strip_all('"bla'))
        self.assertEqual('bla', parser._strip_all('bla" '))
        self.assertEqual('  bla', parser._strip_all('"  bla" '))
        self.assertEqual('some text', parser._strip_all('"some text" '))

    def test_get_attributes_for_sanity(self):
        '''
        Verifies the parsing of attributes.
        '''
        self.assertEquals(parser.get_attributes('foo=bar'), ('foo', 'bar'))
        self.assertEquals(parser.get_attributes('foo=bar '), ('foo', 'bar'))
        self.assertEquals(parser.get_attributes('foo= "some stuff"'),
                          ('foo', 'some stuff'))
        self.assertEquals(parser.get_attributes('foo = "bar"'), ('foo', 'bar'))
示例#22
0
SUSPEND_ACTION = Action(scheme="http://schemas.ogf.org/occi/sla#",
                        term="suspend",
                        title="Suspend suspend an agreement")

UNSUSPEND_ACTION = Action(scheme="http://schemas.ogf.org/occi/sla#",
                          term="unsuspend",
                          title="Unsuspend a suspened agreement")

AGREEMENT = Kind(
    scheme="http://schemas.ogf.org/occi/sla#",
    term="agreement",
    title="SLA Agreement",
    attributes={
        "occi.agreement.state": "immutable",
        "occi.agreement.agreedAt": "immutable",
        "occi.agreement.effectiveFrom": "mutable",
        "occi.agreement.effectiveUntil": "mutable"
    },
    actions=[ACCEPT_ACTION, REJECT_ACTION, SUSPEND_ACTION, UNSUSPEND_ACTION],
    related=[Resource.kind],
    location="/agreement/")

#  Agreement Link Definition
# ************************************

AGREEMENT_LINK = Kind(scheme="http://schemas.ogf.org/occi/sla#",
                      term="agreement_link",
                      title="SLA Agreement Link",
                      related=[Link.kind],
                      location="/agreement_link/")
示例#23
0
from occi.core_model import Kind
from occi.core_model import Link
from occi.core_model import Mixin
from occi.core_model import Action
from occi.core_model import Resource

#  Violation defintiion
# ************************************

VIOLATION = Kind(scheme="http://schemas.ogf.org/occi/sla#",
                 term="violation",
                 title="SLA Agreement violation",
                 attributes={
                     "occi.violation.timestamp.start": "immutable",
                     "occi.violation.term": "immutable",
                     "occi.violation.metrics": "immutable",
                     "occi.violation.device": "immutable",
                     "occi.violation.remedy": "immutable"
                 },
                 actions=[],
                 related=[Resource.kind],
                 location="/violation/")

#  Agreement Violation Link Definition
# ************************************

VIOLATION_LINK = Kind(scheme="http://schemas.ogf.org/occi/sla#",
                      term="violation_link",
                      title="SLA Violation Link",
                      related=[Link.kind],
                      location="/violation_link/")
示例#24
0
                 'restart', 'Restart a compute resource', {'method': ''})

SUSPEND = Action('http://schemas.ogf.org/occi/infrastructure/compute/action#',
                 'suspend', 'Suspend a compute resource', {'method': ''})

COMPUTE_ATTRIBUTES = {
    'occi.compute.architecture': '',
    'occi.compute.cores': '',
    'occi.compute.hostname': '',
    'occi.compute.speed': '',
    'occi.compute.memory': '',
    'occi.compute.state': 'immutable'
}

COMPUTE = Kind('http://schemas.ogf.org/occi/infrastructure#', 'compute',
               [Resource.kind], [START, STOP, RESTART, SUSPEND],
               'Compute Resource', COMPUTE_ATTRIBUTES, '/compute/')

#==============================================================================
# Network
#==============================================================================

UP = Action('http://schemas.ogf.org/occi/infrastructure/network/action#', 'up',
            'Bring up a network resource')

DOWN = Action('http://schemas.ogf.org/occi/infrastructure/network/action#',
              'down', 'Bring down a network resource')

NETWORK_ATTRIBUTES = {
    'occi.network.vlan': '',
    'occi.network.label': '',