Skip to content

Commit 48e2692

Browse files
author
Eidmantas Ivanauskas
committed
test(git): add unit test for branch grouping logic in batched writer
1 parent fa19d21 commit 48e2692

File tree

2 files changed

+21
-36
lines changed

2 files changed

+21
-36
lines changed

pkg/argocd/git.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,18 +201,24 @@ func (rw *repoWriter) loop() {
201201

202202
func (rw *repoWriter) flushBatch(batch []writeIntent) {
203203
// Group intents by resolved push branch to avoid mixing branches
204+
byBranch := groupIntentsByBranch(batch)
205+
for branch, intents := range byBranch {
206+
rw.commitBatch(branch, intents)
207+
}
208+
}
209+
210+
// groupIntentsByBranch groups write intents by their resolved push branch.
211+
// This is a pure function intended for testing and reuse.
212+
func groupIntentsByBranch(batch []writeIntent) map[string][]writeIntent {
204213
byBranch := map[string][]writeIntent{}
205214
for _, wi := range batch {
206215
branch := getWriteBackBranch(wi.app)
207216
if wi.wbc.GitWriteBranch != "" {
208-
// honor template-derived write branch if set already on wbc
209217
branch = wi.wbc.GitWriteBranch
210218
}
211219
byBranch[branch] = append(byBranch[branch], wi)
212220
}
213-
for branch, intents := range byBranch {
214-
rw.commitBatch(branch, intents)
215-
}
221+
return byBranch
216222
}
217223

218224
func (rw *repoWriter) commitBatch(branch string, intents []writeIntent) {

pkg/argocd/git_test.go

Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,28 @@ package argocd
22

33
import (
44
"testing"
5-
gitm "github.com/argoproj-labs/argocd-image-updater/ext/git/mocks"
65
"github.com/argoproj-labs/argocd-image-updater/pkg/common"
76
v1alpha1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
87
)
98

10-
// Test that batched writer does not mix different branches in one commit/push
11-
func TestRepoWriter_GroupsByBranch(t *testing.T) {
12-
// Build two apps targeting different write branches via annotation
13-
appA := &v1alpha1.Application{}
14-
appA.Annotations = map[string]string{
15-
common.WriteBackMethodAnnotation: "git",
16-
common.GitBranchAnnotation: "main:appA-branch",
17-
}
18-
appB := &v1alpha1.Application{}
19-
appB.Annotations = map[string]string{
20-
common.WriteBackMethodAnnotation: "git",
21-
common.GitBranchAnnotation: "main:appB-branch",
22-
}
23-
24-
// Minimal wbc for both
25-
wbcA := &WriteBackConfig{GitRepo: "https://example/repo.git", GitCommitUser: "u", GitCommitEmail: "e"}
26-
wbcB := &WriteBackConfig{GitRepo: "https://example/repo.git", GitCommitUser: "u", GitCommitEmail: "e"}
27-
// bind write-branch via parseGitConfig path expectation (we rely on getWriteBackBranch during grouping)
28-
wbcA.GitWriteBranch = "appA-branch"
29-
wbcB.GitWriteBranch = "appB-branch"
30-
31-
// Use mocks for git client by intercepting commitChangesGit is hard; instead, exercise grouping directly.
32-
rw := &repoWriter{repoURL: wbcA.GitRepo}
33-
// Replace commitBatch with an interceptor
34-
got := make(map[string]int)
35-
rw.commitBatch = func(branch string, intents []writeIntent) { got[branch] = len(intents) }
9+
// Test that grouping does not mix different branches in one commit/push
10+
func Test_groupIntentsByBranch(t *testing.T) {
11+
appA := &v1alpha1.Application{Annotations: map[string]string{common.GitBranchAnnotation: "main:appA-branch"}}
12+
appB := &v1alpha1.Application{Annotations: map[string]string{common.GitBranchAnnotation: "main:appB-branch"}}
13+
wbcA := &WriteBackConfig{GitRepo: "https://example/repo.git", GitWriteBranch: "appA-branch"}
14+
wbcB := &WriteBackConfig{GitRepo: "https://example/repo.git", GitWriteBranch: "appB-branch"}
3615

37-
rw.flushBatch([]writeIntent{
16+
by := groupIntentsByBranch([]writeIntent{
3817
{app: appA, wbc: wbcA, changeList: []ChangeEntry{{}}, writeFn: writeOverrides},
3918
{app: appB, wbc: wbcB, changeList: []ChangeEntry{{}}, writeFn: writeOverrides},
4019
{app: appA, wbc: wbcA, changeList: []ChangeEntry{{}}, writeFn: writeOverrides},
4120
})
4221

43-
if got["appA-branch"] != 2 {
44-
t.Fatalf("expected 2 intents for appA-branch, got %d", got["appA-branch"])
22+
if len(by["appA-branch"]) != 2 {
23+
t.Fatalf("expected 2 intents for appA-branch, got %d", len(by["appA-branch"]))
4524
}
46-
if got["appB-branch"] != 1 {
47-
t.Fatalf("expected 1 intent for appB-branch, got %d", got["appB-branch"])
25+
if len(by["appB-branch"]) != 1 {
26+
t.Fatalf("expected 1 intent for appB-branch, got %d", len(by["appB-branch"]))
4827
}
4928
}
5029

0 commit comments

Comments
 (0)