def CORE_READ002_COMMON(category, links=[]): check_url = True check_200ok = False err_msg = [] headers = [] headers.append('Content-Type: text/occi') headers += renderer_httpheaders.render_category(occi.Category({'term': category['term'], 'scheme': category['scheme'], 'class': category['class']}))[1] body, response_headers, http_status, content_type = connection.get(url=occi_config['url'] + category['location'], headers=headers) try: locations = renderer.parse_locations(body, response_headers) except occi.ParseError as pe: locations = [] check_url = False err_msg.append(str(pe)) links[0:] = locations if re.match(r'^HTTP/.* 200 OK', http_status): check_200ok = True else: err_msg.append('Returned HTTP status is not 200 OK (%s)' % http_status) check_ct2, tmp_err_msg = check_content_type(content_type) err_msg += tmp_err_msg return [check_url and check_ct2 and check_200ok, err_msg]
def test(self=None): err_msg = [] body, response_headers, http_status, content_type, check_pretest = Test.pretest_http_status("200 OK", err_msg) if not check_pretest: return [False, err_msg] #kind = Test.search_category({'class': 'kind'}) kind = Test.search_category({'class': 'kind', 'term': 'compute'}) #print kind if kind: for item in ['location', 'term', 'scheme']: if item not in kind.keys(): err_msg.append('No %s in OCCI Kind' % item) return [False, err_msg] else: err_msg.append('No OCCI Kind found') return [False, err_msg] new_cat_s, new_cat_h = renderer.render_resource( categories=[ occi.Category({ 'term': 'compute', 'scheme': 'http://schemas.ogf.org/occi/infrastructure#', 'class': 'kind', 'title': 'titulek', }), ], attributes=[ occi.Attribute({ 'name': 'occi.core.id', 'value': gen_id('Compute'), }), occi.Attribute({ 'name': 'occi.core.title', 'value': 'titulek', }), occi.Attribute({ 'name': 'occi.core.summary', 'value': 'sumarko', }), occi.Attribute({ 'name': 'occi.compute.architecture', 'value': 'arch', }), ] ) body, response_headers, http_status, content_type = connection.post(url=occi_config['url'] + kind['location'], headers=['Content-Type: %s' % occi_config['mimetype']] + new_cat_h, body=new_cat_s) Test.clear_categories() check_create, tmp_err_msg = check_http_status("201 Created", http_status) err_msg += tmp_err_msg if not check_create: print >> sys.stderr, body return [check_create, err_msg]
def test(self=None): err_msg = [] category = occi.Category({'term': 'compute', 'scheme': 'http://schemas.ogf.org/occi/infrastructure#', 'class': 'kind'}) body, response_headers, http_status, content_type, check_pretest = Test.pretest_http_status("200 OK", err_msg) if not check_pretest: return [False, err_msg] return INFRA_CREATE_COMMON('compute', [category], [], err_msg)
def test(self=None): err_msg = [] category = occi.Category({'term': 'network', 'scheme': 'http://schemas.ogf.org/occi/infrastructure#', 'class': 'kind'}) additional_attributes = [occi.AttributeDefinition({"name": "occi.core.title", "required": True})] body, response_headers, http_status, content_type, check_pretest = Test.pretest_http_status("200 OK", err_msg) if not check_pretest: return [False, err_msg] return INFRA_CREATE_COMMON('network', [category], additional_attributes, err_msg)
def test(self=None): err_msg = [] categories = [ occi.Category({'term': 'compute', 'scheme': 'http://schemas.ogf.org/occi/infrastructure#', 'class': 'kind'}) ] body, response_headers, http_status, content_type, check_pretest = Test.pretest_http_status("200 OK", err_msg) if not check_pretest: return [False, err_msg] # OS template os_tpl = Test.search_category({'class': 'mixin', 'rel': 'http://schemas.ogf.org/occi/infrastructure#os_tpl'}) if occi_config['curlverbose']: print '[OCCI/INFRA/CREATE/004] os_tpl: %s' % str(os_tpl) if not os_tpl: err_msg.append('No OCCI OS Template found') return [False, err_msg] # 'term': 'uuid_ttylinux_0', 'scheme': 'http://occi.myriad5.zcu.cz/occi/infrastructure/os_tpl#', 'class': 'mixin' categories.append(occi.Category({'term': os_tpl['term'], 'scheme': os_tpl['scheme'], 'class': 'mixin'})) if 'attributes' in os_tpl: tpl_attributes = os_tpl['attributes'] else: tpl_attributes = [] # resource template resource_tpl = Test.search_category({'class': 'mixin', 'rel': 'http://schemas.ogf.org/occi/infrastructure#resource_tpl'}) if occi_config['curlverbose']: print '[OCCI/INFRA/CREATE/004] resource_tpl: %s' % str(resource_tpl) if not resource_tpl: err_msg.append('No OCCI Resource Template found') return [False, err_msg] if resource_tpl: categories.append(occi.Category({'term': resource_tpl['term'], 'scheme': resource_tpl['scheme'], 'class': 'mixin'})) if 'attributes' in resource_tpl: tpl_attributes += resource_tpl['attributes'] if occi_config['curlverbose']: print '[OCCI/INFRA/CREATE/004] attributes: %s' % str(tpl_attributes) return INFRA_CREATE_COMMON('compute', categories, tpl_attributes, err_msg)
def test(self=None): err_msg = [] filtered_categories = [] check = True body, response_headers, http_status, content_type, check_pretest = Test.pretest_http_status("200 OK", err_msg) if not Test.categories: err_msg += ['No categories returned'] return [False, err_msg] filter = occi.Category({ 'term': Test.categories[0]['term'], 'scheme': Test.categories[0]['scheme'], 'class': Test.categories[0]['class'], }) cat_in = [] cat_in.append('Content-Type: text/occi') cat_in += renderer_httpheaders.render_category(filter)[1] body, response_headers, http_status, content_type = connection.get(headers=cat_in) check_ct, err_msg = check_content_type(content_type) if not re.match(r'^HTTP/.* 200 OK', http_status): check = False err_msg.append('HTTP status on filter not 200 OK (%s)' % http_status) check_rct, tmp_err_msg = check_requested_content_type(content_type) err_msg += tmp_err_msg try: filtered_categories = renderer.parse_categories(body, response_headers) except occi.ParseError as pe: check = False err_msg.append(str(pe)) check_filter = False for cat in filtered_categories: if match_category(cat, filter): check_filter = True break if not check_filter: err_msg.append('Category "%s" (scheme "%s") not in filtered result' % (filter['term'], filter['scheme'])) return [check and check_pretest and check_ct and check_rct and check_filter, err_msg]
def test(self=None): err_msg = [] body, response_headers, http_status, content_type, check_pretest = Test.pretest_http_status("200 OK", err_msg) category = occi.Category(occi_config['occi.tests.category']) check, err_msg = INFRA_CREATE_COMMON('compute', [category], [], err_msg) if not check: return [False, err_msg] check, err_msg, tmp_urls = CORE_READ_URL(occi_config['occi.tests.category']) if not tmp_urls: err_msg += ["OCCI entity URL not found!"] return [False, err_msg] url = tmp_urls[0] if occi_config['curlverbose']: print '[OCCI/CORE/DELETE/001] entity URL: %s' % str(url) body, response_headers, http_status, content_type = connection.get(url=url) check_exist1, tmp_err_msg = check_http_status("200 OK", http_status) err_msg += tmp_err_msg if occi_config['curlverbose']: print '[OCCI/CORE/DELETE/001] entity body: %s' % str(body) body, response_headers, http_status, content_type = connection.delete(url=url) check_delete1, tmp_err_msg = check_http_status('2\d\d ', http_status) err_msg += tmp_err_msg # It takes some time to delete machine, second delete action force it # Not testing result of the operation (various backends have different behaviour) body, response_headers, http_status, content_type = connection.delete(url=url) body, response_headers, http_status, content_type = connection.get(url=url) check_exist2, tmp_err_msg = check_http_status("404 Not Found", http_status) err_msg += tmp_err_msg Test.clear_categories() return [check_exist1 and check_exist2 and check_delete1, err_msg]
def parse_category_body(self, body): """Parse OCCI Category body Example:: entity;scheme="http://schemas.ogf.org/occi/core#";class="kind";title="entity";location="/entity/";attributes="occi.core.id{immutable required} occi.core.title" :param string body: text to parse :return: OCCI Category :rtype: occi.Category """ category = occi.Category() chunks = TextRenderer.reChunks.split(body) if not chunks[0]: raise occi.ParseError('Invalid format of category, term expected', body) category['term'] = chunks[0] # skip the first chunk (category term) for chunk in chunks[1:]: keyvalue = TextRenderer.reKeyValue.split(chunk, 1) if len(keyvalue) != 2: raise occi.ParseError('Key/value pair expected in category', chunk) key = keyvalue[0] value = keyvalue[1] keymatch = TextRenderer.reKeyCheck.match(key) if keymatch is None: raise occi.ParseError( 'Invalid characters in category property', chunk) # every value quoted, only class has quoting optional valuematch = TextRenderer.reQuoted.match(value) if valuematch is None and key != 'class': raise occi.ParseError( 'Category value not properly quoted or unexpected EOF', chunk) if valuematch: value = valuematch.group(1) # sanity check: there should not be any quotes now if value[0] == '"' or (len(value) >= 2 and value[-1] == '"'): raise occi.ParseError('Unexpected quotes in category', chunk) if key == 'location': if not check_url(value): raise occi.ParseError( 'URL is not valid in OCCI Category location', chunk) category[key] = value elif key == 'scheme': if not check_url(value): raise occi.ParseError( 'URL is not valid in OCCI Category scheme', chunk) category[key] = value elif key == 'attributes': category[key] = self.parse_attribute_defs(value) elif key == 'actions': category[key] = self.parse_actions(value) elif key in ['class', 'title', 'rel']: category[key] = value else: raise occi.ParseError('Unknown key "%s" in category' % key, chunk) if not category.validate(): raise occi.ParseError('Missing fields in OCCI Category', body) return category
def INFRA_CREATE_LINK(resource_name, resource_type): """ Opennebula requires running compute instance. :param string resource_name: Resource Name (storage, network) :param string resource_type: Resource Type (link, interface) :return: status, err_msg :rtype: [bool, string[]] """ err_msg = [] check = True check_link = False compute_links = [] resource_links = [] resourcelink = None attributes = {} attribute_definitions = [] body, response_headers, http_status, content_type, check_pretest = Test.pretest_http_status("200 OK", err_msg) if not check_pretest: return [False, err_msg] compute = Test.search_category({'term': 'compute', 'scheme': 'http://schemas.ogf.org/occi/infrastructure#'}) resource = Test.search_category({'term': resource_name, 'scheme': 'http://schemas.ogf.org/occi/infrastructure#'}) check_read, tmp_err_msg = CORE_READ002_COMMON(category=compute, links=compute_links) if not check_read: check = False err_msg += tmp_err_msg check_read, tmp_err_msg = CORE_READ002_COMMON(category=resource, links=resource_links) if not check_read: check = False err_msg += tmp_err_msg #print resource_links #print compute_links if not resource_links or not compute_links: if not resource_links: err_msg.append('No %s found' % resource_name) if not compute_links: err_msg.append('No compute found') return [False, err_msg] resourcelink = Test.search_category({'term': '%s%s' % (resource_name, resource_type), 'scheme': 'http://schemas.ogf.org/occi/infrastructure#', 'class': 'kind'}) if not resourcelink: err_msg.append('No %slink kind found' % resource_name) return [False, err_msg] #print resourcelink if 'attributes' in resourcelink: attribute_definitions += resourcelink['attributes'] attributes['occi.core.id'] = occi.Attribute({'name': 'occi.core.id', 'value': gen_id('%s%s' % (resource_name.capitalize(), resource_type))}) attributes['occi.core.source'] = occi.Attribute({'name': 'occi.core.source', 'value': compute_links[0]}) attributes['occi.core.target'] = occi.Attribute({'name': 'occi.core.target', 'value': resource_links[0]}) if not get_attributes(attribute_definitions, attributes, err_msg): check = False #print attributes new_resourcelink_s, new_resourcelink_h = renderer.render_resource( categories=[ occi.Category({'term': resourcelink['term'], 'scheme': resourcelink['scheme'], 'class': resourcelink['class']}), ], links=None, attributes=attributes.values() ) body, response_headers, http_status, content_type = connection.post(url=occi_config['url'] + resourcelink['location'], headers=['Content-Type: %s' % occi_config['mimetype']] + new_resourcelink_h, body=new_resourcelink_s) check_create, tmp_err_msg = check_http_status("201 Created", http_status) err_msg += tmp_err_msg if not check_create: print >> sys.stderr, body Test.clear_categories() resource_link = resource_links[0] resource_link_rel = urlparse.urlparse(resource_link).path body, response_headers, http_status, content_type = connection.get(url=compute_links[0]) result_links = [] try: result_categories, result_links, result_attributes = renderer.parse_resource(body, response_headers) except occi.ParseError as pe: err_msg += [str(pe)] #print resource_link #print result_links for l in result_links: # accept both: relative and absolute URI if l['uri'] == resource_link or l['uri'] == resource_link_rel: check_link = True break if not check_link: err_msg += ["OCCI Compute Resource is NOT linked with OCCI %s Resource!" % (resource_name.capitalize())] print >> sys.stderr, body return [check and check_create and check_link, err_msg]
import time import urlparse from occi_libs import * import transport import occi renderer = None renderer_big = None renderer_httpheaders = None connection = None #'Category: offline;scheme="http://schemas.ogf.org/occi/infrastructure/storagelink/action#";class="action";title="deactivate storagelink"' required_categories = [ occi.Category({'scheme': 'http://schemas.ogf.org/occi/core#', 'term': 'entity'}), occi.Category({'scheme': 'http://schemas.ogf.org/occi/core#', 'term': 'resource'}), occi.Category({'scheme': 'http://schemas.ogf.org/occi/core#', 'term': 'link'}), ] example_attributes = { 'occi.core.id': occi.Attribute({'name': 'occi.core.id', 'value': '1'}), 'occi.core.title': occi.Attribute({'name': 'occi.core.title', 'value': 'Test_title_%d' % time.time()}), 'occi.storage.size': occi.Attribute({'name': 'occi.storage.size', 'type': 'number', 'value': 0.1}), 'occi.storage.state': occi.Attribute({'name': 'occi.storage.state', 'type': 'enum', 'value': "inactive"}), 'occi.storagelink.deviceid': occi.Attribute({'name': 'occi.storagelink.deviceid', 'type': 'string', 'value': "/dev/blk0"}) } class Test: """Base class for OCCI compliance tests"""