def setUp(self): """ It is called by child class and it is same method as Avocado/Unittest has. It prepares environment for systemd nspawn based testing * installing dependencies from config * setup environment from config :return: None """ common.trans_dict["ROOT"] = self.chrootpath core.print_info("name of CHROOT directory:", self.chrootpath) self.setRepositoriesAndWhatToInstall() # never move this line to __init__ this localtion can change before setUp (set repositories) self.chrootpath_baseimage = os.path.abspath( self.baseprefix + self.component_name + "_image_" + hashlib.md5(" ".join(self.repos)).hexdigest()) self.__image_base = nspawn.Image(location=self.chrootpath_baseimage, packageset=self.whattoinstallrpm, repos=self.repos, ignore_installed=True) self.__image = self.__image_base.create_snapshot(self.chrootpath) self.__container = nspawn.Container(image=self.__image, name=self.name) self._callSetupFromConfig() self.__container.boot_machine( nspawn_add_option_list=common.conf["nspawn"] ["additional_boot_options"])
def _create_app_by_template(self): """ It creates an application in OpenShift environment by OpenShift template Steps: * oc cluster up * oc create -f <template> -n openshift * oc new-app memcached -p APPLICATION_NAME=memcached :return: """ self._register_docker_to_openshift_registry() self.runHost('oc get is') oc_template_app = self.runHost('oc process -f "%s"' % self.template, verbose=core.is_debug()) self._change_openshift_account() oc_template_create = None try: oc_template_create = self.runHost('oc create -f %s -n %s' % (self.template, self.project_name), verbose=core.is_debug()) except CmdError as cme: core.print_info('oc create -f failed with traceback %s' % cme.message) self.runHost('oc status') self._oc_get_output('all') return False self._change_openshift_account(account=common.get_openshift_user(), password=common.get_openshift_passwd()) template_name = self._get_openshift_template() time.sleep(1) self._create_app(template=template_name) self.runHost('oc status') return True
def prepare_env(self): core.print_info('Loaded config for name: {}'.format( self.config['name'])) self.installTestDependencies() core.print_info( "WARNING: Testing is going to be performed on this machine") pass
def start(self, command="/bin/bash"): """ starts the OpenShift application :param command: Do not use it directly (It is defined in config.yaml) :return: None """ # Clean environment before running tests try: self._app_remove() except Exception as e: core.print_info(e, "OpenShift applications were removed") pass project = self.runHost('oc new-project %s' % self.project_name, ignore_status=True, verbose=core.is_debug()) if self.template is None: if not self._app_exists(): # This part is used for running an application without template or s2i self._create_app() else: core.print_debug(self.template) self._change_openshift_account(account=common.get_openshift_user(), password=common.get_openshift_passwd()) self._remove_apps_from_openshift_resources(common.conf["openshift"]["template"]) if not self._create_app_by_template(): return False # Verify application is really deploy and prepared for testing. if not self._verify_pod(): return False self._get_ip_instance()
def __init__(self, *args, **kwargs): super(AvocadoTest, self).__init__(*args, **kwargs) self.backend = get_backend() self.moduleType = common.get_module_type() self.moduleType_base = common.get_module_type_base() self.moduleProfile = common.get_profile() core.print_info( "Module Type: %s - Backend: %s - Profile: %s" % (self.moduleType, self.moduleType_base, self.moduleProfile))
def tearDown(self): """ Cleanup environment and call also cleanup from config :return: None """ super(ContainerHelper, self).tearDown() if common.get_if_do_cleanup(): core.print_info("To run a command inside a container execute: ", "docker exec %s /bin/bash" % self.docker_id)
def test_smoke(self): self.start() time.sleep(1) session = pexpect.spawn( "telnet %s %s" % (self.ip_address, self.getConfig()['service']['port'])) session.sendline('set Test 0 100 4\r\n\n') session.sendline('JournalDev\r\n\n') core.print_info('Expecting STORED') session.expect('STORED') session.close()
def __stop_service(self): """ Internal method, do not use it anyhow :return: None """ if os.path.exists('/var/run/docker.sock'): core.print_info("Stopping Docker") service_manager = service.ServiceManager() service_manager.stop('docker')
def __stop_openshift_cluster(self): """ Internal method, do not use it anyhow. It stops OpenShift cluster :return: None """ if common.get_openshift_local(): if int(self.__oc_status()) == 0: core.print_info("Stopping OpenShift") self.runHost("oc cluster down", verbose=common.is_not_silent()) else: core.print_info("OpenShift is already stopped.")
def tearDown(self): """ Cleanup environment and call also cleanup from config :return: None """ super(OpenShiftHelper, self).tearDown() try: self._app_remove() except Exception as e: core.print_info(e, "OpenShift application already removed") pass
def __prepare_selinux(self): # disable selinux by default if not turned off if not os.environ.get('MTF_SKIP_DISABLING_SELINUX'): # https://github.com/fedora-modularity/meta-test-family/issues/53 # workaround because systemd nspawn is now working well in F-26 if not os.path.exists(selinux_state_file): core.print_info("Disabling selinux") actual_state = self.runHost("getenforce", ignore_status=True).stdout.strip() with open(selinux_state_file, 'w') as openfile: openfile.write(actual_state) if setseto not in actual_state: self.runHost("setenforce %s" % setseto, verbose=core.is_not_silent(), sudo=True)
def stop(self): """ This method checks if the application is deployed within OpenShift environment and removes service, deployment config and imagestream from OpenShift. :return: None """ self._change_openshift_account(account=common.get_openshift_user(), password=common.get_openshift_passwd()) self._oc_get_output('all') if self._app_exists(): try: self._app_remove() except Exception as e: core.print_info(e, "OpenShift application already removed") pass
def add_insecure_registry(self, registry): """ https://github.com/fedora-modularity/meta-test-family/issues/52 Deprecated: Append registry into inserure registry. :param registry: string cotain value to add to insecure registry variable to config file :return: """ if registry not in open('/etc/sysconfig/docker', 'r').read(): core.print_info("Adding %s to insecure registry" % registry) with open("/etc/sysconfig/docker", "a") as myfile: myfile.write( "INSECURE_REGISTRY='--insecure-registry $REGISTRY %s'" % registry)
def _app_exists(self): """ It checks if an application already exists in OpenShift environment :return: True, application exists False, application does not exist """ oc_status = self.runHost("oc get dc %s -o json" % self.app_name, ignore_status=True) if int(oc_status.exit_status) == 0: core.print_info("Application already exists.") return True oc_pods = self._oc_get_output('pods') # Check if 'items' in json output is empty or not if not oc_pods: return False # check if 'items', which is not empty, in json output contains app_name if not self._check_resource_in_json(oc_pods): return False return True
def tearDown(self): """ cleanup environment after test is finished and call cleanup section in config file :return: None """ if common.get_if_do_cleanup() and not common.get_if_reuse(): try: self.__container.stop() except: pass try: self.__container.rm() except: pass else: core.print_info("tearDown skipped", "running nspawn: %s" % self.name) core.print_info("To connect to a machine use:", "machinectl shell root@%s /bin/bash" % self.name)
def _get_ip_instance(self): """ This method verifies that we can obtain an IP address of the application deployed within OpenShift. :return: True: getting IP address was successful False: getting IP address was not successful """ service = self._oc_get_output('service') try: for svc in service: if svc.get('metadata').get('labels').get('app') == self.app_name: self.ip_address = svc.get('spec').get("clusterIP") common.trans_dict['GUESTIPADDR'] = self.ip_address return True except KeyError as e: core.print_info(e.message) return False except IndexError as e: core.print_info(e.message) return False
def __start_openshift_cluster(self): """ Internal method, do not use it anyhow. It starts OpenShift cluster :return: None """ if common.get_openshift_local(): if int(self.__oc_status()) == 0: core.print_info("Seems like OpenShift is already started.") else: oc_run = self.runHost("oc cluster up", ignore_status=True) core.print_info(oc_run.stdout) core.print_info(oc_run.stderr)
def __cleanup(self): if not os.environ.get('MTF_SKIP_DISABLING_SELINUX'): core.print_info("Turning back selinux to previous state") actual_state = self.runHost("getenforce", ignore_status=True).stdout.strip() if os.path.exists(selinux_state_file): core.print_info("Turning back selinux to previous state") with open(selinux_state_file, 'r') as openfile: stored_state = openfile.readline() if stored_state != actual_state: self.runHost("setenforce %s" % stored_state, ignore_status=True, verbose=core.is_not_silent(), sudo=True) os.remove(selinux_state_file) else: core.print_info("Selinux state is not stored, skipping.")
def prepare_env(self): core.print_info('Loaded config for name: {}'.format( self.config['name'])) self.__start_openshift_cluster()
def prepare_env(self): core.print_info('Loaded config for name: {}'.format( self.config['name'])) self.installTestDependencies() self.__install_env() self.__start_service()
def cleanup_env(self): core.print_info("WARNING: No cleanup as it can destroy this machine") pass
def prepare_env(self): core.print_info('Loaded config for name: {}'.format(self.config['name'])) self.installTestDependencies() self.__prepare_selinux() self.__install_machined()