def test_add_user(): with get_test_volttron_home(messagebus='zmq') as vhome: webuserpath = os.path.join(vhome, ___WEB_USER_FILE_NAME__) assert not os.path.exists(webuserpath) username_test = "test" username_test_passwd = "passwd" adminep = AdminEndpoints() adminep.add_user(username_test, username_test_passwd, ['admin']) # since add_user is async with persistance we use sleep to allow the write # gevent.sleep(0.01) assert os.path.exists(webuserpath) with open(webuserpath) as fp: users = jsonapi.load(fp) assert len(users) == 1 assert users.get(username_test) is not None user = users.get(username_test) objid = id(user) assert ['admin'] == user['groups'] assert user['hashed_password'] is not None original_hashed_passwordd = user['hashed_password'] # raise ValueError if not overwrite == True with pytest.raises( ValueError, match= f"The user {username_test} is already present and overwrite not set to True" ): adminep.add_user(username_test, username_test_passwd, ['admin']) # make sure the overwrite works because we are changing the group adminep.add_user(username_test, username_test_passwd, ['read_only', 'jr-devs'], overwrite=True) assert os.path.exists(webuserpath) with open(webuserpath) as fp: users = jsonapi.load(fp) assert len(users) == 1 assert users.get(username_test) is not None user = users.get(username_test) assert objid != id(user) assert ['read_only', 'jr-devs'] == user['groups'] assert user['hashed_password'] is not None assert original_hashed_passwordd != user['hashed_password']
def update_ca_db(self, cert, ca_name, serial): """ Update the CA db with details of the file that the ca signed. :param cert: cert that was signed by ca_name :param ca_name: name of the ca that signed the cert """ db_file = self.ca_db_file(ca_name) ca_db = {} dn = "C={}/ST={}/L={}/O={}/OU={}/CN={}".format( _get_cert_attribute_value(cert, NameOID.COUNTRY_NAME), _get_cert_attribute_value(cert, NameOID.STATE_OR_PROVINCE_NAME), _get_cert_attribute_value(cert, NameOID.LOCALITY_NAME), _get_cert_attribute_value(cert, NameOID.ORGANIZATION_NAME), _get_cert_attribute_value(cert, NameOID.ORGANIZATIONAL_UNIT_NAME), _get_cert_attribute_value(cert, NameOID.COMMON_NAME)) if os.path.exists(db_file): with open(db_file, "r") as f: ca_db = jsonapi.load(f) entries = ca_db.get(dn, {}) entries['status'] = "valid" entries['expiry'] = cert.not_valid_after.strftime("%Y-%m-%d " "%H:%M:%S.%f%z") entries['serial_number'] = cert.serial_number ca_db[dn] = entries with open(db_file, 'w+') as outfile: jsonapi.dump(ca_db, outfile, indent=4) with open(self.ca_serial_file(ca_name), "w+") as f: f.write(str(serial+1)) # next available serial is current + 1
def load(self): try: with open(self.filename, 'r') as json_file: return jsonapi.load(json_file) except ValueError: # If the file is empty jsonapi.load will raise ValueError return {}
def metadata(self): '''Parse package.dist-info/metadata.json and return a dictionary.''' try: return self._metadata except AttributeError: with open(os.path.join(self.distinfo, 'metadata.json')) as file: self._metadata = jsonapi.load(file) return self._metadata
def do_load_volttron(self, line): """ Load existed driver config and csv to volttron (Make sure volttron is running). Option to add file if the file does not exist. """ driver_name = line if not driver_name: print( "\nList of all existed drivers in the selected config directory: " ) self.do_list_drivers('') print("\nEnter driver name: ", end='') driver_name = input().lower() # Load driver config config_file = self.get_existed_file(self._directories['config_dir'], "{0}.config".format(driver_name)) driver_name = config_file.split('.')[0] config_dir = self._directories['config_dir'] config_path = "{0}/{1}".format(config_dir, config_file) if config_file: self._sh( 'volttron-ctl config store platform.driver devices/{0} {1}'. format(driver_name, config_path)) else: self.do_load_volttron(driver_name) with open(config_path, 'r') as config_file: driver_config = jsonapi.load(config_file) csv_config = driver_config['registry_config'].split('//')[1] csv_map = driver_config['driver_config']['register_map'].split('//')[1] csv_dir = self._directories['csv_dir'] # Load registry_config csv_config = self.get_existed_file(self._directories['csv_dir'], csv_config) if csv_config: self._sh( 'volttron-ctl config store platform.driver {1} {0}/{1} --csv'. format(csv_dir, csv_config)) else: print( 'Please add csv file {1} to the directory {0}/{1} and redo load_volttron' .format(csv_dir, csv_config)) # Load register_map csv_map = self.get_existed_file(self._directories['csv_dir'], csv_map) if csv_map: self._sh( 'volttron-ctl config store platform.driver {1} {0}/{1} --csv'. format(csv_dir, csv_map)) self.list_volttron_config("Load successful!") else: print( 'Please add csv file {1} to the directory {0}/{1} and redo load_volttron' .format(csv_dir, csv_map))
def test_invalid_unicode_key(keystore_instance1): """ Bypass KeyStore API and directly edit key to make it unicode. The keys should always be ASCII characters. This is a precaution against a corrupted key store. """ with open(keystore_instance1.filename) as fp: keystore_json = jsonapi.load(fp) keystore_json['public'] = '\u0100' keystore_instance1.update(keystore_json) assert keystore_instance1.public is None
def _read_execreqs(self, dist_info): execreqs_json = os.path.join(dist_info, 'execreqs.json') try: with ignore_enoent, open(execreqs_json) as file: return jsonapi.load(file) except Exception as exc: msg = 'error reading execution requirements: {}: {}'.format( execreqs_json, exc) _log.error(msg) raise ValueError(msg) _log.warning('missing execution requirements: %s', execreqs_json) return {}
def auth_instance(volttron_instance): with open(os.path.join(volttron_instance.volttron_home, "auth.json"), 'r') as f: auth_file = jsonapi.load(f) print(auth_file) try: yield volttron_instance finally: with with_os_environ(volttron_instance.env): with open( os.path.join(volttron_instance.volttron_home, "auth.json"), 'w') as f: jsonapi.dump(auth_file, f)
def send_json(self, pdefs, func_def_path, send_json_path='', send_json=None): """ Send a json in order for testing purpose. :param pdefs: point definitions :param func_def_path: path to function definition :param send_json_path: path to json that will be sent to the outstation :param send_json: json that will be sent to the outstation :return: """ if send_json_path: send_json = jsonapi.load(open(send_json_path), object_pairs_hook=OrderedDict) try: function_id = send_json['function_id'] except KeyError: raise MesaMasterTestException('Missing function_id') fdefs = FunctionDefinitions(pdefs, function_definitions_path=func_def_path) try: fdef = fdefs[function_id] except KeyError: raise MesaMasterTestException( 'Invalid function_id {}'.format(function_id)) step = 1 for name, value in send_json.items(): if name not in ['name', 'function_id', 'function_name']: pdef = pdefs.point_named(name) step_def = fdef[pdef] if step != step_def.step_number: raise MesaMasterTestException( "Step not in order: {}".format(step)) if type(value) == list: self.send_array(value, pdef) else: send_func = self.SEND_FUNCTIONS.get( step_def.fcodes[0] if step_def.fcodes else DIRECT_OPERATE, None) self.send_command(send_func, pdef, value) step += 1
def __init__(self, func_test_path='', func_test_json=None, func_def_path='', point_def_path=''): self.func_def_path = func_def_path or FUNCTION_DEF_PATH self.point_definitions = PointDefinitions( point_definitions_path=point_def_path or POINT_DEF_PATH) self.ftest = func_test_json or jsonapi.load(open(func_test_path)) self.function_id = self.ftest.get('function_id', self.ftest.get('id', None)) self.function_name = self.ftest.get('function_name', self.ftest.get('name', None)) self.name = self.ftest.get('name', None) self.points = { k: v for k, v in self.ftest.items() if k not in ["name", "function_id", "function_name", "id"] }
def test_set_platform_password_setup(): with get_test_volttron_home(messagebus='zmq') as vhome: # Note these passwords are not right so we expect to be redirected back to the # first.html params = urlencode( dict(username='******', password1='goodwin', password2='wowsa')) env = get_test_web_env("/admin/setpassword", method='POST') # , input_data=input) jinja_mock = env['JINJA2_TEMPLATE_ENV'] adminep = AdminEndpoints() response = adminep.admin(env, params) assert 'Location' not in response.headers assert 200 == response.status_code assert 'text/html' == response.headers.get('Content-Type') assert 1 == jinja_mock.get_template.call_count assert ('first.html', ) == jinja_mock.get_template.call_args[0] assert 1 == jinja_mock.get_template.return_value.render.call_count jinja_mock.reset_mock() # Now we have the correct password1 and password2 set we expect to redirected to # /admin/login.html params = urlencode( dict(username='******', password1='wowsa', password2='wowsa')) env = get_test_web_env("/admin/setpassword", method='POST') # , input_data=input) # expect Location and Content-Type headers to be set response = adminep.admin(env, params) assert 3 == len(response.headers) assert 'Location' in response.headers assert '/admin/login.html' == response.headers.get('Location') assert 302 == response.status_code webuserpath = os.path.join(vhome, 'web-users.json') with open(webuserpath) as wup: users = jsonapi.load(wup) assert users.get('bart') is not None user = users.get('bart') assert user['hashed_password'] is not None assert argon2.verify("wowsa", user['hashed_password'])
def prep_config(volttron_home): src_driver = os.getcwd( ) + '/services/core/MasterDriverAgent/example_configurations/test_fakedriver.config' new_driver = volttron_home + '/test_fakedriver.config' shutil.copy(src_driver, new_driver) with open(new_driver, 'r+') as f: config = jsonapi.load(f) config['registry_config'] = os.getcwd( ) + '/services/core/MasterDriverAgent/example_configurations/fake.csv' f.seek(0) f.truncate() jsonapi.dump(config, f) master_config = { "agentid": "master_driver", "driver_config_list": [new_driver] } return master_config
def do_driver_config(self, line): """ Get the driver config for the selected driver Option to select the driver if no selected driver found Get a specific driver config: do_driver_config <name> <name>: name of a driver config in config_dir List all driver before selecting a name: do_driver_config """ driver_name = line if not driver_name: self.do_list_drivers('') print("\nEnter the driver name: ", end='') driver_name = input() existed = False config_dir = self.get_existed_directory( self._directories['config_dir'], 'config_dir') if config_dir: for f in os.listdir(config_dir): if f.endswith('.config') and f.split('.')[0] == driver_name: existed = True with open("{0}/{1}.config".format(config_dir, driver_name), 'r') as config_file: config_dict = jsonapi.load(config_file) print("\nDRIVER: {0}".format(driver_name.upper())) for k in config_dict.keys(): if k != 'driver_config': print("{0:17}: {1}".format(k, config_dict[k])) else: driver_config = config_dict['driver_config'] print("{0:17}:".format(k)) for key in driver_config.keys(): print("{0:17} {1:17}: {2}".format( '', key, driver_config[key])) if not existed: print("The driver '{0}' does not exist.".format(driver_name)) else: print("Directory config_dir '{0}' does not exist.".format( self._directories['config_dir']))
if menu == 1: pass # kafka_topic, device_point, new_new_value = raw_input('input kafka_topic, device_point, new_new_value: ').split(' ') # # msg = { # 'message': 'message from VOLTTRON to Cloud', # 'new_value': new_value, # 'device_point': device_point # } # print('msg: {}\n'.format(msg)) # # send message to broker # producer.send(kafka_topic, msg) elif menu == 2: with open('message.json') as f: data = jsonapi.load(f) kafka_topic = data["kafka_topic"] message_list = data["message_list"] for message in message_list: sender = message['sender'] topic = message['topic'] value = message['value'] description = message['description'] msg = { 'kafka_message_sender': sender, 'topic': topic, 'value': value, 'description': description } print('msg: {}'.format(msg))
def convert_json_file_to_dict(json_file): """Convert a json file to a dictionary.""" send_json = os.path.abspath(os.path.join(os.path.dirname(__file__), 'data', json_file)) return jsonapi.load(open(send_json))
import pytest from volttron.platform import jsonapi, get_ops from volttrontesting.utils.utils import poll_gevent_sleep _test_config = { "base_topic": "test1/sysmon", "cpu_check_interval": 1, "memory_check_interval": 1, "disk_check_interval": 1, "disk_path": "/" } config_path = os.path.join(get_ops("SysMonAgent"), "sysmonagent.config") with open(config_path, "r") as config_file: default_config_json = jsonapi.load(config_file) assert isinstance(default_config_json, dict) @pytest.fixture() def sysmon_tester_agent(request, volttron_instance, tmpdir): """ Fixture used for setting up SysMonAgent and tester agent """ config = tmpdir.mkdir('config').join('config') config.write(jsonapi.dumps(_test_config)) sysmon_uuid = volttron_instance.install_agent( agent_dir=get_ops("SysMonAgent"), config_file=_test_config, start=True) agent = volttron_instance.build_agent()