Skip to content

Commit 8690a4c

Browse files
authored
Tweak tests for standard repo structure (#14)
1 parent 7bf45be commit 8690a4c

File tree

2 files changed

+58
-23
lines changed

2 files changed

+58
-23
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ This is a pre-alpha PoC for promoting versions of files between environments, re
66

77
## Building
88

9+
You need Go version 1.14 to build this project.
10+
911
```shell
1012
$ go build ./cmd/services
1113
```

pkg/git/repository_test.go

Lines changed: 56 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@ import (
1414
"github.com/rhd-gitops-example/services/test"
1515
)
1616

17-
const testRepository = "https://github.com/bigkevmcd/env-staging.git"
17+
const testRepository = "https://github.com/mnuttall/staging.git"
1818

1919
func TestRepoName(t *testing.T) {
2020
nameTests := []struct {
2121
url string
2222
wantName string
2323
wantErr string
2424
}{
25-
{testRepository, "env-staging", ""},
26-
{"https://github.com/bigkevmcd", "", "could not identify repo: https://github.com/bigkevmcd"},
25+
{testRepository, "staging", ""},
26+
{"https://github.com/mnuttall", "", "could not identify repo: https://github.com/mnuttall"},
2727
}
2828

2929
for _, tt := range nameTests {
@@ -48,7 +48,7 @@ func TestCloneCreatesDirectory(t *testing.T) {
4848
err = r.Clone()
4949
assertNoError(t, err)
5050

51-
contents, err := ioutil.ReadFile(path.Join(tempDir, "path", "env-staging/service-a/deployment.txt"))
51+
contents, err := ioutil.ReadFile(path.Join(tempDir, "path", "staging/services/service-a/base/config/deployment.txt"))
5252
assertNoError(t, err)
5353

5454
want := "This is the staging version of this file.\n"
@@ -61,14 +61,15 @@ func TestClone(t *testing.T) {
6161
r, cleanup := cloneTestRepository(t)
6262
defer cleanup()
6363

64-
contents, err := ioutil.ReadFile(path.Join(r.LocalPath, "env-staging/service-a/deployment.txt"))
64+
contents, err := ioutil.ReadFile(path.Join(r.LocalPath, "staging/services/service-a/base/config/deployment.txt"))
6565
assertNoError(t, err)
6666
want := "This is the staging version of this file.\n"
6767
if diff := cmp.Diff(want, string(contents)); diff != "" {
6868
t.Fatalf("failed to read file: %s", diff)
6969
}
7070
}
7171

72+
/* TestCheckout relates to earlier behaviour that dealt with branches within repos. We can revisit this.
7273
func TestCheckout(t *testing.T) {
7374
r, cleanup := cloneTestRepository(t)
7475
defer cleanup()
@@ -84,18 +85,29 @@ func TestCheckout(t *testing.T) {
8485
t.Fatalf("failed to read file: %s", diff)
8586
}
8687
}
88+
*/
8789

8890
func TestWalk(t *testing.T) {
8991
r, cleanup := cloneTestRepository(t)
9092
defer cleanup()
9193

9294
visited := []string{}
93-
err := r.Walk("service-a", func(prefix, name string) error {
95+
err := r.Walk("services/service-a", func(prefix, name string) error {
9496
visited = append(visited, name)
9597
return nil
9698
})
99+
97100
assertNoError(t, err)
98-
want := []string{"service-a/deployment.txt", "service-a/files/myfile.txt"}
101+
want := []string{
102+
"service-a/base/config/300-deployment.yaml",
103+
"service-a/base/config/310-service.yaml",
104+
"service-a/base/config/deployment.txt",
105+
"service-a/base/config/kustomization.yaml",
106+
"service-a/base/kustomization.yaml",
107+
"service-a/overlays/kustomization.yaml",
108+
"service-a/overlays/staging-deployment.yaml",
109+
"service-a/overlays/staging-service.yaml",
110+
}
99111
if diff := cmp.Diff(want, visited); diff != "" {
100112
t.Fatalf("failed to read file: %s", diff)
101113
}
@@ -105,17 +117,27 @@ func TestWriteFile(t *testing.T) {
105117
r, cleanup := cloneTestRepository(t)
106118
defer cleanup()
107119

108-
err := r.WriteFile(strings.NewReader("this is some text"), "service-a/new-file.txt")
120+
err := r.WriteFile(strings.NewReader("this is some text"), "services/service-a/base/config/new-file.txt")
109121
assertNoError(t, err)
110122

111123
visited := []string{}
112-
err = r.Walk("service-a", func(prefix, name string) error {
124+
err = r.Walk("services/service-a", func(prefix, name string) error {
113125
visited = append(visited, name)
114126
return nil
115127
})
116128
assertNoError(t, err)
117129
sort.Strings(visited)
118-
want := []string{"service-a/deployment.txt", "service-a/files/myfile.txt", "service-a/new-file.txt"}
130+
want := []string{
131+
"service-a/base/config/300-deployment.yaml",
132+
"service-a/base/config/310-service.yaml",
133+
"service-a/base/config/deployment.txt",
134+
"service-a/base/config/kustomization.yaml",
135+
"service-a/base/config/new-file.txt",
136+
"service-a/base/kustomization.yaml",
137+
"service-a/overlays/kustomization.yaml",
138+
"service-a/overlays/staging-deployment.yaml",
139+
"service-a/overlays/staging-service.yaml",
140+
}
119141
if diff := cmp.Diff(want, visited); diff != "" {
120142
t.Fatalf("failed to read file: %s", diff)
121143
}
@@ -133,17 +155,28 @@ func TestCopyFile(t *testing.T) {
133155
err = tmpfile.Close()
134156
assertNoError(t, err)
135157

136-
err = r.CopyFile(tmpfile.Name(), "service-a/copy/copied.txt")
158+
err = r.CopyFile(tmpfile.Name(), "services/service-a/copy/copied.txt")
137159
assertNoError(t, err)
138160

139161
visited := []string{}
140-
err = r.Walk("service-a", func(prefix, name string) error {
162+
err = r.Walk("services/service-a", func(prefix, name string) error {
141163
visited = append(visited, name)
142164
return nil
143165
})
166+
144167
assertNoError(t, err)
145168
sort.Strings(visited)
146-
want := []string{"service-a/copy/copied.txt", "service-a/deployment.txt", "service-a/files/myfile.txt"}
169+
want := []string{
170+
"service-a/base/config/300-deployment.yaml",
171+
"service-a/base/config/310-service.yaml",
172+
"service-a/base/config/deployment.txt",
173+
"service-a/base/config/kustomization.yaml",
174+
"service-a/base/kustomization.yaml",
175+
"service-a/copy/copied.txt",
176+
"service-a/overlays/kustomization.yaml",
177+
"service-a/overlays/staging-deployment.yaml",
178+
"service-a/overlays/staging-service.yaml",
179+
}
147180
if diff := cmp.Diff(want, visited); diff != "" {
148181
t.Fatalf("failed to read file: %s", diff)
149182
}
@@ -162,14 +195,14 @@ func TestCopyWithMissingSource(t *testing.T) {
162195
func TestStageFiles(t *testing.T) {
163196
r, cleanup := cloneTestRepository(t)
164197
defer cleanup()
165-
err := r.WriteFile(strings.NewReader("this is some text"), "service-a/new-file.txt")
198+
err := r.WriteFile(strings.NewReader("this is some text"), "services/service-a/new-file.txt")
166199
assertNoError(t, err)
167200

168-
err = r.StageFiles("service-a/new-file.txt")
201+
err = r.StageFiles("services/service-a/new-file.txt")
169202
assertNoError(t, err)
170203

171-
out := assertExecGit(t, r.repoPath("service-a"), "status", "--porcelain")
172-
want := "A service-a/new-file.txt\n"
204+
out := assertExecGit(t, r.repoPath("services/service-a"), "status", "--porcelain")
205+
want := "A services/service-a/new-file.txt\n"
173206
if diff := cmp.Diff(want, string(out)); diff != "" {
174207
t.Fatalf("file status not modified: %s", diff)
175208
}
@@ -184,15 +217,15 @@ func TestStageFiles(t *testing.T) {
184217
func TestCommit(t *testing.T) {
185218
r, cleanup := cloneTestRepository(t)
186219
defer cleanup()
187-
err := r.WriteFile(strings.NewReader("this is some text"), "service-a/new-file.txt")
220+
err := r.WriteFile(strings.NewReader("this is some text"), "services/service-a/new-file.txt")
188221
assertNoError(t, err)
189-
err = r.StageFiles("service-a/new-file.txt")
222+
err = r.StageFiles("services/service-a/new-file.txt")
190223
assertNoError(t, err)
191224

192225
err = r.Commit("this is a test commit", &Author{Name: "Test User", Email: "[email protected]"})
193226
assertNoError(t, err)
194227

195-
out := strings.Split(string(assertExecGit(t, r.repoPath("service-a"), "log", "-n", "1")), "\n")
228+
out := strings.Split(string(assertExecGit(t, r.repoPath("services/service-a"), "log", "-n", "1")), "\n")
196229
want := []string{"Author: Test User <[email protected]>", " this is a test commit"}
197230
if diff := cmp.Diff(want, out, cmpopts.IgnoreSliceElements(func(s string) bool {
198231
return strings.HasPrefix(s, "commit") || strings.HasPrefix(s, "Date:") || s == ""
@@ -209,9 +242,9 @@ func TestPush(t *testing.T) {
209242
defer cleanup()
210243
err := r.CheckoutAndCreate("my-new-branch")
211244
assertNoError(t, err)
212-
err = r.WriteFile(strings.NewReader("this is some text"), "service-a/new-file.txt")
245+
err = r.WriteFile(strings.NewReader("this is some text"), "services/service-a/new-file.txt")
213246
assertNoError(t, err)
214-
err = r.StageFiles("service-a/new-file.txt")
247+
err = r.StageFiles("services/service-a/new-file.txt")
215248
assertNoError(t, err)
216249
err = r.Commit("this is a test commit", &Author{Name: "Test User", Email: "[email protected]"})
217250
assertNoError(t, err)
@@ -233,7 +266,7 @@ func authenticatedURL(t *testing.T) string {
233266
t.Helper()
234267
parsed, err := url.Parse(testRepository)
235268
if err != nil {
236-
t.Fatalf("failed to parse git repo url %v: %w", testRepository, err)
269+
t.Fatalf("failed to parse git repo url %v: %v", testRepository, err)
237270
}
238271
parsed.User = url.UserPassword("promotion", authToken())
239272
return parsed.String()

0 commit comments

Comments
 (0)