def has_permission(self, user=None): """ Check permission. :param user: Username """ if user is None: user = user_get_username() return bool(user == self.user)
def test_real_ids(): """Real IDs.""" os.environ.pop("DOCKNV_TEST_ID") os.environ.pop("DOCKNV_TEST_USERNAME") assert user_get_username() != "test" os.environ["DOCKNV_TEST_ID"] = "1" os.environ["DOCKNV_TEST_USERNAME"] = "******"
def test_session_config(): """Session config.""" with using_temporary_directory() as tempdir: project_path = tempdir os.environ["DOCKNV_USER_PATH"] = project_path session = UserSession.load_from_path(user_get_username(), project_path) # No configuration set assert session.get_current_configuration() is None # Setting one configuration session.set_current_configuration("pouet") assert session.get_current_configuration() == "pouet" # Unset session.unset_current_configuration() assert session.get_current_configuration() is None
def __init__(self, project_path, config_data): """ Project data constructor. :param project_path: Project path (str) :param config_data: Config data (dict) """ self.project_path = project_path self.project_name = project_get_name_from_path(project_path) self.schemas = SchemaCollection.load_from_data( config_data.get("schemas", [])) self.config_data = config_data self.session = UserSession.load_from_path(user_get_username(), project_path) self.database = Database.load_from_project(self) self.lifecycle = ProjectLifecycle(self) self.images = ImageCollection.load_from_project(self)
def create( self, name, environment="default", schemas=None, services=None, volumes=None, networks=None, namespace=None, ): """ Create configuration. :param name: Name (str) :param environment: Environment name (str) :param schemas: Schemas (list) :param services: Services (list) :param volumes: Volumes (list) :param networks: Networks (list) :param namespace: Namespace (str?) """ database = self.project.database # Resolve schemas schemas = schemas or [] services, volumes, networks = self.project.schemas.resolve_schemas( schemas, services, volumes, networks) # Create configuration config = Configuration( database, name, user_get_username(), environment, services, volumes, networks, namespace, ) database.create_configuration(config) database.save() self.project.session.set_current_configuration(name) self.project.session.save()
def update_configuration(self, config): """ Update configuration. :param config: Configuration """ config_name = config.name # Check if configuration exists config = self.get_configuration(config_name) # Check for permission if not config.has_permission(): raise PermissionDenied(user_get_username()) # Generate composefile config.generate_composefile() # Generate environment file config.generate_environment_file()
def test_session_paths(): """Session paths.""" with using_temporary_directory() as tempdir: project_path = tempdir os.environ["DOCKNV_USER_PATH"] = project_path session = UserSession.load_from_path(user_get_username(), project_path) paths = session.get_paths() assert paths.get_project_root() == os.path.join( project_path, ".docknv") assert paths.get_user_root() == os.path.join(project_path, ".docknv", "test") assert paths.get_user_configuration_root("toto") == os.path.join( project_path, ".docknv", "test", "toto") assert paths.get_file_path("tutu") == os.path.join( project_path, ".docknv", "test", "tutu") assert paths.get_file_path("tutu", "toto") == os.path.join( project_path, ".docknv", "test", "toto", "tutu")
def remove_configuration(self, config_name, force=False): """ Remove configuration. :param config_name: Configuration name (str) :param force: Force (bool) (default: False) """ # Check if configuration exists config = self.get_configuration(config_name) # Check for permission if not config.has_permission(): raise PermissionDenied(user_get_username()) choice = prompt_yes_no( f"/!\\ are you sure you want to remove " f"configuration `{config_name}` ?", force, ) if choice: # Active session for current user if self.project.session.get_current_configuration() == config_name: self.project.session.unset_current_configuration() self.project.session.save() # Remove configuration path. path = config.get_path() if os.path.exists(path): shutil.rmtree(path) # Remove database entry del self.configurations[config_name] # Write database self.save()
def test_session_existing(): """Session tests.""" with using_temporary_directory() as tempdir: project_path = tempdir os.environ["DOCKNV_USER_PATH"] = project_path session_file = os.path.join(project_path, ".docknv", "test", "docknv.yml") os.makedirs(os.path.dirname(session_file)) with open(session_file, mode="w") as handle: handle.write("current:") session = UserSession.load_from_path(user_get_username(), project_path) assert "current" in session.session_data assert session.session_data["current"] is None # Save session.save() # Remove session.remove_path(force=True) session.remove_path(force=True) session.remove_path("toto", force=True)
def test_session_lock(): """Session lock.""" with using_temporary_directory() as tempdir: project_path = tempdir os.environ["DOCKNV_USER_PATH"] = project_path session = UserSession.load_from_path(user_get_username(), project_path) lock = session.get_lock() assert lock.get_file() == f"{project_path}/.test.lock" # Lock should be disabled assert not lock.is_enabled # Unlocking should work assert lock.unlock() # Locking should work assert lock.lock() # Lock should be enabled assert lock.is_enabled # Lockfile should contain a $ with open(lock.get_file(), mode="r") as handle: assert handle.read() == "$" # Relocking should return False assert not lock.lock() # But unlocking should work assert lock.unlock() # And is should be disabled assert not lock.is_enabled # And the file should not exist with pytest.raises(IOError): with open(lock.get_file(), mode="r") as handle: pass # Try-lock test with lock.try_lock(): # Lock should be enabled assert lock.is_enabled # Now, lock should be disabled assert not lock.is_enabled # Second try-lock test assert lock.lock() with pytest.raises(ProjectLocked): with lock.try_lock(): pass assert lock.is_enabled # Third try-lock test assert lock.unlock() with pytest.raises(RuntimeError): with lock.try_lock(): assert lock.is_enabled # Raise exception raise RuntimeError("oops") # Should be unlocked assert not lock.is_enabled # Try-lock w/ timeout test lock.lock() with pytest.raises(ProjectLocked): with lock.try_lock(timeout=2): pass # Try-lock w/ timeout, waiting for unlock def unlock_thread(): time.sleep(2) lock.unlock() thr1 = threading.Thread(target=unlock_thread) thr1.start() assert lock.is_enabled with lock.try_lock(timeout=2): pass thr1.join() assert not lock.is_enabled # Try-lock /w infinite timeout, waiting for unlock def unlock_thread2(): time.sleep(3) lock.unlock() lock.lock() thr1 = threading.Thread(target=unlock_thread2) thr1.start() with lock.try_lock(timeout=-1): pass thr1.join() assert not lock.is_enabled