Skip to content

Commit 25f0a80

Browse files
committed
make things cleaner
1 parent 3599dfb commit 25f0a80

File tree

4 files changed

+61
-194
lines changed

4 files changed

+61
-194
lines changed

scripts/release/branch_detection.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,13 @@ def get_current_branch() -> Optional[str]:
2121
["git", "rev-parse", "--abbrev-ref", "HEAD"], capture_output=True, text=True, check=True
2222
)
2323
branch = result.stdout.strip()
24-
if branch and branch != "HEAD":
24+
if branch == "HEAD":
25+
return "master"
26+
if branch != "":
2527
return branch
2628
except (subprocess.CalledProcessError, FileNotFoundError):
2729
return "master"
30+
return "master"
2831

2932
def get_cache_scope() -> str:
3033
"""

scripts/release/build/image_build_process.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ def execute_docker_build(
185185
base_registry
186186
)
187187

188-
ensure_ecr_cache_repository(base_cache_repo)
188+
# ensure_ecr_cache_repository(base_cache_repo)
189189

190190
logger.info(f"Building image: {tag}")
191191
logger.info(f"Platforms: {platforms}")
Lines changed: 34 additions & 161 deletions
Original file line numberDiff line numberDiff line change
@@ -1,110 +1,76 @@
1-
import os
21
import subprocess
32
from unittest.mock import MagicMock, patch
43

5-
import pytest
6-
74
from scripts.release.branch_detection import (
85
get_cache_scope,
96
get_current_branch,
10-
get_version_id,
11-
is_evg_patch,
12-
is_running_in_evg,
137
)
148

159

1610
class TestGetCurrentBranch:
1711
"""Test branch detection logic for different scenarios."""
1812

19-
@patch("scripts.release.branch_detection.is_running_in_evg")
2013
@patch("subprocess.run")
21-
def test_local_development_master_branch(self, mock_run, mock_is_evg):
22-
"""Test local development on master branch."""
23-
mock_is_evg.return_value = False
14+
def test_master_branch(self, mock_run):
15+
"""Test detection of master branch."""
2416
mock_run.return_value = MagicMock(stdout="master\n", returncode=0)
2517

2618
result = get_current_branch()
2719

2820
assert result == "master"
2921
mock_run.assert_called_once_with(
30-
["git", "rev-parse", "--abbrev-ref", "HEAD"], capture_output=True, text=True, check=True
22+
["git", "rev-parse", "--abbrev-ref", "HEAD"],
23+
capture_output=True,
24+
text=True,
25+
check=True
3126
)
3227

33-
@patch("scripts.release.branch_detection.is_running_in_evg")
3428
@patch("subprocess.run")
35-
def test_local_development_feature_branch(self, mock_run, mock_is_evg):
36-
"""Test local development on feature branch."""
37-
mock_is_evg.return_value = False
29+
def test_feature_branch(self, mock_run):
30+
"""Test detection of feature branch."""
3831
mock_run.return_value = MagicMock(stdout="feature/new-cache\n", returncode=0)
3932

4033
result = get_current_branch()
4134

4235
assert result == "feature/new-cache"
36+
mock_run.assert_called_once_with(
37+
["git", "rev-parse", "--abbrev-ref", "HEAD"],
38+
capture_output=True,
39+
text=True,
40+
check=True
41+
)
4342

44-
@patch("scripts.release.branch_detection.is_running_in_evg")
4543
@patch("subprocess.run")
46-
def test_local_development_detached_head(self, mock_run, mock_is_evg):
47-
"""Test local development in detached HEAD state."""
48-
mock_is_evg.return_value = False
44+
def test_detached_head(self, mock_run):
45+
"""Test detection when in detached HEAD state."""
4946
mock_run.return_value = MagicMock(stdout="HEAD\n", returncode=0)
5047

5148
result = get_current_branch()
5249

5350
assert result == "master" # fallback to master
5451

55-
@patch("scripts.release.branch_detection.is_running_in_evg")
5652
@patch("subprocess.run")
57-
def test_local_development_git_error(self, mock_run, mock_is_evg):
58-
"""Test local development when git command fails."""
59-
mock_is_evg.return_value = False
60-
mock_run.side_effect = subprocess.CalledProcessError(1, "git")
53+
def test_empty_output(self, mock_run):
54+
"""Test detection when git returns empty output."""
55+
mock_run.return_value = MagicMock(stdout="\n", returncode=0)
6156

6257
result = get_current_branch()
6358

6459
assert result == "master" # fallback to master
6560

66-
@patch("scripts.release.branch_detection.is_running_in_evg")
67-
@patch("scripts.release.branch_detection.is_evg_patch")
68-
@patch("subprocess.run")
69-
def test_evergreen_non_patch_build(self, mock_run, mock_is_patch, mock_is_evg):
70-
"""Test Evergreen non-patch build."""
71-
mock_is_evg.return_value = True
72-
mock_is_patch.return_value = False
73-
mock_run.return_value = MagicMock(stdout="master\n", returncode=0)
74-
75-
result = get_current_branch()
76-
77-
assert result == "master"
78-
79-
@patch("scripts.release.branch_detection.is_running_in_evg")
80-
@patch("scripts.release.branch_detection.is_evg_patch")
8161
@patch("subprocess.run")
82-
def test_evergreen_patch_build_branch_detection(self, mock_run, mock_is_patch, mock_is_evg):
83-
"""Test Evergreen patch build with successful branch detection."""
84-
mock_is_evg.return_value = True
85-
mock_is_patch.return_value = True
86-
87-
# Mock git for-each-ref output
88-
mock_run.side_effect = [
89-
MagicMock(
90-
stdout="origin/feature/cache-improvement abc123\norigin/evg-pr-test-123 abc123\norigin/main def456\n",
91-
returncode=0,
92-
),
93-
MagicMock(stdout="abc123\n", returncode=0),
94-
]
62+
def test_git_command_fails(self, mock_run):
63+
"""Test fallback when git command fails."""
64+
mock_run.side_effect = subprocess.CalledProcessError(1, "git")
9565

9666
result = get_current_branch()
9767

98-
assert result == "feature/cache-improvement"
68+
assert result == "master" # fallback to master
9969

100-
@patch("scripts.release.branch_detection.is_running_in_evg")
101-
@patch("scripts.release.branch_detection.is_evg_patch")
10270
@patch("subprocess.run")
103-
def test_evergreen_patch_build_fallback(self, mock_run, mock_is_patch, mock_is_evg):
104-
"""Test Evergreen patch build fallback when branch detection fails."""
105-
mock_is_evg.return_value = True
106-
mock_is_patch.return_value = True
107-
mock_run.side_effect = subprocess.CalledProcessError(1, "git")
71+
def test_git_not_found(self, mock_run):
72+
"""Test fallback when git is not found."""
73+
mock_run.side_effect = FileNotFoundError("git not found")
10874

10975
result = get_current_branch()
11076

@@ -114,124 +80,31 @@ def test_evergreen_patch_build_fallback(self, mock_run, mock_is_patch, mock_is_e
11480
class TestGetCacheScope:
11581
"""Test cache scope generation for different scenarios."""
11682

117-
@patch("scripts.release.branch_detection.get_current_branch")
118-
@patch("scripts.release.branch_detection.is_evg_patch")
119-
@patch("scripts.release.branch_detection.get_version_id")
120-
def test_master_branch_non_patch(self, mock_version_id, mock_is_patch, mock_branch):
121-
"""Test cache scope for master branch non-patch build."""
122-
mock_branch.return_value = "master"
123-
mock_is_patch.return_value = False
124-
mock_version_id.return_value = None
125-
126-
result = get_cache_scope()
127-
128-
assert result == "master"
12983

13084
@patch("scripts.release.branch_detection.get_current_branch")
131-
@patch("scripts.release.branch_detection.is_evg_patch")
132-
@patch("scripts.release.branch_detection.get_version_id")
133-
def test_feature_branch_non_patch(self, mock_version_id, mock_is_patch, mock_branch):
134-
"""Test cache scope for feature branch non-patch build."""
85+
def test_feature_branch(self, mock_branch):
86+
"""Test cache scope for feature branch."""
13587
mock_branch.return_value = "feature/new-cache"
136-
mock_is_patch.return_value = False
137-
mock_version_id.return_value = None
13888

13989
result = get_cache_scope()
14090

14191
assert result == "feature-new-cache"
14292

14393
@patch("scripts.release.branch_detection.get_current_branch")
144-
@patch("scripts.release.branch_detection.is_evg_patch")
145-
@patch("scripts.release.branch_detection.get_version_id")
146-
def test_patch_build_with_version_id(self, mock_version_id, mock_is_patch, mock_branch):
147-
"""Test cache scope for patch build with version ID."""
148-
mock_branch.return_value = "feature/new-cache"
149-
mock_is_patch.return_value = True
150-
mock_version_id.return_value = "6899b7e35bfaee00077db986"
151-
152-
result = get_cache_scope()
153-
154-
assert result == "feature-new-cache-6899b7e3"
155-
156-
@patch("scripts.release.branch_detection.get_current_branch")
157-
@patch("scripts.release.branch_detection.is_evg_patch")
158-
@patch("scripts.release.branch_detection.get_version_id")
159-
def test_patch_build_without_version_id(self, mock_version_id, mock_is_patch, mock_branch):
160-
"""Test cache scope for patch build without version ID."""
161-
mock_branch.return_value = "feature/new-cache"
162-
mock_is_patch.return_value = True
163-
mock_version_id.return_value = None
164-
165-
result = get_cache_scope()
166-
167-
assert result == "feature-new-cache"
168-
169-
@patch("scripts.release.branch_detection.get_current_branch")
170-
@patch("scripts.release.branch_detection.is_evg_patch")
171-
@patch("scripts.release.branch_detection.get_version_id")
172-
def test_branch_name_sanitization(self, mock_version_id, mock_is_patch, mock_branch):
94+
def test_branch_name_sanitization(self, mock_branch):
17395
"""Test branch name sanitization for cache scope."""
17496
mock_branch.return_value = "Feature/NEW_cache@123"
175-
mock_is_patch.return_value = False
176-
mock_version_id.return_value = None
17797

17898
result = get_cache_scope()
17999

180100
assert result == "feature-new_cache-123"
181101

102+
182103
@patch("scripts.release.branch_detection.get_current_branch")
183-
@patch("scripts.release.branch_detection.is_evg_patch")
184-
@patch("scripts.release.branch_detection.get_version_id")
185-
def test_dependabot_branch(self, mock_version_id, mock_is_patch, mock_branch):
186-
"""Test cache scope for dependabot branch."""
187-
mock_branch.return_value = "dependabot/npm_and_yarn/lodash-4.17.21"
188-
mock_is_patch.return_value = False
189-
mock_version_id.return_value = None
104+
def test_complex_branch_name(self, mock_branch):
105+
"""Test cache scope for complex branch name with special characters."""
106+
mock_branch.return_value = "user/feature-123_test.branch"
190107

191108
result = get_cache_scope()
192109

193-
assert result == "dependabot-npm_and_yarn-lodash-4.17.21"
194-
195-
196-
class TestEnvironmentDetection:
197-
"""Test environment detection functions."""
198-
199-
def test_is_evg_patch_true(self):
200-
"""Test is_evg_patch returns True when is_patch is 'true'."""
201-
with patch.dict(os.environ, {"is_patch": "true"}):
202-
assert is_evg_patch() is True
203-
204-
def test_is_evg_patch_false(self):
205-
"""Test is_evg_patch returns False when is_patch is 'false'."""
206-
with patch.dict(os.environ, {"is_patch": "false"}):
207-
assert is_evg_patch() is False
208-
209-
def test_is_evg_patch_default(self):
210-
"""Test is_evg_patch returns False when is_patch is not set."""
211-
with patch.dict(os.environ, {}, clear=True):
212-
assert is_evg_patch() is False
213-
214-
def test_is_running_in_evg_true(self):
215-
"""Test is_running_in_evg returns True when RUNNING_IN_EVG is 'true'."""
216-
with patch.dict(os.environ, {"RUNNING_IN_EVG": "true"}):
217-
assert is_running_in_evg() is True
218-
219-
def test_is_running_in_evg_false(self):
220-
"""Test is_running_in_evg returns False when RUNNING_IN_EVG is 'false'."""
221-
with patch.dict(os.environ, {"RUNNING_IN_EVG": "false"}):
222-
assert is_running_in_evg() is False
223-
224-
def test_is_running_in_evg_default(self):
225-
"""Test is_running_in_evg returns False when RUNNING_IN_EVG is not set."""
226-
with patch.dict(os.environ, {}, clear=True):
227-
assert is_running_in_evg() is False
228-
229-
def test_get_version_id_set(self):
230-
"""Test get_version_id returns value when version_id is set."""
231-
with patch.dict(os.environ, {"version_id": "6899b7e35bfaee00077db986"}):
232-
assert get_version_id() == "6899b7e35bfaee00077db986"
233-
234-
def test_get_version_id_not_set(self):
235-
"""Test get_version_id returns None when version_id is not set."""
236-
with patch.dict(os.environ, {}, clear=True):
237-
assert get_version_id() is None
110+
assert result == "user-feature-123_test.branch"

scripts/release/tests/image_build_process_test.py

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,71 +2,62 @@
22

33
from scripts.release.build.image_build_process import (
44
build_cache_configuration,
5+
ensure_all_cache_repositories,
56
)
67

78

89
class TestBuildCacheConfiguration:
910
"""Test cache configuration building for different scenarios."""
1011

1112
@patch("scripts.release.build.image_build_process.get_cache_scope")
12-
@patch("scripts.release.build.image_build_process.get_current_branch")
13-
def test_master_branch(self, mock_branch, mock_scope):
13+
def test_master_branch(self, mock_scope):
1414
"""Test cache configuration for master branch."""
15-
mock_branch.return_value = "master"
1615
mock_scope.return_value = "master"
1716

1817
base_registry = "268558157000.dkr.ecr.us-east-1.amazonaws.com/dev/cache/mongodb-kubernetes"
1918
cache_from, cache_to = build_cache_configuration(base_registry)
2019

21-
# Should read from master and shared
20+
# Should read from master only
2221
expected_from = [
23-
{"type": "registry", "ref": f"{base_registry}:master"},
24-
{"type": "registry", "ref": f"{base_registry}:shared"},
22+
{"type": "registry", "ref": f"{base_registry}:master"}
2523
]
2624
assert cache_from == expected_from
2725

2826
# Should write to master
29-
assert len(cache_to) == 1
30-
assert cache_to[0]["ref"] == f"{base_registry}:master"
31-
assert cache_to[0]["mode"] == "max"
32-
assert cache_to[0]["oci-mediatypes"] == "true"
33-
assert cache_to[0]["image-manifest"] == "true"
27+
assert cache_to["ref"] == f"{base_registry}:master"
28+
assert cache_to["mode"] == "max"
29+
assert cache_to["oci-mediatypes"] == "true"
30+
assert cache_to["image-manifest"] == "true"
3431

3532
@patch("scripts.release.build.image_build_process.get_cache_scope")
36-
@patch("scripts.release.build.image_build_process.get_current_branch")
37-
def test_feature_branch(self, mock_branch, mock_scope):
33+
def test_feature_branch(self, mock_scope):
3834
"""Test cache configuration for feature branch."""
39-
mock_branch.return_value = "feature/new-cache"
4035
mock_scope.return_value = "feature-new-cache"
4136

4237
base_registry = "268558157000.dkr.ecr.us-east-1.amazonaws.com/dev/cache/mongodb-kubernetes"
4338
cache_from, cache_to = build_cache_configuration(base_registry)
4439

45-
# Should read from branch, master, and shared
40+
# Should read from branch and master
4641
expected_from = [
4742
{"type": "registry", "ref": f"{base_registry}:feature-new-cache"},
48-
{"type": "registry", "ref": f"{base_registry}:master"},
49-
{"type": "registry", "ref": f"{base_registry}:shared"},
43+
{"type": "registry", "ref": f"{base_registry}:master"}
5044
]
5145
assert cache_from == expected_from
5246

53-
# Should write to branch only (not master since we're not on master)
54-
assert len(cache_to) == 1
55-
assert cache_to[0]["ref"] == f"{base_registry}:feature-new-cache"
56-
assert cache_to[0]["mode"] == "max"
57-
assert cache_to[0]["oci-mediatypes"] == "true"
58-
assert cache_to[0]["image-manifest"] == "true"
47+
# Should write to branch only
48+
assert cache_to["ref"] == f"{base_registry}:feature-new-cache"
49+
assert cache_to["mode"] == "max"
50+
assert cache_to["oci-mediatypes"] == "true"
51+
assert cache_to["image-manifest"] == "true"
5952

6053
@patch("scripts.release.build.image_build_process.get_cache_scope")
61-
@patch("scripts.release.build.image_build_process.get_current_branch")
62-
def test_patch_build_with_version_id(self, mock_branch, mock_scope):
63-
"""Test cache configuration for patch build with version ID."""
64-
mock_branch.return_value = "feature/new-cache"
65-
mock_scope.return_value = "feature-new-cache-6899b7e3"
54+
def test_sanitized_branch_name(self, mock_scope):
55+
"""Test cache configuration with sanitized branch name."""
56+
mock_scope.return_value = "feature-new-cache-123"
6657

6758
base_registry = "268558157000.dkr.ecr.us-east-1.amazonaws.com/dev/cache/mongodb-kubernetes"
6859
cache_from, cache_to = build_cache_configuration(base_registry)
6960

70-
# Should include version ID in cache refs
71-
assert cache_from[0]["ref"] == f"{base_registry}:feature-new-cache-6899b7e3"
72-
assert cache_to[0]["ref"] == f"{base_registry}:feature-new-cache-6899b7e3"
61+
# Should include sanitized branch name in cache refs
62+
assert cache_from[0]["ref"] == f"{base_registry}:feature-new-cache-123"
63+
assert cache_to["ref"] == f"{base_registry}:feature-new-cache-123"

0 commit comments

Comments
 (0)