Something went wrong. Try again.
Hands pods short-lived GitHub, Gitea, and atproto credentials for their ServiceAccount tokens
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990package github
import ( "strings" "testing"
"github.com/prometheus/client_golang/prometheus"
"tangled.org/guid.foo/lanyard/github/githubtest" "tangled.org/guid.foo/lanyard/metrics/metricstest")
func TestEveryCallToGitHubIsCountedByOperationAndAnswer(t *testing.T) { other, err := ParseKey(githubtest.OtherKeyPEM()) if err != nil { t.Fatal(err) } cases := []struct { name string act func(*testing.T, *world) series string }{ { name: "an App GitHub knows", act: func(t *testing.T, w *world) { w.client.Slug(t.Context(), App{ClientID: clientID, Key: githubtest.Key()}) }, series: `lanyard_github_calls_total{code="2xx",operation="app"} 1`, }, { name: "a key GitHub refuses", act: func(t *testing.T, w *world) { w.client.Slug(t.Context(), App{ClientID: clientID, Key: other}) }, series: `lanyard_github_calls_total{code="4xx",operation="app"} 1`, }, { name: "the installation of a mint", act: func(t *testing.T, w *world) { w.tokens.Get(t.Context(), w.request("widgets", write, nil)) }, series: `lanyard_github_calls_total{code="2xx",operation="installation"} 1`, }, { name: "the token of a mint", act: func(t *testing.T, w *world) { w.tokens.Get(t.Context(), w.request("widgets", write, nil)) }, series: `lanyard_github_calls_total{code="2xx",operation="access_token"} 1`, }, { name: "the read of the repository of a mint", act: func(t *testing.T, w *world) { w.tokens.Get(t.Context(), w.request("widgets", write, nil)) }, series: `lanyard_github_calls_total{code="2xx",operation="repository"} 1`, }, { name: "a repository no installation covers", act: func(t *testing.T, w *world) { w.tokens.Get(t.Context(), w.request("nothing", write, nil)) }, series: `lanyard_github_calls_total{code="4xx",operation="installation"} 1`, }, { name: "the revoke of a fork's token", act: func(t *testing.T, w *world) { w.tokens.Get(t.Context(), w.request("borrowed", write, nil)) }, series: `lanyard_github_calls_total{code="2xx",operation="revoke"} 1`, }, { name: "a revoke that no call has made", act: func(*testing.T, *world) {}, series: `lanyard_github_calls_total{code="error",operation="revoke"} 0`, }, { name: "the time of a mint's call", act: func(t *testing.T, w *world) { w.tokens.Get(t.Context(), w.request("widgets", write, nil)) }, series: `lanyard_github_call_duration_seconds_count{operation="access_token"} 1`, }, } for _, c := range cases { t.Run(c.name, func(t *testing.T) { w := newWorld(t) c.act(t, w) if body := metricstest.Scrape(t, w.registry); !strings.Contains(body, c.series) { t.Errorf("the registry lacks %s:\n%s", c.series, body) } }) }}
func TestAGitHubThatCannotBeReachedIsAnErrorCall(t *testing.T) { registry := prometheus.NewRegistry() New("http://127.0.0.1:1", registry).Slug(t.Context(), App{ClientID: clientID, Key: githubtest.Key()}) series := `lanyard_github_calls_total{code="error",operation="app"} 1` if body := metricstest.Scrape(t, registry); !strings.Contains(body, series) { t.Errorf("the registry lacks %s:\n%s", series, body) }}