Skip to content

Commit

Permalink
tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
ajvpot authored and martinbaillie committed Jan 20, 2025
1 parent f35fcf6 commit 330d284
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 9 deletions.
2 changes: 1 addition & 1 deletion github/path_installations.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (b *backend) pathInstallationsWrite(
"took", duration.String(),
"err", err,
)
requestDuration.With(prometheus.Labels{
installationsDuration.With(prometheus.Labels{
"success": strconv.FormatBool(err == nil),
}).Observe(duration.Seconds())
}(time.Now())
Expand Down
65 changes: 57 additions & 8 deletions github/path_installations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,68 @@ package github

import (
"context"
"encoding/json"
"github.com/hashicorp/vault/sdk/logical"
"gotest.tools/assert"
"net/http"
"net/http/httptest"
"testing"
)

const installationsJSON = `
[
{
"id": 1,
"account": {
"login": "octocat",
"id": 1,
"node_id": "MDQ6VXNlcjE=",
"avatar_url": "https://github.com/images/error/octocat_happy.gif",
"gravatar_id": "",
"url": "https://api.github.com/users/octocat",
"html_url": "https://github.com/octocat",
"followers_url": "https://api.github.com/users/octocat/followers",
"following_url": "https://api.github.com/users/octocat/following{/other_user}",
"gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",
"starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/octocat/subscriptions",
"organizations_url": "https://api.github.com/users/octocat/orgs",
"repos_url": "https://api.github.com/users/octocat/repos",
"events_url": "https://api.github.com/users/octocat/events{/privacy}",
"received_events_url": "https://api.github.com/users/octocat/received_events",
"type": "User",
"site_admin": false
},
"access_tokens_url": "https://api.github.com/app/installations/1/access_tokens",
"repositories_url": "https://api.github.com/installation/repositories",
"html_url": "https://github.com/organizations/github/settings/installations/1",
"app_id": 1,
"target_id": 1,
"target_type": "Organization",
"permissions": {
"checks": "write",
"metadata": "read",
"contents": "read"
},
"events": [
"push",
"pull_request"
],
"single_file_name": "config.yaml",
"has_multiple_single_files": true,
"single_file_paths": [
"config.yml",
".github/issue_TEMPLATE.md"
],
"repository_selection": "selected",
"created_at": "2017-07-08T16:18:44-04:00",
"updated_at": "2017-07-08T16:18:44-04:00",
"app_slug": "github-actions",
"suspended_at": null,
"suspended_by": null
}
]
`

func testBackendPathInstallations(t *testing.T, op logical.Operation) {
t.Helper()

Expand All @@ -22,13 +76,8 @@ func testBackendPathInstallations(t *testing.T, op logical.Operation) {
func(w http.ResponseWriter, _ *http.Request) {
t.Helper()

body, _ := json.Marshal(map[string]any{
"installations": []map[string]any{
{"id": 1, "account": map[string]any{"login": "test"}},
},
})
w.WriteHeader(http.StatusOK)
w.Write(body)
w.Write([]byte(installationsJSON))
}),
)
defer ts.Close()
Expand All @@ -53,7 +102,7 @@ func testBackendPathInstallations(t *testing.T, op logical.Operation) {
assert.NilError(t, err)

assert.Assert(t, r != nil)
assert.Assert(t, len(r.Data["installations"].([]map[string]any)) > 0)
assert.Assert(t, len(r.Data) > 0)
})

t.Run("FailedClient", func(t *testing.T) {
Expand Down
7 changes: 7 additions & 0 deletions github/path_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ var requestDuration = prometheus.NewSummaryVec(prometheus.SummaryOpts{
Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001},
}, []string{"success", keyInstallationID, keyOrgName, keyPerms, keyRepoIDs, keyRepos})

// installationsDuration records useful metric data about installation requests.
var installationsDuration = prometheus.NewSummaryVec(prometheus.SummaryOpts{
Name: fmt.Sprintf("%s_installations_duration_seconds", prefixMetrics),
Help: "Total duration of Vault GitHub installation requests in seconds.",
Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001},
}, []string{"success"})

// revokeDuration records useful metric data about backend token revocations.
var revokeDuration = prometheus.NewSummaryVec(prometheus.SummaryOpts{
Name: fmt.Sprintf("%s_revocation_request_duration_seconds", prefixMetrics),
Expand Down

0 comments on commit 330d284

Please sign in to comment.