<commit_before>import ConfigParser
import logging
import logging.config
import os
import tempfile
import threading
import yaml
import dcm
import libcloud.security

from dcm.agent.cloudmetadata import CLOUD_TYPES
import dcm.agent.connection.websocket as websocket
import dcm.agent.exceptions as exceptions
import dcm.agent.job_runner as job_runner
import dcm.agent.tests.utils.test_connection as test_connection

_g_logger = logging.getLogger(__name__)

def get_python_script_dir():
    # we allow it to pull out of the python package for tests and
    # installs that are done from something other than out packaging
    _ROOT = dcm.agent.get_root_location()
    return os.path.join(_ROOT, 'scripts')

class AgentToken(object):
    def __init__(self, encrypted_token, date_string):
        self.encrypted_token = encrypted_token
        self.date_string = date_string

def get_connection_object(conf):
    con_type = conf.connection_type
    if not con_type:
        raise exceptions.AgentOptionValueNotSetException("connection_type")

    # XXX should we stevedore load this or __import__ it or go with a
    # hard coded list?  for now hard coded list

    if con_type == "success_tester":
        source_file = conf.connection_source_file
        if not source_file:
            raise exceptions.AgentOptionValueNotSetException(
                "[connection]source_file",
                msg="Using the %s connection type." % con_type)
        fptr = open(source_file, "r")
        if not conf.connection_dest_file:
            raise exceptions.AgentOptionValueNotSetException(
                "[connection]dest_file",
                msg="Using the %s connection type." % con_type)

        outf = open(conf.connection_dest_file, "w")
        con = test_connection.TestReplySuccessfullyAlways(fptr, outf)
    elif con_type == "ws":
        if not conf.connection_agentmanager_url:
            raise exceptions.AgentOptionValueNotSetException(
                "[connection]agentmanager_url")
        con = websocket.WebSocketConnection(conf.connection_agentmanager_url)
    else:
        raise exceptions.AgentOptionValueException(
            "[connection]type", con_type, "ws,success_tester,dummy")
    return con

class ConfigOpt(object):

    def __init__(self, section, name, t, default=None,
                 options=None, minv=None, maxv=None, help=None):
        self.section = section
        self.name = name
        self.my_type = t
        self.options = options
        self.default = default
        self.minv = minv
        self.maxv = maxv
        self.help = help

    def get_option_name(self):
        option_name = "%s_%s" % (self.section, self.name)
        return option_name

    def get_default(self):
        return self.default

    def get_help(self):
        return self.help

    def get_value(self, parser, default=None, **kwargs):
        if default is None:
            default = self.default
        try:
            v = parser.get(self.section, self.name, default)
        except ConfigParser.NoOptionError:
            v = default
        except ConfigParser.NoSectionError:
            v = default
        if v is None:
            return v
        try:
            if self.my_type == list:
                v = v.split(",")
            elif self.my_type == bool:
                if type(v) == str:
                    v = (v.lower() == "true" or v.lower() == "yes")
                else:
                    v = bool(v)
            else:
                v = self.my_type(v)
        except ValueError:
            raise exceptions.AgentOptionTypeException(
                self.name, self.my_type, v)

        if self.options is not None:
            vx = v
            if type(v) == str:
                vx = vx.lower()
                if vx not in self.options:
                    raise exceptions.AgentOptionValueException(
                        self.name, self.options, v)

        if self.my_type == int or self.my_type == float:
            if self.minv is not None and v < self.minv:
                raise exceptions.AgentOptionRangeException(
                    self.name, self.minv, self.maxv)
            if self.maxv is not None and v > self.maxv:
                raise exceptions.AgentOptionValueException(
                    self.name, self.minv, self.maxv)

        return v

class FilenameOpt(ConfigOpt):

    def __init__(self, section, name, default=None, help=None):
        super(FilenameOpt, self).__init__(section, name, str, default=default,
                                          help=help)

    def get_value(self, parser, relative_path=None, **kwarg):
        v = super(FilenameOpt, self).get_value(parser)
        if v is None:
            return None
        if not os.path.isabs(v):
            v = os.path.join(relative_path, v)
        return os.path.abspath(v)

class AgentConfig(object):
    """
    This is a serializable object that is threaded through to all classes.
    When/if multiprocessing is used it will be send to the worker threads.

    It is semi-read-only.  Any write operation must be done with thread
    primatives.  The exception is set handshake because that will be done
    before any thread is created.
    """

    def __init__(self, conf_files):
        self._cli_args = None
        self._remaining_argv = None
        self.instance_id = None
        self.jr = None
        self.state = "STARTING"

        self.agent_id = None
        self.cloud_id = None
        self.customer_id = None
        self.region_id = None
        self.zone_id = None
        self.server_id = None
        self.server_name = None
        self.storage_idfile = None

        self.imaging_event = threading.Event()

        parse_config_files(self, conf_files)

        if self.storage_idfile is None:
            self.storage_idfile = \
                os.path.join(self.storage_base_dir, "etc", "agentid.txt")
        if self.storage_script_dir is None:
            self.storage_script_dir = \
                os.path.join(self.storage_base_dir, "bin")
        if self.storage_script_dir == "/PYTHON_LIBS_SCRIPTS":
            self.storage_script_dir = None

        if self.storagecloud_ca_cert_dir and\
                os.path.exists(self.storage_ca_cert_dir):
            libcloud.security.CA_CERTS_PATH.append(self.storage_ca_cert_dir)
        if not self.storagecloud_secure:
            libcloud.security.VERIFY_SSL_CERT = False

        setup_logging(self.logging_configfile)

    def set_handshake(self, handshake_doc):
        self.state = "WAITING"
        #if handshake_doc["version"] == dcm.agent.g_version:
        self.agent_id = handshake_doc["agentID"]
        self.cloud_id = handshake_doc["cloudId"]
        self.customer_id = handshake_doc["customerId"]
        self.region_id = handshake_doc["regionId"]
        self.zone_id = handshake_doc["zoneId"]
        self.server_id = handshake_doc["serverId"]
        self.server_name = handshake_doc["serverName"]
        #self.ephemeral_file_system = handshake_doc["ephemeralFileSystem"]
        #self.encrypted_ephemeral_fs_key = \
        #    handshake_doc["encryptedEphemeralFsKey"]
        #else:
        #    raise exceptions.AgentHandshakeException()

    def get_script_location(self, name):
        if self.storage_script_dir is not None:
            path = os.path.join(self.storage_script_dir, name)
            _g_logger.debug("Script location %s" % path)
            if not os.path.exists(path):
                raise exceptions.AgentPluginConfigException(
                    "There is no proper configuration for %s" % name)
            return path

        script_dir = get_python_script_dir()
        _g_logger.debug("Script Dir %s" % script_dir)
        for platform in self.platform_script_locations:
            _g_logger.debug("Script platform %s" % platform)
            path = os.path.join(script_dir, platform, name)
            _g_logger.debug("Script location %s" % path)
            if os.path.exists(path):
                return path
        return None

    def is_upgrading(self):
        return False

    def is_imaging(self):
        return self.imaging_event.is_set()

    def set_imaging(self, b):
        if b:
            self.imaging_event.set()
        else:
            self.imaging_event.clear()

    def get_service_directory(self, service_name):
        return os.path.join(self.storage_services_dir, service_name)

    def start_job_runner(self):
        self.jr = job_runner.JobRunner(self)

    def get_temp_file(self, filename, isdir=False):
        new_dir = tempfile.mkdtemp(dir=self.storage_temppath)
        if isdir:
            return new_dir
        return os.path.join(new_dir, filename)

def _build_options_list():
    option_list = [
        ConfigOpt("pydev", "host", str, default=None, options=None,
                  help="The hostname of the pydev debugger"),
        ConfigOpt("pydev", "port", int, default=None, options=None,
                  help="The port where the pydev debugger is listening"),

        ConfigOpt("workers", "count", int, default=4, options=None,
                  help="The number of worker threads that will be "
                       "processing incoming requests"),

        ConfigOpt("workers", "long_runner_threads", int, default=1,
                  options=None,
                  help="The number of worker threads that will be "
                       "processing long running jobs (anything that "
                       "returns a job description)"),

        ConfigOpt("connection", "type", str, default="wss"
                                                     "", options=None,
                  help="The type of connection object to use.  Supported "
                       "types are ws and fallback"),
        FilenameOpt("connection", "source_file", default=None),
        FilenameOpt("connection", "dest_file", default=None),
        ConfigOpt("connection", "agentmanager_url", str, default=None,
                  help="The url of the agent manager with which this "
                       "agent will communicate."),
        FilenameOpt("logging", "configfile", default=None,
                    help="The location of the log configuration file"),

        FilenameOpt("plugin", "configfile",
                    help="The location of the plugin configuration file"),

        FilenameOpt("storage", "temppath", default="/tmp"),
        FilenameOpt("storage", "services_dir", default="/mnt/services"),
        FilenameOpt("storage", "base_dir", default="/dcm"),
        FilenameOpt("storage", "ephemeral_mountpoint", default="/mnt"),
        FilenameOpt("storage", "operations_path", default="/mnt"),
        FilenameOpt("storage", "idfile", default=None),
        FilenameOpt("storage", "script_dir", default=None),

        FilenameOpt("storagecloud", "ca_cert_dir", default=None),
        FilenameOpt("storagecloud", "secure", default=True),

        ConfigOpt("storage", "mount_enabled", bool, default=True),
        ConfigOpt("storage", "default_filesystem", str, default="ext3"),

        ConfigOpt("system", "user", str, default="dcm"),

        ConfigOpt("cloud", "type", str, default=CLOUD_TYPES.Amazon,
                  help="The type of cloud on which this agent is running"),
        ConfigOpt("cloud", "metadata_url", str,
                  default=None,
                  help="The url of the metadata server.  Not applicable "
                       "to all clouds."),

        ConfigOpt("messaging", "retransmission_timeout", float,
                  default=5),
        ConfigOpt("messaging", "max_at_once", int, default=-1,
                  help="The maximum number of commands that can be "
                       "outstanding at once.  -1 means infinity."),

        ConfigOpt("platform", "script_locations", list,
                  default="common-linux"),
        ConfigOpt("platform", "name", str, default=None,
                  help="The platform/distribution on which this agent is"
                       "being installed.",
                  options=["ubuntu", "el", "suse", "debian"]),
        ConfigOpt("jobs", "retain_job_time", int, default=3600),
        ConfigOpt("test", "skip_handshake", bool, default=False),

        ConfigOpt("intrusion", "type", str, default="ossec",
                  help="The name of the intrusion detection system to use.  "
                       "Currently only ossec is available."),
        ConfigOpt("intrusion", "enabled", bool, default=True),
        ConfigOpt("intrusion", "file", str,
                  default="/var/ossec/logs/alerts/alerts.log",
                  help="The file where alerts are logged."),
    ]

    return option_list

def parse_config_files(agent_conf, config_files):

    option_list = _build_options_list()

    # set all the default values on the agent conf object
    for o in option_list:
        k = o.get_option_name()
        v = o.get_default()
        setattr(agent_conf, k, v)

    for config_file in config_files:

        relative_path = os.path.dirname(config_file)

        parser = ConfigParser.SafeConfigParser()
        parser.read(config_file)

        for opt in option_list:
            try:
                oname = opt.get_option_name()
                v = opt.get_value(parser, relative_path=relative_path,
                                  default=getattr(agent_conf, oname))
                setattr(agent_conf, oname, v)
            except ConfigParser.NoSectionError as nse:
                raise exceptions.AgentOptionSectionNotFoundException(
                    opt.name)

def setup_logging(logging_configfile):
    top_logger = 'dcm.agent'

    if logging_configfile is None:
        loghandler = logging.StreamHandler()
        top_logger = logging.getLogger("")
        top_logger.setLevel(logging.DEBUG)
        top_logger.addHandler(loghandler)
        return

    if not os.path.exists(logging_configfile):
        raise exceptions.AgentOptionPathNotFoundException(
            "logging:configfile", logging_configfile)

    with open(logging_configfile, 'rt') as f:
        config = yaml.load(f.read())
        logging.config.dictConfig(config)
<commit_msg>Changing the default number of worker threads<commit_after>import ConfigParser
import logging
import logging.config
import os
import tempfile
import threading
import yaml
import dcm
import libcloud.security

from dcm.agent.cloudmetadata import CLOUD_TYPES
import dcm.agent.connection.websocket as websocket
import dcm.agent.exceptions as exceptions
import dcm.agent.job_runner as job_runner
import dcm.agent.tests.utils.test_connection as test_connection

_g_logger = logging.getLogger(__name__)

def get_python_script_dir():
    # we allow it to pull out of the python package for tests and
    # installs that are done from something other than out packaging
    _ROOT = dcm.agent.get_root_location()
    return os.path.join(_ROOT, 'scripts')

class AgentToken(object):
    def __init__(self, encrypted_token, date_string):
        self.encrypted_token = encrypted_token
        self.date_string = date_string

def get_connection_object(conf):
    con_type = conf.connection_type
    if not con_type:
        raise exceptions.AgentOptionValueNotSetException("connection_type")

    # XXX should we stevedore load this or __import__ it or go with a
    # hard coded list?  for now hard coded list

    if con_type == "success_tester":
        source_file = conf.connection_source_file
        if not source_file:
            raise exceptions.AgentOptionValueNotSetException(
                "[connection]source_file",
                msg="Using the %s connection type." % con_type)
        fptr = open(source_file, "r")
        if not conf.connection_dest_file:
            raise exceptions.AgentOptionValueNotSetException(
                "[connection]dest_file",
                msg="Using the %s connection type." % con_type)

        outf = open(conf.connection_dest_file, "w")
        con = test_connection.TestReplySuccessfullyAlways(fptr, outf)
    elif con_type == "ws":
        if not conf.connection_agentmanager_url:
            raise exceptions.AgentOptionValueNotSetException(
                "[connection]agentmanager_url")
        con = websocket.WebSocketConnection(conf.connection_agentmanager_url)
    else:
        raise exceptions.AgentOptionValueException(
            "[connection]type", con_type, "ws,success_tester,dummy")
    return con

class ConfigOpt(object):

    def __init__(self, section, name, t, default=None,
                 options=None, minv=None, maxv=None, help=None):
        self.section = section
        self.name = name
        self.my_type = t
        self.options = options
        self.default = default
        self.minv = minv
        self.maxv = maxv
        self.help = help

    def get_option_name(self):
        option_name = "%s_%s" % (self.section, self.name)
        return option_name

    def get_default(self):
        return self.default

    def get_help(self):
        return self.help

    def get_value(self, parser, default=None, **kwargs):
        if default is None:
            default = self.default
        try:
            v = parser.get(self.section, self.name, default)
        except ConfigParser.NoOptionError:
            v = default
        except ConfigParser.NoSectionError:
            v = default
        if v is None:
            return v
        try:
            if self.my_type == list:
                v = v.split(",")
            elif self.my_type == bool:
                if type(v) == str:
                    v = (v.lower() == "true" or v.lower() == "yes")
                else:
                    v = bool(v)
            else:
                v = self.my_type(v)
        except ValueError:
            raise exceptions.AgentOptionTypeException(
                self.name, self.my_type, v)

        if self.options is not None:
            vx = v
            if type(v) == str:
                vx = vx.lower()
                if vx not in self.options:
                    raise exceptions.AgentOptionValueException(
                        self.name, self.options, v)

        if self.my_type == int or self.my_type == float:
            if self.minv is not None and v < self.minv:
                raise exceptions.AgentOptionRangeException(
                    self.name, self.minv, self.maxv)
            if self.maxv is not None and v > self.maxv:
                raise exceptions.AgentOptionValueException(
                    self.name, self.minv, self.maxv)

        return v

class FilenameOpt(ConfigOpt):

    def __init__(self, section, name, default=None, help=None):
        super(FilenameOpt, self).__init__(section, name, str, default=default,
                                          help=help)

    def get_value(self, parser, relative_path=None, **kwarg):
        v = super(FilenameOpt, self).get_value(parser)
        if v is None:
            return None
        if not os.path.isabs(v):
            v = os.path.join(relative_path, v)
        return os.path.abspath(v)

class AgentConfig(object):
    """
    This is a serializable object that is threaded through to all classes.
    When/if multiprocessing is used it will be send to the worker threads.

    It is semi-read-only.  Any write operation must be done with thread
    primatives.  The exception is set handshake because that will be done
    before any thread is created.
    """

    def __init__(self, conf_files):
        self._cli_args = None
        self._remaining_argv = None
        self.instance_id = None
        self.jr = None
        self.state = "STARTING"

        self.agent_id = None
        self.cloud_id = None
        self.customer_id = None
        self.region_id = None
        self.zone_id = None
        self.server_id = None
        self.server_name = None
        self.storage_idfile = None

        self.imaging_event = threading.Event()

        parse_config_files(self, conf_files)

        if self.storage_idfile is None:
            self.storage_idfile = \
                os.path.join(self.storage_base_dir, "etc", "agentid.txt")
        if self.storage_script_dir is None:
            self.storage_script_dir = \
                os.path.join(self.storage_base_dir, "bin")
        if self.storage_script_dir == "/PYTHON_LIBS_SCRIPTS":
            self.storage_script_dir = None

        if self.storagecloud_ca_cert_dir and\
                os.path.exists(self.storage_ca_cert_dir):
            libcloud.security.CA_CERTS_PATH.append(self.storage_ca_cert_dir)
        if not self.storagecloud_secure:
            libcloud.security.VERIFY_SSL_CERT = False

        setup_logging(self.logging_configfile)

    def set_handshake(self, handshake_doc):
        self.state = "WAITING"
        #if handshake_doc["version"] == dcm.agent.g_version:
        self.agent_id = handshake_doc["agentID"]
        self.cloud_id = handshake_doc["cloudId"]
        self.customer_id = handshake_doc["customerId"]
        self.region_id = handshake_doc["regionId"]
        self.zone_id = handshake_doc["zoneId"]
        self.server_id = handshake_doc["serverId"]
        self.server_name = handshake_doc["serverName"]
        #self.ephemeral_file_system = handshake_doc["ephemeralFileSystem"]
        #self.encrypted_ephemeral_fs_key = \
        #    handshake_doc["encryptedEphemeralFsKey"]
        #else:
        #    raise exceptions.AgentHandshakeException()

    def get_script_location(self, name):
        if self.storage_script_dir is not None:
            path = os.path.join(self.storage_script_dir, name)
            _g_logger.debug("Script location %s" % path)
            if not os.path.exists(path):
                raise exceptions.AgentPluginConfigException(
                    "There is no proper configuration for %s" % name)
            return path

        script_dir = get_python_script_dir()
        _g_logger.debug("Script Dir %s" % script_dir)
        for platform in self.platform_script_locations:
            _g_logger.debug("Script platform %s" % platform)
            path = os.path.join(script_dir, platform, name)
            _g_logger.debug("Script location %s" % path)
            if os.path.exists(path):
                return path
        return None

    def is_upgrading(self):
        return False

    def is_imaging(self):
        return self.imaging_event.is_set()

    def set_imaging(self, b):
        if b:
            self.imaging_event.set()
        else:
            self.imaging_event.clear()

    def get_service_directory(self, service_name):
        return os.path.join(self.storage_services_dir, service_name)

    def start_job_runner(self):
        self.jr = job_runner.JobRunner(self)

    def get_temp_file(self, filename, isdir=False):
        new_dir = tempfile.mkdtemp(dir=self.storage_temppath)
        if isdir:
            return new_dir
        return os.path.join(new_dir, filename)

def _build_options_list():
    option_list = [
        ConfigOpt("pydev", "host", str, default=None, options=None,
                  help="The hostname of the pydev debugger"),
        ConfigOpt("pydev", "port", int, default=None, options=None,
                  help="The port where the pydev debugger is listening"),

        ConfigOpt("workers", "count", int, default=1, options=None,
                  help="The number of worker threads that will be "
                       "processing incoming requests"),

        ConfigOpt("workers", "long_runner_threads", int, default=1,
                  options=None,
                  help="The number of worker threads that will be "
                       "processing long running jobs (anything that "
                       "returns a job description)"),

        ConfigOpt("connection", "type", str, default="wss"
                                                     "", options=None,
                  help="The type of connection object to use.  Supported "
                       "types are ws and fallback"),
        FilenameOpt("connection", "source_file", default=None),
        FilenameOpt("connection", "dest_file", default=None),
        ConfigOpt("connection", "agentmanager_url", str, default=None,
                  help="The url of the agent manager with which this "
                       "agent will communicate."),
        FilenameOpt("logging", "configfile", default=None,
                    help="The location of the log configuration file"),

        FilenameOpt("plugin", "configfile",
                    help="The location of the plugin configuration file"),

        FilenameOpt("storage", "temppath", default="/tmp"),
        FilenameOpt("storage", "services_dir", default="/mnt/services"),
        FilenameOpt("storage", "base_dir", default="/dcm"),
        FilenameOpt("storage", "ephemeral_mountpoint", default="/mnt"),
        FilenameOpt("storage", "operations_path", default="/mnt"),
        FilenameOpt("storage", "idfile", default=None),
        FilenameOpt("storage", "script_dir", default=None),

        FilenameOpt("storagecloud", "ca_cert_dir", default=None),
        FilenameOpt("storagecloud", "secure", default=True),

        ConfigOpt("storage", "mount_enabled", bool, default=True),
        ConfigOpt("storage", "default_filesystem", str, default="ext3"),

        ConfigOpt("system", "user", str, default="dcm"),

        ConfigOpt("cloud", "type", str, default=CLOUD_TYPES.Amazon,
                  help="The type of cloud on which this agent is running"),
        ConfigOpt("cloud", "metadata_url", str,
                  default=None,
                  help="The url of the metadata server.  Not applicable "
                       "to all clouds."),

        ConfigOpt("messaging", "retransmission_timeout", float,
                  default=5),
        ConfigOpt("messaging", "max_at_once", int, default=-1,
                  help="The maximum number of commands that can be "
                       "outstanding at once.  -1 means infinity."),

        ConfigOpt("platform", "script_locations", list,
                  default="common-linux"),
        ConfigOpt("platform", "name", str, default=None,
                  help="The platform/distribution on which this agent is"
                       "being installed.",
                  options=["ubuntu", "el", "suse", "debian"]),
        ConfigOpt("jobs", "retain_job_time", int, default=3600),
        ConfigOpt("test", "skip_handshake", bool, default=False),

        ConfigOpt("intrusion", "type", str, default="ossec",
                  help="The name of the intrusion detection system to use.  "
                       "Currently only ossec is available."),
        ConfigOpt("intrusion", "enabled", bool, default=True),
        ConfigOpt("intrusion", "file", str,
                  default="/var/ossec/logs/alerts/alerts.log",
                  help="The file where alerts are logged."),
    ]

    return option_list

def parse_config_files(agent_conf, config_files):

    option_list = _build_options_list()

    # set all the default values on the agent conf object
    for o in option_list:
        k = o.get_option_name()
        v = o.get_default()
        setattr(agent_conf, k, v)

    for config_file in config_files:

        relative_path = os.path.dirname(config_file)

        parser = ConfigParser.SafeConfigParser()
        parser.read(config_file)

        for opt in option_list:
            try:
                oname = opt.get_option_name()
                v = opt.get_value(parser, relative_path=relative_path,
                                  default=getattr(agent_conf, oname))
                setattr(agent_conf, oname, v)
            except ConfigParser.NoSectionError as nse:
                raise exceptions.AgentOptionSectionNotFoundException(
                    opt.name)

def setup_logging(logging_configfile):
    top_logger = 'dcm.agent'

    if logging_configfile is None:
        loghandler = logging.StreamHandler()
        top_logger = logging.getLogger("")
        top_logger.setLevel(logging.DEBUG)
        top_logger.addHandler(loghandler)
        return

    if not os.path.exists(logging_configfile):
        raise exceptions.AgentOptionPathNotFoundException(
            "logging:configfile", logging_configfile)

    with open(logging_configfile, 'rt') as f:
        config = yaml.load(f.read())
        logging.config.dictConfig(config)
<|endoftext|>


<commit_before>#!/usr/bin/env python3

import sched
import time
import configparser
import logging
from email.mime.text import MIMEText
from random import randint
from smtplib import SMTP

def read_subjects(filename):
    file = open(filename)
    result = [line.rstrip() for line in file]
    file.close()
    return result

def random_subject(subjects):
    return subjects[randint(0, len(subjects) - 1)]

def send_mail():
    msg = MIMEText("-kT-", 'plain')
    msg['Subject'] = random_subject(subjects)
    msg['From'] = config['mail']['user']
    msg['To'] = config['mail']['to']

    try:
        conn = SMTP(config['mail']['addr'] + ':' + config['mail']['port'])
        conn.starttls()
        conn.login(config['mail']['user'], config['mail']['pass'])
        try:
            conn.send_message(msg)
        finally:
            conn.quit()
            logging.info("Mail with message '" + msg['Subject'] + "' sent!")
    except Exception as exc:
        logging.error(exc)

    rand_minute = randint(0,30)*60
    logging.info("Next mail in " + str(1800 + rand_minute) + " seconds.")
    s.enter(1800+rand_minute)

subjects = read_subjects('sprueche.txt')
config = configparser.ConfigParser()
config.read("mail.conf")
logging.basicConfig(format='%(levelname)s: %(message)s', level=getattr(logging, config['log']['level'].upper(), None))
logging.debug("Read subjects.")
logging.debug("Read config")

s = sched.scheduler()
s.enter(2, 0, send_mail)
s.run()
logging.debug("Started scheduler.")
<commit_msg>bugfix<commit_after>#!/usr/bin/env python3

import sched
import time
import configparser
import logging
from email.mime.text import MIMEText
from random import randint
from smtplib import SMTP

def read_subjects(filename):
    file = open(filename)
    result = [line.rstrip() for line in file]
    file.close()
    return result

def random_subject(subjects):
    return subjects[randint(0, len(subjects) - 1)]

def send_mail():
    msg = MIMEText("-kT-", 'plain')
    msg['Subject'] = random_subject(subjects)
    msg['From'] = config['mail']['user']
    msg['To'] = config['mail']['to']

    try:
        conn = SMTP(config['mail']['addr'] + ':' + config['mail']['port'])
        conn.starttls()
        conn.login(config['mail']['user'], config['mail']['pass'])
        try:
            conn.send_message(msg)
        finally:
            conn.quit()
            logging.info("Mail with message '" + msg['Subject'] + "' sent!")
    except Exception as exc:
        logging.error(exc)

    rand_minute = randint(0,30)*60
    logging.info("Next mail in " + str(1800 + rand_minute) + " seconds.")
    s.enter(1800+rand_minute, 0, send_mail)

subjects = read_subjects('sprueche.txt')
config = configparser.ConfigParser()
config.read("mail.conf")
logging.basicConfig(format='%(levelname)s: %(message)s', level=getattr(logging, config['log']['level'].upper(), None))
logging.debug("Read subjects.")
logging.debug("Read config")

s = sched.scheduler()
s.enter(2, 0, send_mail)
s.run()
logging.debug("Started scheduler.")
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-

# This file is part of Cockpit.
#
# Copyright (C) 2017 Red Hat, Inc.
#
# Cockpit is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Cockpit is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Cockpit; If not, see <http://www.gnu.org/licenses/>.

import subprocess
import re
from testlib import *

class NetworkCase(MachineCase):
    def setUp(self):
        MachineCase.setUp(self)

        m = self.machine

        # Ensure a clean and consistent state.  We remove rogue
        # connections that might still be here from the time of
        # creating the image and we prevent NM from automatically
        # creating new connections.
        # if the command fails, try again
        failures_allowed = 3
        while True:
            try:
                print(m.execute("nmcli con show"))
                m.execute("""nmcli -f UUID,DEVICE connection show | awk '$2 == "--" { print $1 }' | xargs -r nmcli con del""")
                break
            except subprocess.CalledProcessError:
                failures_allowed -= 1
                if failures_allowed == 0:
                    raise

        m.write("/etc/NetworkManager/conf.d/99-test.conf", "[main]\nno-auto-default=*\n")
        m.execute("systemctl reload-or-restart NetworkManager")

        ver = self.machine.execute("busctl --system get-property org.freedesktop.NetworkManager /org/freedesktop/NetworkManager org.freedesktop.NetworkManager Version || true")
        m = re.match('s "(.*)"', ver)
        if m:
            self.networkmanager_version = map(int, m.group(1).split("."))
        else:
            self.networkmanager_version = [ 0 ]

    def get_iface(self, m, mac):
        def getit():
            path = m.execute("grep -li '%s' /sys/class/net/*/address" % mac)
            return path.split("/")[-2]
        iface = wait(getit).strip()
        print("%s -> %s" % (mac, iface))
        return iface

    def add_iface(self, mac=None, vlan=0, activate=True):
        m = self.machine
        mac = m.add_netiface(networking=self.network.interface(), vlan=vlan)
        # Wait for the interface to show up
        self.get_iface(m, mac)
        # Trigger udev to make sure that it has been renamed to its final name
        m.execute("udevadm trigger && udevadm settle")
        iface = self.get_iface(m, mac)
        wait(lambda: m.execute('nmcli device | grep %s | grep -v unavailable' % iface))
        if activate:
            m.execute("nmcli con add type ethernet ifname %s" % iface)
            m.execute("nmcli dev con %s" % iface)
        return iface

    def wait_for_iface(self, iface, active=True, state=None, prefix="10.111."):
        sel = "#networking-interfaces tr[data-interface='%s']" % iface

        if state:
            text = state
        elif active:
            text = prefix
        else:
            text = "Inactive"

        try:
            self.browser.wait_present(sel)
            self.browser.wait_visible(sel)
            self.browser.wait_in_text(sel, text)
        except:
            print("Interface %s didn't show up." % iface)
            print(self.browser.eval_js("ph_find('#networking-interfaces').outerHTML"))
            print(self.machine.execute("grep . /sys/class/net/*/address"))
            raise

    def iface_con_id(self, iface):
        con_id = self.machine.execute("nmcli -m tabular -t -f GENERAL.CONNECTION device show %s" % iface).strip()
        if con_id == "--":
            return None
        else:
            return con_id

    def slow_down_dhcp(self, delay):
        m = self.machine
        m.needs_writable_usr()
        m.execute("mv /usr/sbin/dhclient /usr/sbin/dhclient.real")
        m.write("/usr/sbin/dhclient", '#! /bin/sh\nsleep %s\nexec /usr/sbin/dhclient.real "$@"' % delay)
        m.execute("chmod a+x /usr/sbin/dhclient")
<commit_msg>test: Really avoid extra connections for additional network interfaces<commit_after># -*- coding: utf-8 -*-

# This file is part of Cockpit.
#
# Copyright (C) 2017 Red Hat, Inc.
#
# Cockpit is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Cockpit is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Cockpit; If not, see <http://www.gnu.org/licenses/>.

import subprocess
import re
from testlib import *

class NetworkCase(MachineCase):
    def setUp(self):
        MachineCase.setUp(self)

        m = self.machine

        # Ensure a clean and consistent state.  We remove rogue
        # connections that might still be here from the time of
        # creating the image and we prevent NM from automatically
        # creating new connections.
        # if the command fails, try again
        failures_allowed = 3
        while True:
            try:
                print(m.execute("nmcli con show"))
                m.execute("""nmcli -f UUID,DEVICE connection show | awk '$2 == "--" { print $1 }' | xargs -r nmcli con del""")
                break
            except subprocess.CalledProcessError:
                failures_allowed -= 1
                if failures_allowed == 0:
                    raise

        m.write("/etc/NetworkManager/conf.d/99-test.conf", "[main]\nno-auto-default=*\n")
        m.execute("systemctl reload-or-restart NetworkManager")

        ver = self.machine.execute("busctl --system get-property org.freedesktop.NetworkManager /org/freedesktop/NetworkManager org.freedesktop.NetworkManager Version || true")
        m = re.match('s "(.*)"', ver)
        if m:
            self.networkmanager_version = map(int, m.group(1).split("."))
        else:
            self.networkmanager_version = [ 0 ]

    def get_iface(self, m, mac):
        def getit():
            path = m.execute("grep -li '%s' /sys/class/net/*/address" % mac)
            return path.split("/")[-2]
        iface = wait(getit).strip()
        print("%s -> %s" % (mac, iface))
        return iface

    def add_iface(self, mac=None, vlan=0, activate=True):
        m = self.machine
        mac = m.add_netiface(networking=self.network.interface(), vlan=vlan)
        # Wait for the interface to show up
        self.get_iface(m, mac)
        # Trigger udev to make sure that it has been renamed to its final name
        m.execute("udevadm trigger && udevadm settle")
        iface = self.get_iface(m, mac)
        wait(lambda: m.execute('nmcli device | grep %s | grep -v unavailable' % iface))
        if activate:
            m.execute("nmcli con add type ethernet ifname %s con-name %s" % (iface, iface))
            m.execute("nmcli con up %s ifname %s" % (iface, iface))
        return iface

    def wait_for_iface(self, iface, active=True, state=None, prefix="10.111."):
        sel = "#networking-interfaces tr[data-interface='%s']" % iface

        if state:
            text = state
        elif active:
            text = prefix
        else:
            text = "Inactive"

        try:
            self.browser.wait_present(sel)
            self.browser.wait_visible(sel)
            self.browser.wait_in_text(sel, text)
        except:
            print("Interface %s didn't show up." % iface)
            print(self.browser.eval_js("ph_find('#networking-interfaces').outerHTML"))
            print(self.machine.execute("grep . /sys/class/net/*/address"))
            raise

    def iface_con_id(self, iface):
        con_id = self.machine.execute("nmcli -m tabular -t -f GENERAL.CONNECTION device show %s" % iface).strip()
        if con_id == "--":
            return None
        else:
            return con_id

    def slow_down_dhcp(self, delay):
        m = self.machine
        m.needs_writable_usr()
        m.execute("mv /usr/sbin/dhclient /usr/sbin/dhclient.real")
        m.write("/usr/sbin/dhclient", '#! /bin/sh\nsleep %s\nexec /usr/sbin/dhclient.real "$@"' % delay)
        m.execute("chmod a+x /usr/sbin/dhclient")
<|endoftext|>


<commit_before>import chatbot_brain

stock = u"What a funny thing to say!"

sentences_ = [
    "red coat myself very unwell this morning that has \
    passed off much better of me if I speak plainer ?",
    "red coat myself very unwell this morning that if it is .",
    "red coat myself very unwell this morning and pack her trunk afresh.",
    "red coat myself very unwell this morning that has passed off pleasantly \
    to the following morning every hope of an illiterate and miserly father \
    and mother .""",
    "red coat myself very well madam said.",
    "red coat myself very well together.",
    "red coat myself very unwell this morning.",
    "red coat myself very well for the first two months.",
    "red coat myself very well that.",
    "red coat myself very unwell this morning that if he comes\
    into the regulars and among his former indolence."
    ]

def test_initialize_bot():
    bot = chatbot_brain.Chatbot()
    assert len(bot.tri_lexicon) == 0
    assert len(bot.bi_lexicon) == 0

def test_fill_lexicon():
    bot = chatbot_brain.Chatbot()
    bot.fill_lexicon()
    assert len(bot.tri_lexicon) > 0
    assert len(bot.bi_lexicon) > 0

def test_compose_response():
    bot = chatbot_brain.Chatbot()
    output = bot.compose_response(input_sent="How are you doing?")
    assert "," not in output[0]
    for sentence in output:
        assert "." not in sentence[:-1]

def test_i_filter_random_empty_words():
    u"""Assert an empty string is not found in the default lexicon."""
    bot = chatbot_brain.Chatbot()
    words = [""]
    assert bot.i_filter_random(words) == stock

def test_i_filter_random_words_not_in_lexicon():
    u"""Assert if all words are not in lexicon the default is returned."""
    bot = chatbot_brain.Chatbot()
    words = ["moose", "bear", "eagle"]
    bot.bi_lexicon = {"car": ["benz"], "boat": ["sail"], "train": ["track"]}
    assert bot.i_filter_random(words) == stock

def test_i_filter_random_words_in_lexicon():
    u"""Assert if all words are in lexicon, a word is returned."""
    bot = chatbot_brain.Chatbot()
    words = ["car", "boat", "train"]
    bot.bi_lexicon = {"car": ["benz"], "boat": ["sail"], "train": ["track"]}
    assert bot.i_filter_random(words) in bot.bi_lexicon

def test_i_filter_random_one_word_in_lexicon():
    u"""Assert if one word is in lexicon that word is returned."""
    bot = chatbot_brain.Chatbot()
    words = ["car", "bear", "eagle"]
    bot.bi_lexicon = {"car": ["benz"], "boat": ["sail"], "train": ["track"]}
    assert bot.i_filter_random(words) == "car"

def test_o_filter_random():
    u"""Assert the returned element is in the initial list."""
    bot = chatbot_brain.Chatbot()
    assert bot.o_filter_random(sentences_) in sentences_

def test_pair_seeds_one_possible_pair():
    u"""Assert if only one word is in the lexicon it and its pair returned."""
    bot = chatbot_brain.Chatbot()
    words = ["car", "bear", "eagle"]
    bot.bi_lexicon = {"car": ["benz"], "boat": ["sail"], "train": ["track"]}
    assert bot._pair_seed(words[0]) == ["car", "benz"]

        # def _pair_seed(self, seed):
        # word_1 = seed
        # word_2 = None
        # while word_2 is None:
        #     try:
        #         next_ = random.choice(self.bi_lexicon[seed])
        #         if next_ not in self.stop_puncts:
        #             word_2 = next_
        #             pair = [word_1, word_2]
        #     except KeyError:
        #         continue
        # return pair

# untested methods:
# _create_chains
# _pair_seed
# _chain_filters
# _filter_recursive
<commit_msg>Add test_pair_seeds_all_possible_pairs() to assert if all words are in the lexicon the seed's pair is returned.<commit_after>import chatbot_brain

stock = u"What a funny thing to say!"

sentences_ = [
    "red coat myself very unwell this morning that has \
    passed off much better of me if I speak plainer ?",
    "red coat myself very unwell this morning that if it is .",
    "red coat myself very unwell this morning and pack her trunk afresh.",
    "red coat myself very unwell this morning that has passed off pleasantly \
    to the following morning every hope of an illiterate and miserly father \
    and mother .""",
    "red coat myself very well madam said.",
    "red coat myself very well together.",
    "red coat myself very unwell this morning.",
    "red coat myself very well for the first two months.",
    "red coat myself very well that.",
    "red coat myself very unwell this morning that if he comes\
    into the regulars and among his former indolence."
    ]

def test_initialize_bot():
    bot = chatbot_brain.Chatbot()
    assert len(bot.tri_lexicon) == 0
    assert len(bot.bi_lexicon) == 0

def test_fill_lexicon():
    bot = chatbot_brain.Chatbot()
    bot.fill_lexicon()
    assert len(bot.tri_lexicon) > 0
    assert len(bot.bi_lexicon) > 0

def test_compose_response():
    bot = chatbot_brain.Chatbot()
    output = bot.compose_response(input_sent="How are you doing?")
    assert "," not in output[0]
    for sentence in output:
        assert "." not in sentence[:-1]

def test_i_filter_random_empty_words():
    u"""Assert an empty string is not found in the default lexicon."""
    bot = chatbot_brain.Chatbot()
    words = [""]
    assert bot.i_filter_random(words) == stock

def test_i_filter_random_words_not_in_lexicon():
    u"""Assert if all words are not in lexicon the default is returned."""
    bot = chatbot_brain.Chatbot()
    words = ["moose", "bear", "eagle"]
    bot.bi_lexicon = {"car": ["benz"], "boat": ["sail"], "train": ["track"]}
    assert bot.i_filter_random(words) == stock

def test_i_filter_random_words_in_lexicon():
    u"""Assert if all words are in lexicon, a word is returned."""
    bot = chatbot_brain.Chatbot()
    words = ["car", "boat", "train"]
    bot.bi_lexicon = {"car": ["benz"], "boat": ["sail"], "train": ["track"]}
    assert bot.i_filter_random(words) in bot.bi_lexicon

def test_i_filter_random_one_word_in_lexicon():
    u"""Assert if one word is in lexicon that word is returned."""
    bot = chatbot_brain.Chatbot()
    words = ["car", "bear", "eagle"]
    bot.bi_lexicon = {"car": ["benz"], "boat": ["sail"], "train": ["track"]}
    assert bot.i_filter_random(words) == "car"

def test_o_filter_random():
    u"""Assert the returned element is in the initial list."""
    bot = chatbot_brain.Chatbot()
    assert bot.o_filter_random(sentences_) in sentences_

def test_pair_seeds_one_possible_pair():
    u"""Assert if only one word is in the lexicon it and its pair returned."""
    bot = chatbot_brain.Chatbot()
    words = ["car", "bear", "eagle"]
    bot.bi_lexicon = {"car": ["benz"], "boat": ["sail"], "train": ["track"]}
    assert bot._pair_seed(words[0]) == ["car", "benz"]

def test_pair_seeds_all_possible_pairs():
    u"""Assert if all words are in the lexicon the seed's pair is returned."""
    bot = chatbot_brain.Chatbot()
    words = ["car", "boat", "train"]
    bot.bi_lexicon = {"car": ["benz"], "boat": ["sail"], "train": ["track"]}
    for word in words:
        assert bot._pair_seed(word) == [word, bot.bi_lexicon[word][0]]

        # def _pair_seed(self, seed):
        # word_1 = seed
        # word_2 = None
        # while word_2 is None:
        #     try:
        #         next_ = random.choice(self.bi_lexicon[seed])
        #         if next_ not in self.stop_puncts:
        #             word_2 = next_
        #             pair = [word_1, word_2]
        #     except KeyError:
        #         continue
        # return pair

# untested methods:
# _create_chains
# _pair_seed
# _chain_filters
# _filter_recursive
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
"""
Unit testing module for pytest-pylint plugin
"""
import mock

pytest_plugins = ('pytester',)  # pylint: disable=invalid-name

def test_basic(testdir):
    """Verify basic pylint checks"""
    testdir.makepyfile("""import sys""")
    result = testdir.runpytest('--pylint')
    assert 'Missing module docstring' in result.stdout.str()
    assert 'Unused import sys' in result.stdout.str()
    assert 'Final newline missing' in result.stdout.str()
    assert 'passed' not in result.stdout.str()
    assert 'Linting files' in result.stdout.str()

def test_disable(testdir):
    """Verify basic pylint checks"""
    testdir.makepyfile("""import sys""")
    result = testdir.runpytest('--pylint --no-pylint')
    assert 'Final newline missing' not in result.stdout.str()
    assert 'Linting files' not in result.stdout.str()

def test_error_control(testdir):
    """Verify that error types are configurable"""
    testdir.makepyfile("""import sys""")
    result = testdir.runpytest('--pylint', '--pylint-error-types=EF')
    assert '1 passed' in result.stdout.str()

def test_pylintrc_file(testdir):
    """Verify that a specified pylint rc file will work."""
    rcfile = testdir.makefile('rc', """
[FORMAT]

max-line-length=3
""")
    testdir.makepyfile("""import sys""")
    result = testdir.runpytest(
        '--pylint', '--pylint-rcfile={0}'.format(rcfile.strpath)
    )
    assert 'Line too long (10/3)' in result.stdout.str()

def test_pylintrc_file_beside_ini(testdir):
    """
    Verify that a specified pylint rc file will work what placed into pytest
    ini dir.
    """
    non_cwd_dir = testdir.mkdir('non_cwd_dir')

    rcfile = non_cwd_dir.join('foo.rc')
    rcfile.write("""
[FORMAT]

max-line-length=3
""")

    inifile = non_cwd_dir.join('foo.ini')
    inifile.write("""
[pytest]
addopts = --pylint --pylint-rcfile={0}
""".format(rcfile.basename))

    pyfile = testdir.makepyfile("""import sys""")

    result = testdir.runpytest(
        pyfile.strpath
    )
    assert 'Line too long (10/3)' not in result.stdout.str()

    result = testdir.runpytest(
        '-c', inifile.strpath, pyfile.strpath
    )
    assert 'Line too long (10/3)' in result.stdout.str()

def test_pylintrc_ignore(testdir):
    """Verify that a pylintrc file with ignores will work."""
    rcfile = testdir.makefile('rc', """
[MASTER]

ignore = test_pylintrc_ignore.py
""")
    testdir.makepyfile("""import sys""")
    result = testdir.runpytest(
        '--pylint', '--pylint-rcfile={0}'.format(rcfile.strpath)
    )
    assert 'collected 0 items' in result.stdout.str()

def test_pylintrc_msg_template(testdir):
    """Verify that msg-template from pylintrc file is handled."""
    rcfile = testdir.makefile('rc', """
[REPORTS]

msg-template=start {msg_id} end
""")
    testdir.makepyfile("""import sys""")
    result = testdir.runpytest(
        '--pylint', '--pylint-rcfile={0}'.format(rcfile.strpath)
    )
    assert 'start W0611 end' in result.stdout.str()

def test_get_rel_path():
    """
    Verify our relative path function.
    """
    from pytest_pylint import get_rel_path
    correct_rel_path = 'How/Are/You/blah.py'
    path = '/Hi/How/Are/You/blah.py'
    parent_path = '/Hi/'
    assert get_rel_path(path, parent_path) == correct_rel_path

    parent_path = '/Hi'
    assert get_rel_path(path, parent_path) == correct_rel_path

def test_multiple_jobs(testdir):
    """
    Assert that the jobs argument is passed through to pylint if provided
    """
    testdir.makepyfile("""import sys""")
    with mock.patch('pytest_pylint.lint.Run') as run_mock:
        jobs = 0
        testdir.runpytest(
            '--pylint', '--pylint-jobs={0}'.format(jobs)
        )
    assert run_mock.call_count == 1
    assert run_mock.call_args[0][0][-2:] == ['-j', str(jobs)]

def test_no_multiple_jobs(testdir):
    """
    If no jobs argument is specified it should not appear in pylint arguments
    """
    testdir.makepyfile("""import sys""")
    with mock.patch('pytest_pylint.lint.Run') as run_mock:
        testdir.runpytest('--pylint')
    assert run_mock.call_count == 1
    assert '-j' not in run_mock.call_args[0][0]
<commit_msg>Add unit test<commit_after># -*- coding: utf-8 -*-
"""
Unit testing module for pytest-pylint plugin
"""
import mock

pytest_plugins = ('pytester',)  # pylint: disable=invalid-name

def test_basic(testdir):
    """Verify basic pylint checks"""
    testdir.makepyfile("""import sys""")
    result = testdir.runpytest('--pylint')
    assert 'Missing module docstring' in result.stdout.str()
    assert 'Unused import sys' in result.stdout.str()
    assert 'Final newline missing' in result.stdout.str()
    assert 'passed' not in result.stdout.str()
    assert 'Linting files' in result.stdout.str()

def test_subdirectories(testdir):
    """Verify pylint checks files in subdirectories"""
    subdir = testdir.mkpydir('mymodule')
    testfile = subdir.join("test_file.py")
    testfile.write("""import sys""")
    result = testdir.runpytest('--pylint')
    assert '[pylint] mymodule/test_file.py' in result.stdout.str()
    assert 'Missing module docstring' in result.stdout.str()
    assert 'Unused import sys' in result.stdout.str()
    assert 'Final newline missing' in result.stdout.str()
    assert '1 failed' in result.stdout.str()
    assert 'Linting files' in result.stdout.str()

def test_disable(testdir):
    """Verify basic pylint checks"""
    testdir.makepyfile("""import sys""")
    result = testdir.runpytest('--pylint --no-pylint')
    assert 'Final newline missing' not in result.stdout.str()
    assert 'Linting files' not in result.stdout.str()

def test_error_control(testdir):
    """Verify that error types are configurable"""
    testdir.makepyfile("""import sys""")
    result = testdir.runpytest('--pylint', '--pylint-error-types=EF')
    assert '1 passed' in result.stdout.str()

def test_pylintrc_file(testdir):
    """Verify that a specified pylint rc file will work."""
    rcfile = testdir.makefile('rc', """
[FORMAT]

max-line-length=3
""")
    testdir.makepyfile("""import sys""")
    result = testdir.runpytest(
        '--pylint', '--pylint-rcfile={0}'.format(rcfile.strpath)
    )
    assert 'Line too long (10/3)' in result.stdout.str()

def test_pylintrc_file_beside_ini(testdir):
    """
    Verify that a specified pylint rc file will work what placed into pytest
    ini dir.
    """
    non_cwd_dir = testdir.mkdir('non_cwd_dir')

    rcfile = non_cwd_dir.join('foo.rc')
    rcfile.write("""
[FORMAT]

max-line-length=3
""")

    inifile = non_cwd_dir.join('foo.ini')
    inifile.write("""
[pytest]
addopts = --pylint --pylint-rcfile={0}
""".format(rcfile.basename))

    pyfile = testdir.makepyfile("""import sys""")

    result = testdir.runpytest(
        pyfile.strpath
    )
    assert 'Line too long (10/3)' not in result.stdout.str()

    result = testdir.runpytest(
        '-c', inifile.strpath, pyfile.strpath
    )
    assert 'Line too long (10/3)' in result.stdout.str()

def test_pylintrc_ignore(testdir):
    """Verify that a pylintrc file with ignores will work."""
    rcfile = testdir.makefile('rc', """
[MASTER]

ignore = test_pylintrc_ignore.py
""")
    testdir.makepyfile("""import sys""")
    result = testdir.runpytest(
        '--pylint', '--pylint-rcfile={0}'.format(rcfile.strpath)
    )
    assert 'collected 0 items' in result.stdout.str()

def test_pylintrc_msg_template(testdir):
    """Verify that msg-template from pylintrc file is handled."""
    rcfile = testdir.makefile('rc', """
[REPORTS]

msg-template=start {msg_id} end
""")
    testdir.makepyfile("""import sys""")
    result = testdir.runpytest(
        '--pylint', '--pylint-rcfile={0}'.format(rcfile.strpath)
    )
    assert 'start W0611 end' in result.stdout.str()

def test_get_rel_path():
    """
    Verify our relative path function.
    """
    from pytest_pylint import get_rel_path
    correct_rel_path = 'How/Are/You/blah.py'
    path = '/Hi/How/Are/You/blah.py'
    parent_path = '/Hi/'
    assert get_rel_path(path, parent_path) == correct_rel_path

    parent_path = '/Hi'
    assert get_rel_path(path, parent_path) == correct_rel_path

def test_multiple_jobs(testdir):
    """
    Assert that the jobs argument is passed through to pylint if provided
    """
    testdir.makepyfile("""import sys""")
    with mock.patch('pytest_pylint.lint.Run') as run_mock:
        jobs = 0
        testdir.runpytest(
            '--pylint', '--pylint-jobs={0}'.format(jobs)
        )
    assert run_mock.call_count == 1
    assert run_mock.call_args[0][0][-2:] == ['-j', str(jobs)]

def test_no_multiple_jobs(testdir):
    """
    If no jobs argument is specified it should not appear in pylint arguments
    """
    testdir.makepyfile("""import sys""")
    with mock.patch('pytest_pylint.lint.Run') as run_mock:
        testdir.runpytest('--pylint')
    assert run_mock.call_count == 1
    assert '-j' not in run_mock.call_args[0][0]
<|endoftext|>


<commit_before>#!/usr/bin/python
# OpenPOWER Automated Test Project
#
# Contributors Listed Below - COPYRIGHT 2015,2017
# [+] International Business Machines Corp.
#
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied. See the License for the specific language governing
# permissions and limitations under the License.
#

# Basic IPL and reboot tests

import unittest

import OpTestConfiguration
from common.OpTestUtil import OpTestUtil
from common.OpTestSystem import OpSystemState

class BasicIPL(unittest.TestCase):
    def setUp(self):
        conf = OpTestConfiguration.conf
        self.host = conf.host()
        self.ipmi = conf.ipmi()
        self.system = conf.system()
        self.bmc = conf.system().bmc()
        self.util = OpTestUtil()
        self.pci_good_data_file = conf.lspci_file()

class BootToPetitboot(BasicIPL):
    def runTest(self):
        self.system.goto_state(OpSystemState.OFF)
        self.system.goto_state(OpSystemState.PETITBOOT)

class SoftPowerOff(BasicIPL):
    def runTest(self):
        self.system.goto_state(OpSystemState.PETITBOOT)
        self.ipmi.ipmi_power_soft()
        print "soft powered off"
        self.system.set_state(OpSystemState.POWERING_OFF)
        print "set state, going to off"
        self.system.goto_state(OpSystemState.OFF)

class BMCReset(BasicIPL):
    def runTest(self):
        self.system.goto_state(OpSystemState.OFF)
        self.bmc.reboot()
        c = 0
        while True:
            try:
                self.ipmi.ipmi_wait_for_standby_state()
            except OpTestError as e:
                c+=1
                if c == 10:
                    raise e
            else:
                break

        self.system.set_state(OpSystemState.POWERING_OFF)
        self.system.goto_state(OpSystemState.OFF)

class BootToOS(BasicIPL):
    def runTest(self):
        print "Currently powered off!"
        self.system.goto_state(OpSystemState.OFF)
        self.system.goto_state(OpSystemState.OS)
        # We booted, SHIP IT!

class OutOfBandWarmReset(BasicIPL):
    def runTest(self):
        # FIXME currently we have to go via OFF to ensure we go to petitboot
        self.system.goto_state(OpSystemState.OFF)
        self.system.goto_state(OpSystemState.PETITBOOT)
        # TODO skip if no IPMI
        # TODO use abstracted out-of-band warm reset
        self.ipmi.ipmi_warm_reset()
        self.system.set_state(OpSystemState.IPLing)
        self.system.goto_state(OpSystemState.PETITBOOT)

class HardPowerCycle(BasicIPL):
    def runTest(self):
        self.system.goto_state(OpSystemState.PETITBOOT)
        self.ipmi.ipmi_power_reset()
        self.system.set_state(OpSystemState.IPLing)
        self.system.goto_state(OpSystemState.PETITBOOT)

class PowerOff(BasicIPL):
    def runTest(self):
        self.system.goto_state(OpSystemState.PETITBOOT)
        self.system.sys_power_off()
        self.system.set_state(OpSystemState.POWERING_OFF)
        self.system.goto_state(OpSystemState.OFF)

def suite():
    suite = unittest.TestSuite()
    # We add these in a somewhat hard-coded order simply to minimise
    # needless reboots
    suite.addTest(BootToPetitboot())
    suite.addTest(SoftPowerOff())
    suite.addTest(BMCReset())
    suite.addTest(BootToOS())
    suite.addTest(OutOfBandWarmReset())
    suite.addTest(HardPowerCycle())
    suite.addTest(PowerOff())
    return suite
<commit_msg>Fix BasicIPL tests for FSP<commit_after>#!/usr/bin/python
# OpenPOWER Automated Test Project
#
# Contributors Listed Below - COPYRIGHT 2015,2017
# [+] International Business Machines Corp.
#
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied. See the License for the specific language governing
# permissions and limitations under the License.
#

# Basic IPL and reboot tests

import unittest

import OpTestConfiguration
from common.OpTestUtil import OpTestUtil
from common.OpTestSystem import OpSystemState

class BasicIPL(unittest.TestCase):
    def setUp(self):
        conf = OpTestConfiguration.conf
        self.host = conf.host()
        self.ipmi = conf.ipmi()
        self.system = conf.system()
        self.bmc = conf.bmc()
        self.util = OpTestUtil()
        self.pci_good_data_file = conf.lspci_file()

class BootToPetitboot(BasicIPL):
    def runTest(self):
        self.system.goto_state(OpSystemState.OFF)
        self.system.goto_state(OpSystemState.PETITBOOT)

class SoftPowerOff(BasicIPL):
    def runTest(self):
        self.system.goto_state(OpSystemState.PETITBOOT)
        self.ipmi.ipmi_power_soft()
        print "soft powered off"
        self.system.set_state(OpSystemState.POWERING_OFF)
        print "set state, going to off"
        self.system.goto_state(OpSystemState.OFF)

class BMCReset(BasicIPL):
    def runTest(self):
        self.system.goto_state(OpSystemState.OFF)
        self.bmc.reboot()
        c = 0
        while True:
            try:
                self.ipmi.ipmi_wait_for_standby_state()
            except OpTestError as e:
                c+=1
                if c == 10:
                    raise e
            else:
                break

        self.system.set_state(OpSystemState.POWERING_OFF)
        self.system.goto_state(OpSystemState.OFF)

class BootToOS(BasicIPL):
    def runTest(self):
        print "Currently powered off!"
        self.system.goto_state(OpSystemState.OFF)
        self.system.goto_state(OpSystemState.OS)
        # We booted, SHIP IT!

class OutOfBandWarmReset(BasicIPL):
    def runTest(self):
        # FIXME currently we have to go via OFF to ensure we go to petitboot
        self.system.goto_state(OpSystemState.OFF)
        self.system.goto_state(OpSystemState.PETITBOOT)
        # TODO skip if no IPMI
        # TODO use abstracted out-of-band warm reset
        self.ipmi.ipmi_warm_reset()
        self.system.set_state(OpSystemState.IPLing)
        self.system.goto_state(OpSystemState.PETITBOOT)

class HardPowerCycle(BasicIPL):
    def runTest(self):
        self.system.goto_state(OpSystemState.PETITBOOT)
        self.ipmi.ipmi_power_reset()
        self.system.set_state(OpSystemState.IPLing)
        self.system.goto_state(OpSystemState.PETITBOOT)

class PowerOff(BasicIPL):
    def runTest(self):
        self.system.goto_state(OpSystemState.PETITBOOT)
        self.system.sys_power_off()
        self.system.set_state(OpSystemState.POWERING_OFF)
        self.system.goto_state(OpSystemState.OFF)

def suite():
    suite = unittest.TestSuite()
    # We add these in a somewhat hard-coded order simply to minimise
    # needless reboots
    suite.addTest(BootToPetitboot())
    suite.addTest(SoftPowerOff())
    suite.addTest(BMCReset())
    suite.addTest(BootToOS())
    suite.addTest(OutOfBandWarmReset())
    suite.addTest(HardPowerCycle())
    suite.addTest(PowerOff())
    return suite
<|endoftext|>


<commit_before>#!/usr/bin/python
# IBM_PROLOG_BEGIN_TAG
# This is an automatically generated prolog.
#
# $Source: op-test-framework/testcases/OpTestEM.py $
#
# OpenPOWER Automated Test Project
#
# Contributors Listed Below - COPYRIGHT 2015
# [+] International Business Machines Corp.
#
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied. See the License for the specific language governing
# permissions and limitations under the License.
#
# IBM_PROLOG_END_TAG

#  @package OpTestEM
#  Energy Management package for OpenPower testing.
#
#  This class will test the functionality of following drivers
#  1. powernv cpuidle driver
#  2. powernv cpufreq driver

import time
import subprocess
import re
import random

import unittest

import OpTestConfiguration
from common.OpTestUtil import OpTestUtil
from common.OpTestSystem import OpSystemState

class OpTestEM():
    def setUp(self):
        conf = OpTestConfiguration.conf
        self.cv_HOST = conf.host()
        self.cv_IPMI = conf.ipmi()
        self.cv_SYSTEM = conf.system()
        self.util = OpTestUtil()
    ##
    # @brief sets the cpu frequency with i_freq value
    #
    # @param i_freq @type str: this is the frequency of cpu to be set
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def set_cpu_freq(self, i_freq):
        l_cmd = "for i in /sys/devices/system/cpu/cpu*/cpufreq/scaling_setspeed; do echo %s > $i; done" % i_freq
        self.cv_HOST.host_run_command(l_cmd)

    ##
    # @brief verify the cpu frequency with i_freq value
    #
    # @param i_freq @type str: this is the frequency to be verified with cpu frequency
    def verify_cpu_freq(self, i_freq):
        l_cmd = "cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_cur_freq"
        cur_freq = self.cv_HOST.host_run_command(l_cmd)
        if not cur_freq[0] == i_freq:
            # (According to Vaidy) it may take milliseconds to have the
            # request for a frequency change to come into effect.
            # So, if we happen to be *really* quick checking the result,
            # we may have checked before it has taken effect. So, we
            # sleep for a (short) amount of time and retry.
            time.sleep(0.2)
            cur_freq = self.cv_HOST.host_run_command(l_cmd)

        self.assertEqual(cur_freq[0], i_freq,
                         "CPU frequency not changed to %s" % i_freq)

    ##
    # @brief sets the cpu governer with i_gov governer
    #
    # @param i_gov @type str: this is the governer to be set for all cpu's
    def set_cpu_gov(self, i_gov):
        l_cmd = "for i in /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor; do echo %s > $i; done" % i_gov
        self.cv_HOST.host_run_command(l_cmd)

    ##
    # @brief verify the cpu governer with i_gov governer
    #
    # @param i_gov @type str: this is the governer to be verified with cpu governer
    def verify_cpu_gov(self, i_gov):
        l_cmd = "cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor"
        cur_gov = self.cv_HOST.host_run_command(l_cmd)
        self.assertEqual(cur_gov[0], i_gov, "CPU governor not changed to %s" % i_gov)

    ##
    # @brief enable cpu idle state i_idle
    #
    # @param i_idle @type str: this is the cpu idle state to be enabled
    def enable_idle_state(self, i_idle):
        l_cmd = "cpupower idle-set -e %s" % i_idle
        self.cv_HOST.host_run_command(l_cmd)

    ##
    # @brief disable cpu idle state i_idle
    #
    # @param i_idle @type str: this is the cpu idle state to be disabled
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def disable_idle_state(self, i_idle):
        l_cmd = "cpupower idle-set -d %s" % i_idle
        self.cv_HOST.host_run_command(l_cmd)

    ##
    # @brief verify whether cpu idle state i_idle enabled
    #
    # @param i_idle @type str: this is the cpu idle state to be verified for enable
    def verify_enable_idle_state(self, i_idle):
        l_cmd = "cat /sys/devices/system/cpu/cpu0/cpuidle/state%s/disable" % i_idle
        cur_value = self.cv_HOST.host_run_command(l_cmd)
        self.assertEqual(cur_value[0], "0", "CPU state%s not enabled" % i_idle)

    ##
    # @brief verify whether cpu idle state i_idle disabled
    #
    # @param i_idle @type str: this is the cpu idle state to be verified for disable
    def verify_disable_idle_state(self, i_idle):
        l_cmd = "cat /sys/devices/system/cpu/cpu0/cpuidle/state%s/disable" % i_idle
        cur_value = self.cv_HOST.host_run_command(l_cmd)
        self.assertEqual(cur_value[0], "1", "CPU state%s not disabled" % i_idle)

class slw_info(OpTestEM, unittest.TestCase):
    # @brief This function just gathers the host CPU SLW info
    def runTest(self):
        self.cv_SYSTEM.goto_state(OpSystemState.OS)

        # Get OS level
        l_oslevel = self.cv_HOST.host_get_OS_Level()
        # Get kernel version
        l_kernel = self.cv_HOST.host_get_kernel_version()
        self.cv_HOST.host_check_command("cpupower")
        self.cv_HOST.host_run_command("cpupower idle-info")
        self.cv_HOST.host_run_command("hexdump -c /proc/device-tree/ibm,enabled-idle-states")
        try:
            self.cv_HOST.host_run_command("cat /sys/firmware/opal/msglog | grep -i slw")
        except CommandFailed as cf:
            pass # we may have no slw entries in msglog

class cpu_freq_states(OpTestEM, unittest.TestCase):
    # @brief This function will cover following test steps
    #        2. Check the cpupower utility is available in host.
    #        3. Get available cpu scaling frequencies
    #        4. Set the userspace governer for all cpu's
    #        5. test the cpufreq driver by set/verify cpu frequency
    def runTest(self):
        self.cv_SYSTEM.goto_state(OpSystemState.OS)
        # Get OS level
        l_oslevel = self.cv_HOST.host_get_OS_Level()

        # Get kernel version
        l_kernel = self.cv_HOST.host_get_kernel_version()

        self.cv_HOST.host_check_command("cpupower")
        # Get available cpu scaling frequencies
        l_res = self.cv_HOST.host_run_command("cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies")
        freq_list = l_res[0].split(' ')[:-1] # remove empty entry at end
        print freq_list

        # Set the cpu governer to userspace
        self.set_cpu_gov("userspace")
        self.verify_cpu_gov("userspace")
        for i in range(1, 100):
            i_freq = random.choice(freq_list)
            self.set_cpu_freq(i_freq)
            self.verify_cpu_freq(i_freq)
        pass

class cpu_idle_states(OpTestEM, unittest.TestCase):
    ##
    # @brief This function will cover following test steps
    #        1. It will get the OS and kernel versions.
    #        2. Check the cpupower utility is available in host.
    #        3. Set the userspace governer for all cpu's
    #        4. test the cpuidle driver by enable/disable/verify the idle states
    def runTest(self):
        self.cv_SYSTEM.sys_bmc_power_on_validate_host()

        # Get OS level
        l_oslevel = self.cv_HOST.host_get_OS_Level()

        # Get kernel version
        l_kernel = self.cv_HOST.host_get_kernel_version()

        self.cv_HOST.host_check_command("cpupower")

        # TODO: Check the runtime idle states = expected idle states!
        idle_states = self.cv_HOST.host_run_command("find /sys/devices/system/cpu/cpu*/cpuidle/ -type d -regex '.*state[0-9]*' -printf '%f\\n'|sort -u|sed -e 's/^state//'")
        print repr(idle_states)
        # currently p8 cpu has 3 states
        for i in idle_states:
            self.enable_idle_state(i)
            self.verify_enable_idle_state(i)
        for i in idle_states:
            self.disable_idle_state(i)
            self.verify_disable_idle_state(i)
        # and reset back to enabling idle.
        for i in idle_states:
            self.enable_idle_state(i)
            self.verify_enable_idle_state(i)

        pass

def suite():
    s = unittest.TestSuite()
    s.addTest(slw_info())
    s.addTest(cpu_freq_states())
    s.addTest(cpu_idle_states())
    return s
<commit_msg>OpTestEM: use SSH connection over IPMI<commit_after>#!/usr/bin/python
# IBM_PROLOG_BEGIN_TAG
# This is an automatically generated prolog.
#
# $Source: op-test-framework/testcases/OpTestEM.py $
#
# OpenPOWER Automated Test Project
#
# Contributors Listed Below - COPYRIGHT 2015
# [+] International Business Machines Corp.
#
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied. See the License for the specific language governing
# permissions and limitations under the License.
#
# IBM_PROLOG_END_TAG

#  @package OpTestEM
#  Energy Management package for OpenPower testing.
#
#  This class will test the functionality of following drivers
#  1. powernv cpuidle driver
#  2. powernv cpufreq driver

import time
import subprocess
import re
import random

import unittest

import OpTestConfiguration
from common.OpTestUtil import OpTestUtil
from common.OpTestSystem import OpSystemState

class OpTestEM():
    def setUp(self):
        conf = OpTestConfiguration.conf
        self.cv_HOST = conf.host()
        self.cv_IPMI = conf.ipmi()
        self.cv_SYSTEM = conf.system()
        self.util = OpTestUtil()
    ##
    # @brief sets the cpu frequency with i_freq value
    #
    # @param i_freq @type str: this is the frequency of cpu to be set
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def set_cpu_freq(self, i_freq):
        l_cmd = "for i in /sys/devices/system/cpu/cpu*/cpufreq/scaling_setspeed; do echo %s > $i; done" % i_freq
        self.c.run_command(l_cmd)

    ##
    # @brief verify the cpu frequency with i_freq value
    #
    # @param i_freq @type str: this is the frequency to be verified with cpu frequency
    def verify_cpu_freq(self, i_freq):
        l_cmd = "cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_cur_freq"
        cur_freq = self.c.run_command(l_cmd)
        if not cur_freq[0] == i_freq:
            # (According to Vaidy) it may take milliseconds to have the
            # request for a frequency change to come into effect.
            # So, if we happen to be *really* quick checking the result,
            # we may have checked before it has taken effect. So, we
            # sleep for a (short) amount of time and retry.
            time.sleep(0.2)
            cur_freq = self.c.run_command(l_cmd)

        self.assertEqual(cur_freq[0], i_freq,
                         "CPU frequency not changed to %s" % i_freq)

    ##
    # @brief sets the cpu governer with i_gov governer
    #
    # @param i_gov @type str: this is the governer to be set for all cpu's
    def set_cpu_gov(self, i_gov):
        l_cmd = "for i in /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor; do echo %s > $i; done" % i_gov
        self.c.run_command(l_cmd)

    ##
    # @brief verify the cpu governer with i_gov governer
    #
    # @param i_gov @type str: this is the governer to be verified with cpu governer
    def verify_cpu_gov(self, i_gov):
        l_cmd = "cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor"
        cur_gov = self.c.run_command(l_cmd)
        self.assertEqual(cur_gov[0], i_gov, "CPU governor not changed to %s" % i_gov)

    ##
    # @brief enable cpu idle state i_idle
    #
    # @param i_idle @type str: this is the cpu idle state to be enabled
    def enable_idle_state(self, i_idle):
        l_cmd = "cpupower idle-set -e %s" % i_idle
        self.c.run_command(l_cmd)

    ##
    # @brief disable cpu idle state i_idle
    #
    # @param i_idle @type str: this is the cpu idle state to be disabled
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def disable_idle_state(self, i_idle):
        l_cmd = "cpupower idle-set -d %s" % i_idle
        self.c.run_command(l_cmd)

    ##
    # @brief verify whether cpu idle state i_idle enabled
    #
    # @param i_idle @type str: this is the cpu idle state to be verified for enable
    def verify_enable_idle_state(self, i_idle):
        l_cmd = "cat /sys/devices/system/cpu/cpu0/cpuidle/state%s/disable" % i_idle
        cur_value = self.c.run_command(l_cmd)
        self.assertEqual(cur_value[0], "0", "CPU state%s not enabled" % i_idle)

    ##
    # @brief verify whether cpu idle state i_idle disabled
    #
    # @param i_idle @type str: this is the cpu idle state to be verified for disable
    def verify_disable_idle_state(self, i_idle):
        l_cmd = "cat /sys/devices/system/cpu/cpu0/cpuidle/state%s/disable" % i_idle
        cur_value = self.c.run_command(l_cmd)
        self.assertEqual(cur_value[0], "1", "CPU state%s not disabled" % i_idle)

class slw_info(OpTestEM, unittest.TestCase):
    # @brief This function just gathers the host CPU SLW info
    def runTest(self):
        self.cv_SYSTEM.goto_state(OpSystemState.OS)
        self.c = self.cv_SYSTEM.host().get_ssh_connection()

        # Get OS level
        l_oslevel = self.cv_HOST.host_get_OS_Level()
        # Get kernel version
        l_kernel = self.cv_HOST.host_get_kernel_version()
        self.cv_HOST.host_check_command("cpupower")
        self.c.run_command("cpupower idle-info")
        self.c.run_command("hexdump -c /proc/device-tree/ibm,enabled-idle-states")
        try:
            self.c.run_command("cat /sys/firmware/opal/msglog | grep -i slw")
        except CommandFailed as cf:
            pass # we may have no slw entries in msglog

class cpu_freq_states(OpTestEM, unittest.TestCase):
    # @brief This function will cover following test steps
    #        2. Check the cpupower utility is available in host.
    #        3. Get available cpu scaling frequencies
    #        4. Set the userspace governer for all cpu's
    #        5. test the cpufreq driver by set/verify cpu frequency
    def runTest(self):
        self.cv_SYSTEM.goto_state(OpSystemState.OS)
        self.c = self.cv_SYSTEM.host().get_ssh_connection()

        # Get OS level
        l_oslevel = self.cv_HOST.host_get_OS_Level()

        # Get kernel version
        l_kernel = self.cv_HOST.host_get_kernel_version()

        self.cv_HOST.host_check_command("cpupower")
        # Get available cpu scaling frequencies
        l_res = self.c.run_command("cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies")
        freq_list = l_res[0].split(' ')[:-1] # remove empty entry at end
        print freq_list

        # Set the cpu governer to userspace
        self.set_cpu_gov("userspace")
        self.verify_cpu_gov("userspace")
        for i in range(1, 100):
            i_freq = random.choice(freq_list)
            self.set_cpu_freq(i_freq)
            self.verify_cpu_freq(i_freq)
        pass

class cpu_idle_states(OpTestEM, unittest.TestCase):
    ##
    # @brief This function will cover following test steps
    #        1. It will get the OS and kernel versions.
    #        2. Check the cpupower utility is available in host.
    #        3. Set the userspace governer for all cpu's
    #        4. test the cpuidle driver by enable/disable/verify the idle states
    def runTest(self):
        self.cv_SYSTEM.sys_bmc_power_on_validate_host()
        self.c = self.cv_SYSTEM.host().get_ssh_connection()

        # Get OS level
        l_oslevel = self.cv_HOST.host_get_OS_Level()

        # Get kernel version
        l_kernel = self.cv_HOST.host_get_kernel_version()

        self.cv_HOST.host_check_command("cpupower")

        # TODO: Check the runtime idle states = expected idle states!
        idle_states = self.c.run_command("find /sys/devices/system/cpu/cpu*/cpuidle/ -type d -regex '.*state[0-9]*' -printf '%f\\n'|sort -u|sed -e 's/^state//'")
        print repr(idle_states)
        # currently p8 cpu has 3 states
        for i in idle_states:
            self.enable_idle_state(i)
            self.verify_enable_idle_state(i)
        for i in idle_states:
            self.disable_idle_state(i)
            self.verify_disable_idle_state(i)
        # and reset back to enabling idle.
        for i in idle_states:
            self.enable_idle_state(i)
            self.verify_enable_idle_state(i)

        pass

def suite():
    s = unittest.TestSuite()
    s.addTest(slw_info())
    s.addTest(cpu_freq_states())
    s.addTest(cpu_idle_states())
    return s
<|endoftext|>


<commit_before>def func(x):
    return x+1

def test_func():
    assert func(2) == 3<commit_msg>testing ci build<commit_after>import pytest

def func(x):
    return x+1

""" test func """
def test_func():
    assert func(2) == 3<|endoftext|>