def test_schema_crud(a_class): a_class.create() f1 = Class.SchemaField(name='foo') f2 = Class.SchemaField(name='bar') f3 = Class.SchemaField(name='baz') a_class.edit_schema(add_fields=(f1, f2)) a_class.edit_schema(remove_fields=(f1, ), add_fields=(f3, ))
def test_add_class_inherited(a_class): subclass = Class(name=generate_random_string(8), namespace=a_class.namespace, description="subclass", inherits_from=a_class) a_class.create() subclass.create()
def original_class(request, original_domain): # take the Request class and copy it for own purposes. cls = Class( name="Request", namespace=Namespace(name="System", parent=Domain(name="ManageIQ (Locked)"))) cls = cls.copy_to(original_domain) request.addfinalizer(lambda: cls.delete() if cls.exists() else None) return cls
def _make_class(domain): name = fauxfactory.gen_alphanumeric(8) description = fauxfactory.gen_alphanumeric(32) cls = Class(name=name, description=description, namespace=_make_namespace(domain)) cls.create() return cls
def _make_class(): name = generate_random_string(8) description = generate_random_string(32) cls = Class(name=name, description=description, namespace=_make_namespace()) cls.create() return cls
def original_method(request, original_method_write_data, original_domain): method = Method(name=fauxfactory.gen_alphanumeric(), data=METHOD_TORSO.format(original_method_write_data), cls=Class(name="Request", namespace=Namespace(name="System", parent=original_domain), setup_schema=[ Class.SchemaField(name="meth5", type_="Method") ])) method.create() request.addfinalizer(lambda: method.delete() if method.exists() else None) return method
def test_same_class_name_different_namespace(a_namespace): other_namespace = _make_namespace() name = generate_random_string(8) cls1 = Class(name=name, namespace=a_namespace) cls2 = Class(name=name, namespace=other_namespace) cls1.create() cls2.create() # delete one and check the other still exists cls1.delete() assert cls2.exists()
def test_same_class_name_different_namespace(make_namespace): other_namespace = ta.make_namespace() name = fauxfactory.gen_alphanumeric(8) cls1 = Class(name=name, namespace=make_namespace) cls2 = Class(name=name, namespace=other_namespace) cls1.create() cls2.create() # delete one and check the other still exists cls1.delete() assert cls2.exists()
def test_create_snapshot_via_ae(request, domain, test_vm): """This test checks whether the vm.create_snapshot works in AE. Prerequisities: * A VMware provider * A VM that has been discovered by CFME Steps: * Clone the Request class inside the System namespace into a new domain * Add a method named ``snapshot`` and insert the provided code there. * Add an instance named ``snapshot`` and set the methd from previous step as ``meth5`` * Run the simulation of the method against the VM, preferably setting ``snap_name`` to something that can be checked * Wait until snapshot with such name appears. """ # PREPARE file = data_path.join("ui").join("automate").join( "test_create_snapshot_via_ae.rb") with file.open("r") as f: method_contents = f.read() miq_domain = Domain("ManageIQ (Locked)") miq_class = Class("Request", namespace=Namespace("System", domain=miq_domain)) request_cls = miq_class.copy_to(domain) request.addfinalizer(request_cls.delete) method = Method("snapshot", data=method_contents, cls=request_cls) method.create() request.addfinalizer(method.delete) instance = Instance("snapshot", values={"meth5": "snapshot"}, cls=request_cls) instance.create() request.addfinalizer(instance.delete) # SIMULATE snap_name = fauxfactory.gen_alpha() snapshot = Vm.Snapshot(name=snap_name, parent_vm=test_vm) simulate(instance="Request", request="snapshot", attribute=["VM and Instance", test_vm.name], execute_methods=True, avp={"snap_name": snap_name}) wait_for(snapshot.does_snapshot_exist, timeout="2m", delay=10) # Clean up if it appeared snapshot.delete()
def copy_instance(request, copy_domain): miq_domain = Domain(name="ManageIQ (Locked)", enabled=True) instance = Instance(name="InspectMe", cls=Class(name="Request", namespace=Namespace(name="System", parent=miq_domain))) instance.copy_to(copy_domain)
def original_instance(request, original_method, original_domain): instance = Instance(name=fauxfactory.gen_alphanumeric(), values={"meth5": { "value": original_method.name }}, cls=Class(name="Request", namespace=Namespace(name="System", parent=original_domain), setup_schema=[ Class.SchemaField(name="meth5", type_="Method") ])) instance.create() request.addfinalizer(lambda: instance.delete() if instance.exists() else None) return instance
def create_method(request, copy_domain): method = Method(name="InspectMe", data=METHOD_TORSO, cls=Class(name="Request", namespace=Namespace(name="System", parent=copy_domain))) method.create() return method
def test_create_snapshot_via_ae(request, domain, test_vm): """This test checks whether the vm.create_snapshot works in AE. Prerequisities: * A VMware provider * A VM that has been discovered by CFME Steps: * Clone the Request class inside the System namespace into a new domain * Add a method named ``snapshot`` and insert the provided code there. * Add an instance named ``snapshot`` and set the methd from previous step as ``meth5`` * Run the simulation of the method against the VM, preferably setting ``snap_name`` to something that can be checked * Wait until snapshot with such name appears. """ # PREPARE file = data_path.join("ui").join("automate").join("test_create_snapshot_via_ae.rb") with file.open("r") as f: method_contents = f.read() miq_domain = Domain("ManageIQ (Locked)") miq_class = Class("Request", namespace=Namespace("System", domain=miq_domain)) request_cls = miq_class.copy_to(domain) request.addfinalizer(request_cls.delete) method = Method("snapshot", data=method_contents, cls=request_cls) method.create() request.addfinalizer(method.delete) instance = Instance("snapshot", values={"meth5": "snapshot"}, cls=request_cls) instance.create() request.addfinalizer(instance.delete) # SIMULATE snap_name = fauxfactory.gen_alpha() snapshot = Vm.Snapshot(name=snap_name, parent_vm=test_vm) simulate( instance="Request", request="snapshot", attribute=["VM and Instance", test_vm.name], execute_methods=True, avp={"snap_name": snap_name}, ) wait_for(snapshot.does_snapshot_exist, timeout="2m", delay=10) # Clean up if it appeared snapshot.delete()
def cls(request, domain, namespace): tcls = Class( name="Request", namespace=namespace, setup_schema=[Class.SchemaField(name="rel5", type_="Relationship")]) tcls.create() request.addfinalizer(lambda: tcls.delete() if tcls.exists() else None) return tcls
def test_add_vm_to_service(myservice, request, copy_domain): """Tests adding vm to service Metadata: test_flag: provision """ method_torso = """ def add_to_service vm = $evm.root['vm'] service = $evm.vmdb('service').find_by_name('%s') user = $evm.root['user'] if service && vm $evm.log('info', "XXXXXXXX Attaching Service to VM: [#{service.name}][#{vm.name}]") vm.add_to_service(service) vm.owner = user if user vm.group = user.miq_group if user end end $evm.log("info", "Listing Root Object Attributes:") $evm.log("info", "===========================================") add_to_service """ % myservice.service_name method = Method( name="InspectMe", data=method_torso, cls=Class( name="Request", namespace=Namespace( name="System", parent=copy_domain ) ) ) method.create() request.addfinalizer(lambda: method.delete() if method.exists() else None) simulate( instance="Request", message="create", request=method.name, attribute=["VM and Instance", "auto_test_services"], # Random selection, does not matter execute_methods=True ) myservice.check_vm_add("auto_test_services") request.addfinalizer(lambda: myservice.delete(myservice.service_name))
def a_class(a_namespace): return Class(name=generate_random_string(8), description=generate_random_string(32), namespace=a_namespace)
def cls(request, domain, namespace): tcls = Class(name="Request", namespace=namespace, setup_schema=[Class.SchemaField(name="rel5", type_="Relationship")]) tcls.create() request.addfinalizer(lambda: tcls.delete() if tcls.exists() else None) return tcls
def setup_for_event_testing(ssh_client, db, listener_info, providers): # FIX THE ENV ERROR IF PRESENT if ssh_client.run_command("ruby -v")[0] != 0: success = ssh_client.run_command("echo 'source /etc/default/evm' >> .bashrc")[0] == 0 assert success, "Issuing the patch command was unsuccessful" # Verify it works assert ssh_client.run_command("ruby -v")[0] == 0, "Patch failed" # IMPORT AUTOMATE NAMESPACE qe_automate_namespace_xml = "qe_event_handler.xml" qe_automate_namespace_script = "qe_event_handler.rb" local_automate_script = local(__file__)\ .new(basename="../data/%s" % qe_automate_namespace_script)\ .strpath local_automate_file = local(__file__)\ .new(basename="../data/%s" % qe_automate_namespace_xml)\ .strpath tmp_automate_file = "/tmp/%s" % qe_automate_namespace_xml # Change the information with open(local_automate_file, "r") as input_xml, \ open(tmp_automate_file, "w") as output_xml: tree = etree.parse(input_xml) root = tree.getroot() def set_text(xpath, text): field = root.xpath(xpath) assert len(field) == 1 field[0].text = text set_text("//MiqAeSchema/MiqAeField[@name='url']", re.sub(r"^http://([^/]+)/?$", "\\1", listener_info.host)) set_text("//MiqAeSchema/MiqAeField[@name='port']", str(listener_info.port)) # Put the custom script from an external file with open(local_automate_script, "r") as script: set_text("//MiqAeMethod[@name='relay_events']", etree.CDATA(script.read())) et = etree.ElementTree(root) et.write(output_xml) # copy xml file to appliance # but before that, let's check whether it's there because we may have already applied this file if ssh_client.run_command("ls /root/%s" % qe_automate_namespace_xml)[0] != 0: ssh_client.put_file(tmp_automate_file, '/root/') # We have to convert it first for new version convert_cmd = version.pick({ "default": None, "5.3.0.0": "evm:automate:convert DOMAIN=Default FILE=/root/{} ZIP_FILE=/root/{}.zip".format( qe_automate_namespace_xml, qe_automate_namespace_xml), }) if convert_cmd is not None: logger.info("Converting namespace for use on newer appliance...") return_code, stdout = ssh_client.run_rake_command(convert_cmd) if return_code != 0: logger.error("Namespace conversion was unsuccessful") logger.error(stdout) # We didn't successfully do that so remove the file to know # that it's needed to do it again when run again ssh_client.run_command("rm -f /root/%s*" % qe_automate_namespace_xml) raise AutomateImportError(stdout) # run rake cmd on appliance to import automate namespace rake_cmd = version.pick({ "default": "evm:automate:import FILE=/root/{}".format(qe_automate_namespace_xml), "5.3.0.0": "evm:automate:import ZIP_FILE=/root/{}.zip DOMAIN=Default OVERWRITE=true " "PREVIEW=false".format(qe_automate_namespace_xml), }) logger.info("Importing the QE Automation namespace ...") return_code, stdout = ssh_client.run_rake_command(rake_cmd) if return_code != 0: logger.error("Namespace import was unsuccessful") logger.error(stdout) # We didn't successfully do that so remove the file to know # that it's needed to do it again when run again ssh_client.run_command("rm -f /root/%s*" % qe_automate_namespace_xml) raise AutomateImportError(stdout) # CREATE AUTOMATE INSTANCE HOOK if db is None or db.session.query(db['miq_ae_instances'].name)\ .filter(db['miq_ae_instances'].name == "RelayEvents").count() == 0: # Check presence instance = Instance( name="RelayEvents", display_name="RelayEvents", description="relationship hook to link to custom QE events relay namespace", values={ "rel2": { "value": "/QE/Automation/APIMethods/relay_events?event=$evm.object['event']" } }, cls=Class(name="Automation Requests (Request)", namespace=Namespace("System")) ) instance.create() # IMPORT POLICIES policy_yaml = "profile_relay_events.yaml" policy_path = local(__file__).new(basename="../data/%s" % policy_yaml) if not is_imported("Automate event policies"): import_file(policy_path.strpath) # ASSIGN POLICY PROFILES for provider in providers: prov_obj = get_from_config(provider) prov_obj.assign_policy_profiles("Automate event policies") flash.assert_no_errors()
def setup_for_event_testing(ssh_client, db, listener_info, providers): domain_name = "EventTesting" domain = Domain(name=domain_name, enabled=True) if not domain.exists(): domain.create() # FIX THE ENV ERROR IF PRESENT if ssh_client.run_command("ruby -v")[0] != 0: logger.info("Pathing env to correctly source EVM environment") success = ssh_client.run_command("echo 'source /etc/default/evm' >> .bashrc")[0] == 0 assert success, "Issuing the patch command was unsuccessful" # Verify it works assert ssh_client.run_command("ruby -v")[0] == 0, "Patch failed" # INSTALL REST-CLIENT - REQUIRED FOR THE EVENT DISPATCHER SCRIPT if ssh_client.run_rails_command("\"require 'rest-client'\"")[0] != 0: # We have to install the gem logger.info("Installing rest-client ruby gem that is required by the event dispatcher.") success = ssh_client.run_command("gem install rest-client")[0] == 0 assert success, "Could not install 'rest-client' gem" # Verify it works assert ssh_client.run_rails_command("\"require 'rest-client'\"")[0] == 0 # IMPORT AUTOMATE NAMESPACE qe_automate_namespace_xml = "qe_event_handler.xml" qe_automate_namespace_script = "qe_event_handler.rb" local_automate_script = local(__file__)\ .new(basename="../data/{}".format(qe_automate_namespace_script))\ .strpath local_automate_file = local(__file__)\ .new(basename="../data/{}".format(qe_automate_namespace_xml))\ .strpath tmp_automate_file = "/tmp/{}".format(qe_automate_namespace_xml) # Change the information with open(local_automate_file, "r") as input_xml, \ open(tmp_automate_file, "w") as output_xml: tree = etree.parse(input_xml) root = tree.getroot() def set_text(xpath, text): field = root.xpath(xpath) assert len(field) == 1 field[0].text = text set_text("//MiqAeSchema/MiqAeField[@name='url']", re.sub(r"^http://([^/]+)/?$", "\\1", listener_info.host)) set_text("//MiqAeSchema/MiqAeField[@name='port']", str(listener_info.port)) # Put the custom script from an external file with open(local_automate_script, "r") as script: set_text("//MiqAeMethod[@name='relay_events']", etree.CDATA(script.read())) et = etree.ElementTree(root) et.write(output_xml) # copy xml file to appliance # but before that, let's check whether it's there because we may have already applied this file if ssh_client.run_command("ls /root/{}".format(qe_automate_namespace_xml))[0] != 0: ssh_client.put_file(tmp_automate_file, '/root/') # We have to convert it first for new version convert_cmd = version.pick({ version.LOWEST: None, "5.3.0.0": "evm:automate:convert DOMAIN={} FILE=/root/{} ZIP_FILE=/root/{}.zip".format( domain_name, qe_automate_namespace_xml, qe_automate_namespace_xml), }) if convert_cmd is not None: logger.info("Converting namespace for use on newer appliance...") return_code, stdout = ssh_client.run_rake_command(convert_cmd) if return_code != 0: logger.error("Namespace conversion was unsuccessful") logger.error(stdout) # We didn't successfully do that so remove the file to know # that it's needed to do it again when run again ssh_client.run_command("rm -f /root/{}*".format(qe_automate_namespace_xml)) raise AutomateImportError(stdout) # run rake cmd on appliance to import automate namespace rake_cmd = version.pick({ version.LOWEST: "evm:automate:import FILE=/root/{}".format(qe_automate_namespace_xml), "5.3.0.0": "evm:automate:import ZIP_FILE=/root/{}.zip DOMAIN={} OVERWRITE=true " "PREVIEW=false".format(qe_automate_namespace_xml, domain_name), }) logger.info("Importing the QE Automation namespace ...") return_code, stdout = ssh_client.run_rake_command(rake_cmd) if return_code != 0: logger.error("Namespace import was unsuccessful") logger.error(stdout) # We didn't successfully do that so remove the file to know # that it's needed to do it again when run again ssh_client.run_command("rm -f /root/{}*".format(qe_automate_namespace_xml)) raise AutomateImportError(stdout) # CREATE AUTOMATE INSTANCE HOOK if db is None or db.session.query(db['miq_ae_instances'].name)\ .filter(db['miq_ae_instances'].name == "RelayEvents").count() == 0: original_class = Class( name=version.pick({ version.LOWEST: "Automation Requests (Request)", "5.3": "Request" }), namespace=Namespace("System", domain=Domain("ManageIQ (Locked)"))) copied_class = original_class.copy_to(domain) instance = Instance( name="RelayEvents", display_name="RelayEvents", description="relationship hook to link to custom QE events relay namespace", values={ "rel2": { "value": "/QE/Automation/APIMethods/relay_events?event=$evm.object['event']" } }, cls=copied_class, ) instance.create() # IMPORT POLICIES policy_yaml = "profile_relay_events.yaml" policy_path = local(__file__).new(basename="../data/{}".format(policy_yaml)) if not is_imported("Automate event policies"): import_file(policy_path.strpath) # ASSIGN POLICY PROFILES for provider in providers: prov_obj = get_crud(provider) if not prov_obj.exists: prov_obj.create() prov_obj.assign_policy_profiles("Automate event policies") flash.assert_no_errors()
def cls(request, domain): original_class = Class(name='Request', namespace=Namespace( name='System', domain=Domain(name='ManageIQ (Locked)'))) return original_class.copy_to(domain)
def cls(request, domain): original_class = Class( name='Request', namespace=Namespace(name='System', domain=Domain(name='ManageIQ (Locked)'))) return original_class.copy_to(domain)
def a_class(ns=None, request=None): if not ns: ns = make_namespace(request=request) return Class(name=fauxfactory.gen_alphanumeric(8), description=fauxfactory.gen_alphanumeric(32), namespace=ns)