def test_get_config_error(): """ Test get_config with json type. """ with pytest.raises(ValueError): etcdgo.get_config(None, "json") with pytest.raises(ValueError): client = etcd3.Etcd3Client() etcdgo.get_config(client, None) with pytest.raises(NotImplementedError): client = etcd3.Etcd3Client() etcdgo.get_config(client, "txt")
def test_get_config_yaml(): """ Test get_config with yaml type. """ client = etcd3.Etcd3Client() obj = etcdgo.get_config(client, "yaml") assert isinstance(obj, etcdgo.config.YamlConfig)
def test_get_config_ini(): """ Test get_config with ini type. """ client = etcd3.Etcd3Client() obj = etcdgo.get_config(client, "ini") assert isinstance(obj, etcdgo.config.IniConfig)
def test_get_config_json(): """ Test get_config with json type. """ client = etcd3.Etcd3Client() obj = etcdgo.get_config(client, "json") assert isinstance(obj, etcdgo.config.JsonConfig)
def test_get_config_basefolder(mocker): """ Test get_config using basefolder. """ mocker.patch('etcdgo.config.YamlConfig.__init__', return_value=None) client = etcd3.Etcd3Client() test_basefolder = "/config_test" etcdgo.get_config(client, "yaml", basefolder=test_basefolder) etcdgo.config.YamlConfig.__init__.assert_called_with( client, basefolder=test_basefolder)
def __init__(self, fhost, fid, etcdhost="etcdv3service.sas.pvt", etcdport=2379, logger=None): self.fhost = fhost self.fid = fid #: Set trigger to stop polling self._poll_stop_trigger = threading.Event() #: Flag indicating polling is ongoing self._is_polling = threading.Event() #: Set trigger to pause polling (while executing a command) self._poll_pause_trigger = threading.Event() #: Flag indicating polling has paused self._poll_is_paused = threading.Event() #: List of etcd watch IDs, used to kill watch processes self._etcd_watch_ids = [] if logger is None: self.logger = logging.getLogger("Snap2FengineEtcdClient:%s" % self.fhost) stderr_handler = logging.StreamHandler(sys.stderr) self.logger.addHandler(stderr_handler) self.set_log_level("info") else: self.logger = logger try: self.feng = snap2_fengine.Snap2Fengine(fhost) except: self.logger.exception("Couldn't initialize F-Engine on host %s" % self.fhost) self.feng = None raise RuntimeError("Couldn't initialize F-Engine on host %s" % self.fhost) # If the SNAP isn't connected even though the instantiation worked, give up. TODO: be smarter try: is_connected = self.feng.is_connected() except: raise RuntimeError("Couldn't query SNAP connection status on host %s" % self.fhost) if not is_connected: raise RuntimeError("Couldn't connect to F-Engine on host %s" % self.fhost) self.ec = etcd3.Etcd3Client(host=etcdhost, port=etcdport) try: val, meta = self.ec.get('foo') except: self.logger.exception("Failed to connect to Etcd server on host %s" % etcdhost) raise self.cmd_key = ETCD_CMD_ROOT + "%d" % self.fid self.cmd_resp_key = ETCD_RESP_ROOT + "%d" % self.fid self.mon_key = ETCD_MON_ROOT + "%d" % self.fid self.logger.debug("Command key is %s" % self.cmd_key) self.logger.debug("Command response key is %s" % self.cmd_resp_key) self.logger.debug("Monitor key root is %s" % self.mon_key)
def Main(): arg_p = getArgParser() parsed_args = arg_p.parse_args() try: # instantiate a etcd client (version 3.x api used) client3 = etcd3.Etcd3Client(host=parsed_args.host, port=parsed_args.port) # check which action command is used if parsed_args.action_name == "subscribe": subscriber(client3, parsed_args.key, timeout=parsed_args.timeout) elif parsed_args.action_name == "publish": publisher(client3, parsed_args.key, pub_val=parsed_args.value) elif parsed_args.action_name == "delete": publisher(client3, parsed_args.key, action=parsed_args.action_name) except ConnectionFailedError as ce: print "Connection failed: ", ce except Etcd3Exception as ee: print "Some exception in etcd3 client: ", ee
def __init__(self, etcdhost="etcdv3service.sas.pvt", logger=None, noetcd=False): if logger is None: self.logger = logging.getLogger("Snap2FengineEtcd") stderr_handler = logging.StreamHandler(sys.stderr) self.logger.addHandler(stderr_handler) self.set_log_level("info") else: self.logger = logger self.noetcd = noetcd if not noetcd: # Connect to ETCD and ping the connection self.ec = etcd3.Etcd3Client(etcdhost) try: val, meta = self.ec.get('foo') except: self.logger.error("Failed to connect to Etcd server on host %s" % etcdhost) raise else: self.ec = None self._fengs = {} #: List of active Fengine connections
def _callback(type): obj = etcdgo.get_config(etcd3.Etcd3Client(), type, basefolder="/config_test") return obj
def cli(args, hostname, port, base_folder): """ Etcdgo command line to push/pull configurations. """ args.client = etcd3.Etcd3Client(hostname, port) args.base_folder = base_folder
def get_etcd_client(self) -> etcd3.Etcd3Client: return etcd3.Etcd3Client(**self.dict['etcd'])
def __init__(self): self.client = etcd3.Etcd3Client(host=ETCDHOST, port=ETCDPORT, user=ETCDUSER, password=ETCDPASSWD)
def test_adminrouter_grpc_proxy_port(docker_backend: Docker, artifact_path: Path, request: SubRequest, log_dir: Path, rsa_keypair: Tuple[str, str], jwt_token: Callable[[str, str, int], str] ) -> None: random_port = random.randint(63000, 64000) with Cluster( cluster_backend=docker_backend, masters=1, agents=0, public_agents=0, ) as cluster: uid = str(uuid.uuid4()) config = { 'superuser_service_account_uid': uid, 'superuser_service_account_public_key': rsa_keypair[1], 'adminrouter_grpc_proxy_port': '{}'.format(random_port), } cluster.install_dcos_from_path( dcos_installer=artifact_path, dcos_config={ **cluster.base_config, **config, }, output=Output.LOG_AND_CAPTURE, ip_detect_path=docker_backend.ip_detect_path, ) wait_for_dcos_oss( cluster=cluster, request=request, log_dir=log_dir, ) master = next(iter(cluster.masters)) master_ip = master.public_ip_address login_endpoint = 'http://{}/acs/api/v1/auth/login'.format(master_ip) service_login_token = jwt_token(uid, rsa_keypair[0], 30) token_response = requests.post( login_endpoint, json={'uid': uid, 'token': service_login_token} ) assert token_response.status_code == 200 token = token_response.json().get('token') etcd = etcd3.Etcd3Client( host=list(cluster.masters)[0].public_ip_address, port=random_port, timeout=None, ) etcd.metadata = (('authorization', 'token={}'.format(token)),) etcd.watcher = etcd3.watch.Watcher( etcd3.etcdrpc.WatchStub(etcd.channel), timeout=etcd.timeout, call_credentials=etcd.call_credentials, metadata=etcd.metadata ) value, meta = etcd.get('probably-invalid-key') assert value is None assert meta is None