-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlib-gitops-functions.bats
More file actions
143 lines (118 loc) · 4.42 KB
/
Copy pathlib-gitops-functions.bats
File metadata and controls
143 lines (118 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/env bats
load 'test_helper/setup'
setup() {
setup_common
source "${BATS_TEST_DIRNAME}/../scripts/lib/common.sh"
source "${BATS_TEST_DIRNAME}/../scripts/lib/gitops-functions.sh"
export GITHUB_REPOSITORY="Staffbase/my-service"
export GITHUB_SHA="abcdef1234567890"
export INPUT_DOCKER_REGISTRY="registry.staffbase.com"
export INPUT_DOCKER_IMAGE="my-service"
export INPUT_TAG="main-abcdef12"
export INPUT_PUSH="true"
export INPUT_GITOPS_USER="Staffbot"
export INPUT_GITOPS_TOKEN="fake-token"
export INPUT_GITOPS_ORGANIZATION="Staffbase"
export INPUT_GITOPS_REPOSITORY="mops"
export IMAGE="registry.staffbase.com/my-service:main-abcdef12"
# Create mock yq
mkdir -p "${TEST_TEMP_DIR}/mocks"
cat > "${TEST_TEMP_DIR}/mocks/yq" << 'YQ_MOCK'
#!/usr/bin/env bash
echo "yq $*" >> "${MOCK_CALLS_DIR}/yq_calls.log"
# For -e (evaluate/check), just succeed
# For -i (in-place edit), do nothing
exit 0
YQ_MOCK
chmod +x "${TEST_TEMP_DIR}/mocks/yq"
export MOCK_CALLS_DIR="$TEST_TEMP_DIR"
# Create mock git
cat > "${TEST_TEMP_DIR}/mocks/git" << 'GIT_MOCK'
#!/usr/bin/env bash
echo "git $*" >> "${MOCK_CALLS_DIR}/git_calls.log"
case "$1" in
diff-index) exit 1 ;; # simulate changes exist
*) exit 0 ;;
esac
GIT_MOCK
chmod +x "${TEST_TEMP_DIR}/mocks/git"
export PATH="${TEST_TEMP_DIR}/mocks:$PATH"
}
teardown() {
teardown_common
}
# --- update_file ---
@test "update_file calls yq to check and update field" {
update_file "deployment.yaml" "spec.image" "$IMAGE"
assert [ -f "${TEST_TEMP_DIR}/yq_calls.log" ]
grep -q 'yq -e .spec.image deployment.yaml' "${TEST_TEMP_DIR}/yq_calls.log"
grep -q 'yq -i' "${TEST_TEMP_DIR}/yq_calls.log"
}
@test "update_file always writes deploy.staffbase.com/repositoryFullName annotation" {
update_file "deployment.yaml" "spec.image" "$IMAGE"
grep -q 'deploy.staffbase.com/repositoryFullName' "${TEST_TEMP_DIR}/yq_calls.log"
}
@test "update_file always writes deploy.staffbase.com/commitSha annotation" {
update_file "deployment.yaml" "spec.image" "$IMAGE"
grep -q 'deploy.staffbase.com/commitSha' "${TEST_TEMP_DIR}/yq_calls.log"
}
@test "update_file writes deploy.staffbase.com/version annotation with INPUT_TAG" {
update_file "deployment.yaml" "spec.image" "$IMAGE"
grep -q "deploy.staffbase.com/version.*${INPUT_TAG}" "${TEST_TEMP_DIR}/yq_calls.log"
}
@test "update_file does not write deploy.staffbase.com/deployment-id annotation" {
update_file "deployment.yaml" "spec.image" "$IMAGE"
! grep -q 'deployment-id' "${TEST_TEMP_DIR}/yq_calls.log"
}
@test "update_file does not write legacy /repo or /sha annotation keys" {
update_file "deployment.yaml" "spec.image" "$IMAGE"
! grep -qE 'deploy\.staffbase\.com/(repo|sha)"' "${TEST_TEMP_DIR}/yq_calls.log"
}
# --- commit_changes ---
@test "commit_changes commits and pushes when push is true" {
commit_changes
grep -q 'git add' "${TEST_TEMP_DIR}/git_calls.log"
grep -q 'git commit' "${TEST_TEMP_DIR}/git_calls.log"
grep -q 'git push' "${TEST_TEMP_DIR}/git_calls.log"
}
@test "commit_changes skips when push is false" {
export INPUT_PUSH="false"
commit_changes
[[ ! -f "${TEST_TEMP_DIR}/git_calls.log" ]]
}
@test "commit_changes skips commit when no changes" {
# Override git mock: diff-index returns 0 (no changes)
cat > "${TEST_TEMP_DIR}/mocks/git" << 'GIT_MOCK'
#!/usr/bin/env bash
echo "git $*" >> "${MOCK_CALLS_DIR}/git_calls.log"
exit 0
GIT_MOCK
chmod +x "${TEST_TEMP_DIR}/mocks/git"
commit_changes
! grep -q 'git commit' "${TEST_TEMP_DIR}/git_calls.log"
}
# --- process_file_updates ---
@test "process_file_updates processes multi-line input" {
local file_list="file1.yaml spec.image
file2.yaml spec.container.image"
process_file_updates "$file_list" "false"
local yq_count
yq_count=$(grep -c 'yq -e' "${TEST_TEMP_DIR}/yq_calls.log")
[[ "$yq_count" -eq 2 ]]
}
@test "process_file_updates skips empty lines" {
local file_list="file1.yaml spec.image
file2.yaml spec.image"
process_file_updates "$file_list" "false"
local yq_count
yq_count=$(grep -c 'yq -e' "${TEST_TEMP_DIR}/yq_calls.log")
[[ "$yq_count" -eq 2 ]]
}
@test "process_file_updates commits when should_commit is true" {
process_file_updates "file1.yaml spec.image" "true"
grep -q 'git commit' "${TEST_TEMP_DIR}/git_calls.log"
}
@test "process_file_updates skips commit when should_commit is false" {
process_file_updates "file1.yaml spec.image" "false"
! grep -q 'git commit' "${TEST_TEMP_DIR}/git_calls.log" 2>/dev/null || true
}