Beispiel #1
0
	def test_empty_contextualize(self):
		radl = """
			system test (
			cpu.count>=1
			)
			
			deploy test 1
			
			contextualize ()
			"""
		r = parse_radl(radl)
		r.check()
		self.assertEqual(r.contextualize.items, {})
		
		radl_json = dump_radl_json(r)
		r = parse_radl_json(radl_json)
		r.check()
		self.assertEqual(r.contextualize.items, {})
		
		radl = """
			system test (
			cpu.count>=1
			)
			
			deploy test 1
			"""
		r = parse_radl(radl)
		r.check()
		self.assertEqual(r.contextualize.items, None)
		
		radl_json = dump_radl_json(r)
		r = parse_radl_json(radl_json)
		r.check()
		self.assertEqual(r.contextualize.items, None)
Beispiel #2
0
	def test_references(self):

		r = parse_radl(TESTS_PATH + "/test_radl_ref.radl")
		self.radl_check(r, [2, 2, 0, 2, 2])
		
		radl_json = dump_radl_json(r)
		r = parse_radl_json(radl_json)
		self.radl_check(r, [2, 2, 0, 2, 2])
Beispiel #3
0
def format_output(res, default_type="text/plain", field_name=None, list_field_name=None):
    """
    Format the output of the API responses
    """
    accept = get_media_type('Accept')

    if accept:
        content_type = None
        for accept_item in accept:
            if accept_item in ["application/json", "application/*"]:
                if isinstance(res, RADL):
                    if field_name:
                        res_dict = {field_name: radlToSimple(res)}
                        info = json.dumps(res_dict)
                    else:
                        info = dump_radl_json(res, enter="", indent="")
                # This is the case of the "contains" properties
                elif isinstance(res, dict) and all(isinstance(x, Feature) for x in res.values()):
                    features = Features()
                    features.props = res
                    res_dict = featuresToSimple(features)
                    if field_name:
                        res_dict = {field_name: res_dict}
                    info = json.dumps(res_dict)
                else:
                    # Always return a complex object to make easier parsing
                    # steps
                    info = format_output_json(res, field_name, list_field_name)
                content_type = "application/json"
                break
            elif accept_item in [default_type, "*/*", "text/*"]:
                if default_type == "application/json":
                    info = format_output_json(res, field_name, list_field_name)
                else:
                    if isinstance(res, list):
                        info = "\n".join(res)
                    else:
                        info = str(res)
                content_type = default_type
                break

        if content_type:
            bottle.response.content_type = content_type
        else:
            return return_error(415, "Unsupported Accept Media Types: %s" % ",".join(accept))
    else:
        if default_type == "application/json":
            info = format_output_json(res, field_name, list_field_name)
        else:
            if isinstance(res, list):
                info = "\n".join(res)
            else:
                info = str(res)
        bottle.response.content_type = default_type

    return info
Beispiel #4
0
def format_output(res, default_type="text/plain", field_name=None, list_field_name=None):
    """
    Format the output of the API responses
    """
    accept = get_media_type('Accept')

    if accept:
        content_type = None
        for accept_item in accept:
            if accept_item in ["application/json", "application/*"]:
                if isinstance(res, RADL):
                    if field_name:
                        res_dict = {field_name: radlToSimple(res)}
                        info = json.dumps(res_dict)
                    else:
                        info = dump_radl_json(res, enter="", indent="")
                # This is the case of the "contains" properties
                elif isinstance(res, dict) and all(isinstance(x, Feature) for x in res.values()):
                    features = Features()
                    features.props = res
                    res_dict = featuresToSimple(features)
                    if field_name:
                        res_dict = {field_name: res_dict}
                    info = json.dumps(res_dict)
                else:
                    # Always return a complex object to make easier parsing
                    # steps
                    info = format_output_json(res, field_name, list_field_name)
                content_type = "application/json"
                break
            elif accept_item in [default_type, "*/*", "text/*"]:
                if default_type == "application/json":
                    info = format_output_json(res, field_name, list_field_name)
                else:
                    if isinstance(res, list):
                        info = "\n".join(res)
                    else:
                        info = str(res)
                content_type = default_type
                break

        if content_type:
            bottle.response.content_type = content_type
        else:
            return return_error(415, "Unsupported Accept Media Types: %s" % ",".join(accept))
    else:
        if default_type == "application/json":
            info = format_output_json(res, field_name, list_field_name)
        else:
            if isinstance(res, list):
                info = "\n".join(res)
            else:
                info = str(res)
        bottle.response.content_type = default_type

    return info
Beispiel #5
0
	def test_ansible_host(self):

		radl = """
ansible ansible_master (host = 'host' and credentials.username = '******' and credentials.password = '******')
network net ()

system main (
ansible_host = 'ansible_master' and
net_interface.0.connection = 'net'
)		"""
		r = parse_radl(radl)
		self.radl_check(r)
		
		radl_json = dump_radl_json(r)
		r = parse_radl_json(radl_json)
		self.radl_check(r)

		radl = """
ansible ansible_master (host = 'host' and credentials.username = '******' and credentials.password = '******')
network net ()

system main (
ansible_host = 'ansible_master1' and
net_interface.0.connection = 'net'
)		"""
		r = parse_radl(radl)
		
		with self.assertRaises(RADLParseException):
			self.radl_check(r)
			
		radl = """
ansible ansible_master (credentials.username = '******' and credentials.password = '******')
network net ()

system main (
net_interface.0.connection = 'net'
)		"""
		r = parse_radl(radl)
		
		with self.assertRaises(RADLParseException):
			self.radl_check(r)
			
		radl = """
ansible ansible_master (host = 'host' and credentials.username = '******')
network net ()

system main (
net_interface.0.connection = 'net'
)		"""
		r = parse_radl(radl)
		
		with self.assertRaises(RADLParseException):
			self.radl_check(r)
Beispiel #6
0
	def test_basic0(self):

		r = parse_radl(TESTS_PATH + "/test_radl_1.radl")
		self.radl_check(r, [2, 2, 0, 0, 0])
		s = r.get_system_by_name("main")
		self.assertEqual(s.getValue("cpu.arch"), "x86_64")
		self.assertEqual(s.getValue("net_interface.0.connection"), "publica")
		
		radl_json = dump_radl_json(r)
		r = parse_radl_json(radl_json)
		self.radl_check(r, [2, 2, 0, 0, 0])
		s = r.get_system_by_name("main")
		self.assertEqual(s.getValue("cpu.arch"), "x86_64")
		self.assertEqual(s.getValue("net_interface.0.connection"), "publica")
Beispiel #7
0
	def test_basic(self):
		r = parse_radl(TESTS_PATH + "/test_radl_0.radl")
		self.radl_check(r, [1, 1, 1, 1, 0])
		s = r.get_system_by_name("cursoaws")
		self.assertIsInstance(s, system)
		self.assertEqual(len(s.features), 17)
		self.assertEqual(s.getValue("disk.0.os.name"), "linux")
		
		radl_json = dump_radl_json(r)
		r = parse_radl_json(radl_json)
		s = r.get_system_by_name("cursoaws")
		self.assertIsInstance(s, system)
		self.assertEqual(len(s.features), 17)
		self.assertEqual(s.getValue("disk.0.os.name"), "linux")
Beispiel #8
0
	def test_concrete(self):

		r = parse_radl(TESTS_PATH + "/test_radl_conc.radl")
		self.radl_check(r)
		s = r.get_system_by_name("main")
		self.assertIsInstance(s, system)
		concrete_s, score = s.concrete()
		self.assertIsInstance(concrete_s, system)
		self.assertEqual(score, 201)
		
		radl_json = dump_radl_json(r)
		r = parse_radl_json(radl_json)
		self.radl_check(r)
		s = r.get_system_by_name("main")
		self.assertIsInstance(s, system)
		concrete_s, score = s.concrete()
		self.assertIsInstance(concrete_s, system)
		self.assertEqual(score, 201)