@@ -18,7 +18,7 @@ import (
1818)
1919
2020func 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
3131func 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
7288func 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+
123182func githubRateLimit () * apitest.Mock {
124183 return apitest .NewMock ().
125184 Get ("https://api.github.com/rate_limit" ).
0 commit comments