-
Notifications
You must be signed in to change notification settings - Fork 8
/
repository_test.go
313 lines (274 loc) · 8.94 KB
/
repository_test.go
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package git
import (
"fmt"
"io/ioutil"
"net/url"
"os"
"path"
"sort"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/rhd-gitops-example/services/test"
)
const testRepository = "https://github.com/mnuttall/staging.git"
func TestRepoName(t *testing.T) {
nameTests := []struct {
url string
wantName string
wantErr string
}{
{testRepository, "staging", ""},
{"https://github.com/mnuttall", "", "could not identify repository name: https://github.com/mnuttall"},
}
for _, tt := range nameTests {
n, err := repoName(tt.url)
if tt.wantName != n {
t.Errorf("repoName(%s) got name %s, want %s", tt.url, n, tt.wantName)
continue
}
if tt.wantErr != "" && err != nil && err.Error() != tt.wantErr {
t.Errorf("repoName(%s) got error %s, want %s", tt.url, err, tt.wantErr)
}
}
}
// If the directory provided doesn't exist e.g. "~/.promotion/cache" then the
// directory is created during the clone process.
func TestCloneCreatesDirectory(t *testing.T) {
tempDir, cleanup := makeTempDir(t)
defer cleanup()
r, err := NewRepository(testRepository, path.Join(tempDir, "path"), false)
assertNoError(t, err)
err = r.Clone()
assertNoError(t, err)
contents, err := ioutil.ReadFile(path.Join(tempDir, "path", "staging/services/service-a/base/config/deployment.txt"))
assertNoError(t, err)
want := "This is the staging version of this file.\n"
if diff := cmp.Diff(want, string(contents)); diff != "" {
t.Fatalf("failed to read file: %s", diff)
}
}
func TestClone(t *testing.T) {
r, cleanup := cloneTestRepository(t)
defer cleanup()
contents, err := ioutil.ReadFile(path.Join(r.LocalPath, "staging/services/service-a/base/config/deployment.txt"))
assertNoError(t, err)
want := "This is the staging version of this file.\n"
if diff := cmp.Diff(want, string(contents)); diff != "" {
t.Fatalf("failed to read file: %s", diff)
}
}
func TestWalk(t *testing.T) {
r, cleanup := cloneTestRepository(t)
defer cleanup()
visited := []string{}
err := r.Walk("services/service-a", func(prefix, name string) error {
visited = append(visited, name)
return nil
})
assertNoError(t, err)
want := []string{
"service-a/base/config/300-deployment.yaml",
"service-a/base/config/310-service.yaml",
"service-a/base/config/deployment.txt",
"service-a/base/config/kustomization.yaml",
"service-a/base/kustomization.yaml",
"service-a/overlays/kustomization.yaml",
"service-a/overlays/staging-deployment.yaml",
"service-a/overlays/staging-service.yaml",
}
if diff := cmp.Diff(want, visited); diff != "" {
t.Fatalf("failed to read file: %s", diff)
}
}
func TestWriteFile(t *testing.T) {
r, cleanup := cloneTestRepository(t)
defer cleanup()
err := r.WriteFile(strings.NewReader("this is some text"), "services/service-a/base/config/new-file.txt")
assertNoError(t, err)
visited := []string{}
err = r.Walk("services/service-a", func(prefix, name string) error {
visited = append(visited, name)
return nil
})
assertNoError(t, err)
sort.Strings(visited)
want := []string{
"service-a/base/config/300-deployment.yaml",
"service-a/base/config/310-service.yaml",
"service-a/base/config/deployment.txt",
"service-a/base/config/kustomization.yaml",
"service-a/base/config/new-file.txt",
"service-a/base/kustomization.yaml",
"service-a/overlays/kustomization.yaml",
"service-a/overlays/staging-deployment.yaml",
"service-a/overlays/staging-service.yaml",
}
if diff := cmp.Diff(want, visited); diff != "" {
t.Fatalf("failed to read file: %s", diff)
}
}
func TestCopyFile(t *testing.T) {
r, cleanup := cloneTestRepository(t)
defer cleanup()
tmpfile, err := ioutil.TempFile("", "source")
assertNoError(t, err)
defer os.Remove(tmpfile.Name())
content := []byte(`test content`)
_, err = tmpfile.Write(content)
assertNoError(t, err)
err = tmpfile.Close()
assertNoError(t, err)
err = r.CopyFile(tmpfile.Name(), "services/service-a/copy/copied.txt")
assertNoError(t, err)
visited := []string{}
err = r.Walk("services/service-a", func(prefix, name string) error {
visited = append(visited, name)
return nil
})
assertNoError(t, err)
sort.Strings(visited)
want := []string{
"service-a/base/config/300-deployment.yaml",
"service-a/base/config/310-service.yaml",
"service-a/base/config/deployment.txt",
"service-a/base/config/kustomization.yaml",
"service-a/base/kustomization.yaml",
"service-a/copy/copied.txt",
"service-a/overlays/kustomization.yaml",
"service-a/overlays/staging-deployment.yaml",
"service-a/overlays/staging-service.yaml",
}
if diff := cmp.Diff(want, visited); diff != "" {
t.Fatalf("failed to read file: %s", diff)
}
}
func TestCopyWithMissingSource(t *testing.T) {
r, cleanup := cloneTestRepository(t)
defer cleanup()
tempDir, cleanup := makeTempDir(t)
defer cleanup()
err := r.CopyFile(path.Join(tempDir, "unknown.txt"), "service-a/copy/copied.txt")
test.AssertErrorMatch(t, "failed to get permissions for existing file.*stat.*no such file or directory", err)
}
func TestStageFiles(t *testing.T) {
r, cleanup := cloneTestRepository(t)
defer cleanup()
err := r.WriteFile(strings.NewReader("this is some text"), "services/service-a/new-file.txt")
assertNoError(t, err)
err = r.StageFiles("services/service-a/new-file.txt")
assertNoError(t, err)
out := assertExecGit(t, r, r.repoPath("services/service-a"), "status", "--porcelain")
want := "A services/service-a/new-file.txt\n"
if diff := cmp.Diff(want, string(out)); diff != "" {
t.Fatalf("file status not modified: %s", diff)
}
}
// The output of the git log -n looks like this:
// commit c88ebbcdef14604aed32f20166582be2762348fd (HEAD -> master)
// Author: Git User <user@example.com>
// Date: Wed Mar 18 11:48:20 2020 +0000
//
// testing.
func TestCommit(t *testing.T) {
r, cleanup := cloneTestRepository(t)
defer cleanup()
err := r.WriteFile(strings.NewReader("this is some text"), "services/service-a/new-file.txt")
assertNoError(t, err)
err = r.StageFiles("services/service-a/new-file.txt")
assertNoError(t, err)
err = r.Commit("this is a test commit", &Author{Name: "Test User", Email: "testing@example.com"})
assertNoError(t, err)
out := strings.Split(string(assertExecGit(t, r, r.repoPath("services/service-a"), "log", "-n", "1")), "\n")
want := []string{"Author: Test User <testing@example.com>", " this is a test commit"}
if diff := cmp.Diff(want, out, cmpopts.IgnoreSliceElements(func(s string) bool {
return strings.HasPrefix(s, "commit") || strings.HasPrefix(s, "Date:") || s == ""
})); diff != "" {
t.Fatalf("file commit match failed: %s", diff)
}
}
func TestExecGit(t *testing.T) {
r, cleanup := cloneTestRepository(t)
r.debug = true
captured := ""
r.logger = func(f string, v ...interface{}) {
captured = fmt.Sprintf(f, v...)
}
defer cleanup()
_, err := r.execGit(r.repoPath(), nil, "unknown")
test.AssertErrorMatch(t, "exit status 1", err)
want := "DEBUG: git: 'unknown' is not a git command. See 'git --help'."
if strings.TrimSpace(captured) != want {
t.Fatalf("execGit debug log failed: got %s, want %s", captured, want)
}
}
func TestPush(t *testing.T) {
if authToken() == "" {
t.Skip("no auth token to push the branch upstream")
}
r, cleanup := cloneTestRepository(t)
defer cleanup()
err := r.CheckoutAndCreate("my-new-branch")
assertNoError(t, err)
err = r.WriteFile(strings.NewReader("this is some text"), "services/service-a/new-file.txt")
assertNoError(t, err)
err = r.StageFiles("services/service-a/new-file.txt")
assertNoError(t, err)
err = r.Commit("this is a test commit", &Author{Name: "Test User", Email: "testing@example.com"})
assertNoError(t, err)
err = r.Push("my-new-branch")
assertNoError(t, err)
}
func TestDebugEnabled(t *testing.T) {
tempDir, cleanup := makeTempDir(t)
defer cleanup()
r, err := NewRepository(testRepository, path.Join(tempDir, "path"), true)
assertNoError(t, err)
if !r.debug {
t.Fatalf("Debug not set to true")
}
}
func cloneTestRepository(t *testing.T) (*Repository, func()) {
tempDir, cleanup := makeTempDir(t)
r, err := NewRepository(authenticatedURL(t), tempDir, false)
assertNoError(t, err)
err = r.Clone()
assertNoError(t, err)
return r, cleanup
}
func authenticatedURL(t *testing.T) string {
t.Helper()
parsed, err := url.Parse(testRepository)
if err != nil {
t.Fatalf("failed to parse git repo url %v: %v", testRepository, err)
}
parsed.User = url.UserPassword("promotion", authToken())
return parsed.String()
}
func makeTempDir(t *testing.T) (string, func()) {
t.Helper()
dir, err := ioutil.TempDir(os.TempDir(), "promote")
assertNoError(t, err)
return dir, func() {
err := os.RemoveAll(dir)
assertNoError(t, err)
}
}
func assertExecGit(t *testing.T, r *Repository, gitPath string, args ...string) []byte {
t.Helper()
out, err := r.execGit(gitPath, nil, args...)
if err != nil {
t.Fatalf("assertExecGit failed: %s (%s)", err, out)
}
return out
}
func assertNoError(t *testing.T, err error) {
t.Helper()
if err != nil {
t.Fatal(err)
}
}
func authToken() string {
return os.Getenv("TEST_GITHUB_TOKEN")
}