def test_0020_config_invalid_value_types(self):
        # tests for when config file has incorrect value types
        invalid_values_config1 = yaml_to_dict(ACTIVE_CONFIG_FILEPATH)
        invalid_values_config1['vcd'] = True
        invalid_values_config1['vcs'] = 'a'

        invalid_values_config2 = yaml_to_dict(ACTIVE_CONFIG_FILEPATH)
        invalid_values_config2['vcd']['username'] = True
        invalid_values_config2['vcd']['api_version'] = 123
        invalid_values_config2['vcd']['port'] = 'a'

        invalid_values_config3 = yaml_to_dict(ACTIVE_CONFIG_FILEPATH)
        invalid_values_config3['vcs'][0]['username'] = True
        invalid_values_config3['vcs'][0]['password'] = 123
        invalid_values_config3['vcs'][0]['verify'] = 'a'

        invalid_values_config4 = yaml_to_dict(ACTIVE_CONFIG_FILEPATH)
        invalid_values_config4['broker']['templates'][0]['cpu'] = 'a'
        invalid_values_config4['broker']['templates'][0]['name'] = 123

        configs = [
            invalid_values_config1, invalid_values_config2,
            invalid_values_config3, invalid_values_config4
        ]

        for config_dict in configs:
            dict_to_yaml_file(config_dict, ACTIVE_CONFIG_FILEPATH)
            try:
                get_validated_config(ACTIVE_CONFIG_FILEPATH)
                print(f"{ACTIVE_CONFIG_FILEPATH} passed validation when "
                      f"it should not have")
                assert False
            except ValueError:
                pass
    def test_0010_config_invalid_keys(self):
        """Tests that config file with invalid/extra keys or invalid value
        types do not pass config validation.
        """

        # 3 tests for when config file has missing or extra keys
        invalid_keys_config1 = yaml_to_dict(ACTIVE_CONFIG_FILEPATH)
        del invalid_keys_config1['amqp']
        invalid_keys_config1['extra_section'] = True

        invalid_keys_config2 = yaml_to_dict(ACTIVE_CONFIG_FILEPATH)
        del invalid_keys_config2['vcs'][0]['username']
        invalid_keys_config2['vcs'][0]['extra_property'] = 'a'

        invalid_keys_config3 = yaml_to_dict(ACTIVE_CONFIG_FILEPATH)
        del invalid_keys_config3['broker']['templates'][0]['mem']
        del invalid_keys_config3['broker']['templates'][0]['name']
        invalid_keys_config3['broker']['templates'][0]['extra_property'] = 0

        configs = [
            invalid_keys_config1, invalid_keys_config2, invalid_keys_config3
        ]

        for config_dict in configs:
            dict_to_yaml_file(config_dict, ACTIVE_CONFIG_FILEPATH)
            try:
                get_validated_config(ACTIVE_CONFIG_FILEPATH)
                print(f"{ACTIVE_CONFIG_FILEPATH} passed validation when "
                      f"it should not have")
                assert False
            except KeyError:
                pass
 def test_0030_config_valid(self):
     """Tests that config file with valid keys and value types pass
     config validation.
     """
     try:
         get_validated_config(ACTIVE_CONFIG_FILEPATH)
     except (KeyError, ValueError):
         print(f"{ACTIVE_CONFIG_FILEPATH} did not pass validation "
               f"when it should have")
         assert False
def check(ctx, config, check_install, template):
    """Validate CSE configuration."""
    try:
        config_dict = get_validated_config(config)
    except (KeyError, ValueError, Exception):
        # TODO replace Exception with specific (see validate_amqp_config)
        click.secho(f"Config file '{config}' is invalid", fg='red')
        return
    if check_install:
        try:
            check_cse_installation(config_dict, check_template=template)
        except EntityNotFoundException:
            click.secho("CSE installation is invalid", fg='red')
Esempio n. 5
0
    def run(self):
        self.config = get_validated_config(self.config_file)
        if self.should_check_config:
            check_cse_installation(self.config)

        log_file = 'cse.log'
        handler = RotatingFileHandler(log_file,
                                      maxBytes=10 * 1024 * 1024,
                                      backupCount=10)
        logging.basicConfig(level=logging.DEBUG,
                            format='%(asctime)s %(name)-12s %(lineno)s '
                            '%(levelname)-8s %(message)s',
                            datefmt='%m-%d %H:%M:%S',
                            handlers=(handler, ))
        logging.getLogger("pika").setLevel(logging.WARNING)

        message = """
Container Service Extension for vCloud Director running
config file: {config_file}
see file log file for details: {log_file}
waiting for requests, press Ctrl+C to finish
""".format(config_file=self.config_file, log_file=log_file)

        signal.signal(signal.SIGINT, signal_handler)
        click.secho(message)
        LOGGER.info(message)

        amqp = self.config['amqp']
        num_consumers = self.config['service']['listeners']

        for n in range(num_consumers):
            try:
                c = MessageConsumer(amqp['host'], amqp['port'], amqp['ssl'],
                                    amqp['vhost'], amqp['username'],
                                    amqp['password'], amqp['exchange'],
                                    amqp['routing_key'], self.config,
                                    self.config['vcd']['verify'],
                                    self.config['vcd']['log'])
                name = 'MessageConsumer-%s' % n
                t = Thread(name=name, target=consumer_thread, args=(c, ))
                t.daemon = True
                t.start()
                LOGGER.info('started thread %s', t.ident)
                self.threads.append(t)
                self.consumers.append(c)
                time.sleep(0.25)
            except KeyboardInterrupt:
                break
            except Exception:
                print(traceback.format_exc())

        LOGGER.info('num of threads started: %s', len(self.threads))

        self.is_enabled = True

        while True:
            try:
                time.sleep(1)
                if self.should_stop and self.active_requests_count() == 0:
                    break
            except KeyboardInterrupt:
                break
            except Exception:
                click.secho(traceback.format_exc())
                sys.exit(1)

        LOGGER.info('stop detected')
        LOGGER.info('closing connections...')
        for c in self.consumers:
            try:
                c.stop()
            except Exception:
                pass
        LOGGER.info('done')
Esempio n. 6
0
    def run(self):
        self.config = get_validated_config(self.config_file)
        if self.should_check_config:
            check_cse_installation(self.config)

        configure_server_logger()

        message = f"Container Service Extension for vCloudDirector" \
                  f"\nServer running using config file: {self.config_file}" \
                  f"\nLog files: {SERVER_INFO_LOG_FILEPATH}, " \
                  f"{SERVER_DEBUG_LOG_FILEPATH}" \
                  f"\nwaiting for requests (ctrl+c to close)"

        signal.signal(signal.SIGINT, signal_handler)
        click.secho(message)
        LOGGER.info(message)

        amqp = self.config['amqp']
        num_consumers = self.config['service']['listeners']

        for n in range(num_consumers):
            try:
                c = MessageConsumer(amqp['host'], amqp['port'], amqp['ssl'],
                                    amqp['vhost'], amqp['username'],
                                    amqp['password'], amqp['exchange'],
                                    amqp['routing_key'], self.config,
                                    self.config['vcd']['verify'],
                                    self.config['vcd']['log'])
                name = 'MessageConsumer-%s' % n
                t = Thread(name=name, target=consumer_thread, args=(c, ))
                t.daemon = True
                t.start()
                LOGGER.info('started thread %s', t.ident)
                self.threads.append(t)
                self.consumers.append(c)
                time.sleep(0.25)
            except KeyboardInterrupt:
                break
            except Exception:
                print(traceback.format_exc())

        LOGGER.info('num of threads started: %s', len(self.threads))

        self.is_enabled = True

        while True:
            try:
                time.sleep(1)
                if self.should_stop and self.active_requests_count() == 0:
                    break
            except KeyboardInterrupt:
                break
            except Exception:
                click.secho(traceback.format_exc())
                sys.exit(1)

        LOGGER.info('stop detected')
        LOGGER.info('closing connections...')
        for c in self.consumers:
            try:
                c.stop()
            except Exception:
                pass
        LOGGER.info('done')