def do_import(self): print("doing import, name=%s, desc=%s, path=%s" % (name, desc, path)) (data, rc) = utils.subprocess_sp( None, ["cobbler", "import", "--name=test-%s" % name, "--path=%s" % path], shell=False) print(data) self.assertEqual(rc, 0) # TODO: scan output of import to build list of imported distros/profiles # and compare to expected list. Then use that list to run reports # and for later cleanup (data, rc) = utils.subprocess_sp( None, ["cobbler", "distro", "report", "--name=test-%s" % name], shell=False) print(data) self.assertEqual(rc, 0) (data, rc) = utils.subprocess_sp( None, ["cobbler", "profile", "report", "--name=test-%s" % name], shell=False) print(data) self.assertEqual(rc, 0) (data, rc) = utils.subprocess_sp(None, [ "cobbler", "distro", "remove", "--recursive", "--name=test-%s" % name ], shell=False) print(data) self.assertEqual(rc, 0)
def test_09_cobbler_reposync(self): """Runs 'cobbler reposync'""" (data,rc) = utils.subprocess_sp(None,["cobbler","reposync"],shell=False) self.assertEqual(rc,0) (data,rc) = utils.subprocess_sp(None,["cobbler","reposync","--tries=3"],shell=False) self.assertEqual(rc,0) (data,rc) = utils.subprocess_sp(None,["cobbler","reposync","--no-fail"],shell=False) self.assertEqual(rc,0)
def test_09_cobbler_reposync(self): """Runs 'cobbler reposync'""" (data, rc) = utils.subprocess_sp(None, ["cobbler", "reposync"], shell=False) self.assertEqual(rc, 0) (data, rc) = utils.subprocess_sp(None, ["cobbler", "reposync", "--tries=3"], shell=False) self.assertEqual(rc, 0) (data, rc) = utils.subprocess_sp(None, ["cobbler", "reposync", "--no-fail"], shell=False) self.assertEqual(rc, 0)
def report_distro(name): """ Asks the cobbler cli about a report for the given distribution. :param name: Name of the distribution. :return: A touple of the data which is returned by the cobbler-cli-client and the status. """ return utils.subprocess_sp(None, ["cobbler", "distro", "report", "--name=test-%s" % name], shell=False)
def report_profile(name): """ Asks the cobbler cli about a report for a given profile. :param name: Name of the profile. :return: A touple of the data which is returned by the cobbler-cli-client and the status. """ return utils.subprocess_sp(None, ["cobbler", "profile", "report", "--name=test-%s" % name], shell=False)
def remove_distro(name): """ Performs a remove for a distribution via the cobbler-cli for a given distribution. :param name: Name of the distribution. :return: A touple of the data which is returned by the cobbler-cli-client and the status. """ return utils.subprocess_sp(None, ["cobbler", "distro", "remove", "--recursive", "--name=test-%s" % name], shell=False)
def do_import(self): print "doing import, name=%s, desc=%s, path=%s" % (name,desc,path) (data,rc) = utils.subprocess_sp(None,["cobbler","import","--name=test-%s" % name,"--path=%s" % path],shell=False) print data self.assertEqual(rc,0) # TODO: scan output of import to build list of imported distros/profiles # and compare to expected list. Then use that list to run reports # and for later cleanup (data,rc) = utils.subprocess_sp(None,["cobbler","distro","report","--name=test-%s" % name],shell=False) print data self.assertEqual(rc,0) (data,rc) = utils.subprocess_sp(None,["cobbler","profile","report","--name=test-%s" % name],shell=False) print data self.assertEqual(rc,0) (data,rc) = utils.subprocess_sp(None,["cobbler","distro","remove","--name=test-%s" % name],shell=False) print data self.assertEqual(rc,0)
def import_distro(name, path): """ Imports a distribution with the cobbler cli-command into a running system. :param name: Name of the distro. :param path: Path to the distro. :return: A touple of the data which is returned by the cobbler-cli-client and the status. """ return utils.subprocess_sp(None, ["cobbler", "import", "--name=test-%s" % name, "--path=%s" % path], shell=False)
def tearDown(self): """ Cleanup here """ for d in self.imported_distros: try: (data,rc) = utils.subprocess_sp(None,["cobbler","distro","remove","--name=%s" % d],shell=False) except: print "Failed to remove distro '%s' during cleanup" % d
def report_distro(name): """ Asks the cobbler cli about a report for the given distribution. :param name: Name of the distribution. :return: A touple of the data which is returned by the cobbler-cli-client and the status. """ return utils.subprocess_sp( None, ["cobbler", "distro", "report", "--name=test-%s" % name], shell=False)
def report_profile(name): """ Asks the cobbler cli about a report for a given profile. :param name: Name of the profile. :return: A touple of the data which is returned by the cobbler-cli-client and the status. """ return utils.subprocess_sp( None, ["cobbler", "profile", "report", "--name=test-%s" % name], shell=False)
def test_subprocess_sp(): # Arrange # Act result_out, result_rc = utils.subprocess_sp("echo Test") # Assert # The newline makes sense in my (@SchoolGuy) eyes since we want to have multiline output also in a single string. assert result_out == "Test\n" assert result_rc == 0
def remove_distro(name): """ Performs a remove for a distribution via the cobbler-cli for a given distribution. :param name: Name of the distribution. :return: A touple of the data which is returned by the cobbler-cli-client and the status. """ return utils.subprocess_sp(None, [ "cobbler", "distro", "remove", "--recursive", "--name=test-%s" % name ], shell=False)
def tearDown(self): """ Cleanup here """ for d in self.imported_distros: try: (data, rc) = utils.subprocess_sp( None, ["cobbler", "distro", "remove", "--name=%s" % d], shell=False) except: print "Failed to remove distro '%s' during cleanup" % d
def import_distro(name, path): """ Imports a distribution with the cobbler cli-command into a running system. :param name: Name of the distro. :param path: Path to the distro. :return: A touple of the data which is returned by the cobbler-cli-client and the status. """ return utils.subprocess_sp( None, ["cobbler", "import", "--name=test-%s" % name, "--path=%s" % path], shell=False)
def run_cmd(cmd): """ Run a command @param cmd str command @return str output @raise Exception if return code is not 0 """ print("run cmd: %s" % cmd) args = shlex.split(cmd) (output, rc) = utils.subprocess_sp(None, args, shell=False) if rc != 0: raise Exception return output
def test_08_cobbler_acl_removegroup(self): """Runs 'cobbler aclsetup --removegroup'""" (data, rc) = utils.subprocess_sp( None, ["cobbler", "aclsetup", "--removegroup=cobbler"], shell=False) self.assertEqual(rc, 0)
def test_05_cobbler_acl_adduser(self): """Runs 'cobbler aclsetup --adduser'""" (data, rc) = utils.subprocess_sp( None, ["cobbler", "aclsetup", "--adduser=cobbler"], shell=False) self.assertEqual(rc, 0)
def test_04_cobbler_signature_update(self): """Runs 'cobbler signature update'""" (data, rc) = utils.subprocess_sp(None, ["cobbler", "signature", "update"], shell=False) self.assertEqual(rc, 0)
def test_03_cobbler_signature_report(self): """Runs 'cobbler signature report'""" (data, rc) = utils.subprocess_sp(None, ["cobbler", "signature", "report"], shell=False) self.assertEqual(rc, 0)
def test_05_cobbler_acl_adduser(self): """Runs 'cobbler aclsetup --adduser'""" (data,rc) = utils.subprocess_sp(None,["cobbler","aclsetup","--adduser=vagrant"],shell=False) self.assertEqual(rc,0)
def _power(self, system, power_operation: str, user: Optional[str] = None, password: Optional[str] = None, logger=None) -> Optional[bool]: """ Performs a power operation on a system. Internal method :param system: Cobbler system :type system: System :param power_operation: power operation. Valid values: on, off, status. Rebooting is implemented as a set of 2 operations (off and on) in a higher level method. :param user: power management user. If user and password are not supplied, environment variables COBBLER_POWER_USER and COBBLER_POWER_PASS will be used. :param password: power management password :param logger: logger :type logger: Logger :return: bool/None if power operation is 'status', return if system is on; otherwise, return None :raise CX: if there are errors """ if logger is None: logger = self.logger power_command = get_power_command(system.power_type) if not power_command: utils.die(logger, "no power type set for system") power_info = { "type": system.power_type, "address": system.power_address, "user": system.power_user, "id": system.power_id, "options": system.power_options, "identity_file": system.power_identity_file } logger.info("cobbler power configuration is: %s" % json.dumps(power_info)) # if no username/password data, check the environment if not system.power_user and not user: user = os.environ.get("COBBLER_POWER_USER", "") if not system.power_pass and not password: password = os.environ.get("COBBLER_POWER_PASS", "") power_input = self._get_power_input(system, power_operation, logger, user, password) logger.info("power command: %s" % power_command) logger.info("power command input: %s" % power_input) rc = -1 for x in range(0, POWER_RETRIES): output, rc = utils.subprocess_sp(logger, power_command, shell=False, input=power_input) # Allowed return codes: 0, 1, 2 # Source: https://github.com/ClusterLabs/fence-agents/blob/master/doc/FenceAgentAPI.md#agent-operations-and-return-values if power_operation in ("on", "off", "reboot"): if rc == 0: return None elif power_operation == "status": if rc in (0, 2): match = re.match(r'^(Status:|.+power\s=)\s(on|off)$', output, re.IGNORECASE | re.MULTILINE) if match: power_status = match.groups()[1] if power_status.lower() == 'on': return True else: return False error_msg = "command succeeded (rc=%s), but output ('%s') was not understood" % ( rc, output) utils.die(logger, error_msg) raise CX(error_msg) time.sleep(2) if not rc == 0: error_msg = "command failed (rc=%s), please validate the physical setup and cobbler config" % rc utils.die(logger, error_msg) raise CX(error_msg)
def test_07_cobbler_acl_removeuser(self): """Runs 'cobbler aclsetup --removeuser'""" (data,rc) = utils.subprocess_sp(None,["cobbler","aclsetup","--removeuser=cobbler"],shell=False) self.assertEqual(rc,0)
def test_01_cobbler_status(self): """Runs 'cobbler status'""" (data,rc) = utils.subprocess_sp(None,["cobbler","status"],shell=False) self.assertEqual(rc,0)
def test_00_cobbler_version(self): """Runs 'cobbler version'""" (data,rc) = utils.subprocess_sp(None,["cobbler","version"],shell=False) self.assertEqual(rc,0)
def test_04_cobbler_signature_update(self): """Runs 'cobbler signature update'""" (data,rc) = utils.subprocess_sp(None,["cobbler","signature","update"],shell=False) self.assertEqual(rc,0)
def test_03_cobbler_signature_report(self): """Runs 'cobbler signature report'""" (data,rc) = utils.subprocess_sp(None,["cobbler","signature","report"],shell=False) self.assertEqual(rc,0)
def _power(self, system, power_operation, user=None, password=None, logger=None): """ Performs a power operation on a system. Internal method @param System system Cobbler system @param str power_operation power operation. Valid values: on, off, status. Rebooting is implemented as a set of 2 operations (off and on) in a higher level method. @param str user power management user. If user and password are not supplied, environment variables COBBLER_POWER_USER and COBBLER_POWER_PASS will be used. @param str password power management password @param Logger logger logger @return bool/None if power operation is 'status', return if system is on; otherwise, return None @raise CX if there are errors """ if logger is None: logger = self.logger power_command = get_power_command(system.power_type) if not power_command: utils.die(logger, "no power type set for system") meta = utils.blender(self.api, False, system) meta["power_mode"] = power_operation logger.info("cobbler power configuration is:") logger.info(" type : %s" % system.power_type) logger.info(" address: %s" % system.power_address) logger.info(" user : %s" % system.power_user) logger.info(" id : %s" % system.power_id) logger.info(" options: %s" % system.power_options) logger.info("identity_file: %s" % system.power_identity_file) # if no username/password data, check the environment if not system.power_user and not user: user = os.environ.get("COBBLER_POWER_USER", "") if not system.power_pass and not password: password = os.environ.get("COBBLER_POWER_PASS", "") power_input = self._get_power_input(system, power_operation, logger, user, password) logger.info("power command: %s" % power_command) logger.info("power command input: %s" % power_input) for x in range(0, POWER_RETRIES): output, rc = utils.subprocess_sp(logger, power_command, shell=False, input=power_input) # fencing agent returns 2 if the system is powered off if rc == 0 or (rc == 2 and power_operation == 'status'): # If the desired state is actually a query for the status # return different information than command return code if power_operation == 'status': match = re.match(r'^(Status:|.+power\s=)\s(on|off)$', output, re.IGNORECASE | re.MULTILINE) if match: power_status = match.groups()[1] if power_status.lower() == 'on': return True else: return False error_msg = "command succeeded (rc=%s), but output ('%s') was not understood" % (rc, output) utils.die(logger, error_msg) raise CX(error_msg) return None else: time.sleep(2) if not rc == 0: error_msg = "command failed (rc=%s), please validate the physical setup and cobbler config" % rc utils.die(logger, error_msg) raise CX(error_msg)
def test_00_cobbler_version(self): """Runs 'cobbler version'""" (data, rc) = utils.subprocess_sp(None, ["cobbler", "version"], shell=False) self.assertEqual(rc, 0)
def test_02_cobbler_sync(self): """Runs 'cobbler sync'""" (data,rc) = utils.subprocess_sp(None,["cobbler","sync"],shell=False) self.assertEqual(rc,0)
def test_A_00_check_distros_available(self): """Validating distros location""" (data, rc) = utils.subprocess_sp(None, "ls -l /vagrant/distros", shell=True) self.assertEqual(rc, 0)
def test_01_cobbler_status(self): """Runs 'cobbler status'""" (data, rc) = utils.subprocess_sp(None, ["cobbler", "status"], shell=False) self.assertEqual(rc, 0)
def test_08_cobbler_acl_removegroup(self): """Runs 'cobbler aclsetup --removegroup'""" (data,rc) = utils.subprocess_sp(None,["cobbler","aclsetup","--removegroup=vagrant"],shell=False) self.assertEqual(rc,0)
def test_02_cobbler_sync(self): """Runs 'cobbler sync'""" (data, rc) = utils.subprocess_sp(None, ["cobbler", "sync"], shell=False) self.assertEqual(rc, 0)
def test_A_00_check_mount(self): """Validating remote mount location""" (data,rc) = utils.subprocess_sp(None,"mount | grep mnt | grep nfs4",shell=True) self.assertEqual(rc,0)
def test_A_00_check_distros_available(self): """Validating distros location""" (data,rc) = utils.subprocess_sp(None,"ls -l /vagrant/distros",shell=True) self.assertEqual(rc,0)
def test_06_cobbler_acl_addgroup(self): """Runs 'cobbler aclsetup --addgroup'""" (data,rc) = utils.subprocess_sp(None,["cobbler","aclsetup","--addgroup=cobbler"],shell=False) self.assertEqual(rc,0)