示例#1
0
def _setup():
    # Set up logger which logs everything which happens during and before config
    # parsing to sys.stdout
    logging.setup(DEFAULT_LOGGING_CONF_PATH)

    # 1. parse config args
    config.parse_args()

    # 2. setup logging.
    logging.setup(cfg.CONF.sensorcontainer.logging)

    # 3. all other setup which requires config to be parsed and logging to
    # be correctly setup.
    username = cfg.CONF.database.username if hasattr(cfg.CONF.database,
                                                     'username') else None
    password = cfg.CONF.database.password if hasattr(cfg.CONF.database,
                                                     'password') else None
    db_setup(cfg.CONF.database.db_name,
             cfg.CONF.database.host,
             cfg.CONF.database.port,
             username=username,
             password=password)
    register_exchanges()
    register_common_signal_handlers()

    # 4. Register internal triggers
    register_internal_trigger_types()
示例#2
0
def _setup(argv):
    config.parse_args()

    # 2. setup logging
    log_level = logging.DEBUG
    logging.basicConfig(format='%(asctime)s %(levelname)s [-] %(message)s',
                        level=log_level)

    if not cfg.CONF.verbose:
        # Note: We still want to print things at the following log levels: INFO, ERROR, CRITICAL
        exclude_log_levels = [logging.AUDIT, logging.DEBUG]
        handlers = logging.getLoggerClass().manager.root.handlers

        for handler in handlers:
            handler.addFilter(LogLevelFilter(log_levels=exclude_log_levels))

    # 3. all other setup which requires config to be parsed and logging to
    # be correctly setup.
    username = cfg.CONF.database.username if hasattr(cfg.CONF.database,
                                                     'username') else None
    password = cfg.CONF.database.password if hasattr(cfg.CONF.database,
                                                     'password') else None
    db_setup(cfg.CONF.database.db_name,
             cfg.CONF.database.host,
             cfg.CONF.database.port,
             username=username,
             password=password)
示例#3
0
def _setup():
    # Set up logger which logs everything which happens during and before config
    # parsing to sys.stdout
    logging.setup(DEFAULT_LOGGING_CONF_PATH)

    # 1. parse config args
    config.parse_args()

    # 2. setup logging.
    logging.setup(cfg.CONF.sensorcontainer.logging)

    # 3. all other setup which requires config to be parsed and logging to
    # be correctly setup.
    username = cfg.CONF.database.username if hasattr(cfg.CONF.database, 'username') else None
    password = cfg.CONF.database.password if hasattr(cfg.CONF.database, 'password') else None
    db_setup(cfg.CONF.database.db_name, cfg.CONF.database.host, cfg.CONF.database.port,
             username=username, password=password)
    register_exchanges()
    register_common_signal_handlers()

    # 4. Register internal triggers
    # Note: We need to do import here because of a messed up configuration
    # situation (this module depends on configuration being parsed)
    from st2common.triggers import register_internal_trigger_types
    register_internal_trigger_types()
示例#4
0
def main():
    _monkey_patch()

    cli_opts = [
        cfg.StrOpt('timestamp', default=None,
                   help='Will delete data older than ' +
                   'this timestamp. (default 48 hours). ' +
                   'Example value: 2015-03-13T19:01:27.255542Z'),
        cfg.StrOpt('action-ref', default='',
                   help='action-ref to delete executions for.')
    ]
    do_register_cli_opts(cli_opts)
    config.parse_args()

    # Get config values
    timestamp = cfg.CONF.timestamp
    action_ref = cfg.CONF.action_ref
    username = cfg.CONF.database.username if hasattr(cfg.CONF.database, 'username') else None
    password = cfg.CONF.database.password if hasattr(cfg.CONF.database, 'password') else None

    # Connect to db.
    db_setup(cfg.CONF.database.db_name, cfg.CONF.database.host, cfg.CONF.database.port,
             username=username, password=password)

    if not timestamp:
        now = datetime.now()
        timestamp = now - timedelta(days=DEFAULT_TIMEDELTA_DAYS)
    else:
        timestamp = datetime.strptime(timestamp, '%Y-%m-%dT%H:%M:%S.%fZ')

    # Purge models.
    _purge_executions(timestamp=timestamp, action_ref=action_ref)

    # Disconnect from db.
    db_teardown()
示例#5
0
    def test_db_setup(self, mock_mongoengine):
        db_setup(
            db_name="name",
            db_host="host",
            db_port=12345,
            username="******",
            password="******",
            authentication_mechanism="MONGODB-X509",
        )

        call_args = mock_mongoengine.connection.connect.call_args_list[0][0]
        call_kwargs = mock_mongoengine.connection.connect.call_args_list[0][1]

        self.assertEqual(call_args, ("name", ))
        self.assertEqual(
            call_kwargs,
            {
                "host": "host",
                "port": 12345,
                "username": "******",
                "password": "******",
                "tz_aware": True,
                "authentication_mechanism": "MONGODB-X509",
                "ssl": True,
                "ssl_match_hostname": True,
                "connectTimeoutMS": 3000,
                "serverSelectionTimeoutMS": 3000,
            },
        )
示例#6
0
def _setup():
    # Set up logger which logs everything which happens during and before config
    # parsing to sys.stdout
    logging.setup(DEFAULT_LOGGING_CONF_PATH)

    # 1. parse config args
    config.parse_args()

    # 2. setup logging.
    logging.setup(cfg.CONF.sensorcontainer.logging)

    # 3. all other setup which requires config to be parsed and logging to
    # be correctly setup.
    username = cfg.CONF.database.username if hasattr(cfg.CONF.database,
                                                     'username') else None
    password = cfg.CONF.database.password if hasattr(cfg.CONF.database,
                                                     'password') else None
    db_setup(cfg.CONF.database.db_name,
             cfg.CONF.database.host,
             cfg.CONF.database.port,
             username=username,
             password=password)
    register_exchanges()
    register_common_signal_handlers()

    # 4. Register internal triggers
    # Note: We need to do import here because of a messed up configuration
    # situation (this module depends on configuration being parsed)
    from st2common.triggers import register_internal_trigger_types
    register_internal_trigger_types()
示例#7
0
    def test_db_setup(self, mock_mongoengine):
        db_setup(db_name='name',
                 db_host='host',
                 db_port=12345,
                 username='******',
                 password='******',
                 authentication_mechanism='MONGODB-X509')

        call_args = mock_mongoengine.connection.connect.call_args_list[0][0]
        call_kwargs = mock_mongoengine.connection.connect.call_args_list[0][1]

        self.assertEqual(call_args, ('name', ))
        self.assertEqual(
            call_kwargs, {
                'host': 'host',
                'port': 12345,
                'username': '******',
                'password': '******',
                'tz_aware': True,
                'authentication_mechanism': 'MONGODB-X509',
                'ssl': True,
                'ssl_match_hostname': True,
                'connectTimeoutMS': 3000,
                'serverSelectionTimeoutMS': 3000
            })
示例#8
0
文件: api.py 项目: timff/st2
def _setup():
    # Set up logger which logs everything which happens during and before config
    # parsing to sys.stdout
    logging.setup(DEFAULT_LOGGING_CONF_PATH)

    # 1. parse args to setup config.
    config.parse_args()

    # 2. setup logging.
    logging.setup(cfg.CONF.auth.logging)

    if cfg.CONF.auth.mode not in VALID_MODES:
        raise ValueError('Valid modes are: %s' % (','.join(VALID_MODES)))

    # 3. all other setup which requires config to be parsed and logging to
    # be correctly setup.
    username = cfg.CONF.database.username if hasattr(cfg.CONF.database,
                                                     'username') else None
    password = cfg.CONF.database.password if hasattr(cfg.CONF.database,
                                                     'password') else None
    db_setup(cfg.CONF.database.db_name,
             cfg.CONF.database.host,
             cfg.CONF.database.port,
             username=username,
             password=password)
示例#9
0
    def __init__(self, pack, file_path, class_name, trigger_types,
                 poll_interval=None, parent_args=None):
        """
        :param pack: Name of the pack this sensor belongs to.
        :type pack: ``str``

        :param file_path: Path to the sensor module file.
        :type file_path: ``str``

        :param class_name: Sensor class name.
        :type class_name: ``str``

        :param trigger_types: A list of references to trigger types which
                                  belong to this sensor.
        :type trigger_types: ``list`` of ``str``

        :param poll_interval: Sensor poll interval (in seconds).
        :type poll_interval: ``int`` or ``None``

        :param parent_args: Command line arguments passed to the parent process.
        :type parse_args: ``list``
        """
        self._pack = pack
        self._file_path = file_path
        self._class_name = class_name
        self._trigger_types = trigger_types or []
        self._poll_interval = poll_interval
        self._parent_args = parent_args or []
        self._trigger_names = {}

        # 1. Parse the config with inherited parent args
        try:
            config.parse_args(args=self._parent_args)
        except Exception:
            pass

        # 2. Establish DB connection
        username = cfg.CONF.database.username if hasattr(cfg.CONF.database, 'username') else None
        password = cfg.CONF.database.password if hasattr(cfg.CONF.database, 'password') else None
        db_setup(cfg.CONF.database.db_name, cfg.CONF.database.host, cfg.CONF.database.port,
                 username=username, password=password)

        # 3. Instantiate the watcher
        self._trigger_watcher = TriggerWatcher(create_handler=self._handle_create_trigger,
                                               update_handler=self._handle_update_trigger,
                                               delete_handler=self._handle_delete_trigger,
                                               trigger_types=self._trigger_types,
                                               queue_suffix='sensorwrapper')

        # 4. Set up logging
        self._logger = logging.getLogger('SensorWrapper.%s' %
                                         (self._class_name))
        logging.setup(cfg.CONF.sensorcontainer.logging)

        if '--debug' in parent_args:
            set_log_level_for_all_loggers()

        self._sensor_instance = self._get_sensor_instance()
示例#10
0
def _setup_db():
    username = cfg.CONF.database.username if hasattr(cfg.CONF.database,
                                                     'username') else None
    password = cfg.CONF.database.password if hasattr(cfg.CONF.database,
                                                     'password') else None
    db_setup(cfg.CONF.database.db_name,
             cfg.CONF.database.host,
             cfg.CONF.database.port,
             username=username,
             password=password)
示例#11
0
    def __init__(self, pack, file_path, class_name, trigger_types,
                 poll_interval=None, parent_args=None):
        """
        :param pack: Name of the pack this sensor belongs to.
        :type pack: ``str``

        :param file_path: Path to the sensor module file.
        :type file_path: ``str``

        :param class_name: Sensor class name.
        :type class_name: ``str``

        :param trigger_types: A list of references to trigger types which
                                  belong to this sensor.
        :type trigger_types: ``list`` of ``str``

        :param poll_interval: Sensor poll interval (in seconds).
        :type poll_interval: ``int`` or ``None``

        :param parent_args: Command line arguments passed to the parent process.
        :type parse_args: ``list``
        """
        self._pack = pack
        self._file_path = file_path
        self._class_name = class_name
        self._trigger_types = trigger_types or []
        self._poll_interval = poll_interval
        self._parent_args = parent_args or []
        self._trigger_names = {}

        # 1. Parse the config with inherited parent args
        try:
            config.parse_args(args=self._parent_args)
        except Exception:
            pass

        # 2. Establish DB connection
        username = cfg.CONF.database.username if hasattr(cfg.CONF.database, 'username') else None
        password = cfg.CONF.database.password if hasattr(cfg.CONF.database, 'password') else None
        db_setup(cfg.CONF.database.db_name, cfg.CONF.database.host, cfg.CONF.database.port,
                 username=username, password=password)

        # 3. Instantiate the watcher
        self._trigger_watcher = TriggerWatcher(create_handler=self._handle_create_trigger,
                                               update_handler=self._handle_update_trigger,
                                               delete_handler=self._handle_delete_trigger,
                                               trigger_types=self._trigger_types,
                                               queue_suffix='sensorwrapper')

        # 4. Set up logging
        self._logger = logging.getLogger('SensorWrapper.%s' %
                                         (self._class_name))
        logging.setup(cfg.CONF.sensorcontainer.logging)

        self._sensor_instance = self._get_sensor_instance()
示例#12
0
 def initialize(self):
     # 1. Setup db connection
     username = cfg.CONF.database.username if hasattr(
         cfg.CONF.database, 'username') else None
     password = cfg.CONF.database.password if hasattr(
         cfg.CONF.database, 'password') else None
     db_setup(cfg.CONF.database.db_name,
              cfg.CONF.database.host,
              cfg.CONF.database.port,
              username=username,
              password=password)
示例#13
0
文件: unload.py 项目: Bala96/st2
 def initialize(self):
     # 1. Setup db connection
     username = cfg.CONF.database.username if hasattr(cfg.CONF.database, 'username') else None
     password = cfg.CONF.database.password if hasattr(cfg.CONF.database, 'password') else None
     db_setup(cfg.CONF.database.db_name, cfg.CONF.database.host, cfg.CONF.database.port,
              username=username, password=password,
              ssl=cfg.CONF.database.ssl,
              ssl_keyfile=cfg.CONF.database.ssl_keyfile,
              ssl_certfile=cfg.CONF.database.ssl_certfile,
              ssl_cert_reqs=cfg.CONF.database.ssl_cert_reqs,
              ssl_ca_certs=cfg.CONF.database.ssl_ca_certs,
              ssl_match_hostname=cfg.CONF.database.ssl_match_hostname)
示例#14
0
文件: api.py 项目: bjoernbessert/st2
def _setup():
    # 1. parse args to setup config.
    config.parse_args()

    # 2. setup logging.
    logging.setup(cfg.CONF.api.logging)

    # 3. all other setup which requires config to be parsed and logging to
    # be correctly setup.
    username = cfg.CONF.database.username if hasattr(cfg.CONF.database, 'username') else None
    password = cfg.CONF.database.password if hasattr(cfg.CONF.database, 'password') else None
    db_setup(cfg.CONF.database.db_name, cfg.CONF.database.host, cfg.CONF.database.port,
             username=username, password=password)
示例#15
0
def _setup():
    config.parse_args()

    # 2. setup logging.
    logging.basicConfig(format='%(asctime)s %(levelname)s [-] %(message)s',
                        level=logging.DEBUG)

    # 3. all other setup which requires config to be parsed and logging to
    # be correctly setup.
    username = cfg.CONF.database.username if hasattr(cfg.CONF.database, 'username') else None
    password = cfg.CONF.database.password if hasattr(cfg.CONF.database, 'password') else None
    db_setup(cfg.CONF.database.db_name, cfg.CONF.database.host, cfg.CONF.database.port,
             username=username, password=password)
示例#16
0
文件: unload.py 项目: zwunix/st2
 def initialize(self):
     # 1. Setup db connection
     username = cfg.CONF.database.username if hasattr(cfg.CONF.database, 'username') else None
     password = cfg.CONF.database.password if hasattr(cfg.CONF.database, 'password') else None
     db_setup(cfg.CONF.database.db_name, cfg.CONF.database.host, cfg.CONF.database.port,
              username=username, password=password,
              ssl=cfg.CONF.database.ssl,
              ssl_keyfile=cfg.CONF.database.ssl_keyfile,
              ssl_certfile=cfg.CONF.database.ssl_certfile,
              ssl_cert_reqs=cfg.CONF.database.ssl_cert_reqs,
              ssl_ca_certs=cfg.CONF.database.ssl_ca_certs,
              authentication_mechanism=cfg.CONF.database.authentication_mechanism,
              ssl_match_hostname=cfg.CONF.database.ssl_match_hostname)
def main():
    config.parse_args()

    # Connect to db.
    username = cfg.CONF.database.username if hasattr(cfg.CONF.database, 'username') else None
    password = cfg.CONF.database.password if hasattr(cfg.CONF.database, 'password') else None
    db_setup(cfg.CONF.database.db_name, cfg.CONF.database.host, cfg.CONF.database.port,
             username=username, password=password)

    # Migrate rules.
    migrate_rules()

    # Disconnect from db.
    db_teardown()
示例#18
0
def main():
    monkey_patch()

    cli_opts = [
        cfg.BoolOpt('sensors', default=False,
                    help='diff sensor alone.'),
        cfg.BoolOpt('actions', default=False,
                    help='diff actions alone.'),
        cfg.BoolOpt('rules', default=False,
                    help='diff rules alone.'),
        cfg.BoolOpt('all', default=False,
                    help='diff sensors, actions and rules.'),
        cfg.BoolOpt('verbose', default=False),
        cfg.BoolOpt('simple', default=False,
                    help='In simple mode, tool only tells you if content is missing.' +
                         'It doesn\'t show you content diff between disk and db.'),
        cfg.StrOpt('pack-dir', default=None, help='Path to specific pack to diff.')
    ]
    do_register_cli_opts(cli_opts)
    config.parse_args()

    username = cfg.CONF.database.username if hasattr(cfg.CONF.database, 'username') else None
    password = cfg.CONF.database.password if hasattr(cfg.CONF.database, 'password') else None

    # Connect to db.
    db_setup(cfg.CONF.database.db_name, cfg.CONF.database.host, cfg.CONF.database.port,
             username=username, password=password)

    # Diff content
    pack_dir = cfg.CONF.pack_dir or None
    content_diff = not cfg.CONF.simple

    if cfg.CONF.all:
        _diff_sensors(pack_dir=pack_dir, verbose=cfg.CONF.verbose, content_diff=content_diff)
        _diff_actions(pack_dir=pack_dir, verbose=cfg.CONF.verbose, content_diff=content_diff)
        _diff_rules(pack_dir=pack_dir, verbose=cfg.CONF.verbose, content_diff=content_diff)
        return

    if cfg.CONF.sensors:
        _diff_sensors(pack_dir=pack_dir, verbose=cfg.CONF.verbose, content_diff=content_diff)

    if cfg.CONF.actions:
        _diff_actions(pack_dir=pack_dir, verbose=cfg.CONF.verbose, content_diff=content_diff)

    if cfg.CONF.rules:
        _diff_rules(pack_dir=pack_dir, verbose=cfg.CONF.verbose, content_diff=content_diff)

    # Disconnect from db.
    db_teardown()
示例#19
0
def main():
    _monkey_patch()

    cli_opts = [
        cfg.BoolOpt("sensors", default=False, help="diff sensor alone."),
        cfg.BoolOpt("actions", default=False, help="diff actions alone."),
        cfg.BoolOpt("rules", default=False, help="diff rules alone."),
        cfg.BoolOpt("all", default=False, help="diff sensors, actions and rules."),
        cfg.BoolOpt("verbose", default=False),
        cfg.BoolOpt(
            "simple",
            default=False,
            help="In simple mode, tool only tells you if content is missing."
            + "It doesn't show you content diff between disk and db.",
        ),
        cfg.StrOpt("pack-dir", default=None, help="Path to specific pack to diff."),
    ]
    do_register_cli_opts(cli_opts)
    config.parse_args()

    username = cfg.CONF.database.username if hasattr(cfg.CONF.database, "username") else None
    password = cfg.CONF.database.password if hasattr(cfg.CONF.database, "password") else None

    # Connect to db.
    db_setup(
        cfg.CONF.database.db_name, cfg.CONF.database.host, cfg.CONF.database.port, username=username, password=password
    )

    # Diff content
    pack_dir = cfg.CONF.pack_dir or None
    content_diff = not cfg.CONF.simple

    if cfg.CONF.all:
        _diff_sensors(pack_dir=pack_dir, verbose=cfg.CONF.verbose, content_diff=content_diff)
        _diff_actions(pack_dir=pack_dir, verbose=cfg.CONF.verbose, content_diff=content_diff)
        _diff_rules(pack_dir=pack_dir, verbose=cfg.CONF.verbose, content_diff=content_diff)
        return

    if cfg.CONF.sensors:
        _diff_sensors(pack_dir=pack_dir, verbose=cfg.CONF.verbose, content_diff=content_diff)

    if cfg.CONF.actions:
        _diff_actions(pack_dir=pack_dir, verbose=cfg.CONF.verbose, content_diff=content_diff)

    if cfg.CONF.rules:
        _diff_rules(pack_dir=pack_dir, verbose=cfg.CONF.verbose, content_diff=content_diff)

    # Disconnect from db.
    db_teardown()
示例#20
0
def _setup():
    # Set up logger which logs everything which happens during and before config
    # parsing to sys.stdout
    logging.setup(DEFAULT_LOGGING_CONF_PATH)

    # 1. parse args to setup config.
    config.parse_args()
    # 2. setup logging.
    logging.setup(cfg.CONF.resultstracker.logging)
    # 3. all other setup which requires config to be parsed and logging to
    # be correctly setup.
    username = cfg.CONF.database.username if hasattr(cfg.CONF.database, 'username') else None
    password = cfg.CONF.database.password if hasattr(cfg.CONF.database, 'password') else None
    db_setup(cfg.CONF.database.db_name, cfg.CONF.database.host, cfg.CONF.database.port,
             username=username, password=password)
示例#21
0
def _setup():
    # 1. parse args to setup config.
    config.parse_args()
    # 2. setup logging.
    logging.setup(cfg.CONF.actionrunner.logging)
    # 3. all other setup which requires config to be parsed and logging to
    # be correctly setup.
    username = cfg.CONF.database.username if hasattr(cfg.CONF.database,
                                                     'username') else None
    password = cfg.CONF.database.password if hasattr(cfg.CONF.database,
                                                     'password') else None
    db_setup(cfg.CONF.database.db_name,
             cfg.CONF.database.host,
             cfg.CONF.database.port,
             username=username,
             password=password)
示例#22
0
def db_setup():
    username = getattr(cfg.CONF.database, 'username', None)
    password = getattr(cfg.CONF.database, 'password', None)

    connection = db.db_setup(db_name=cfg.CONF.database.db_name, db_host=cfg.CONF.database.host,
                             db_port=cfg.CONF.database.port, username=username, password=password)
    return connection
示例#23
0
    def setUpClass(cls):
        super(SensorContainerTestCase, cls).setUpClass()

        st2tests.config.parse_args()

        username = cfg.CONF.database.username if hasattr(
            cfg.CONF.database, 'username') else None
        password = cfg.CONF.database.password if hasattr(
            cfg.CONF.database, 'password') else None
        cls.db_connection = db_setup(cfg.CONF.database.db_name,
                                     cfg.CONF.database.host,
                                     cfg.CONF.database.port,
                                     username=username,
                                     password=password,
                                     ensure_indexes=False)

        # Register sensors
        register_sensors(packs_base_paths=[PACKS_BASE_PATH],
                         use_pack_cache=False)

        # Create virtualenv for examples pack
        virtualenv_path = '/tmp/virtualenvs/examples'

        run_command(cmd=['rm', '-rf', virtualenv_path])

        cmd = [
            'virtualenv', '--system-site-packages', '--python', PYTHON_BINARY,
            virtualenv_path
        ]
        run_command(cmd=cmd)
示例#24
0
文件: base.py 项目: zsjohny/st2
    def _establish_connection_and_re_create_db(cls):
        username = cfg.CONF.database.username if hasattr(
            cfg.CONF.database, 'username') else None
        password = cfg.CONF.database.password if hasattr(
            cfg.CONF.database, 'password') else None
        cls.db_connection = db_setup(cfg.CONF.database.db_name,
                                     cfg.CONF.database.host,
                                     cfg.CONF.database.port,
                                     username=username,
                                     password=password,
                                     ensure_indexes=False)

        cls._drop_collections()
        cls.db_connection.drop_database(cfg.CONF.database.db_name)

        # Explicitly ensure indexes after we re-create the DB otherwise ensure_indexes could failure
        # inside db_setup if test inserted invalid data.
        # NOTE: This is only needed in distributed scenarios (production deployments) where
        # multiple services can start up at the same time and race conditions are possible.
        if cls.ensure_indexes:
            if len(cls.ensure_indexes_models) == 0 or len(
                    cls.ensure_indexes_models) > 1:
                msg = (
                    'Ensuring indexes for all the models, this could significantly slow down '
                    'the tests')
                print('#' * len(msg), file=sys.stderr)
                print(msg, file=sys.stderr)
                print('#' * len(msg), file=sys.stderr)

            db_ensure_indexes(cls.ensure_indexes_models)
示例#25
0
def _setup():
    # Set up logger which logs everything which happens during and before config
    # parsing to sys.stdout
    logging.setup(DEFAULT_LOGGING_CONF_PATH)

    # 1. parse args to setup config.
    config.parse_args()
    # 2. setup logging.
    logging.setup(cfg.CONF.notifier.logging)
    # 3. all other setup which requires config to be parsed and logging to
    # be correctly setup.
    username = cfg.CONF.database.username if hasattr(cfg.CONF.database, 'username') else None
    password = cfg.CONF.database.password if hasattr(cfg.CONF.database, 'password') else None
    db_setup(cfg.CONF.database.db_name, cfg.CONF.database.host, cfg.CONF.database.port,
             username=username, password=password)
    register_exchanges()
    def setUpClass(cls):
        super(SensorContainerTestCase, cls).setUpClass()
        return

        st2tests.config.parse_args()

        username = cfg.CONF.database.username if hasattr(
            cfg.CONF.database, 'username') else None
        password = cfg.CONF.database.password if hasattr(
            cfg.CONF.database, 'password') else None
        cls.db_connection = db_setup(cfg.CONF.database.db_name,
                                     cfg.CONF.database.host,
                                     cfg.CONF.database.port,
                                     username=username,
                                     password=password,
                                     ensure_indexes=False)

        # Register sensors
        register_sensors(packs_base_paths=['/opt/stackstorm/packs'],
                         use_pack_cache=False)

        # Create virtualenv for examples pack
        virtualenv_path = '/opt/stackstorm/virtualenvs/examples'
        cmd = ['virtualenv', '--system-site-packages', virtualenv_path]
        run_command(cmd=cmd)
示例#27
0
    def setUpClass(cls):
        super(SensorContainerTestCase, cls).setUpClass()

        st2tests.config.parse_args()

        username = cfg.CONF.database.username if hasattr(cfg.CONF.database, 'username') else None
        password = cfg.CONF.database.password if hasattr(cfg.CONF.database, 'password') else None
        cls.db_connection = db_setup(
            cfg.CONF.database.db_name, cfg.CONF.database.host, cfg.CONF.database.port,
            username=username, password=password, ensure_indexes=False)

        # NOTE: We need to perform this patching because test fixtures are located outside of the
        # packs base paths directory. This will never happen outside the context of test fixtures.
        cfg.CONF.content.packs_base_paths = PACKS_BASE_PATH

        # Register sensors
        register_sensors(packs_base_paths=[PACKS_BASE_PATH], use_pack_cache=False)

        # Create virtualenv for examples pack
        virtualenv_path = '/tmp/virtualenvs/examples'

        run_command(cmd=['rm', '-rf', virtualenv_path])

        cmd = ['virtualenv', '--system-site-packages', '--python', PYTHON_BINARY, virtualenv_path]
        run_command(cmd=cmd)
示例#28
0
    def setUpClass(cls):
        super(SensorContainerTestCase, cls).setUpClass()

        st2tests.config.parse_args()

        username = cfg.CONF.database.username if hasattr(
            cfg.CONF.database, 'username') else None
        password = cfg.CONF.database.password if hasattr(
            cfg.CONF.database, 'password') else None
        cls.db_connection = db_setup(cfg.CONF.database.db_name,
                                     cfg.CONF.database.host,
                                     cfg.CONF.database.port,
                                     username=username,
                                     password=password,
                                     ensure_indexes=False)

        # NOTE: We need to perform this patching because test fixtures are located outside of the
        # packs base paths directory. This will never happen outside the context of test fixtures.
        cfg.CONF.content.packs_base_paths = PACKS_BASE_PATH

        # Register sensors
        register_sensors(packs_base_paths=[PACKS_BASE_PATH],
                         use_pack_cache=False)

        # Create virtualenv for examples pack
        virtualenv_path = '/tmp/virtualenvs/examples'

        run_command(cmd=['rm', '-rf', virtualenv_path])

        cmd = [
            'virtualenv', '--system-site-packages', '--python', PYTHON_BINARY,
            virtualenv_path
        ]
        run_command(cmd=cmd)
示例#29
0
文件: base.py 项目: timff/st2
 def _establish_connection_and_re_create_db(cls):
     username = cfg.CONF.database.username if hasattr(cfg.CONF.database, 'username') else None
     password = cfg.CONF.database.password if hasattr(cfg.CONF.database, 'password') else None
     cls.db_connection = db_setup(
         cfg.CONF.database.db_name, cfg.CONF.database.host, cfg.CONF.database.port,
         username=username, password=password)
     cls._drop_collections()
     cls.db_connection.drop_database(cfg.CONF.database.db_name)
示例#30
0
文件: unload.py 项目: timff/st2
    def initialize(self):
        # 1. Parse config
        try:
            config.parse_args()
        except:
            pass

        # 2. Setup db connection
        username = cfg.CONF.database.username if hasattr(
            cfg.CONF.database, 'username') else None
        password = cfg.CONF.database.password if hasattr(
            cfg.CONF.database, 'password') else None
        db_setup(cfg.CONF.database.db_name,
                 cfg.CONF.database.host,
                 cfg.CONF.database.port,
                 username=username,
                 password=password)
示例#31
0
 def _establish_connection_and_re_create_db(cls):
     username = cfg.CONF.database.username if hasattr(cfg.CONF.database, 'username') else None
     password = cfg.CONF.database.password if hasattr(cfg.CONF.database, 'password') else None
     cls.db_connection = db_setup(
         cfg.CONF.database.db_name, cfg.CONF.database.host, cfg.CONF.database.port,
         username=username, password=password)
     cls._drop_collections()
     cls.db_connection.drop_database(cfg.CONF.database.db_name)
示例#32
0
def _setup():
    config.parse_args()

    # 2. setup logging.
    logging.basicConfig(format='%(asctime)s %(levelname)s [-] %(message)s',
                        level=logging.DEBUG)

    # 3. all other setup which requires config to be parsed and logging to
    # be correctly setup.
    username = cfg.CONF.database.username if hasattr(cfg.CONF.database,
                                                     'username') else None
    password = cfg.CONF.database.password if hasattr(cfg.CONF.database,
                                                     'password') else None
    db_setup(cfg.CONF.database.db_name,
             cfg.CONF.database.host,
             cfg.CONF.database.port,
             username=username,
             password=password)
示例#33
0
文件: test_db.py 项目: StackStorm/st2
    def test_db_setup(self, mock_mongoengine):
        db_setup(db_name='name', db_host='host', db_port=12345, username='******',
                 password='******', authentication_mechanism='MONGODB-X509')

        call_args = mock_mongoengine.connection.connect.call_args_list[0][0]
        call_kwargs = mock_mongoengine.connection.connect.call_args_list[0][1]

        self.assertEqual(call_args, ('name',))
        self.assertEqual(call_kwargs, {
            'host': 'host',
            'port': 12345,
            'username': '******',
            'password': '******',
            'tz_aware': True,
            'authentication_mechanism': 'MONGODB-X509',
            'ssl': True,
            'ssl_match_hostname': True
        })
示例#34
0
文件: base.py 项目: bjoernbessert/st2
 def setUpClass(cls):
     st2tests.config.parse_args()
     username = cfg.CONF.database.username if hasattr(cfg.CONF.database, 'username') else None
     password = cfg.CONF.database.password if hasattr(cfg.CONF.database, 'password') else None
     DbTestCase.db_connection = db_setup(
         cfg.CONF.database.db_name, cfg.CONF.database.host, cfg.CONF.database.port,
         username=username, password=password)
     cls.drop_collections()
     DbTestCase.db_connection.drop_database(cfg.CONF.database.db_name)
示例#35
0
def db_setup():
    username = getattr(cfg.CONF.database, 'username', None)
    password = getattr(cfg.CONF.database, 'password', None)

    connection = db.db_setup(db_name=cfg.CONF.database.db_name,
                             db_host=cfg.CONF.database.host,
                             db_port=cfg.CONF.database.port,
                             username=username,
                             password=password)
    return connection
示例#36
0
def main():
    _monkey_patch()

    cli_opts = [
        cfg.StrOpt('timestamp',
                   default=None,
                   help='Will delete data older than ' +
                   'this timestamp. (default 48 hours). ' +
                   'Example value: 2015-03-13T19:01:27.255542Z'),
        cfg.StrOpt('action-ref',
                   default='',
                   help='action-ref to delete executions for.')
    ]
    do_register_cli_opts(cli_opts)
    config.parse_args()

    # Get config values
    timestamp = cfg.CONF.timestamp
    action_ref = cfg.CONF.action_ref
    username = cfg.CONF.database.username if hasattr(cfg.CONF.database,
                                                     'username') else None
    password = cfg.CONF.database.password if hasattr(cfg.CONF.database,
                                                     'password') else None

    # Connect to db.
    db_setup(cfg.CONF.database.db_name,
             cfg.CONF.database.host,
             cfg.CONF.database.port,
             username=username,
             password=password)

    if not timestamp:
        now = datetime.now()
        timestamp = now - timedelta(days=DEFAULT_TIMEDELTA_DAYS)
    else:
        timestamp = datetime.strptime(timestamp, '%Y-%m-%dT%H:%M:%S.%fZ')

    # Purge models.
    _purge_executions(timestamp=timestamp, action_ref=action_ref)

    # Disconnect from db.
    db_teardown()
示例#37
0
def _setup(argv):
    config.parse_args()

    # 2. setup logging
    log_level = logging.DEBUG
    logging.basicConfig(format='%(asctime)s %(levelname)s [-] %(message)s', level=log_level)

    if not cfg.CONF.verbose:
        # Note: We still want to print things at the following log levels: INFO, ERROR, CRITICAL
        exclude_log_levels = [logging.AUDIT, logging.DEBUG]
        handlers = logging.getLoggerClass().manager.root.handlers

        for handler in handlers:
            handler.addFilter(LogLevelFilter(log_levels=exclude_log_levels))

    # 3. all other setup which requires config to be parsed and logging to
    # be correctly setup.
    username = cfg.CONF.database.username if hasattr(cfg.CONF.database, 'username') else None
    password = cfg.CONF.database.password if hasattr(cfg.CONF.database, 'password') else None
    db_setup(cfg.CONF.database.db_name, cfg.CONF.database.host, cfg.CONF.database.port,
             username=username, password=password)
示例#38
0
    def _establish_connection_and_re_create_db(cls):
        username = cfg.CONF.database.username if hasattr(cfg.CONF.database, 'username') else None
        password = cfg.CONF.database.password if hasattr(cfg.CONF.database, 'password') else None
        cls.db_connection = db_setup(
            cfg.CONF.database.db_name, cfg.CONF.database.host, cfg.CONF.database.port,
            username=username, password=password, ensure_indexes=False)
        cls._drop_collections()
        cls.db_connection.drop_database(cfg.CONF.database.db_name)

        # Explicity ensure indexes after we re-create the DB otherwise ensure_indexes could failure
        # inside db_setup if test inserted invalid data
        db_ensure_indexes()
示例#39
0
文件: base.py 项目: AlexeyDeyneko/st2
    def _establish_connection_and_re_create_db(cls):
        username = cfg.CONF.database.username if hasattr(cfg.CONF.database, 'username') else None
        password = cfg.CONF.database.password if hasattr(cfg.CONF.database, 'password') else None
        cls.db_connection = db_setup(
            cfg.CONF.database.db_name, cfg.CONF.database.host, cfg.CONF.database.port,
            username=username, password=password, ensure_indexes=False)
        cls._drop_collections()
        cls.db_connection.drop_database(cfg.CONF.database.db_name)

        # Explicity ensure indexes after we re-create the DB otherwise ensure_indexes could failure
        # inside db_setup if test inserted invalid data
        db_ensure_indexes()
示例#40
0
 def setUpClass(cls):
     st2tests.config.parse_args()
     username = cfg.CONF.database.username if hasattr(
         cfg.CONF.database, 'username') else None
     password = cfg.CONF.database.password if hasattr(
         cfg.CONF.database, 'password') else None
     DbTestCase.db_connection = db_setup(cfg.CONF.database.db_name,
                                         cfg.CONF.database.host,
                                         cfg.CONF.database.port,
                                         username=username,
                                         password=password)
     cls.drop_collections()
     DbTestCase.db_connection.drop_database(cfg.CONF.database.db_name)
示例#41
0
    def setUpClass(cls):
        super(SensorContainerTestCase, cls).setUpClass()
        return

        st2tests.config.parse_args()

        username = cfg.CONF.database.username if hasattr(cfg.CONF.database, 'username') else None
        password = cfg.CONF.database.password if hasattr(cfg.CONF.database, 'password') else None
        cls.db_connection = db_setup(
            cfg.CONF.database.db_name, cfg.CONF.database.host, cfg.CONF.database.port,
            username=username, password=password, ensure_indexes=False)

        # Register sensors
        register_sensors(packs_base_paths=['/opt/stackstorm/packs'], use_pack_cache=False)

        # Create virtualenv for examples pack
        virtualenv_path = '/opt/stackstorm/virtualenvs/examples'
        cmd = ['virtualenv', '--system-site-packages', virtualenv_path]
        run_command(cmd=cmd)
示例#42
0
    def setUpClass(cls):
        super(SensorContainerTestCase, cls).setUpClass()

        st2tests.config.parse_args()

        username = cfg.CONF.database.username if hasattr(cfg.CONF.database, 'username') else None
        password = cfg.CONF.database.password if hasattr(cfg.CONF.database, 'password') else None
        cls.db_connection = db_setup(
            cfg.CONF.database.db_name, cfg.CONF.database.host, cfg.CONF.database.port,
            username=username, password=password, ensure_indexes=False)

        # Register sensors
        register_sensors(packs_base_paths=[PACKS_BASE_PATH], use_pack_cache=False)

        # Create virtualenv for examples pack
        virtualenv_path = '/tmp/virtualenvs/examples'

        run_command(cmd=['rm', '-rf', virtualenv_path])

        cmd = ['virtualenv', '--system-site-packages', '--python', PYTHON_BINARY, virtualenv_path]
        run_command(cmd=cmd)
示例#43
0
文件: base.py 项目: nzlosh/st2
    def _establish_connection_and_re_create_db(cls):
        username = cfg.CONF.database.username if hasattr(cfg.CONF.database, 'username') else None
        password = cfg.CONF.database.password if hasattr(cfg.CONF.database, 'password') else None
        cls.db_connection = db_setup(
            cfg.CONF.database.db_name, cfg.CONF.database.host, cfg.CONF.database.port,
            username=username, password=password, ensure_indexes=False)

        cls._drop_collections()
        cls.db_connection.drop_database(cfg.CONF.database.db_name)

        # Explicitly ensure indexes after we re-create the DB otherwise ensure_indexes could failure
        # inside db_setup if test inserted invalid data.
        # NOTE: This is only needed in distributed scenarios (production deployments) where
        # multiple services can start up at the same time and race conditions are possible.
        if cls.ensure_indexes:
            if len(cls.ensure_indexes_models) == 0 or len(cls.ensure_indexes_models) > 1:
                msg = ('Ensuring indexes for all the models, this could significantly slow down '
                       'the tests')
                print('#' * len(msg), file=sys.stderr)
                print(msg, file=sys.stderr)
                print('#' * len(msg), file=sys.stderr)

            db_ensure_indexes(cls.ensure_indexes_models)
示例#44
0
文件: test_db.py 项目: lyandut/st2
    def test_db_setup_connecting_info_logging(self, mock_log, mock_mongoengine):
        # Verify that password is not included in the log message
        db_name = 'st2'
        db_port = '27017'
        username = '******'
        password = '******'

        # 1. Password provided as separate argument
        db_host = 'localhost'
        username = '******'
        password = '******'
        db_setup(db_name=db_name, db_host=db_host, db_port=db_port, username=username,
                 password=password)

        expected_message = 'Connecting to database "st2" @ "localhost:27017" as user "user_st2".'
        actual_message = mock_log.info.call_args_list[0][0][0]
        self.assertEqual(expected_message, actual_message)

        # Check for helpful error messages if the connection is successful
        expected_log_message = ('Successfully connected to database "st2" @ "localhost:27017" as '
                                'user "user_st2".')
        actual_log_message = mock_log.info.call_args_list[1][0][0]
        self.assertEqual(expected_log_message, actual_log_message)

        # 2. Password provided as part of uri string (single host)
        db_host = 'mongodb://*****:*****@127.0.0.2:5555'
        username = None
        password = None
        db_setup(db_name=db_name, db_host=db_host, db_port=db_port, username=username,
                 password=password)

        expected_message = 'Connecting to database "st2" @ "127.0.0.2:5555" as user "user_st22".'
        actual_message = mock_log.info.call_args_list[2][0][0]
        self.assertEqual(expected_message, actual_message)

        expected_log_message = ('Successfully connected to database "st2" @ "127.0.0.2:5555" as '
                                'user "user_st22".')
        actual_log_message = mock_log.info.call_args_list[3][0][0]
        self.assertEqual(expected_log_message, actual_log_message)

        # 3. Password provided as part of uri string (single host) - username
        # provided as argument has precedence
        db_host = 'mongodb://*****:*****@127.0.0.2:5555'
        username = '******'
        password = None
        db_setup(db_name=db_name, db_host=db_host, db_port=db_port, username=username,
                 password=password)

        expected_message = 'Connecting to database "st2" @ "127.0.0.2:5555" as user "user_st23".'
        actual_message = mock_log.info.call_args_list[4][0][0]
        self.assertEqual(expected_message, actual_message)

        expected_log_message = ('Successfully connected to database "st2" @ "127.0.0.2:5555" as '
                                'user "user_st23".')
        actual_log_message = mock_log.info.call_args_list[5][0][0]
        self.assertEqual(expected_log_message, actual_log_message)

        # 4. Just host provided in the url string
        db_host = 'mongodb://127.0.0.2:5555'
        username = '******'
        password = '******'
        db_setup(db_name=db_name, db_host=db_host, db_port=db_port, username=username,
                 password=password)

        expected_message = 'Connecting to database "st2" @ "127.0.0.2:5555" as user "user_st24".'
        actual_message = mock_log.info.call_args_list[6][0][0]
        self.assertEqual(expected_message, actual_message)

        expected_log_message = ('Successfully connected to database "st2" @ "127.0.0.2:5555" as '
                                'user "user_st24".')
        actual_log_message = mock_log.info.call_args_list[7][0][0]
        self.assertEqual(expected_log_message, actual_log_message)

        # 5. Multiple hosts specified as part of connection uri
        db_host = 'mongodb://*****:*****@host1,host2,host3'
        username = None
        password = '******'
        db_setup(db_name=db_name, db_host=db_host, db_port=db_port, username=username,
                 password=password)

        expected_message = ('Connecting to database "st2" @ "host1:27017,host2:27017,host3:27017 '
                            '(replica set)" as user "user6".')
        actual_message = mock_log.info.call_args_list[8][0][0]
        self.assertEqual(expected_message, actual_message)

        expected_log_message = ('Successfully connected to database "st2" @ '
                                '"host1:27017,host2:27017,host3:27017 '
                                '(replica set)" as user "user6".')
        actual_log_message = mock_log.info.call_args_list[9][0][0]
        self.assertEqual(expected_log_message, actual_log_message)

        # 6. Check for error message when failing to establish a connection
        mock_connect = mock.Mock()
        mock_connect.admin.command = mock.Mock(side_effect=ConnectionFailure('Failed to connect'))
        mock_mongoengine.connection.connect.return_value = mock_connect

        db_host = 'mongodb://*****:*****@ "localhost:9797" as user "user_st2".'
        actual_message = mock_log.info.call_args_list[10][0][0]
        self.assertEqual(expected_message, actual_message)

        expected_message = ('Failed to connect to database "st2" @ "localhost:9797" as user '
                            '"user_st2": Failed to connect')
        actual_message = mock_log.error.call_args_list[0][0][0]
        self.assertEqual(expected_message, actual_message)
示例#45
0
文件: test_db.py 项目: wdowling/st2
    def test_db_setup_connecting_info_logging(self, mock_log):
        # Verify that password is not included in the log message
        db_name = 'st2'
        db_port = '27017'
        username = '******'
        password = '******'

        # 1. Password provided as separate argument
        db_host = 'localhost'
        username = '******'
        password = '******'
        db_setup(db_name=db_name, db_host=db_host, db_port=db_port, username=username,
                 password=password)

        expected_message = 'Connecting to database "st2" @ "localhost:27017" as user "user_st2".'
        actual_message = mock_log.info.call_args_list[0][0][0]
        self.assertEqual(expected_message, actual_message)

        # 2. Password provided as part of uri string (single host)
        db_host = 'mongodb://*****:*****@127.0.0.2:5555'
        username = None
        password = None
        db_setup(db_name=db_name, db_host=db_host, db_port=db_port, username=username,
                 password=password)

        expected_message = 'Connecting to database "st2" @ "127.0.0.2:5555" as user "user_st22".'
        actual_message = mock_log.info.call_args_list[1][0][0]
        self.assertEqual(expected_message, actual_message)

        # 3. Password provided as part of uri string (single host) - username
        # provided as argument has precedence
        db_host = 'mongodb://*****:*****@127.0.0.2:5555'
        username = '******'
        password = None
        db_setup(db_name=db_name, db_host=db_host, db_port=db_port, username=username,
                 password=password)

        expected_message = 'Connecting to database "st2" @ "127.0.0.2:5555" as user "user_st23".'
        actual_message = mock_log.info.call_args_list[2][0][0]
        self.assertEqual(expected_message, actual_message)

        # 4. Just host provided in the url string
        db_host = 'mongodb://127.0.0.2:5555'
        username = '******'
        password = '******'
        db_setup(db_name=db_name, db_host=db_host, db_port=db_port, username=username,
                 password=password)

        expected_message = 'Connecting to database "st2" @ "127.0.0.2:5555" as user "user_st24".'
        actual_message = mock_log.info.call_args_list[3][0][0]
        self.assertEqual(expected_message, actual_message)

        # 5. Multiple hosts specified as part of connection uri
        db_host = 'mongodb://*****:*****@host1,host2,host3'
        username = None
        password = '******'
        db_setup(db_name=db_name, db_host=db_host, db_port=db_port, username=username,
                 password=password)

        expected_message = ('Connecting to database "st2" @ "host1:27017,host2:27017,host3:27017 '
                            '(replica set)" as user "user6".')
        actual_message = mock_log.info.call_args_list[4][0][0]
        self.assertEqual(expected_message, actual_message)
示例#46
0
# See the License for the specific language governing permissions and
# limitations under the License.

from pecan import load_app
from oslo.config import cfg

from st2auth import config  # noqa
from st2common import log as logging
from st2common.models import db


cfg.CONF(args=['--config-file', '/etc/st2/st2.conf'])

logging.setup(cfg.CONF.auth.logging)

username = cfg.CONF.database.username if hasattr(cfg.CONF.database, 'username') else None
password = cfg.CONF.database.password if hasattr(cfg.CONF.database, 'password') else None
db.db_setup(cfg.CONF.database.db_name, cfg.CONF.database.host, cfg.CONF.database.port,
            username=username, password=password)

pecan_config = {
    'app': {
        'root': 'st2auth.controllers.root.RootController',
        'modules': ['st2auth'],
        'debug': cfg.CONF.auth.debug,
        'errors': {'__force_dict__': True}
    }
}

application = load_app(pecan_config)
示例#47
0
    def test_db_setup_connecting_info_logging(self, mock_log,
                                              mock_mongoengine):
        # Verify that password is not included in the log message
        db_name = 'st2'
        db_port = '27017'
        username = '******'
        password = '******'

        # 1. Password provided as separate argument
        db_host = 'localhost'
        username = '******'
        password = '******'
        db_setup(db_name=db_name,
                 db_host=db_host,
                 db_port=db_port,
                 username=username,
                 password=password)

        expected_message = 'Connecting to database "st2" @ "localhost:27017" as user "user_st2".'
        actual_message = mock_log.info.call_args_list[0][0][0]
        self.assertEqual(expected_message, actual_message)

        # Check for helpful error messages if the connection is successful
        expected_log_message = (
            'Successfully connected to database "st2" @ "localhost:27017" as '
            'user "user_st2".')
        actual_log_message = mock_log.info.call_args_list[1][0][0]
        self.assertEqual(expected_log_message, actual_log_message)

        # 2. Password provided as part of uri string (single host)
        db_host = 'mongodb://*****:*****@127.0.0.2:5555'
        username = None
        password = None
        db_setup(db_name=db_name,
                 db_host=db_host,
                 db_port=db_port,
                 username=username,
                 password=password)

        expected_message = 'Connecting to database "st2" @ "127.0.0.2:5555" as user "user_st22".'
        actual_message = mock_log.info.call_args_list[2][0][0]
        self.assertEqual(expected_message, actual_message)

        expected_log_message = (
            'Successfully connected to database "st2" @ "127.0.0.2:5555" as '
            'user "user_st22".')
        actual_log_message = mock_log.info.call_args_list[3][0][0]
        self.assertEqual(expected_log_message, actual_log_message)

        # 3. Password provided as part of uri string (single host) - username
        # provided as argument has precedence
        db_host = 'mongodb://*****:*****@127.0.0.2:5555'
        username = '******'
        password = None
        db_setup(db_name=db_name,
                 db_host=db_host,
                 db_port=db_port,
                 username=username,
                 password=password)

        expected_message = 'Connecting to database "st2" @ "127.0.0.2:5555" as user "user_st23".'
        actual_message = mock_log.info.call_args_list[4][0][0]
        self.assertEqual(expected_message, actual_message)

        expected_log_message = (
            'Successfully connected to database "st2" @ "127.0.0.2:5555" as '
            'user "user_st23".')
        actual_log_message = mock_log.info.call_args_list[5][0][0]
        self.assertEqual(expected_log_message, actual_log_message)

        # 4. Just host provided in the url string
        db_host = 'mongodb://127.0.0.2:5555'
        username = '******'
        password = '******'
        db_setup(db_name=db_name,
                 db_host=db_host,
                 db_port=db_port,
                 username=username,
                 password=password)

        expected_message = 'Connecting to database "st2" @ "127.0.0.2:5555" as user "user_st24".'
        actual_message = mock_log.info.call_args_list[6][0][0]
        self.assertEqual(expected_message, actual_message)

        expected_log_message = (
            'Successfully connected to database "st2" @ "127.0.0.2:5555" as '
            'user "user_st24".')
        actual_log_message = mock_log.info.call_args_list[7][0][0]
        self.assertEqual(expected_log_message, actual_log_message)

        # 5. Multiple hosts specified as part of connection uri
        db_host = 'mongodb://*****:*****@host1,host2,host3'
        username = None
        password = '******'
        db_setup(db_name=db_name,
                 db_host=db_host,
                 db_port=db_port,
                 username=username,
                 password=password)

        expected_message = (
            'Connecting to database "st2" @ "host1:27017,host2:27017,host3:27017 '
            '(replica set)" as user "user6".')
        actual_message = mock_log.info.call_args_list[8][0][0]
        self.assertEqual(expected_message, actual_message)

        expected_log_message = ('Successfully connected to database "st2" @ '
                                '"host1:27017,host2:27017,host3:27017 '
                                '(replica set)" as user "user6".')
        actual_log_message = mock_log.info.call_args_list[9][0][0]
        self.assertEqual(expected_log_message, actual_log_message)

        # 6. Check for error message when failing to establish a connection
        mock_connect = mock.Mock()
        mock_connect.admin.command = mock.Mock(
            side_effect=ConnectionFailure('Failed to connect'))
        mock_mongoengine.connection.connect.return_value = mock_connect

        db_host = 'mongodb://*****:*****@ "localhost:9797" as user "user_st2".'
        actual_message = mock_log.info.call_args_list[10][0][0]
        self.assertEqual(expected_message, actual_message)

        expected_message = (
            'Failed to connect to database "st2" @ "localhost:9797" as user '
            '"user_st2": Failed to connect')
        actual_message = mock_log.error.call_args_list[0][0][0]
        self.assertEqual(expected_message, actual_message)
示例#48
0
def _setup_db():
    username = cfg.CONF.database.username if hasattr(cfg.CONF.database, 'username') else None
    password = cfg.CONF.database.password if hasattr(cfg.CONF.database, 'password') else None
    db_setup(cfg.CONF.database.db_name, cfg.CONF.database.host, cfg.CONF.database.port,
             username=username, password=password)
示例#49
0
 def initialize(self):
     # 1. Setup db connection
     username = cfg.CONF.database.username if hasattr(cfg.CONF.database, 'username') else None
     password = cfg.CONF.database.password if hasattr(cfg.CONF.database, 'password') else None
     db_setup(cfg.CONF.database.db_name, cfg.CONF.database.host, cfg.CONF.database.port,
              username=username, password=password)
示例#50
0
def main():
    monkey_patch()

    cli_opts = [
        cfg.BoolOpt('sensors', default=False, help='diff sensor alone.'),
        cfg.BoolOpt('actions', default=False, help='diff actions alone.'),
        cfg.BoolOpt('rules', default=False, help='diff rules alone.'),
        cfg.BoolOpt('all',
                    default=False,
                    help='diff sensors, actions and rules.'),
        cfg.BoolOpt('verbose', default=False),
        cfg.BoolOpt(
            'simple',
            default=False,
            help='In simple mode, tool only tells you if content is missing.' +
            'It doesn\'t show you content diff between disk and db.'),
        cfg.StrOpt('pack-dir',
                   default=None,
                   help='Path to specific pack to diff.')
    ]
    do_register_cli_opts(cli_opts)
    config.parse_args()

    username = cfg.CONF.database.username if hasattr(cfg.CONF.database,
                                                     'username') else None
    password = cfg.CONF.database.password if hasattr(cfg.CONF.database,
                                                     'password') else None

    # Connect to db.
    db_setup(cfg.CONF.database.db_name,
             cfg.CONF.database.host,
             cfg.CONF.database.port,
             username=username,
             password=password)

    # Diff content
    pack_dir = cfg.CONF.pack_dir or None
    content_diff = not cfg.CONF.simple

    if cfg.CONF.all:
        _diff_sensors(pack_dir=pack_dir,
                      verbose=cfg.CONF.verbose,
                      content_diff=content_diff)
        _diff_actions(pack_dir=pack_dir,
                      verbose=cfg.CONF.verbose,
                      content_diff=content_diff)
        _diff_rules(pack_dir=pack_dir,
                    verbose=cfg.CONF.verbose,
                    content_diff=content_diff)
        return

    if cfg.CONF.sensors:
        _diff_sensors(pack_dir=pack_dir,
                      verbose=cfg.CONF.verbose,
                      content_diff=content_diff)

    if cfg.CONF.actions:
        _diff_actions(pack_dir=pack_dir,
                      verbose=cfg.CONF.verbose,
                      content_diff=content_diff)

    if cfg.CONF.rules:
        _diff_rules(pack_dir=pack_dir,
                    verbose=cfg.CONF.verbose,
                    content_diff=content_diff)

    # Disconnect from db.
    db_teardown()