Skip to content

Commit fa19d21

Browse files
author
Eidmantas Ivanauskas
committed
test(git): verify batched writer groups intents by branch and avoids cross-branch mixing
1 parent 16a965e commit fa19d21

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

pkg/argocd/git_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,55 @@
11
package argocd
22

3+
import (
4+
"testing"
5+
gitm "github.com/argoproj-labs/argocd-image-updater/ext/git/mocks"
6+
"github.com/argoproj-labs/argocd-image-updater/pkg/common"
7+
v1alpha1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
8+
)
9+
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) }
36+
37+
rw.flushBatch([]writeIntent{
38+
{app: appA, wbc: wbcA, changeList: []ChangeEntry{{}}, writeFn: writeOverrides},
39+
{app: appB, wbc: wbcB, changeList: []ChangeEntry{{}}, writeFn: writeOverrides},
40+
{app: appA, wbc: wbcA, changeList: []ChangeEntry{{}}, writeFn: writeOverrides},
41+
})
42+
43+
if got["appA-branch"] != 2 {
44+
t.Fatalf("expected 2 intents for appA-branch, got %d", got["appA-branch"])
45+
}
46+
if got["appB-branch"] != 1 {
47+
t.Fatalf("expected 1 intent for appB-branch, got %d", got["appB-branch"])
48+
}
49+
}
50+
51+
package argocd
52+
353
import (
454
"os"
555
"testing"

0 commit comments

Comments
 (0)