Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@
.vscode/

# Build artifact
./main
main

# Test coverage artifact
coverage.out
14 changes: 11 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
.PHONY: install test build
.PHONY: install test build test-coverage coverage-report coverage

install:
@go mod download

build:
@go build -o main

test:
@go test -v -race ./...

build:
@go build -o main
test-coverage:
@go test -v -race -coverprofile=coverage.out -coverpkg=./... ./...

coverage-report:
@go tool cover -html=coverage.out

coverage: test-coverage coverage-report
71 changes: 65 additions & 6 deletions test/github_exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
)

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

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

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

test.Mocks(
githubRepos(),
githubRateLimit(),
githubReleases(),
githubPulls(),
githubUserRepos(),
githubUserReleases(),
githubUserPulls(),
githubOrgRepos(),
githubReleases(),
githubPulls(),
githubRateLimit(),
).
Get("/metrics").
Expect(t).
Expand Down Expand Up @@ -65,12 +71,22 @@ func TestGithubExporter(t *testing.T) {
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`)).
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`)).
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`)).
Assert(bodyContains(`github_repo_forks{archived="false",fork="false",language="Go",license="mit",private="false",repo="myRepo",user="myUser"} 10`)).
Assert(bodyContains(`github_repo_pull_request_count{repo="myRepo",user="myUser"} 3`)).
Assert(bodyContains(`github_repo_open_issues{archived="false",fork="false",language="Go",license="mit",private="false",repo="myRepo",user="myUser"} 2`)).
Assert(bodyContains(`github_repo_size_kb{archived="false",fork="false",language="Go",license="mit",private="false",repo="myRepo",user="myUser"} 946`)).
Assert(bodyContains(`github_repo_stars{archived="false",fork="false",language="Go",license="mit",private="false",repo="myRepo",user="myUser"} 120`)).
Assert(bodyContains(`github_repo_watchers{archived="false",fork="false",language="Go",license="mit",private="false",repo="myRepo",user="myUser"} 5`)).
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`)).
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`)).
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`)).
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`)).
Status(http.StatusOK).
End()
}

func TestGithubExporterHttpErrorHandling(t *testing.T) {
test, collector := apiTest(withConfig("myOrg/myRepo"))
test, collector := apiTest(withConfig())
defer prometheus.Unregister(&collector)

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

func withConfig(repos string) config.Config {
_ = os.Setenv("REPOS", repos)
func withConfig() config.Config {
_ = os.Setenv("REPOS", "myOrg/myRepo")
_ = os.Setenv("ORGS", "myOrg")
_ = os.Setenv("USERS", "myUser")

_ = os.Setenv("GITHUB_TOKEN", "12345")
cfg, err := config.Init()
if err != nil {
Expand All @@ -120,6 +139,46 @@ func githubRepos() *apitest.Mock {
End()
}

func githubUserRepos() *apitest.Mock {
return apitest.NewMock().
Get("https://api.github.com/users/myUser/repos").
RespondWith().
Times(1).
Body(readFile("testdata/user_repos_response.json")).
Status(200).
End()
}

func githubUserReleases() *apitest.Mock {
return apitest.NewMock().
Get("https://api.github.com/repos/myUser/myRepo/releases").
RespondWith().
Times(1).
Body(readFile("testdata/user_releases_response.json")).
Status(200).
End()
}

func githubUserPulls() *apitest.Mock {
return apitest.NewMock().
Get("https://api.github.com/repos/myUser/myRepo/pulls").
RespondWith().
Times(1).
Body(readFile("testdata/user_pulls_response.json")).
Status(http.StatusOK).
End()
}

func githubOrgRepos() *apitest.Mock {
return apitest.NewMock().
Get("https://api.github.com/orgs/myOrg/repos").
RespondWith().
Times(1).
Body(readFile("testdata/org_repos_response.json")).
Status(200).
End()
}

func githubRateLimit() *apitest.Mock {
return apitest.NewMock().
Get("https://api.github.com/rate_limit").
Expand Down
104 changes: 104 additions & 0 deletions test/testdata/org_repos_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
[
{
"id": 163222413,
"node_id": "MDEwOlJlcG9zaXRvcnkxNjMyMjI0MTM=",
"name": "myRepo",
"full_name": "myOrg/myRepo",
"private": false,
"owner": {
"login": "myOrg",
"id": 1219157,
"node_id": "MDQ6VXNlcjEyMTkxNTc=",
"avatar_url": "https://avatars1.githubusercontent.com/u/1219157?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/myOrg",
"html_url": "https://github.com/myOrg",
"followers_url": "https://api.github.com/users/myOrg/followers",
"following_url": "https://api.github.com/users/myOrg/following{/other_user}",
"gists_url": "https://api.github.com/users/myOrg/gists{/gist_id}",
"starred_url": "https://api.github.com/users/myOrg/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/myOrg/subscriptions",
"organizations_url": "https://api.github.com/users/myOrg/orgs",
"repos_url": "https://api.github.com/users/myOrg/repos",
"events_url": "https://api.github.com/users/myOrg/events{/privacy}",
"received_events_url": "https://api.github.com/users/myOrg/received_events",
"type": "User",
"site_admin": false
},
"html_url": "https://github.com/myOrg/myRepo",
"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.",
"fork": false,
"url": "https://api.github.com/repos/myOrg/myRepo",
"forks_url": "https://api.github.com/repos/myOrg/myRepo/forks",
"keys_url": "https://api.github.com/repos/myOrg/myRepo/keys{/key_id}",
"collaborators_url": "https://api.github.com/repos/myOrg/myRepo/collaborators{/collaborator}",
"teams_url": "https://api.github.com/repos/myOrg/myRepo/teams",
"hooks_url": "https://api.github.com/repos/myOrg/myRepo/hooks",
"issue_events_url": "https://api.github.com/repos/myOrg/myRepo/issues/events{/number}",
"events_url": "https://api.github.com/repos/myOrg/myRepo/events",
"assignees_url": "https://api.github.com/repos/myOrg/myRepo/assignees{/user}",
"branches_url": "https://api.github.com/repos/myOrg/myRepo/branches{/branch}",
"tags_url": "https://api.github.com/repos/myOrg/myRepo/tags",
"blobs_url": "https://api.github.com/repos/myOrg/myRepo/git/blobs{/sha}",
"git_tags_url": "https://api.github.com/repos/myOrg/myRepo/git/tags{/sha}",
"git_refs_url": "https://api.github.com/repos/myOrg/myRepo/git/refs{/sha}",
"trees_url": "https://api.github.com/repos/myOrg/myRepo/git/trees{/sha}",
"statuses_url": "https://api.github.com/repos/myOrg/myRepo/statuses/{sha}",
"languages_url": "https://api.github.com/repos/myOrg/myRepo/languages",
"stargazers_url": "https://api.github.com/repos/myOrg/myRepo/stargazers",
"contributors_url": "https://api.github.com/repos/myOrg/myRepo/contributors",
"subscribers_url": "https://api.github.com/repos/myOrg/myRepo/subscribers",
"subscription_url": "https://api.github.com/repos/myOrg/myRepo/subscription",
"commits_url": "https://api.github.com/repos/myOrg/myRepo/commits{/sha}",
"git_commits_url": "https://api.github.com/repos/myOrg/myRepo/git/commits{/sha}",
"comments_url": "https://api.github.com/repos/myOrg/myRepo/comments{/number}",
"issue_comment_url": "https://api.github.com/repos/myOrg/myRepo/issues/comments{/number}",
"contents_url": "https://api.github.com/repos/myOrg/myRepo/contents/{+path}",
"compare_url": "https://api.github.com/repos/myOrg/myRepo/compare/{base}...{head}",
"merges_url": "https://api.github.com/repos/myOrg/myRepo/merges",
"archive_url": "https://api.github.com/repos/myOrg/myRepo/{archive_format}{/ref}",
"downloads_url": "https://api.github.com/repos/myOrg/myRepo/downloads",
"issues_url": "https://api.github.com/repos/myOrg/myRepo/issues{/number}",
"pulls_url": "https://api.github.com/repos/myOrg/myRepo/pulls{/number}",
"milestones_url": "https://api.github.com/repos/myOrg/myRepo/milestones{/number}",
"notifications_url": "https://api.github.com/repos/myOrg/myRepo/notifications{?since,all,participating}",
"labels_url": "https://api.github.com/repos/myOrg/myRepo/labels{/name}",
"releases_url": "https://api.github.com/repos/myOrg/myRepo/releases{/id}",
"deployments_url": "https://api.github.com/repos/myOrg/myRepo/deployments",
"created_at": "2018-12-26T22:27:19Z",
"updated_at": "2019-08-22T20:25:14Z",
"pushed_at": "2019-08-22T20:25:16Z",
"git_url": "git://github.com/myOrg/myRepo.git",
"ssh_url": "git@github.com:myOrg/myRepo.git",
"clone_url": "https://github.com/myOrg/myRepo.git",
"svn_url": "https://github.com/myOrg/myRepo",
"homepage": "https://myRepo.dev",
"size": 946,
"stargazers_count": 120,
"watchers_count": 120,
"language": "Go",
"has_issues": true,
"has_projects": true,
"has_downloads": true,
"has_wiki": false,
"has_pages": false,
"forks_count": 10,
"mirror_url": null,
"archived": false,
"disabled": false,
"open_issues_count": 5,
"license": {
"key": "mit",
"name": "MIT License",
"spdx_id": "MIT",
"url": "https://api.github.com/licenses/mit",
"node_id": "MDc6TGljZW5zZTEz"
},
"forks": 10,
"open_issues": 5,
"watchers": 120,
"default_branch": "master",
"network_count": 10,
"subscribers_count": 5
}
]
Loading