def test_read_settings_file(): # Arrange # Default path should be fine for the tests. # Act result = settings.read_settings_file() # Assert assert isinstance(result, dict) assert result.get("include")
def deserialize_raw(collection_type: str): """ Get a collection from mongodb and parse it into an object. :param collection_type: The collection type to fetch. :return: The first element of the collection requested. """ if collection_type == "settings": return settings.read_settings_file() else: __connect() collection = mongodb[collection_type] return collection.find()
def test_settingsfile_migration_content(tmpdir: pathlib.Path): # Arrange src = "/test_dir/tests/test_data/settings_old" dst = os.path.join(tmpdir, "settings") shutil.copyfile(src, dst) # Act result_settings = settings.read_settings_file(filepath=dst) settings_obj = Settings().from_dict(result_settings) clean_settings = settings.validate_settings(settings_obj.to_dict()) # Assert assert isinstance(clean_settings, dict) assert "include" in result_settings
def test_settingsfile_migration_extension(tmpdir: pathlib.Path): # Arrange src = "/test_dir/tests/test_data/settings_old" dst = os.path.join(tmpdir, "settings") shutil.copyfile(src, dst) expected_path = os.path.join(tmpdir, "settings.yaml") # Act result_settings = settings.read_settings_file(filepath=dst) # Assert assert os.path.exists(expected_path) assert isinstance(result_settings, dict) assert "include" in result_settings
def test_settingsfile_migrate_gpxe_ipxe(): # Arrange new_settings = "/etc/cobbler/settings.yaml" old_settings = "/test_dir/tests/test_data/settings_old" # adjust for test container %s/code/test_dir/ # Act with open(new_settings) as main_settingsfile: content_new = yaml.safe_load(main_settingsfile.read()) with open(old_settings) as old_settingsfile: content_old = yaml.safe_load(old_settingsfile.read()) new_settings_file = settings.read_settings_file(new_settings) direct_settings = settings.__migrate_settingsfile_gpxe_ipxe(content_new) # Assert assert isinstance(content_old, dict) and "enable_gpxe" in content_old assert isinstance(direct_settings, dict) and "enable_ipxe" in direct_settings assert "enable_gpxe" not in direct_settings assert isinstance(new_settings_file, dict) and "enable_ipxe" in new_settings_file assert "enable_gpxe" not in new_settings_file
def deserialize_raw(collection_types: str): """ Loads a collection from the disk. :param collection_types: The type of collection to load. :return: The loaded dictionary. """ if collection_types == "settings": return settings.read_settings_file() else: results = [] path = os.path.join(libpath, collection_types) all_files = glob.glob("%s/*.json" % path) for f in all_files: with open(f) as file_descriptor: json_data = file_descriptor.read() _dict = json.loads(json_data) results.append(_dict) return results
def application(environ, start_response): if 'VIRTUALENV' in environ and environ['VIRTUALENV'] != "": # VIRTUALENV Support # see http://code.google.com/p/modwsgi/wiki/VirtualEnvironments import site import distutils.sysconfig site.addsitedir(distutils.sysconfig.get_python_lib(prefix=environ['VIRTUALENV'])) # Now all modules are available even under a virtualenv from cobbler.services import CobblerSvc my_uri = urllib.parse.unquote(environ['REQUEST_URI']) form = {} # canonicalizes uri, mod_python does this, mod_wsgi does not my_uri = os.path.realpath(my_uri) tokens = my_uri.split("/") tokens = tokens[3:] label = True field = "" for t in tokens: if label: field = t else: form[field] = t label = not label form["query_string"] = urllib.parse.parse_qs(environ['QUERY_STRING']) # This MAC header is set by anaconda during a kickstart booted with the # kssendmac kernel option. The field will appear here as something # like: eth0 XX:XX:XX:XX:XX:XX mac_counter = 0 remote_macs = [] mac_header = "HTTP_X_RHN_PROVISIONING_MAC_%d" % mac_counter while environ.get(mac_header, None): remote_macs.append(environ[mac_header]) mac_counter = mac_counter + 1 mac_header = "HTTP_X_RHN_PROVISIONING_MAC_%d" % mac_counter form["REMOTE_MACS"] = remote_macs # REMOTE_ADDR isn't a required wsgi attribute so it may be naive to assume # it's always present in this context. form["REMOTE_ADDR"] = environ.get("REMOTE_ADDR", None) # Read config for the XMLRPC port to connect to: ydata = settings.read_settings_file() remote_port = ydata.get("xmlrpc_port", 25151) # instantiate a CobblerWeb object cw = CobblerSvc(server="http://127.0.0.1:%s" % remote_port) # check for a valid path/mode # handle invalid paths gracefully mode = form.get('op', 'index') # TODO: We could do proper exception handling here and return # corresponding HTTP status codes: status = "200 OK" # Execute corresponding operation on the CobblerSvc object: func = getattr(cw, mode) try: content = func(**form) if content.find("# *** ERROR ***") != -1: status = '500 SERVER ERROR' print("possible cheetah template error") # TODO: Not sure these strings are the right ones to look for... elif content.find("# profile not found") != -1 or \ content.find("# system not found") != -1 or \ content.find("# object not found") != -1: print(("content not found: %s" % my_uri)) status = "404 NOT FOUND" except xmlrpc.server.Fault as err: status = "500 SERVER ERROR" content = err.faultString # req.content_type = "text/plain;charset=utf-8" response_headers = [('Content-type', 'text/plain;charset=utf-8'), ('Content-Length', str(len(content)))] start_response(status, response_headers) return [content.encode('utf-8')]