Skip to content

Commit f6b3669

Browse files
improve test coverage (#135)
* add test coverage makefile helpers * add tests for orgs and users
1 parent 13de96d commit f6b3669

7 files changed

Lines changed: 1492 additions & 10 deletions

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@
44
.vscode/
55

66
# Build artifact
7-
./main
7+
main
8+
9+
# Test coverage artifact
10+
coverage.out

Makefile

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
1-
.PHONY: install test build
1+
.PHONY: install test build test-coverage coverage-report coverage
22

33
install:
44
@go mod download
55

6+
build:
7+
@go build -o main
8+
69
test:
710
@go test -v -race ./...
811

9-
build:
10-
@go build -o main
12+
test-coverage:
13+
@go test -v -race -coverprofile=coverage.out -coverpkg=./... ./...
14+
15+
coverage-report:
16+
@go tool cover -html=coverage.out
17+
18+
coverage: test-coverage coverage-report

test/github_exporter_test.go

Lines changed: 65 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
)
1919

2020
func TestHomepage(t *testing.T) {
21-
test, collector := apiTest(withConfig("a/b"))
21+
test, collector := apiTest(withConfig())
2222
defer prometheus.Unregister(&collector)
2323

2424
test.Get("/").
@@ -29,14 +29,20 @@ func TestHomepage(t *testing.T) {
2929
}
3030

3131
func TestGithubExporter(t *testing.T) {
32-
test, collector := apiTest(withConfig("myOrg/myRepo"))
32+
test, collector := apiTest(withConfig())
3333
defer prometheus.Unregister(&collector)
3434

3535
test.Mocks(
3636
githubRepos(),
37-
githubRateLimit(),
3837
githubReleases(),
3938
githubPulls(),
39+
githubUserRepos(),
40+
githubUserReleases(),
41+
githubUserPulls(),
42+
githubOrgRepos(),
43+
githubReleases(),
44+
githubPulls(),
45+
githubRateLimit(),
4046
).
4147
Get("/metrics").
4248
Expect(t).
@@ -65,12 +71,22 @@ func TestGithubExporter(t *testing.T) {
6571
Assert(bodyContains(`github_repo_release_downloads{created_at="2019-02-28T08:25:53Z",name="myRepo_1.3.0_windows_amd64.tar.gz",release="1.3.0",repo="myRepo",tag="1.3.0",user="myOrg"} 21`)).
6672
Assert(bodyContains(`github_repo_release_downloads{created_at="2019-05-02T15:22:16Z",name="myRepo_2.0.0_checksums.txt",release="2.0.0",repo="myRepo",tag="2.0.0",user="myOrg"} 14564`)).
6773
Assert(bodyContains(`github_repo_release_downloads{created_at="2019-05-02T15:22:16Z",name="myRepo_2.0.0_windows_amd64.tar.gz",release="2.0.0",repo="myRepo",tag="2.0.0",user="myOrg"} 55`)).
74+
Assert(bodyContains(`github_repo_forks{archived="false",fork="false",language="Go",license="mit",private="false",repo="myRepo",user="myUser"} 10`)).
75+
Assert(bodyContains(`github_repo_pull_request_count{repo="myRepo",user="myUser"} 3`)).
76+
Assert(bodyContains(`github_repo_open_issues{archived="false",fork="false",language="Go",license="mit",private="false",repo="myRepo",user="myUser"} 2`)).
77+
Assert(bodyContains(`github_repo_size_kb{archived="false",fork="false",language="Go",license="mit",private="false",repo="myRepo",user="myUser"} 946`)).
78+
Assert(bodyContains(`github_repo_stars{archived="false",fork="false",language="Go",license="mit",private="false",repo="myRepo",user="myUser"} 120`)).
79+
Assert(bodyContains(`github_repo_watchers{archived="false",fork="false",language="Go",license="mit",private="false",repo="myRepo",user="myUser"} 5`)).
80+
Assert(bodyContains(`github_repo_release_downloads{created_at="2019-02-28T08:25:53Z",name="myRepo_1.3.0_checksums.txt",release="1.3.0",repo="myRepo",tag="1.3.0",user="myUser"} 7292`)).
81+
Assert(bodyContains(`github_repo_release_downloads{created_at="2019-02-28T08:25:53Z",name="myRepo_1.3.0_windows_amd64.tar.gz",release="1.3.0",repo="myRepo",tag="1.3.0",user="myUser"} 21`)).
82+
Assert(bodyContains(`github_repo_release_downloads{created_at="2019-05-02T15:22:16Z",name="myRepo_2.0.0_checksums.txt",release="2.0.0",repo="myRepo",tag="2.0.0",user="myUser"} 14564`)).
83+
Assert(bodyContains(`github_repo_release_downloads{created_at="2019-05-02T15:22:16Z",name="myRepo_2.0.0_windows_amd64.tar.gz",release="2.0.0",repo="myRepo",tag="2.0.0",user="myUser"} 55`)).
6884
Status(http.StatusOK).
6985
End()
7086
}
7187

7288
func TestGithubExporterHttpErrorHandling(t *testing.T) {
73-
test, collector := apiTest(withConfig("myOrg/myRepo"))
89+
test, collector := apiTest(withConfig())
7490
defer prometheus.Unregister(&collector)
7591

7692
// Test that the exporter returns when an error occurs
@@ -100,8 +116,11 @@ func apiTest(conf config.Config) (*apitest.APITest, exporter.Exporter) {
100116
Handler(server.Handler), exp
101117
}
102118

103-
func withConfig(repos string) config.Config {
104-
_ = os.Setenv("REPOS", repos)
119+
func withConfig() config.Config {
120+
_ = os.Setenv("REPOS", "myOrg/myRepo")
121+
_ = os.Setenv("ORGS", "myOrg")
122+
_ = os.Setenv("USERS", "myUser")
123+
105124
_ = os.Setenv("GITHUB_TOKEN", "12345")
106125
cfg, err := config.Init()
107126
if err != nil {
@@ -120,6 +139,46 @@ func githubRepos() *apitest.Mock {
120139
End()
121140
}
122141

142+
func githubUserRepos() *apitest.Mock {
143+
return apitest.NewMock().
144+
Get("https://api.github.com/users/myUser/repos").
145+
RespondWith().
146+
Times(1).
147+
Body(readFile("testdata/user_repos_response.json")).
148+
Status(200).
149+
End()
150+
}
151+
152+
func githubUserReleases() *apitest.Mock {
153+
return apitest.NewMock().
154+
Get("https://api.github.com/repos/myUser/myRepo/releases").
155+
RespondWith().
156+
Times(1).
157+
Body(readFile("testdata/user_releases_response.json")).
158+
Status(200).
159+
End()
160+
}
161+
162+
func githubUserPulls() *apitest.Mock {
163+
return apitest.NewMock().
164+
Get("https://api.github.com/repos/myUser/myRepo/pulls").
165+
RespondWith().
166+
Times(1).
167+
Body(readFile("testdata/user_pulls_response.json")).
168+
Status(http.StatusOK).
169+
End()
170+
}
171+
172+
func githubOrgRepos() *apitest.Mock {
173+
return apitest.NewMock().
174+
Get("https://api.github.com/orgs/myOrg/repos").
175+
RespondWith().
176+
Times(1).
177+
Body(readFile("testdata/org_repos_response.json")).
178+
Status(200).
179+
End()
180+
}
181+
123182
func githubRateLimit() *apitest.Mock {
124183
return apitest.NewMock().
125184
Get("https://api.github.com/rate_limit").
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
[
2+
{
3+
"id": 163222413,
4+
"node_id": "MDEwOlJlcG9zaXRvcnkxNjMyMjI0MTM=",
5+
"name": "myRepo",
6+
"full_name": "myOrg/myRepo",
7+
"private": false,
8+
"owner": {
9+
"login": "myOrg",
10+
"id": 1219157,
11+
"node_id": "MDQ6VXNlcjEyMTkxNTc=",
12+
"avatar_url": "https://avatars1.githubusercontent.com/u/1219157?v=4",
13+
"gravatar_id": "",
14+
"url": "https://api.github.com/users/myOrg",
15+
"html_url": "https://github.com/myOrg",
16+
"followers_url": "https://api.github.com/users/myOrg/followers",
17+
"following_url": "https://api.github.com/users/myOrg/following{/other_user}",
18+
"gists_url": "https://api.github.com/users/myOrg/gists{/gist_id}",
19+
"starred_url": "https://api.github.com/users/myOrg/starred{/owner}{/repo}",
20+
"subscriptions_url": "https://api.github.com/users/myOrg/subscriptions",
21+
"organizations_url": "https://api.github.com/users/myOrg/orgs",
22+
"repos_url": "https://api.github.com/users/myOrg/repos",
23+
"events_url": "https://api.github.com/users/myOrg/events{/privacy}",
24+
"received_events_url": "https://api.github.com/users/myOrg/received_events",
25+
"type": "User",
26+
"site_admin": false
27+
},
28+
"html_url": "https://github.com/myOrg/myRepo",
29+
"description": "A simple and extensible behavioural testing library written in golang. You can use api test to simplify REST API, HTTP handler and e2e tests.",
30+
"fork": false,
31+
"url": "https://api.github.com/repos/myOrg/myRepo",
32+
"forks_url": "https://api.github.com/repos/myOrg/myRepo/forks",
33+
"keys_url": "https://api.github.com/repos/myOrg/myRepo/keys{/key_id}",
34+
"collaborators_url": "https://api.github.com/repos/myOrg/myRepo/collaborators{/collaborator}",
35+
"teams_url": "https://api.github.com/repos/myOrg/myRepo/teams",
36+
"hooks_url": "https://api.github.com/repos/myOrg/myRepo/hooks",
37+
"issue_events_url": "https://api.github.com/repos/myOrg/myRepo/issues/events{/number}",
38+
"events_url": "https://api.github.com/repos/myOrg/myRepo/events",
39+
"assignees_url": "https://api.github.com/repos/myOrg/myRepo/assignees{/user}",
40+
"branches_url": "https://api.github.com/repos/myOrg/myRepo/branches{/branch}",
41+
"tags_url": "https://api.github.com/repos/myOrg/myRepo/tags",
42+
"blobs_url": "https://api.github.com/repos/myOrg/myRepo/git/blobs{/sha}",
43+
"git_tags_url": "https://api.github.com/repos/myOrg/myRepo/git/tags{/sha}",
44+
"git_refs_url": "https://api.github.com/repos/myOrg/myRepo/git/refs{/sha}",
45+
"trees_url": "https://api.github.com/repos/myOrg/myRepo/git/trees{/sha}",
46+
"statuses_url": "https://api.github.com/repos/myOrg/myRepo/statuses/{sha}",
47+
"languages_url": "https://api.github.com/repos/myOrg/myRepo/languages",
48+
"stargazers_url": "https://api.github.com/repos/myOrg/myRepo/stargazers",
49+
"contributors_url": "https://api.github.com/repos/myOrg/myRepo/contributors",
50+
"subscribers_url": "https://api.github.com/repos/myOrg/myRepo/subscribers",
51+
"subscription_url": "https://api.github.com/repos/myOrg/myRepo/subscription",
52+
"commits_url": "https://api.github.com/repos/myOrg/myRepo/commits{/sha}",
53+
"git_commits_url": "https://api.github.com/repos/myOrg/myRepo/git/commits{/sha}",
54+
"comments_url": "https://api.github.com/repos/myOrg/myRepo/comments{/number}",
55+
"issue_comment_url": "https://api.github.com/repos/myOrg/myRepo/issues/comments{/number}",
56+
"contents_url": "https://api.github.com/repos/myOrg/myRepo/contents/{+path}",
57+
"compare_url": "https://api.github.com/repos/myOrg/myRepo/compare/{base}...{head}",
58+
"merges_url": "https://api.github.com/repos/myOrg/myRepo/merges",
59+
"archive_url": "https://api.github.com/repos/myOrg/myRepo/{archive_format}{/ref}",
60+
"downloads_url": "https://api.github.com/repos/myOrg/myRepo/downloads",
61+
"issues_url": "https://api.github.com/repos/myOrg/myRepo/issues{/number}",
62+
"pulls_url": "https://api.github.com/repos/myOrg/myRepo/pulls{/number}",
63+
"milestones_url": "https://api.github.com/repos/myOrg/myRepo/milestones{/number}",
64+
"notifications_url": "https://api.github.com/repos/myOrg/myRepo/notifications{?since,all,participating}",
65+
"labels_url": "https://api.github.com/repos/myOrg/myRepo/labels{/name}",
66+
"releases_url": "https://api.github.com/repos/myOrg/myRepo/releases{/id}",
67+
"deployments_url": "https://api.github.com/repos/myOrg/myRepo/deployments",
68+
"created_at": "2018-12-26T22:27:19Z",
69+
"updated_at": "2019-08-22T20:25:14Z",
70+
"pushed_at": "2019-08-22T20:25:16Z",
71+
"git_url": "git://github.com/myOrg/myRepo.git",
72+
"ssh_url": "git@github.com:myOrg/myRepo.git",
73+
"clone_url": "https://github.com/myOrg/myRepo.git",
74+
"svn_url": "https://github.com/myOrg/myRepo",
75+
"homepage": "https://myRepo.dev",
76+
"size": 946,
77+
"stargazers_count": 120,
78+
"watchers_count": 120,
79+
"language": "Go",
80+
"has_issues": true,
81+
"has_projects": true,
82+
"has_downloads": true,
83+
"has_wiki": false,
84+
"has_pages": false,
85+
"forks_count": 10,
86+
"mirror_url": null,
87+
"archived": false,
88+
"disabled": false,
89+
"open_issues_count": 5,
90+
"license": {
91+
"key": "mit",
92+
"name": "MIT License",
93+
"spdx_id": "MIT",
94+
"url": "https://api.github.com/licenses/mit",
95+
"node_id": "MDc6TGljZW5zZTEz"
96+
},
97+
"forks": 10,
98+
"open_issues": 5,
99+
"watchers": 120,
100+
"default_branch": "master",
101+
"network_count": 10,
102+
"subscribers_count": 5
103+
}
104+
]

0 commit comments

Comments
 (0)