def test_all(capfd: Capture) -> None: exec_command(capfd, "restart", "This command is no longer available") create_project( capfd=capfd, name="first", auth="postgres", frontend="no", ) init_project(capfd) start_registry(capfd) pull_images(capfd) start_project(capfd) start_date1 = get_container_start_date(capfd, "backend") exec_command( capfd, "start", "Stack started", ) start_date2 = get_container_start_date(capfd, "backend") # The service is not restarted because its definition is unchanged assert start_date1 == start_date2 if Configuration.swarm_mode: exec_command( capfd, "remove backend", "first_backend scaled to 0", "verify: Service converged", "Services removed", ) exec_command( capfd, "start --force", "Stack started", ) start_date3 = get_container_start_date(capfd, "backend") assert start_date2 != start_date3
def test_password_backend(capfd: Capture, faker: Faker) -> None: project_name = random_project_name(faker) create_project( capfd=capfd, name=project_name, auth="postgres", frontend="no", ) init_project(capfd, "-e API_AUTOSTART=1") # Let's simplify this task by removing task history # Otherwise the wait_until very usual fails due to the logs or previous tasks if Configuration.swarm_mode: docker.swarm.update(task_history_limit=0) start_registry(capfd) now = datetime.now() today = now.strftime("%Y-%m-%d") exec_command( capfd, "password backend --random", "Can't update backend because it is not running. Please start your stack", ) exec_command( capfd, "password", f"backend AUTH_DEFAULT_PASSWORD {colors.RED}N/A", ) pull_images(capfd) start_project(capfd) wait_until(capfd, "logs backend --tail 10", "Boot completed") # in dev mode Flask loads the app two times... A "boot completed" only states that # the app is loaded at least once, and the second time will success for sure # But can't say if now flask is really ready or still loading the second time # Added a sleep to wait for the eventual second load time.sleep(2) exec_command(capfd, "logs backend --tail 10") r = requests.post( "http://127.0.0.1:8080/auth/login", json={ "username": get_variable_from_projectrc("AUTH_DEFAULT_USERNAME"), "password": get_variable_from_projectrc("AUTH_DEFAULT_PASSWORD"), }, ) exec_command(capfd, "logs backend --tail 10") assert r.status_code == 200 backend_start_date = get_container_start_date(capfd, "backend") backend_pass1 = get_variable_from_projectrc("AUTH_DEFAULT_PASSWORD") exec_command( capfd, "password backend --random", "backend was running, restarting services...", "The password of backend has been changed. ", "Please find the new password into your .projectrc file as " "AUTH_DEFAULT_PASSWORD variable", ) backend_pass2 = get_variable_from_projectrc("AUTH_DEFAULT_PASSWORD") assert backend_pass1 != backend_pass2 backend_start_date2 = get_container_start_date(capfd, "backend", wait=True) # Verify that backend is restarted assert backend_start_date2 != backend_start_date # This is needed to wait for the service rolling update if Configuration.swarm_mode: time.sleep(5) wait_until(capfd, "logs backend --tail 10", "Boot completed") # in dev mode Flask loads the app two times... A "boot completed" only states that # the app is loaded at least once, and the second time will success for sure # But can't say if now flask is really ready or still loading the second time # Added a sleep to wait for the eventual second load time.sleep(2) r = requests.post( "http://127.0.0.1:8080/auth/login", json={ "username": get_variable_from_projectrc("AUTH_DEFAULT_USERNAME"), "password": get_variable_from_projectrc("AUTH_DEFAULT_PASSWORD"), }, ) exec_command(capfd, "logs backend --tail 10") assert r.status_code == 200 exec_command( capfd, "password", f"backend AUTH_DEFAULT_PASSWORD {colors.GREEN}{today}", ) mypassword = faker.pystr() exec_command( capfd, f"password backend --password {mypassword}", "The password of backend has been changed. ", ) assert mypassword == get_variable_from_projectrc("AUTH_DEFAULT_PASSWORD") exec_command( capfd, "password --show", mypassword, ) # This is needed to wait for the service rolling update if Configuration.swarm_mode: time.sleep(5) wait_until(capfd, "logs backend --tail 10", "Boot completed") # in dev mode Flask loads the app two times... A "boot completed" only states that # the app is loaded at least once, and the second time will success for sure # But can't say if now flask is really ready or still loading the second time # Added a sleep to wait for the eventual second load time.sleep(2) r = requests.post( "http://127.0.0.1:8080/auth/login", json={ "username": get_variable_from_projectrc("AUTH_DEFAULT_USERNAME"), "password": get_variable_from_projectrc("AUTH_DEFAULT_PASSWORD"), }, ) exec_command(capfd, "logs backend --tail 10") assert r.status_code == 200 future = now + timedelta(days=PASSWORD_EXPIRATION + 1) expired = (now + timedelta(days=PASSWORD_EXPIRATION)).strftime("%Y-%m-%d") with freeze_time(future): exec_command( capfd, "password", f"backend AUTH_DEFAULT_PASSWORD {colors.RED}{today}", ) exec_command( capfd, "check -i main --no-git --no-builds", f"AUTH_DEFAULT_PASSWORD is expired on {expired}", ) # Cleanup the stack for the next test exec_command(capfd, "remove", "Stack removed")
def test_password_mysql(capfd: Capture, faker: Faker) -> None: project_name = random_project_name(faker) create_project( capfd=capfd, name=project_name, auth="mysql", frontend="no", ) init_project(capfd, "-e API_AUTOSTART=1") start_registry(capfd) now = datetime.now() today = now.strftime("%Y-%m-%d") exec_command( capfd, "password mariadb --random", "Can't update mariadb because it is not running. Please start your stack", ) exec_command( capfd, "password", f"mariadb ALCHEMY_PASSWORD {colors.RED}N/A", # f"mariadb MYSQL_ROOT_PASSWORD {colors.RED}N/A", ) pull_images(capfd) start_project(capfd) service_verify(capfd, "sqlalchemy") backend_start_date = get_container_start_date(capfd, "backend") mariadb_start_date = get_container_start_date(capfd, "mariadb") mariadb_pass1 = get_variable_from_projectrc("ALCHEMY_PASSWORD") exec_command( capfd, "password mariadb --random", "mariadb was running, restarting services...", "The password of mariadb has been changed. ", "Please find the new password into your .projectrc file as " "ALCHEMY_PASSWORD variable", ) mariadb_pass2 = get_variable_from_projectrc("ALCHEMY_PASSWORD") assert mariadb_pass1 != mariadb_pass2 backend_start_date2 = get_container_start_date(capfd, "backend", wait=True) mariadb_start_date2 = get_container_start_date(capfd, "mariadb", wait=False) # Verify that both backend and mariadb are restarted assert backend_start_date2 != backend_start_date assert mariadb_start_date2 != mariadb_start_date service_verify(capfd, "sqlalchemy") exec_command( capfd, "password", f"mariadb ALCHEMY_PASSWORD {colors.GREEN}{today}", # f"mariadb MYSQL_ROOT_PASSWORD {colors.GREEN}{today}", ) mypassword = faker.pystr() exec_command( capfd, f"password mariadb --password {mypassword}", "The password of mariadb has been changed. ", ) assert mypassword == get_variable_from_projectrc("ALCHEMY_PASSWORD") exec_command( capfd, "password --show", mypassword, ) if Configuration.swarm_mode: time.sleep(5) service_verify(capfd, "sqlalchemy") future = now + timedelta(days=PASSWORD_EXPIRATION + 1) expired = (now + timedelta(days=PASSWORD_EXPIRATION)).strftime("%Y-%m-%d") with freeze_time(future): exec_command( capfd, "password", f"mariadb ALCHEMY_PASSWORD {colors.RED}{today}", ) exec_command( capfd, "check -i main --no-git --no-builds", f"ALCHEMY_PASSWORD is expired on {expired}", ) # Cleanup the stack for the next test exec_command(capfd, "remove", "Stack removed")
def test_password_redis(capfd: Capture, faker: Faker) -> None: project_name = random_project_name(faker) create_project( capfd=capfd, name=project_name, auth="no", frontend="no", services=["redis"], ) init_project(capfd, "-e API_AUTOSTART=1") start_registry(capfd) now = datetime.now() today = now.strftime("%Y-%m-%d") exec_command( capfd, "password", f"redis REDIS_PASSWORD {colors.RED}N/A", ) redis_pass1 = get_variable_from_projectrc("REDIS_PASSWORD") exec_command( capfd, "password redis --random", "redis was not running, restart is not needed", "The password of redis has been changed. ", "Please find the new password into your .projectrc file as " "REDIS_PASSWORD variable", ) redis_pass2 = get_variable_from_projectrc("REDIS_PASSWORD") assert redis_pass1 != redis_pass2 exec_command( capfd, "password", f"redis REDIS_PASSWORD {colors.GREEN}{today}", ) pull_images(capfd) start_project(capfd) service_verify(capfd, "redis") backend_start_date = get_container_start_date(capfd, "backend") redis_start_date = get_container_start_date(capfd, "redis") exec_command( capfd, "password redis --random", "redis was running, restarting services...", "The password of redis has been changed. ", "Please find the new password into your .projectrc file as " "REDIS_PASSWORD variable", ) redis_pass3 = get_variable_from_projectrc("REDIS_PASSWORD") assert redis_pass2 != redis_pass3 backend_start_date2 = get_container_start_date(capfd, "backend", wait=True) redis_start_date2 = get_container_start_date(capfd, "redis", wait=False) # Verify that both backend and redis are restarted assert backend_start_date2 != backend_start_date assert redis_start_date2 != redis_start_date service_verify(capfd, "redis") exec_command( capfd, "password", f"redis REDIS_PASSWORD {colors.GREEN}{today}", ) mypassword = faker.pystr() exec_command( capfd, f"password redis --password {mypassword}", "The password of redis has been changed. ", ) assert mypassword == get_variable_from_projectrc("REDIS_PASSWORD") exec_command( capfd, "password --show", mypassword, ) if Configuration.swarm_mode: time.sleep(5) service_verify(capfd, "redis") future = now + timedelta(days=PASSWORD_EXPIRATION + 1) expired = (now + timedelta(days=PASSWORD_EXPIRATION)).strftime("%Y-%m-%d") with freeze_time(future): exec_command( capfd, "password", f"redis REDIS_PASSWORD {colors.RED}{today}", ) exec_command( capfd, "check -i main --no-git --no-builds", f"REDIS_PASSWORD is expired on {expired}", ) # Cleanup the stack for the next test exec_command(capfd, "remove", "Stack removed")
def test_password_flower(capfd: Capture, faker: Faker) -> None: project_name = random_project_name(faker) create_project( capfd=capfd, name=project_name, auth="no", frontend="no", services=["flower"], ) init_project(capfd, "-e API_AUTOSTART=1") start_registry(capfd) now = datetime.now() today = now.strftime("%Y-%m-%d") exec_command( capfd, "password", f"flower FLOWER_PASSWORD {colors.RED}N/A", ) flower_pass1 = get_variable_from_projectrc("FLOWER_PASSWORD") exec_command( capfd, "password flower --random", "flower was not running, restart is not needed", "The password of flower has been changed. ", "Please find the new password into your .projectrc file as " "FLOWER_PASSWORD variable", ) flower_pass2 = get_variable_from_projectrc("FLOWER_PASSWORD") assert flower_pass1 != flower_pass2 exec_command( capfd, "password", f"flower FLOWER_PASSWORD {colors.GREEN}{today}", ) pull_images(capfd) start_project(capfd) flower_start_date = get_container_start_date(capfd, "flower", wait=True) exec_command( capfd, "password flower --random", "flower was running, restarting services...", "The password of flower has been changed. ", "Please find the new password into your .projectrc file as " "FLOWER_PASSWORD variable", ) flower_pass3 = get_variable_from_projectrc("FLOWER_PASSWORD") assert flower_pass2 != flower_pass3 flower_start_date2 = get_container_start_date(capfd, "flower", wait=True) assert flower_start_date2 != flower_start_date exec_command( capfd, "password", f"flower FLOWER_PASSWORD {colors.GREEN}{today}", ) mypassword = faker.pystr() exec_command( capfd, f"password flower --password {mypassword}", "The password of flower has been changed. ", ) assert mypassword == get_variable_from_projectrc("FLOWER_PASSWORD") exec_command( capfd, "password --show", mypassword, ) future = now + timedelta(days=PASSWORD_EXPIRATION + 1) expired = (now + timedelta(days=PASSWORD_EXPIRATION)).strftime("%Y-%m-%d") with freeze_time(future): exec_command( capfd, "password", f"flower FLOWER_PASSWORD {colors.RED}{today}", ) exec_command( capfd, "check -i main --no-git --no-builds", f"FLOWER_PASSWORD is expired on {expired}", ) # Cleanup the stack for the next test exec_command(capfd, "remove", "Stack removed")
def test_password_registry(capfd: Capture, faker: Faker) -> None: if not Configuration.swarm_mode: return None project_name = random_project_name(faker) create_project( capfd=capfd, name=project_name, auth="postgres", frontend="no", ) init_project(capfd) now = datetime.now() today = now.strftime("%Y-%m-%d") exec_command( capfd, "password", f"registry REGISTRY_PASSWORD {colors.RED}N/A", ) registry_pass1 = get_variable_from_projectrc("REGISTRY_PASSWORD") docker.container.remove(REGISTRY, force=True) exec_command( capfd, "password registry --random", "registry was not running, restart is not needed", "The password of registry has been changed. ", "Please find the new password into your .projectrc file as " "REGISTRY_PASSWORD variable", ) registry_pass2 = get_variable_from_projectrc("REGISTRY_PASSWORD") assert registry_pass1 != registry_pass2 start_registry(capfd) exec_command( capfd, "password", f"registry REGISTRY_PASSWORD {colors.GREEN}{today}", ) exec_command(capfd, "images", "This registry contains ") registry_start_date = get_container_start_date(capfd, "registry", wait=True) exec_command( capfd, "password registry --random", "registry was running, restarting services...", "The password of registry has been changed. ", "Please find the new password into your .projectrc file as " "REGISTRY_PASSWORD variable", ) registry_pass3 = get_variable_from_projectrc("REGISTRY_PASSWORD") assert registry_pass2 != registry_pass3 registry_start_date2 = get_container_start_date(capfd, "registry", wait=True) assert registry_start_date2 != registry_start_date exec_command(capfd, "images", "This registry contains ") exec_command( capfd, "password", f"registry REGISTRY_PASSWORD {colors.GREEN}{today}", ) future = now + timedelta(days=PASSWORD_EXPIRATION + 1) expired = (now + timedelta(days=PASSWORD_EXPIRATION)).strftime("%Y-%m-%d") with freeze_time(future): exec_command( capfd, "password", f"registry REGISTRY_PASSWORD {colors.GREEN}{today}", ) exec_command( capfd, "check -i main --no-git --no-builds", f"REGISTRY_PASSWORD is expired on {expired}", ) # This is needed otherwise the following tests will be unable to start # a new instance of the registry and will fail with registry auth errors exec_command(capfd, "remove registry", "Service registry removed")
def test_password_rabbit(capfd: Capture, faker: Faker) -> None: project_name = random_project_name(faker) create_project( capfd=capfd, name=project_name, auth="no", frontend="no", services=["rabbit"], ) init_project(capfd, "-e API_AUTOSTART=1") start_registry(capfd) now = datetime.now() today = now.strftime("%Y-%m-%d") exec_command( capfd, "password rabbit --random", "Can't update rabbit because it is not running. Please start your stack", ) exec_command( capfd, "password", f"rabbit RABBITMQ_PASSWORD {colors.RED}N/A", ) pull_images(capfd) start_project(capfd) service_verify(capfd, "rabbitmq") # ############## RABBIT ##################### backend_start_date = get_container_start_date(capfd, "backend") rabbit_start_date = get_container_start_date(capfd, "rabbit") rabbit_pass1 = get_variable_from_projectrc("RABBITMQ_PASSWORD") exec_command( capfd, "password rabbit --random", "rabbit was running, restarting services...", "The password of rabbit has been changed. ", "Please find the new password into your .projectrc file as " "RABBITMQ_PASSWORD variable", ) rabbit_pass2 = get_variable_from_projectrc("RABBITMQ_PASSWORD") assert rabbit_pass1 != rabbit_pass2 backend_start_date2 = get_container_start_date(capfd, "backend", wait=True) rabbit_start_date2 = get_container_start_date(capfd, "rabbit", wait=False) # Verify that both backend and rabbit are restarted assert backend_start_date2 != backend_start_date assert rabbit_start_date2 != rabbit_start_date service_verify(capfd, "rabbitmq") exec_command( capfd, "password", f"rabbit RABBITMQ_PASSWORD {colors.GREEN}{today}", ) # Needed to prevent random: # failed to update service xyz_rabbit: # Error response from daemon: # rpc error: code = Unknown desc = update out of sequence if Configuration.swarm_mode: time.sleep(3) mypassword = faker.pystr() exec_command( capfd, f"password rabbit --password {mypassword}", "The password of rabbit has been changed. ", ) assert mypassword == get_variable_from_projectrc("RABBITMQ_PASSWORD") exec_command( capfd, "password --show", mypassword, ) if Configuration.swarm_mode: time.sleep(5) service_verify(capfd, "rabbitmq") future = now + timedelta(days=PASSWORD_EXPIRATION + 1) expired = (now + timedelta(days=PASSWORD_EXPIRATION)).strftime("%Y-%m-%d") with freeze_time(future): exec_command( capfd, "password", f"rabbit RABBITMQ_PASSWORD {colors.RED}{today}", ) exec_command( capfd, "check -i main --no-git --no-builds", f"RABBITMQ_PASSWORD is expired on {expired}", ) # Cleanup the stack for the next test exec_command(capfd, "remove", "Stack removed")