Skip to content

Commit 8d04712

Browse files
author
Tom Hieß
committed
added test infrastructure
1 parent 2525050 commit 8d04712

6 files changed

Lines changed: 279 additions & 26 deletions

File tree

main.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package main
22

33
import (
44
"awesome-ci/src/service"
5-
"awesome-ci/src/tools"
65
"flag"
76
"fmt"
87
"log"
@@ -118,8 +117,6 @@ func printNoValidCommand(usage func()) {
118117

119118
func main() {
120119

121-
tools.DoGit(`.`)
122-
123120
flag.Usage = func() {
124121
fmt.Println("awesome-ci makes your CI easy.")
125122
fmt.Println(" Find more information and examples at: https://github.com/fullstack-devops/awesome-ci")

src/acigithub/github-client.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ var (
1919
githubToken, isgithubToken = os.LookupEnv("GITHUB_TOKEN")
2020
)
2121

22+
func SetGithubRepo(repo string) {
23+
githubRepository = repo
24+
}
25+
2226
// NewGitHubClient Creates a new GitHub Client
2327
// Needs the Environment Variables: GITHUB_TOKEN
2428
// Needs the optional Environment Variables: GITHUB_ENTERPRISE_SERVER_URL

src/acigithub/release.go

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"time"
1111

1212
"github.com/go-git/go-git/v5"
13-
"github.com/go-git/go-git/v5/plumbing"
1413
"github.com/go-git/go-git/v5/plumbing/object"
1514
"github.com/google/go-github/v39/github"
1615
)
@@ -185,7 +184,7 @@ func GetLatestReleaseVersion(owner string, repo string) (latestRelease *github.R
185184
listOptions.Page = response.NextPage
186185
}
187186

188-
return findLatestRelease(`/Users/tgr/workspace/daimler/awesome-ci`, releaseMap)
187+
return findLatestRelease(`.`, releaseMap)
189188
}
190189

191190
func findLatestRelease(directory string, githubReleaseMap map[string]*github.RepositoryRelease) (latestRelease *github.RepositoryRelease, err error) {
@@ -195,7 +194,7 @@ func findLatestRelease(directory string, githubReleaseMap map[string]*github.Rep
195194
return nil, err
196195
}
197196

198-
tagMap, err := getGitTagMap(gitRepo)
197+
commitToTagMap, _, err := tools.GetGitTagMaps(gitRepo)
199198

200199
if err != nil {
201200
return nil, err
@@ -211,9 +210,10 @@ func findLatestRelease(directory string, githubReleaseMap map[string]*github.Rep
211210
var commit *object.Commit
212211
for commit, err = iter.Next(); commit != nil && err == nil; commit, err = iter.Next() {
213212
fmt.Printf("Lookup %s ", commit.Hash.String())
214-
if tagName, found := tagMap[commit.Hash.String()]; found {
213+
if tagName, found := commitToTagMap[commit.Hash.String()]; found {
215214
fmt.Println(tagName)
216-
if latestRelease, found := githubReleaseMap[tagName]; found {
215+
//TODO it's possible to have more then one Tag on a Commit
216+
if latestRelease, found := githubReleaseMap[tagName[0]]; found {
217217
return latestRelease, nil
218218
}
219219
}
@@ -222,21 +222,3 @@ func findLatestRelease(directory string, githubReleaseMap map[string]*github.Rep
222222

223223
return nil, errors.New("could not find latest release")
224224
}
225-
226-
func getGitTagMap(gitRepo *git.Repository) (tagMap map[string]string, err error) {
227-
tagMap = make(map[string]string)
228-
229-
tags, err := gitRepo.Tags()
230-
231-
if err != nil {
232-
return nil, err
233-
}
234-
235-
tags.ForEach(func(r *plumbing.Reference) error {
236-
tagMap[r.Hash().String()] = r.Name().Short()
237-
fmt.Printf("Tag %s %s\n", r.Hash().String(), r.Name().Short())
238-
return nil
239-
})
240-
241-
return tagMap, nil
242-
}

src/service/helper_test.go

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
package service
2+
3+
import (
4+
"awesome-ci/src/acigithub"
5+
"awesome-ci/src/tools"
6+
"context"
7+
"fmt"
8+
"io/ioutil"
9+
"os"
10+
"strings"
11+
"testing"
12+
"time"
13+
14+
"github.com/go-git/go-git/v5"
15+
"github.com/go-git/go-git/v5/plumbing"
16+
"github.com/go-git/go-git/v5/plumbing/transport/http"
17+
"github.com/google/go-github/v39/github"
18+
)
19+
20+
type TestEnvironment struct {
21+
ctx context.Context
22+
testOwner, testRepo string
23+
repoPath string
24+
githubRepo *github.Repository
25+
localGitRep *git.Repository
26+
}
27+
28+
func getTestEnvironment(preparedReleases *[]string, t *testing.T) (testEnv *TestEnvironment, cleanup func()) {
29+
30+
testRepo, ok := os.LookupEnv("ACI_TEST_REPO")
31+
32+
if !ok {
33+
t.Errorf("env var ACI_TEST_REPO not set")
34+
t.FailNow()
35+
}
36+
37+
if !ok {
38+
t.Errorf("env var ACI_TEST_FORK not set")
39+
t.FailNow()
40+
}
41+
42+
testEnv = &TestEnvironment{
43+
ctx: context.Background(),
44+
testOwner: strings.Split(testRepo, "/")[0],
45+
testRepo: strings.Split(testRepo, "/")[1],
46+
}
47+
48+
if tmpDir, err := ioutil.TempDir("/tmp", "aci"); err == nil {
49+
testEnv.repoPath = tmpDir
50+
} else {
51+
t.Errorf("Could not create tmp dir: %s", err)
52+
t.FailNow()
53+
}
54+
55+
if _, err := acigithub.NewGitHubClient(); err != nil {
56+
t.Errorf("Could not create GitHub client: %s", err)
57+
t.FailNow()
58+
}
59+
60+
repo, _, err := acigithub.GithubClient.Repositories.Get(testEnv.ctx, testEnv.testOwner, testEnv.testRepo)
61+
62+
if checkError(err, t) {
63+
t.FailNow()
64+
}
65+
66+
testEnv.githubRepo = repo
67+
68+
gitRepo, err := git.PlainClone(testEnv.repoPath, false, &git.CloneOptions{
69+
URL: *repo.CloneURL,
70+
Auth: &http.BasicAuth{
71+
Username: "notneeded", // yes, this can be anything except an empty string
72+
Password: os.Getenv("GITHUB_TOKEN"),
73+
},
74+
Progress: os.Stdout,
75+
})
76+
77+
if err != nil {
78+
t.Errorf("Could not clone Repository: %s", err)
79+
t.FailNow()
80+
}
81+
82+
testEnv.localGitRep = gitRepo
83+
84+
os.Chdir(testEnv.repoPath)
85+
86+
cleanupReleases, err := prepareReleases(preparedReleases, testEnv, t)
87+
88+
checkError(err, t)
89+
90+
return testEnv, func() {
91+
cleanupReleases()
92+
os.RemoveAll(testEnv.repoPath)
93+
}
94+
}
95+
96+
func prepareReleases(tagNames *[]string, testEnv *TestEnvironment, t *testing.T) (cleanup func(), err error) {
97+
_, tagToCommitMap, err := tools.GetGitTagMaps(testEnv.localGitRep)
98+
99+
if err != nil {
100+
return func() {}, err
101+
}
102+
103+
for _, tagName := range *tagNames {
104+
105+
commit := tagToCommitMap[tagName]
106+
_, _, err := acigithub.GithubClient.Repositories.CreateRelease(testEnv.ctx, testEnv.testOwner, testEnv.testRepo, &github.RepositoryRelease{
107+
TagName: &tagName,
108+
TargetCommitish: &commit,
109+
Name: &tagName,
110+
})
111+
112+
waitingForRelease(tagName, testEnv, t)
113+
114+
if err != nil {
115+
return func() {}, err
116+
}
117+
}
118+
119+
return func() {
120+
deleteReleases(tagNames, testEnv, t)
121+
}, nil
122+
}
123+
124+
func waitingForRelease(tagName string, testEnv *TestEnvironment, t *testing.T) {
125+
126+
for i := 1; i <= 10; i++ { //waiting for release becoming available
127+
_, _, err := acigithub.GithubClient.Repositories.GetReleaseByTag(testEnv.ctx, testEnv.testOwner, testEnv.testRepo, tagName)
128+
if err != nil {
129+
fmt.Printf("Waiting %sms for Release to become available", time.Duration(i*100)*time.Millisecond)
130+
131+
time.Sleep(time.Duration(i*100) * time.Millisecond)
132+
}
133+
}
134+
}
135+
136+
func deleteReleases(tagNames *[]string, testConfig *TestEnvironment, t *testing.T) {
137+
138+
for _, tagName := range *tagNames {
139+
140+
release, _, err := acigithub.GithubClient.Repositories.GetReleaseByTag(testConfig.ctx, testConfig.testOwner, testConfig.testRepo, tagName)
141+
142+
if !checkError(err, t) {
143+
_, err = acigithub.GithubClient.Repositories.DeleteRelease(testConfig.ctx, testConfig.testOwner, testConfig.testRepo, *release.ID)
144+
checkError(err, t)
145+
}
146+
}
147+
148+
}
149+
150+
func resetHeadToTag(tagName string, testEnv *TestEnvironment, t *testing.T) bool {
151+
_, tagToCommitMap, err := tools.GetGitTagMaps(testEnv.localGitRep)
152+
if checkError(err, t) {
153+
return false
154+
}
155+
worktree, err := testEnv.localGitRep.Worktree()
156+
157+
if checkError(err, t) {
158+
return false
159+
}
160+
161+
worktree.Reset(&git.ResetOptions{
162+
Commit: plumbing.NewHash(tagToCommitMap["1.1.0"]),
163+
Mode: git.HardReset,
164+
})
165+
return true
166+
}
167+
168+
func checkError(err error, t *testing.T) bool {
169+
170+
if err != nil {
171+
t.Error(err)
172+
return true
173+
}
174+
175+
return false
176+
}

src/service/release_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package service
2+
3+
import (
4+
"awesome-ci/src/acigithub"
5+
"testing"
6+
)
7+
8+
func TestCreateRelease_1_1_0(t *testing.T) {
9+
10+
preparedReleases := &[]string{"1.0.1"}
11+
12+
testEnv, cleanup := getTestEnvironment(preparedReleases, t)
13+
14+
defer cleanup()
15+
defer deleteReleases(&[]string{"1.1.0"}, testEnv, t)
16+
17+
if !resetHeadToTag("1.1.0", testEnv, t) {
18+
t.FailNow()
19+
}
20+
21+
latestRelease, err := acigithub.GetLatestReleaseVersion(testEnv.testOwner, testEnv.testRepo)
22+
23+
if checkError(err, t) {
24+
t.FailNow()
25+
}
26+
27+
if *latestRelease.TagName != "1.0.1" {
28+
t.Errorf("Latest release should be 1.0.1 ... found %s", *latestRelease.TagName)
29+
t.FailNow()
30+
}
31+
32+
ReleasePublish(&ReleasePublishSet{})
33+
34+
waitingForRelease("1.1.0", testEnv, t)
35+
36+
latestRelease, err = acigithub.GetLatestReleaseVersion(testEnv.testOwner, testEnv.testRepo)
37+
38+
if checkError(err, t) {
39+
t.FailNow()
40+
}
41+
42+
if *latestRelease.TagName != "1.1.0" {
43+
t.Errorf("Latest release should be 1.1.0 ... found %s", *latestRelease.TagName)
44+
}
45+
}
46+
47+
func TestFirstRelease(t *testing.T) {
48+
preparedReleases := &[]string{}
49+
testEnv, cleanup := getTestEnvironment(preparedReleases, t)
50+
51+
defer cleanup()
52+
53+
if !resetHeadToTag("1.0.1", testEnv, t) {
54+
t.FailNow()
55+
}
56+
57+
latestRelease, err := acigithub.GetLatestReleaseVersion(testEnv.testOwner, testEnv.testRepo)
58+
59+
if latestRelease != nil || err == nil {
60+
t.Errorf("There should be no Relase")
61+
}
62+
63+
}

src/tools/tools.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import (
44
"fmt"
55
"os/exec"
66
"strings"
7+
8+
"github.com/go-git/go-git/v5"
9+
"github.com/go-git/go-git/v5/plumbing"
710
)
811

912
func GetDefaultBranch() string {
@@ -31,3 +34,31 @@ func runcmd(cmd string, shell bool) string {
3134
}
3235
return string(out)
3336
}
37+
38+
func GetGitTagMaps(gitRepo *git.Repository) (commitToTagMap map[string][]string, tagToCommitMap map[string]string, err error) {
39+
tagToCommitMap = make(map[string]string)
40+
commitToTagMap = make(map[string][]string)
41+
42+
tags, err := gitRepo.Tags()
43+
44+
if err != nil {
45+
return nil, nil, err
46+
}
47+
48+
tags.ForEach(func(r *plumbing.Reference) error {
49+
50+
tagList, exists := commitToTagMap[r.Hash().String()]
51+
52+
if !exists {
53+
tagList = make([]string, 0)
54+
commitToTagMap[r.Hash().String()] = tagList
55+
}
56+
57+
commitToTagMap[r.Hash().String()] = append(tagList, r.Name().Short())
58+
tagToCommitMap[r.Name().Short()] = r.Hash().String()
59+
fmt.Printf("Tag %s %s\n", r.Hash().String(), r.Name().Short())
60+
return nil
61+
})
62+
63+
return commitToTagMap, tagToCommitMap, nil
64+
}

0 commit comments

Comments
 (0)