def test_0030_config_valid(config): """Test that configs with valid keys and value types pass validation.""" try: get_validated_config(env.ACTIVE_CONFIG_FILEPATH) except (KeyError, ValueError): assert False, f"{env.ACTIVE_CONFIG_FILEPATH} did not pass validation" \ f" when it should have"
def test_0010_config_invalid_keys(config): """Test that config files with invalid/extra keys don't pass validation.""" bad_key_config1 = testutils.yaml_to_dict(env.ACTIVE_CONFIG_FILEPATH) del bad_key_config1['amqp'] bad_key_config1['extra_section'] = True bad_key_config2 = testutils.yaml_to_dict(env.ACTIVE_CONFIG_FILEPATH) del bad_key_config2['vcs'][0]['username'] bad_key_config2['vcs'][0]['extra_property'] = 'a' bad_key_config3 = testutils.yaml_to_dict(env.ACTIVE_CONFIG_FILEPATH) del bad_key_config3['broker']['templates'][0]['mem'] del bad_key_config3['broker']['templates'][0]['name'] bad_key_config3['broker']['templates'][0]['extra_property'] = 0 configs = [ bad_key_config1, bad_key_config2, bad_key_config3 ] for config in configs: testutils.dict_to_yaml_file(config, env.ACTIVE_CONFIG_FILEPATH) try: get_validated_config(env.ACTIVE_CONFIG_FILEPATH) assert False, f"{env.ACTIVE_CONFIG_FILEPATH} passed validation " \ f"when it should not have" except KeyError: pass
def test_0020_config_invalid_value_types(config): """Test that configs with invalid value types don't pass validation.""" bad_values_config1 = testutils.yaml_to_dict(env.ACTIVE_CONFIG_FILEPATH) bad_values_config1['vcd'] = True bad_values_config1['vcs'] = 'a' bad_values_config2 = testutils.yaml_to_dict(env.ACTIVE_CONFIG_FILEPATH) bad_values_config2['vcd']['username'] = True bad_values_config2['vcd']['api_version'] = 123 bad_values_config2['vcd']['port'] = 'a' bad_values_config3 = testutils.yaml_to_dict(env.ACTIVE_CONFIG_FILEPATH) bad_values_config3['vcs'][0]['username'] = True bad_values_config3['vcs'][0]['password'] = 123 bad_values_config3['vcs'][0]['verify'] = 'a' bad_values_config4 = testutils.yaml_to_dict(env.ACTIVE_CONFIG_FILEPATH) bad_values_config4['broker']['templates'][0]['cpu'] = 'a' bad_values_config4['broker']['templates'][0]['name'] = 123 configs = [ bad_values_config1, bad_values_config2, bad_values_config3, bad_values_config4 ] for config in configs: testutils.dict_to_yaml_file(config, env.ACTIVE_CONFIG_FILEPATH) try: get_validated_config(env.ACTIVE_CONFIG_FILEPATH) assert False, f"{env.ACTIVE_CONFIG_FILEPATH} passed validation " \ f"when it should not have" except ValueError: pass
def check(ctx, config, check_install, template): """Validate CSE configuration.""" config_dict = None try: config_dict = get_validated_config(config) except (NotAcceptableException, VcdException, ValueError, KeyError, TypeError) as err: click.secho(str(err), fg='red') except AmqpConnectionError as err: click.secho(str(err), fg='red') click.secho("check config file amqp section.", fg='red') except requests.exceptions.ConnectionError: click.secho("Cannot connect to vCD host (check config file vCD host).", fg='red') except vim.fault.InvalidLogin: click.secho( "vCenter login failed (check config file vCenter " "username/password).", fg='red') if not check_install or config_dict is None: return try: check_cse_installation(config_dict, check_template=template) except EntityNotFoundException: click.secho("CSE installation is invalid", fg='red')
def check(ctx, config, check_install, template): """Validate CSE config file.""" try: check_python_version(ConsoleMessagePrinter()) except Exception as err: click.secho(str(err), fg='red') sys.exit(1) config_dict = None try: config_dict = get_validated_config( config, msg_update_callback=ConsoleMessagePrinter()) except (NotAcceptableException, VcdException, ValueError, KeyError, TypeError) as err: click.secho(str(err), fg='red') except AmqpConnectionError as err: click.secho(str(err), fg='red') click.secho("check config file amqp section.", fg='red') except requests.exceptions.ConnectionError as err: click.secho(f"Cannot connect to {err.request.url}.", fg='red') except vim.fault.InvalidLogin: click.secho("vCenter login failed (check config file vCenter " "username/password).", fg='red') if not check_install or config_dict is None: return try: check_cse_installation(config_dict, check_template=template, msg_update_callback=ConsoleMessagePrinter()) except EntityNotFoundException: click.secho("CSE installation is invalid", fg='red')
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')
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']) 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')
def run(self, msg_update_callback=None): self.config = get_validated_config( self.config_file, msg_update_callback=msg_update_callback) if self.should_check_config: check_cse_installation(self.config, msg_update_callback=msg_update_callback) 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) if msg_update_callback: msg_update_callback.general_no_color(message) LOGGER.info(message) if self.config.get('pks_config'): pks_config = self.config.get('pks_config') self.pks_cache = PksCache( pks_servers=pks_config.get('pks_api_servers', []), pks_accounts=pks_config.get('pks_accounts', []), pvdcs=pks_config.get('pvdcs', []), orgs=pks_config.get('orgs', []), nsxt_servers=pks_config.get('nsxt_servers', [])) 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']) name = 'MessageConsumer-%s' % n t = Thread(name=name, target=consumer_thread, args=(c, )) t.daemon = True t.start() LOGGER.info("Started thread {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(f"Number of threads started: {len(self.threads)}") self._state = ServerState.RUNNING while True: try: time.sleep(1) if self._state == ServerState.STOPPING and \ self.active_requests_count() == 0: break except KeyboardInterrupt: break except Exception: if msg_update_callback: msg_update_callback.general_no_color( 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 self._state = ServerState.STOPPED LOGGER.info("Done")