Compare commits
14
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6c7070f163 | ||
|
|
c00ebb7d6d | ||
|
|
8c13360c0c | ||
|
|
0b576ee4b5 | ||
|
|
c38b91d555 | ||
|
|
9e3c05540f | ||
|
|
55fbeb3187 | ||
|
|
7a53fbe499 | ||
|
|
5abfc44e6f | ||
|
|
f0e4bd8ea5 | ||
|
|
2e84fb0ad4 | ||
|
|
b5e8153a00 | ||
|
|
d3991c4331 | ||
|
|
b7ad854a49 |
@@ -1,61 +0,0 @@
|
||||
from steps.git import GitClone, CheckoutMerged, GitUnmerge
|
||||
from steps.docker import DockerSwarmCheck, DockerSwarmDeploy
|
||||
from enironment import Environment
|
||||
from steps.checks import SimpleLog, UrlCheck
|
||||
from steps.shared_state import SharedStateHolderInMemory
|
||||
|
||||
|
||||
|
||||
clone = GitClone(url="https://github.com/rudolf1/uber_backup.git", branchNamePrefix="photosHelper")
|
||||
|
||||
sharedState = SharedStateHolderInMemory(unmerge=None)
|
||||
|
||||
checkoutMerged = CheckoutMerged(clone,
|
||||
desired_branches=sharedState,
|
||||
push = False,
|
||||
git_user_email="rudolfss13@gmail.com",
|
||||
git_user_name="brencher_bot"
|
||||
)
|
||||
|
||||
sharedState.set_branches([("photosHelper/master", "HEAD")])
|
||||
dockerSwarmCheck = DockerSwarmCheck(
|
||||
stack_name = "photosHelper",
|
||||
)
|
||||
deployDocker = DockerSwarmDeploy(
|
||||
wd=clone,
|
||||
buildDocker=None,
|
||||
stackChecker=dockerSwarmCheck,
|
||||
envs = lambda: {
|
||||
"version": "auto-" + checkoutMerged.progress().version
|
||||
},
|
||||
stack_name = "photosHelper",
|
||||
docker_compose_path = "docker-compose.yml",
|
||||
)
|
||||
unmerge = GitUnmerge(clone, dockerSwarmCheck)
|
||||
|
||||
checkPing = UrlCheck(
|
||||
url="https://backuper.rudolf.keenetic.link/api/server/ping",
|
||||
expected = {"res":"pong"},
|
||||
)
|
||||
logUrls = SimpleLog(message = {
|
||||
"userLinks": {
|
||||
"App": "https://backuper.rudolf.keenetic.link",
|
||||
}
|
||||
})
|
||||
|
||||
backups_helper = Environment(
|
||||
id="backupsHelper",
|
||||
state=sharedState,
|
||||
pipeline=[
|
||||
clone,
|
||||
sharedState,
|
||||
checkoutMerged,
|
||||
dockerSwarmCheck,
|
||||
unmerge,
|
||||
deployDocker,
|
||||
checkPing,
|
||||
logUrls
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
@@ -99,7 +99,7 @@ class DockerComposeBuild(AbstractStep[Dict[str, str]]):
|
||||
pass
|
||||
|
||||
logger.info(f"Building image {image} from {build_ctx}, {build_dockerfile}")
|
||||
img, _ = client.images.build(path=build_ctx, dockerfile=build_dockerfile, tag=image, nocache=not self.build_cache, rm=not self.build_cache)
|
||||
img, _ = client.images.build(path=build_ctx, dockerfile=build_dockerfile, tag=image, nocache=not self.build_cache, rm=True)
|
||||
|
||||
if self.publish:
|
||||
logger.info(f"Pushing image {image}")
|
||||
|
||||
@@ -119,7 +119,6 @@ class CheckoutMerged(AbstractStep[CheckoutAndMergeResult]):
|
||||
desired_branches: AbstractStep[SharedState],
|
||||
git_user_email: str,
|
||||
git_user_name: str,
|
||||
ignore_missing_branches: bool = True,
|
||||
push: bool = True,
|
||||
**kwargs: Any):
|
||||
super().__init__(**kwargs)
|
||||
@@ -128,7 +127,7 @@ class CheckoutMerged(AbstractStep[CheckoutAndMergeResult]):
|
||||
self.git_user_email = git_user_email
|
||||
self.git_user_name = git_user_name
|
||||
self.push = push
|
||||
self.ignore_missing_branches = ignore_missing_branches
|
||||
|
||||
def _find_merge_childs(self, tree: Dict[Commit, List[Commit]], commit: Commit) -> List[Commit]:
|
||||
|
||||
visited = [commit]
|
||||
@@ -156,8 +155,6 @@ class CheckoutMerged(AbstractStep[CheckoutAndMergeResult]):
|
||||
commit = repo.commit(desired_commit)
|
||||
commit_ids[commit] = branch_name
|
||||
except BaseException as e:
|
||||
if not self.ignore_missing_branches:
|
||||
raise e
|
||||
stack = traceback.format_exception(type(e), e, e.__traceback__)
|
||||
logger.error(f"Error finding desired commits for branch {branch_pair}: {str(e)}\n{''.join(stack)}")
|
||||
return commit_ids
|
||||
@@ -232,7 +229,7 @@ class CheckoutMerged(AbstractStep[CheckoutAndMergeResult]):
|
||||
version = '-'.join([x.hexsha[0:8] for x in sorted_commits])
|
||||
|
||||
for ref in repo.refs:
|
||||
if ref.is_remote() and ref.is_valid() and ref.commit == repo.head.commit and ref.name != 'origin/HEAD':
|
||||
if ref.is_remote() and ref.commit == repo.head.commit and ref.name != 'origin/HEAD':
|
||||
logger.info(f"Merge commit {ref.commit} corresponds to branch {ref}")
|
||||
remote_branch_name = ref.name[len('origin/'):]
|
||||
break
|
||||
@@ -296,9 +293,9 @@ class GitUnmerge(AbstractStep[GitUnmergeResult]):
|
||||
for v in version_parts:
|
||||
commit = repo.commit(v)
|
||||
# branches = [head.name for head in repo.heads if head.commit.hexsha == commit.hexsha]
|
||||
branches = [ref.name for ref in repo.remotes.origin.refs if not ref.name.startswith('origin/HEAD') and ref.commit.hexsha == commit.hexsha]
|
||||
branches = [ref.name for ref in repo.remotes.origin.refs if ref.commit.hexsha == commit.hexsha]
|
||||
branches = [b[len('origin/'):] for b in branches if
|
||||
b.startswith('origin/') ]
|
||||
b.startswith('origin/') and not b.startswith('origin/HEAD')]
|
||||
|
||||
if len(branches) == 0 or branches[0].startswith('auto/'):
|
||||
if childs is None:
|
||||
|
||||
@@ -60,10 +60,3 @@ class CachingStep(AbstractStep[T], Generic[T]):
|
||||
def __getattr__(self, name: str) -> Any:
|
||||
"""Delegate unknown attributes to the wrapped step."""
|
||||
return getattr(self._step, name)
|
||||
|
||||
def __setattr__(self, name: str, value: Any) -> None:
|
||||
"""Delegate unknown attributes to the wrapped step."""
|
||||
if name in {"name", "_step", "_result", "_input_hash"}:
|
||||
super().__setattr__(name, value)
|
||||
else:
|
||||
setattr(self._step, name, value)
|
||||
|
||||
+9
-23
@@ -11,7 +11,7 @@ from fastapi.middleware.cors import CORSMiddleware
|
||||
from fastapi.responses import FileResponse
|
||||
|
||||
from app import App
|
||||
from enironment import SharedStateConflictError, get_step
|
||||
from enironment import get_step
|
||||
from utils import custom_json_dumps
|
||||
from processing import reset_caches
|
||||
from secondary import SecondaryManager
|
||||
@@ -186,28 +186,14 @@ class WebApp:
|
||||
elif id in self.core.environments.keys():
|
||||
env = self.core.environments.get(id, None)
|
||||
expected_token = update_data.get('token', '')
|
||||
try:
|
||||
if 'branches' in update_data:
|
||||
if not env:
|
||||
raise RuntimeError(f"Unknown env {update_data.get('id', '')}")
|
||||
env.state.set_branches(update_data.get('branches', []), expected_token=expected_token)
|
||||
if 'dry' in update_data:
|
||||
if not env:
|
||||
raise RuntimeError(f"Unknown env {update_data.get('id', '')}")
|
||||
env.state.set_dry(bool(update_data['dry']), expected_token)
|
||||
except SharedStateConflictError as conflict:
|
||||
# if env:
|
||||
# current_state = env.state.progress()
|
||||
# await self.broadcast_error({
|
||||
# 'code': 'BRANCH_STATE_CONFLICT',
|
||||
# 'envId': id,
|
||||
# 'message': str(conflict),
|
||||
# 'current_state': {
|
||||
# 'branches': current_state.branches,
|
||||
# 'token': current_state.token,
|
||||
# },
|
||||
# })
|
||||
continue
|
||||
if 'branches' in update_data:
|
||||
if not env:
|
||||
raise RuntimeError(f"Unknown env {update_data.get('id', '')}")
|
||||
env.state.set_branches(update_data.get('branches', []), expected_token=expected_token)
|
||||
if 'dry' in update_data:
|
||||
if not env:
|
||||
raise RuntimeError(f"Unknown env {update_data.get('id', '')}")
|
||||
env.state.set_dry(bool(update_data['dry']), expected_token)
|
||||
|
||||
if env:
|
||||
p = get_step(env.pipeline, type(env.state))
|
||||
|
||||
+1
-1
@@ -16,7 +16,7 @@ services:
|
||||
- PYTHONUNBUFFERED=1
|
||||
- VIRTUAL_HOST=brencher.rudolf.keenetic.link
|
||||
- LETSENCRYPT_HOST=brencher.rudolf.keenetic.link
|
||||
- PROFILES=brencher2,torrserv_proxy,immich,registry,gmail_mcp,backupsHelper
|
||||
- PROFILES=brencher2,torrserv_proxy,immich,registry,gmail_mcp
|
||||
- SECONDARY_BRENCHER=http://host.docker.internal:5002
|
||||
deploy:
|
||||
replicas: 1
|
||||
|
||||
+3
-21
@@ -137,20 +137,9 @@ function escapeHtml(str) {
|
||||
}
|
||||
|
||||
let renderedBranches = []
|
||||
let renderedDryRunState = {}
|
||||
let renderedColumnsSignature = ''
|
||||
|
||||
function getColumnsSignature() {
|
||||
return JSON.stringify(columnsByEnv);
|
||||
}
|
||||
|
||||
function renderBranches() {
|
||||
const columnsSignature = getColumnsSignature();
|
||||
if (
|
||||
JSON.stringify(filteredBranches) === JSON.stringify(renderedBranches) &&
|
||||
JSON.stringify(dryRunByEnv) === JSON.stringify(renderedDryRunState) &&
|
||||
columnsSignature === renderedColumnsSignature
|
||||
) {
|
||||
if (JSON.stringify(filteredBranches) === JSON.stringify(renderedBranches) && JSON.stringify(dryRunByEnv) === JSON.stringify(renderedDryRunState)) {
|
||||
return
|
||||
}
|
||||
checkForPendingChanges();
|
||||
@@ -263,9 +252,8 @@ function renderBranches() {
|
||||
filterBranches();
|
||||
};
|
||||
});
|
||||
renderedBranches = [...filteredBranches];
|
||||
renderedBranches = filteredBranches;
|
||||
renderedDryRunState = {...dryRunByEnv};
|
||||
renderedColumnsSignature = columnsSignature;
|
||||
}
|
||||
|
||||
function updateBranchCommit(envId, branch, commit) {
|
||||
@@ -482,13 +470,9 @@ function setupWebSockets() {
|
||||
if (message.error && message.error.code === 'BRANCH_STATE_CONFLICT' && message.error.envId) {
|
||||
const envId = message.error.envId;
|
||||
const current = message.error.current_state || {};
|
||||
const hadLocalSelection = Object.prototype.hasOwnProperty.call(selectedBranchesByEnv, envId);
|
||||
if (Array.isArray(current.branches)) {
|
||||
selectedBranchesByEnv[envId] = [...current.branches];
|
||||
serverSelectedBranchesByEnv[envId] = [...current.branches];
|
||||
// Preserve the user's pending edits if they already changed selection locally.
|
||||
if (!hadLocalSelection) {
|
||||
selectedBranchesByEnv[envId] = [...current.branches];
|
||||
}
|
||||
}
|
||||
if (typeof current.token === 'string') {
|
||||
serverTokenByEnv[envId] = current.token;
|
||||
@@ -519,8 +503,6 @@ function updateEnvironment() {
|
||||
if (JSON.stringify(localSel) !== JSON.stringify(serverSel)) {
|
||||
if (ws && ws.readyState === WebSocket.OPEN) {
|
||||
ws.send(JSON.stringify({ update: { id: envId, branches: selectedBranchesByEnv[envId], token: serverTokenByEnv[envId] } }));
|
||||
// Keep pending-state tracking aligned with what we just submitted.
|
||||
serverSelectedBranchesByEnv[envId] = [...(selectedBranchesByEnv[envId] || [])];
|
||||
}
|
||||
branchFilter.value = '';
|
||||
}
|
||||
|
||||
@@ -1,54 +0,0 @@
|
||||
from enironment import Environment
|
||||
from steps.docker_plain import DockerImageBuild, DockerContainerCheck, DockerContainerDeploy
|
||||
from steps.git import GitClone, CheckoutMerged, GitUnmerge
|
||||
from steps.shared_state import SharedStateHolderInMemory
|
||||
|
||||
clone = GitClone(url="https://github.com/rudolf1/brencher.git")
|
||||
|
||||
container_check = DockerContainerCheck(
|
||||
container_name="test_plain-container"
|
||||
)
|
||||
|
||||
unmerge = GitUnmerge(wd=clone, check=container_check)
|
||||
|
||||
state = SharedStateHolderInMemory(unmerge=unmerge)
|
||||
|
||||
checkoutMerged = CheckoutMerged(clone,
|
||||
desired_branches=state,
|
||||
push=False,
|
||||
git_user_email="rudolfss13@gmail.com",
|
||||
git_user_name="test_bot"
|
||||
)
|
||||
|
||||
# Step 1: Build the image
|
||||
image_build = DockerImageBuild(
|
||||
wd=checkoutMerged,
|
||||
dockerfile_path="Dockerfile_test_app",
|
||||
image_name="test_plain",
|
||||
image_tag=lambda: "auto-" + checkoutMerged.progress().version,
|
||||
nocache=False,
|
||||
)
|
||||
|
||||
# Step 3: Deploy the container
|
||||
container_deploy = DockerContainerDeploy(
|
||||
image_build=image_build,
|
||||
container_name="test_plain-container",
|
||||
ports={"5001/tcp": 5002},
|
||||
environment={"PROFILES": "no_profiles"},
|
||||
restart_policy={"Name": "unless-stopped"},
|
||||
)
|
||||
|
||||
__all__ = ["test_local1"]
|
||||
test_local1 = Environment(
|
||||
id="test_local1",
|
||||
state=state,
|
||||
pipeline=[
|
||||
clone,
|
||||
state,
|
||||
checkoutMerged,
|
||||
image_build,
|
||||
container_deploy,
|
||||
container_deploy,
|
||||
unmerge
|
||||
]
|
||||
)
|
||||
+1
-102
@@ -20,25 +20,6 @@ class BrencherPage:
|
||||
button.wait_for(state="visible", timeout=10000)
|
||||
return button
|
||||
|
||||
def _apply_changes_button(self) -> Locator:
|
||||
button = self._page.locator("#apply-changes")
|
||||
button.wait_for(state="visible", timeout=10000)
|
||||
return button
|
||||
|
||||
def _branch_checkbox(self, branch_name: str) -> Locator:
|
||||
checkbox = self._page.locator(
|
||||
f'input[type="checkbox"][data-env="{self._env_id}"][value="{branch_name}"]'
|
||||
)
|
||||
checkbox.wait_for(state="visible", timeout=10000)
|
||||
return checkbox
|
||||
|
||||
def _desired_commit_select(self, branch_name: str) -> Locator:
|
||||
select = self._page.locator(
|
||||
f'select.desired-commit[data-env="{self._env_id}"][data-branch="{branch_name}"]'
|
||||
)
|
||||
select.wait_for(state="visible", timeout=10000)
|
||||
return select
|
||||
|
||||
def verify_dry_run_on(self) -> None:
|
||||
btn_title = self._dry_run_button().get_attribute("title") or ""
|
||||
assert "Dry run on" in btn_title, f"Expected dry run to be active, got title: {btn_title!r}"
|
||||
@@ -48,39 +29,6 @@ class BrencherPage:
|
||||
self._dry_run_button().click()
|
||||
self._logger.info("Clicked dry-run button to disable dry run")
|
||||
|
||||
def set_branch_unselected(self, branch_name: str) -> None:
|
||||
checkbox = self._branch_checkbox(branch_name)
|
||||
if checkbox.is_checked():
|
||||
checkbox.uncheck()
|
||||
self._logger.info("Unchecked branch '%s'", branch_name)
|
||||
|
||||
def set_branch_desired_commit(self, branch_name: str, commit_hash: str) -> None:
|
||||
self._desired_commit_select(branch_name).select_option(value=commit_hash)
|
||||
self._logger.info("Set desired commit for branch '%s' to '%s'", branch_name, commit_hash)
|
||||
|
||||
def set_apply_changes(self) -> None:
|
||||
self._apply_changes_button().click()
|
||||
self._logger.info("Clicked Apply Changes")
|
||||
|
||||
def set_simulate_branch_state_conflict(self) -> None:
|
||||
self._page.evaluate(
|
||||
"""({ envId }) => {
|
||||
const currentToken =
|
||||
(typeof serverTokenByEnv !== 'undefined' && serverTokenByEnv[envId])
|
||||
? serverTokenByEnv[envId]
|
||||
: 'server-token';
|
||||
const staleToken = `${currentToken}-stale`;
|
||||
if (typeof serverTokenByEnv !== 'undefined') {
|
||||
// Simulate stale client-side token so next apply uses an outdated value.
|
||||
serverTokenByEnv[envId] = staleToken;
|
||||
}
|
||||
}""",
|
||||
{
|
||||
"envId": self._env_id,
|
||||
},
|
||||
)
|
||||
self._logger.info("Set stale branch token for '%s'", self._env_id)
|
||||
|
||||
def click_refresh(self) -> None:
|
||||
self._page.locator("#refresh-branches").click()
|
||||
self._logger.info("Clicked refresh button")
|
||||
@@ -98,55 +46,6 @@ class BrencherPage:
|
||||
timeout=10000,
|
||||
)
|
||||
|
||||
def verify_apply_changes_visible(self) -> None:
|
||||
self._page.wait_for_function(
|
||||
"""() => {
|
||||
const btn = document.querySelector('#apply-changes');
|
||||
return !!btn && !btn.classList.contains('hidden');
|
||||
}""",
|
||||
timeout=10000,
|
||||
)
|
||||
self._logger.info("Apply Changes button is visible")
|
||||
|
||||
def verify_apply_changes_hidden(self) -> None:
|
||||
self._page.wait_for_function(
|
||||
"""() => {
|
||||
const btn = document.querySelector('#apply-changes');
|
||||
return !!btn && btn.classList.contains('hidden');
|
||||
}""",
|
||||
timeout=10000,
|
||||
)
|
||||
self._logger.info("Apply Changes button is hidden")
|
||||
|
||||
def verify_branch_column_value(self, branch_name: str, column_name: str, expected_value: str, timeout_ms: int = 10000) -> None:
|
||||
self._page.wait_for_function(
|
||||
"""({ envId, branchName, columnName, expectedValue }) => {
|
||||
const row = document.querySelector(`tr.branch-row[data-env="${envId}"][data-branch="${branchName}"]`);
|
||||
if (!row) return false;
|
||||
const table = row.closest('table.branches-table');
|
||||
if (!table) return false;
|
||||
const headers = [...table.querySelectorAll('thead th')].map(th => (th.textContent || '').trim());
|
||||
const colIdx = headers.indexOf(columnName);
|
||||
if (colIdx === -1) return false;
|
||||
const cells = [...row.querySelectorAll('td')];
|
||||
if (cells.length <= colIdx) return false;
|
||||
return ((cells[colIdx].textContent || '').trim() === expectedValue);
|
||||
}""",
|
||||
arg={
|
||||
"envId": self._env_id,
|
||||
"branchName": branch_name,
|
||||
"columnName": column_name,
|
||||
"expectedValue": expected_value,
|
||||
},
|
||||
timeout=timeout_ms,
|
||||
)
|
||||
self._logger.info(
|
||||
"Verified column '%s' for branch '%s' equals '%s'",
|
||||
column_name,
|
||||
branch_name,
|
||||
expected_value,
|
||||
)
|
||||
|
||||
def verify_pipeline_step_status(self, step_name: str, expected_status: str, timeout_ms: int = 10000) -> None:
|
||||
"""
|
||||
Verify that a pipeline step reaches the expected status by polling the page DOM.
|
||||
@@ -196,7 +95,7 @@ class BrencherPage:
|
||||
@contextmanager
|
||||
def brencher_page(
|
||||
app_link: str,
|
||||
env_id: str,
|
||||
env_id: str = "brencher_local1",
|
||||
action_logger: logging.Logger | None = None,
|
||||
) -> Generator[BrencherPage, None, None]:
|
||||
active_logger = action_logger or logger
|
||||
|
||||
@@ -1,127 +0,0 @@
|
||||
import logging
|
||||
import threading
|
||||
import time
|
||||
from typing import Any, Generator
|
||||
|
||||
import docker
|
||||
import pytest
|
||||
import requests
|
||||
import socket
|
||||
|
||||
from app import App
|
||||
from tests.configs import nginx
|
||||
from tests.conftest import EventuallyFn, assert_equal
|
||||
from tests.playwright_helper import brencher_page
|
||||
|
||||
|
||||
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
APP_PORT = 5003
|
||||
APP_URL = f"http://localhost:{APP_PORT}"
|
||||
CONTAINER_NAME = "test_plain-container"
|
||||
|
||||
# Module-level reference to server thread to ensure proper cleanup
|
||||
_server_thread: threading.Thread | None = None
|
||||
|
||||
|
||||
def _is_port_free(port: int, timeout: float = 5.0) -> bool:
|
||||
"""Check if a port is available for binding."""
|
||||
deadline = time.time() + timeout
|
||||
while time.time() < deadline:
|
||||
try:
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||
result = sock.connect_ex(('127.0.0.1', port))
|
||||
sock.close()
|
||||
if result != 0:
|
||||
return True
|
||||
except Exception:
|
||||
return True
|
||||
time.sleep(0.1)
|
||||
return False
|
||||
|
||||
|
||||
class TestApplyChangesPlaywright:
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def setup_teardown(self) -> Generator[None, Any, None]:
|
||||
global _server_thread
|
||||
|
||||
def stop_and_remove_container() -> None:
|
||||
client = docker.from_env()
|
||||
try:
|
||||
container = client.containers.get(CONTAINER_NAME)
|
||||
container.stop(timeout=10)
|
||||
container.remove(force=True)
|
||||
logger.info("Container %s stopped and removed", CONTAINER_NAME)
|
||||
except Exception:
|
||||
logger.info("Container %s not found, no need to stop/remove", CONTAINER_NAME)
|
||||
|
||||
# Ensure port is free before starting
|
||||
_is_port_free(APP_PORT, timeout=5.0)
|
||||
|
||||
stop_and_remove_container()
|
||||
yield None
|
||||
|
||||
# Clean up server thread
|
||||
if _server_thread and _server_thread.is_alive():
|
||||
_server_thread.join(timeout=2.0)
|
||||
logger.info("Waited for server thread to finish")
|
||||
|
||||
# Ensure port is free after test
|
||||
_is_port_free(APP_PORT, timeout=5.0)
|
||||
|
||||
stop_and_remove_container()
|
||||
_server_thread = None
|
||||
|
||||
def test_apply_button_hides_after_apply_branch_change(self, eventually: EventuallyFn) -> None:
|
||||
global _server_thread
|
||||
|
||||
# Start app with one pre-selected branch so unchecking creates pending changes.
|
||||
env = nginx.test_local1
|
||||
env.state.set_branches([("test/main", "HEAD")])
|
||||
env.state.set_dry(True)
|
||||
app = App({env.id: env})
|
||||
_server_thread = threading.Thread(target=lambda: app.runWeb(APP_PORT), daemon=True)
|
||||
_server_thread.start()
|
||||
|
||||
eventually(
|
||||
lambda: assert_equal(
|
||||
requests.get(f"{APP_URL}/state", timeout=5).status_code,
|
||||
200,
|
||||
"Server did not respond with 200 OK within timeout",
|
||||
)
|
||||
)
|
||||
|
||||
with brencher_page(APP_URL, env.id) as page:
|
||||
page.set_branch_unselected("test/main")
|
||||
page.verify_apply_changes_visible()
|
||||
page.set_apply_changes()
|
||||
eventually(lambda: page.verify_apply_changes_hidden(), timeout=10, interval=0.5)
|
||||
|
||||
def test_apply_button_reappears_on_branch_conflict(self, eventually: EventuallyFn) -> None:
|
||||
global _server_thread
|
||||
|
||||
env = nginx.test_local1
|
||||
env.state.set_branches([("test/main", "HEAD")])
|
||||
env.state.set_dry(True)
|
||||
app = App({env.id: env})
|
||||
_server_thread = threading.Thread(target=lambda: app.runWeb(APP_PORT), daemon=True)
|
||||
_server_thread.start()
|
||||
|
||||
eventually(
|
||||
lambda: assert_equal(
|
||||
requests.get(f"{APP_URL}/state", timeout=5).status_code,
|
||||
200,
|
||||
"Server did not respond with 200 OK within timeout",
|
||||
)
|
||||
)
|
||||
|
||||
with brencher_page(APP_URL, env.id) as page:
|
||||
page.set_branch_unselected("test/main")
|
||||
page.verify_apply_changes_visible()
|
||||
# Set stale token in client JS object so backend returns a real conflict on apply.
|
||||
page.set_simulate_branch_state_conflict()
|
||||
page.set_apply_changes()
|
||||
eventually(lambda: page.verify_apply_changes_visible(), timeout=10, interval=0.5)
|
||||
@@ -1,182 +0,0 @@
|
||||
import logging
|
||||
import socket
|
||||
import threading
|
||||
import time
|
||||
from typing import Any, Generator
|
||||
|
||||
import docker
|
||||
import pytest
|
||||
import requests
|
||||
|
||||
from app import App
|
||||
from tests.configs import nginx
|
||||
from tests.conftest import EventuallyFn, assert_equal
|
||||
from tests.playwright_helper import brencher_page
|
||||
|
||||
|
||||
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
APP_PORT = 5004
|
||||
APP_URL = f"http://localhost:{APP_PORT}"
|
||||
CONTAINER_NAME = "test_plain-container"
|
||||
TARGET_BRANCH = "test/main"
|
||||
|
||||
_server_thread: threading.Thread | None = None
|
||||
|
||||
|
||||
def _is_port_free(port: int, timeout: float = 5.0) -> bool:
|
||||
"""Check if a port is available for binding."""
|
||||
deadline = time.time() + timeout
|
||||
while time.time() < deadline:
|
||||
try:
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||
result = sock.connect_ex(("127.0.0.1", port))
|
||||
sock.close()
|
||||
if result != 0:
|
||||
return True
|
||||
except Exception:
|
||||
return True
|
||||
time.sleep(0.1)
|
||||
return False
|
||||
|
||||
|
||||
def _get_state() -> dict[str, Any]:
|
||||
response = requests.get(f"{APP_URL}/state", timeout=5)
|
||||
assert response.status_code == 200, f"Expected status 200 from /state, got {response.status_code}"
|
||||
data = response.json()
|
||||
assert isinstance(data, dict), f"Expected dict from /state, got {type(data)}"
|
||||
return data
|
||||
|
||||
|
||||
def _get_deployed_short_hash(env_id: str, branch: str) -> str:
|
||||
state = _get_state()
|
||||
env_state = state.get(env_id)
|
||||
assert isinstance(env_state, dict), f"No environment state for {env_id}: {state}"
|
||||
|
||||
pipeline = env_state.get("pipeline")
|
||||
assert isinstance(pipeline, list), f"Pipeline is not list for {env_id}: {env_state}"
|
||||
|
||||
step = next((p for p in pipeline if p.get("name") == "GitUnmerge"), None)
|
||||
assert isinstance(step, dict), f"GitUnmerge step not found in pipeline: {pipeline}"
|
||||
assert not bool(step.get("is_running")), f"GitUnmerge is still running: {step}"
|
||||
|
||||
status = step.get("status")
|
||||
assert isinstance(status, dict), f"GitUnmerge status is not an object: {status}"
|
||||
|
||||
columns = status.get("columns")
|
||||
assert isinstance(columns, dict), f"GitUnmerge columns missing: {status}"
|
||||
|
||||
deployed = columns.get("Deployed")
|
||||
assert isinstance(deployed, dict), f"Deployed column missing: {columns}"
|
||||
|
||||
value = deployed.get(branch)
|
||||
assert isinstance(value, str) and len(value) == 8, f"Deployed value for {branch} invalid: {value}"
|
||||
return value
|
||||
|
||||
|
||||
def _pick_target_commit(env_id: str, branch: str) -> str:
|
||||
response = requests.get(f"{APP_URL}/branches", timeout=5)
|
||||
assert response.status_code == 200, f"Expected status 200 from /branches, got {response.status_code}"
|
||||
data = response.json()
|
||||
assert isinstance(data, dict), f"Expected dict from /branches, got {type(data)}"
|
||||
|
||||
env_branches = data.get(env_id, {})
|
||||
assert isinstance(env_branches, dict), f"Expected env branch map for {env_id}, got {env_branches}"
|
||||
|
||||
commits = env_branches.get(branch, [])
|
||||
assert isinstance(commits, list), f"Expected commits list for {branch}, got {commits}"
|
||||
|
||||
for commit in commits:
|
||||
if not isinstance(commit, dict):
|
||||
continue
|
||||
hexsha = commit.get("hexsha")
|
||||
if isinstance(hexsha, str) and len(hexsha) >= 8:
|
||||
return hexsha
|
||||
|
||||
raise AssertionError(f"Unable to find any commit for branch {branch}.")
|
||||
|
||||
|
||||
def _assert_deployed_hash_available(env_id: str, branch: str) -> None:
|
||||
value = _get_deployed_short_hash(env_id, branch)
|
||||
assert isinstance(value, str) and len(value) == 8
|
||||
|
||||
|
||||
class TestDeployColumnPlaywright:
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def setup_teardown(self) -> Generator[None, Any, None]:
|
||||
global _server_thread
|
||||
|
||||
def stop_and_remove_container() -> None:
|
||||
client = docker.from_env()
|
||||
try:
|
||||
container = client.containers.get(CONTAINER_NAME)
|
||||
container.stop(timeout=10)
|
||||
container.remove(force=True)
|
||||
logger.info("Container %s stopped and removed", CONTAINER_NAME)
|
||||
except Exception:
|
||||
logger.info("Container %s not found, no need to stop/remove", CONTAINER_NAME)
|
||||
|
||||
_is_port_free(APP_PORT, timeout=5.0)
|
||||
|
||||
stop_and_remove_container()
|
||||
yield None
|
||||
|
||||
if _server_thread and _server_thread.is_alive():
|
||||
_server_thread.join(timeout=2.0)
|
||||
logger.info("Waited for server thread to finish")
|
||||
|
||||
_is_port_free(APP_PORT, timeout=5.0)
|
||||
stop_and_remove_container()
|
||||
_server_thread = None
|
||||
|
||||
def test_deployed_column_updates_after_apply_changes_and_deploy(self, eventually: EventuallyFn) -> None:
|
||||
global _server_thread
|
||||
|
||||
env = nginx.test_local1
|
||||
env.state.set_branches([(TARGET_BRANCH, "HEAD")])
|
||||
env.state.set_dry(True)
|
||||
|
||||
app = App({env.id: env})
|
||||
_server_thread = threading.Thread(target=lambda: app.runWeb(APP_PORT), daemon=True)
|
||||
_server_thread.start()
|
||||
|
||||
eventually(
|
||||
lambda: assert_equal(
|
||||
requests.get(f"{APP_URL}/state", timeout=5).status_code,
|
||||
200,
|
||||
"Server did not respond with 200 OK within timeout",
|
||||
),
|
||||
timeout=20,
|
||||
interval=0.5,
|
||||
)
|
||||
|
||||
target_commit = _pick_target_commit(env.id, TARGET_BRANCH)
|
||||
|
||||
with brencher_page(APP_URL, env.id) as page:
|
||||
page.verify_dry_run_on()
|
||||
page.set_branch_desired_commit(TARGET_BRANCH, target_commit)
|
||||
page.verify_apply_changes_visible()
|
||||
page.set_apply_changes()
|
||||
|
||||
eventually(lambda: page.verify_apply_changes_hidden(), timeout=20, interval=0.5)
|
||||
|
||||
page.set_dry_run_off()
|
||||
eventually(lambda: page.verify_dry_run_off(), timeout=20, interval=0.5)
|
||||
page.click_refresh()
|
||||
|
||||
eventually(
|
||||
lambda: _assert_deployed_hash_available(env.id, TARGET_BRANCH),
|
||||
timeout=90,
|
||||
interval=1.0,
|
||||
)
|
||||
|
||||
deployed_short_hash = _get_deployed_short_hash(env.id, TARGET_BRANCH)
|
||||
|
||||
eventually(
|
||||
lambda: page.verify_branch_column_value(TARGET_BRANCH, "Deployed", deployed_short_hash),
|
||||
timeout=30,
|
||||
interval=0.5,
|
||||
)
|
||||
@@ -1,7 +1,5 @@
|
||||
import logging
|
||||
import threading
|
||||
import time
|
||||
import socket
|
||||
from typing import Any, Callable, Generator, TypeVar
|
||||
|
||||
import docker
|
||||
@@ -10,79 +8,37 @@ import requests
|
||||
from app import App
|
||||
from .conftest import EventuallyFn, assert_equal, check_state
|
||||
from .playwright_helper import brencher_page
|
||||
from tests.configs import nginx
|
||||
from configs import brencher_local1
|
||||
|
||||
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
APP_PORT = 5003
|
||||
APP_URL = f"http://localhost:{APP_PORT}"
|
||||
CONTAINER_NAME = "test_plain-container"
|
||||
|
||||
# Module-level reference to server thread to ensure proper cleanup
|
||||
_server_thread: threading.Thread | None = None
|
||||
|
||||
|
||||
def _is_port_free(port: int, timeout: float = 5.0) -> bool:
|
||||
"""Check if a port is available for binding."""
|
||||
deadline = time.time() + timeout
|
||||
while time.time() < deadline:
|
||||
try:
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||
result = sock.connect_ex(('127.0.0.1', port))
|
||||
sock.close()
|
||||
if result != 0:
|
||||
return True
|
||||
except Exception:
|
||||
return True
|
||||
time.sleep(0.1)
|
||||
return False
|
||||
ENV_ID = "brencher_local1"
|
||||
CONTAINER_NAME = "brencher_plain-container"
|
||||
|
||||
class TestDryRunPlaywright:
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def setup_teardown(self) -> Generator[None, Any, None]:
|
||||
global _server_thread
|
||||
|
||||
def stop_and_remove_container() -> None:
|
||||
client = docker.from_env()
|
||||
try:
|
||||
container = client.containers.get(CONTAINER_NAME)
|
||||
container.stop(timeout=10)
|
||||
container.remove(force=True)
|
||||
logger.info(f"Container {CONTAINER_NAME} stopped and removed")
|
||||
except Exception:
|
||||
logger.info(f"Container {CONTAINER_NAME} not found, no need to stop/remove")
|
||||
|
||||
# Ensure port is free before starting
|
||||
_is_port_free(APP_PORT, timeout=5.0)
|
||||
|
||||
stop_and_remove_container()
|
||||
yield None
|
||||
|
||||
# Clean up server thread
|
||||
if _server_thread and _server_thread.is_alive():
|
||||
_server_thread.join(timeout=2.0)
|
||||
logger.info("Waited for server thread to finish")
|
||||
|
||||
# Ensure port is free after test
|
||||
_is_port_free(APP_PORT, timeout=5.0)
|
||||
|
||||
stop_and_remove_container()
|
||||
_server_thread = None
|
||||
|
||||
client = docker.from_env()
|
||||
containers = client.containers.list(filters={"name": CONTAINER_NAME}, all=True)
|
||||
for container in containers:
|
||||
container.stop(timeout=10)
|
||||
container.remove(force=True)
|
||||
logger.info(f"Container {CONTAINER_NAME} stopped and removed")
|
||||
|
||||
def test_dry_run_prevents_deploy_until_disabled(self, eventually: EventuallyFn) -> None:
|
||||
global _server_thread
|
||||
|
||||
# Start app with nginx_local1 with main branch pre-selected and dry mode on.
|
||||
env = nginx.test_local1
|
||||
env.state.set_branches([("test/main", "HEAD")])
|
||||
# Start app with brencher_local1 with main branch pre-selected and dry mode on.
|
||||
env = brencher_local1.brencher_local1
|
||||
env.state.set_branches([("main", "HEAD")])
|
||||
env.state.set_dry(True)
|
||||
app = App({env.id: env})
|
||||
_server_thread = threading.Thread(target=lambda: app.runWeb(APP_PORT), daemon=True)
|
||||
_server_thread.start()
|
||||
app = App({"brencher_local1": env})
|
||||
server_thread = threading.Thread(target=lambda: app.runWeb(APP_PORT), daemon=True)
|
||||
server_thread.start()
|
||||
|
||||
# Wait for server to be up
|
||||
eventually(lambda: assert_equal(requests.get(f"{APP_URL}/state", timeout=5).status_code, 200, "Server did not respond with 200 OK within timeout"))
|
||||
@@ -90,10 +46,10 @@ class TestDryRunPlaywright:
|
||||
|
||||
# Wait for the pipeline to stabilize with dry=True:
|
||||
# DockerContainerDeploy should return status="dry-run"
|
||||
with brencher_page(APP_URL, env.id) as page:
|
||||
with brencher_page(APP_URL) as page:
|
||||
eventually(
|
||||
lambda: check_state(APP_URL, lambda s: assert_equal(next(
|
||||
p for p in s[env.id]["pipeline"] if p["name"] == "SharedStateHolderInMemory"
|
||||
p for p in s[ENV_ID]["pipeline"] if p["name"] == "SharedStateHolderInMemory"
|
||||
)["status"]['dry'], True, "Pipeline step status is not 'running'")),
|
||||
)
|
||||
|
||||
@@ -114,13 +70,13 @@ class TestDryRunPlaywright:
|
||||
# Wait for pipeline to re-run and actually deploy the container
|
||||
eventually(
|
||||
lambda: check_state(APP_URL, lambda s: assert_equal(next(
|
||||
p for p in s[env.id]["pipeline"] if p["name"] =="SharedStateHolderInMemory"
|
||||
p for p in s[ENV_ID]["pipeline"] if p["name"] =="SharedStateHolderInMemory"
|
||||
)["status"]['dry'], False, "Pipeline step status is not 'running'")),
|
||||
)
|
||||
# Wait for pipeline to re-run and actually deploy the container
|
||||
eventually(
|
||||
lambda: check_state(APP_URL, lambda s: assert_equal({p["name"] for p in s[env.id]["pipeline"] if bool(p['is_running'])}, set(), "Pipeline still running")),
|
||||
timeout=20,
|
||||
lambda: check_state(APP_URL, lambda s: assert_equal({p["name"] for p in s[ENV_ID]["pipeline"] if bool(p['is_running'])}, set(), "Pipeline still running")),
|
||||
timeout=60,
|
||||
interval=1.0,
|
||||
)
|
||||
# page.verify_pipeline_step_status("DockerContainerDeploy", "running")
|
||||
|
||||
@@ -1,61 +0,0 @@
|
||||
from git import BadName
|
||||
import pytest
|
||||
|
||||
from enironment import get_step
|
||||
from steps.git import CheckoutMerged
|
||||
from tests.conftest import repo_helper
|
||||
|
||||
from .test_remote_repo import RemoteRepoHelper
|
||||
|
||||
|
||||
class TestGitRemoveBranch:
|
||||
"""Integration tests for git operations"""
|
||||
|
||||
def test_checkout_merged_one_branch(self, repo_helper: RemoteRepoHelper) -> None:
|
||||
# Create main branch with initial commit
|
||||
commit1 = repo_helper.create_commit(repo_helper.repo, "master", "master", "file1.txt", "content1",
|
||||
"Initial commit") # noqa: F841
|
||||
commit2 = repo_helper.create_commit(repo_helper.repo, "master", "branch1", "file2.txt", "content2",
|
||||
"Branch1 commit") # noqa: F841
|
||||
commit3 = repo_helper.create_commit(repo_helper.repo, "master", "branch2", "file3.txt", "content3",
|
||||
"Branch2 commit") # noqa: F841
|
||||
|
||||
repo_helper.enable_caching()
|
||||
|
||||
repo_helper.set_desired_branches([("branch1", "HEAD"), ("branch2", "HEAD")])
|
||||
repo_helper.progress()
|
||||
|
||||
repo_helper.verify_working_directory_files([
|
||||
('file1.txt', 'content1'),
|
||||
('file2.txt', 'content2'),
|
||||
('file3.txt', 'content3'),
|
||||
])
|
||||
|
||||
assert repo_helper.checkout_merged.progress().version == '-'.join([it[:8] for it in sorted([commit2.hexsha, commit3.hexsha])])
|
||||
|
||||
repo_helper.remove_branch(repo_helper.repo, "branch2")
|
||||
|
||||
# CheckoutMerged should raise an error when it can't find the branch, and ignore_missing_branches is False
|
||||
get_step(repo_helper.env.pipeline, CheckoutMerged).ignore_missing_branches = False
|
||||
|
||||
with pytest.raises(BadName, match="Ref 'origin/branch2' did not resolve to an object"):
|
||||
repo_helper.progress()
|
||||
assert repo_helper.get_desired_branches() == [("branch1", "HEAD"), ("branch2", "HEAD")]
|
||||
with pytest.raises(BadName, match="Ref 'origin/branch2' did not resolve to an object"):
|
||||
repo_helper.checkout_merged.progress()
|
||||
|
||||
# Now set ignore_missing_branches to True and verify that it ignores the missing branch and continues processing
|
||||
get_step(repo_helper.env.pipeline, CheckoutMerged).ignore_missing_branches = True
|
||||
repo_helper.progress()
|
||||
assert repo_helper.get_desired_branches() == [("branch1", "HEAD"), ("branch2", "HEAD")]
|
||||
assert repo_helper.checkout_merged.progress().version == '-'.join([it[:8] for it in [commit2.hexsha]])
|
||||
|
||||
# Removing missing branch should be processed well
|
||||
repo_helper.set_desired_branches([("branch1", "HEAD")])
|
||||
repo_helper.progress()
|
||||
assert repo_helper.checkout_merged.progress().version == '-'.join([it[:8] for it in [commit2.hexsha]])
|
||||
|
||||
repo_helper.verify_working_directory_files([
|
||||
('file1.txt', 'content1'),
|
||||
('file2.txt', 'content2'),
|
||||
])
|
||||
@@ -10,8 +10,8 @@ import tempfile
|
||||
from typing import Dict, Tuple, List, Callable, cast
|
||||
|
||||
import git
|
||||
from enironment import Environment, AbstractStep, wrap_in_cached, get_step, SharedStateHolder
|
||||
from processing import reset_caches
|
||||
|
||||
from enironment import Environment, AbstractStep, SharedStateHolder, get_step
|
||||
from steps.docker import DockerSwarmCheckResult
|
||||
from steps.git import CheckoutAndMergeResult, CheckoutMerged, GitUnmerge, GitClone, HasVersion, GitUnmergeResult
|
||||
from steps.shared_state import SharedStateHolderInMemory
|
||||
@@ -106,22 +106,6 @@ class RemoteRepoHelper:
|
||||
def mock_check(self) -> AbstractStep[Dict[str, HasVersion]]:
|
||||
return get_step(self.env.pipeline, MockDockerSwarmCheck)
|
||||
|
||||
def enable_caching(self) -> None:
|
||||
"""Wrap environment pipeline in caching steps"""
|
||||
self.env = wrap_in_cached(self.env)
|
||||
|
||||
def progress(self) -> None:
|
||||
reset_caches([self.env])
|
||||
for i in self.env.pipeline:
|
||||
i.progress()
|
||||
|
||||
def remove_branch(self, repo: git.Repo, branch_name: str) -> None:
|
||||
"""Remove a branch from the repository"""
|
||||
if branch_name in [head.name for head in repo.heads]:
|
||||
repo.delete_head(branch_name, force=True)
|
||||
else:
|
||||
raise BaseException(f"Branch doesn't exist {branch_name}.")
|
||||
|
||||
def teardown(self) -> None:
|
||||
"""Clean up temporary directories"""
|
||||
if self.remote_dir:
|
||||
@@ -158,9 +142,7 @@ class RemoteRepoHelper:
|
||||
with open(file_path, 'w') as f:
|
||||
f.write(content)
|
||||
repo.index.add([filename])
|
||||
result = repo.index.commit(message)
|
||||
repo.git.checkout(result.hexsha, detach=True)
|
||||
return result
|
||||
return repo.index.commit(message)
|
||||
|
||||
def verify_working_directory_files(self, expected_files: List[Tuple[str, str]]) -> None:
|
||||
"""Verify that working directory contains exactly the expected files with correct content.
|
||||
@@ -186,12 +168,10 @@ class RemoteRepoHelper:
|
||||
|
||||
repo = git.Repo(repo_path)
|
||||
commits = list(repo.iter_commits("--all", max_count=30))
|
||||
refs = []
|
||||
for it in repo.references:
|
||||
try:
|
||||
refs.append(f"{_as_text(it.commit)[:10]}:{_as_text(it.name)}, is_remote:{it.is_remote()}")
|
||||
except Exception as e:
|
||||
refs.append(f"Error reading branch {it.name}: {e}")
|
||||
refs = [
|
||||
f"{_as_text(it.commit)[:10]}:{_as_text(it.name)}, is_remote:{it.is_remote()}"
|
||||
for it in repo.refs
|
||||
]
|
||||
lines = [
|
||||
f"{_as_text(commit.hexsha)[:10]} (p{[_as_text(it.hexsha)[:10] for it in commit.parents]}): {_as_text(commit.message).splitlines()[0]}"
|
||||
for commit in commits
|
||||
|
||||
Reference in New Issue
Block a user