From 330d284cfedf8c146559760c6f04b449578d3b72 Mon Sep 17 00:00:00 2001 From: Alex Vanderpot <553597+ajvpot@users.noreply.github.com> Date: Mon, 17 Jun 2024 13:09:08 -0700 Subject: [PATCH] tests pass --- github/path_installations.go | 2 +- github/path_installations_test.go | 65 +++++++++++++++++++++++++++---- github/path_metrics.go | 7 ++++ 3 files changed, 65 insertions(+), 9 deletions(-) diff --git a/github/path_installations.go b/github/path_installations.go index c98e59b..5364a4f 100644 --- a/github/path_installations.go +++ b/github/path_installations.go @@ -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()) diff --git a/github/path_installations_test.go b/github/path_installations_test.go index a9c1d22..f34bbac 100644 --- a/github/path_installations_test.go +++ b/github/path_installations_test.go @@ -2,7 +2,6 @@ package github import ( "context" - "encoding/json" "github.com/hashicorp/vault/sdk/logical" "gotest.tools/assert" "net/http" @@ -10,6 +9,61 @@ import ( "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() @@ -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() @@ -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) { diff --git a/github/path_metrics.go b/github/path_metrics.go index 4adf5fe..f46a94b 100644 --- a/github/path_metrics.go +++ b/github/path_metrics.go @@ -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),