Skip to content

Commit

Permalink
metrics tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kayra1 committed May 14, 2024
1 parent 1eb36f6 commit 27bd493
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 3 deletions.
9 changes: 6 additions & 3 deletions internal/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ func NewMetricsSubsystem(db *certdb.CertificateRequestsRepository) *PrometheusMe
log.Println(errors.Join(errors.New("error generating metrics repository: "), err))
}
metricsBackend := newPrometheusMetrics()
metricsBackend.generateMetrics(csrs)
go func() {
metricsBackend.generateMetrics(csrs)
time.Sleep(120 * ONE_SECOND)
metricsBackend.generateMetrics(csrs)
}()
metricsBackend.Handler = promhttp.HandlerFor(metricsBackend.registry, promhttp.HandlerOpts{})
return metricsBackend
Expand Down Expand Up @@ -97,10 +98,12 @@ func (pm *PrometheusMetrics) generateMetrics(csrs []certdb.CertificateRequest) {
for _, entry := range csrs {
if entry.Certificate == "" {
outstandingCSRCount += 1
continue
}
if entry.Certificate != "" && entry.Certificate != "rejected" {
certCount += 1
if entry.Certificate == "rejected" {
continue
}
certCount += 1
expiryDate := certificateExpiryDate(entry.Certificate)
daysRemaining := time.Until(expiryDate).Hours() / 24
if daysRemaining < 0 {
Expand Down
112 changes: 112 additions & 0 deletions internal/metrics/metrics_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
package metrics_test

import (
"bytes"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"fmt"
"log"
"math/big"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"

"github.com/canonical/gocert/internal/certdb"
metrics "github.com/canonical/gocert/internal/metrics"
Expand Down Expand Up @@ -41,3 +49,107 @@ func TestPrometheusHandler(t *testing.T) {
log.Fatalln(err)
}
}

// Generates a CSR and Certificate with the given days remaining
func generateCertPair(daysRemaining int) (string, string) {
NotAfterTime := time.Now().AddDate(0, 0, daysRemaining)
key, _ := rsa.GenerateKey(rand.Reader, 2048)

csrTemplate := x509.CertificateRequest{}
certTemplate := x509.Certificate{
SerialNumber: big.NewInt(1),
NotAfter: NotAfterTime,
}

csrBytes, _ := x509.CreateCertificateRequest(rand.Reader, &csrTemplate, key)
certBytes, _ := x509.CreateCertificate(rand.Reader, &certTemplate, &certTemplate, &key.PublicKey, key)

var buff bytes.Buffer
pem.Encode(&buff, &pem.Block{

Check failure on line 68 in internal/metrics/metrics_test.go

View workflow job for this annotation

GitHub Actions / go-lint / lint

Error return value of `pem.Encode` is not checked (errcheck)
Type: "CERTIFICATE REQUEST",
Bytes: csrBytes,
})
csr := buff.String()
buff.Reset()
pem.Encode(&buff, &pem.Block{

Check failure on line 74 in internal/metrics/metrics_test.go

View workflow job for this annotation

GitHub Actions / go-lint / lint

Error return value of `pem.Encode` is not checked (errcheck)
Type: "CERTIFICATE",
Bytes: certBytes,
})
cert := buff.String()
return csr, cert
}

func initializeTestDB(db *certdb.CertificateRequestsRepository) {
for i, v := range []int{5, 10, 32} {
csr, cert := generateCertPair(v)
_, err := db.Create(csr)
if err != nil {
log.Fatalf("couldn't create test csr:%s", err)
}
_, err = db.Update(fmt.Sprint(i+1), cert)
if err != nil {
log.Fatalf("couldn't create test cert:%s", err)
}
}
}

// TestMetrics tests some of the metrics that we currently collect.
func TestMetrics(t *testing.T) {
db, err := certdb.NewCertificateRequestsRepository(":memory:", "CertificateReq")
if err != nil {
log.Fatalln(err)
}
initializeTestDB(db)
m := metrics.NewMetricsSubsystem(db)

request, _ := http.NewRequest("GET", "/", nil)
recorder := httptest.NewRecorder()
m.Handler.ServeHTTP(recorder, request)

if status := recorder.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK)
}
if recorder.Body.String() == "" {
t.Errorf("handler returned an empty body")
}
for _, line := range strings.Split(recorder.Body.String(), "\n") {
if strings.Contains(line, "outstanding_certificate_requests ") && !strings.HasPrefix(line, "#") {
if !strings.HasSuffix(line, "0") {
t.Errorf("outstanding_certificate_requests expected to receive 0")
}
} else if strings.Contains(line, "certificate_requests ") && !strings.HasPrefix(line, "#") {
if !strings.HasSuffix(line, "3") {
t.Errorf("certificate_requests expected to receive 3")
}
} else if strings.Contains(line, "certificates ") && !strings.HasPrefix(line, "#") {
if !strings.HasSuffix(line, "3") {
t.Errorf("certificates expected to receive 3")
}
} else if strings.Contains(line, "certificates_expired ") && !strings.HasPrefix(line, "#") {
if !strings.HasSuffix(line, "0") {
t.Errorf("certificates_expired expected to receive 0")
}
} else if strings.Contains(line, "certificates_expiring_in_1_day ") && !strings.HasPrefix(line, "#") {
if !strings.HasSuffix(line, "0") {
t.Errorf("certificates_expiring_in_1_day expected to receive 0")
}
} else if strings.Contains(line, "certificates_expiring_in_7_days ") && !strings.HasPrefix(line, "#") {
if !strings.HasSuffix(line, "1") {
t.Errorf("certificates_expiring_in_7_days expected to receive 1")
}
} else if strings.Contains(line, "certificates_expiring_in_30_days ") && !strings.HasPrefix(line, "#") {
if !strings.HasSuffix(line, "2") {
t.Errorf("certificates_expiring_in_30_days expected to receive 2")
}
} else if strings.Contains(line, "certificates_expiring_in_90_days ") && !strings.HasPrefix(line, "#") {
if !strings.HasSuffix(line, "3") {
t.Errorf("certificates_expiring_in_90_days expected to receive 3")
}
}
}

err = db.Close()
if err != nil {
log.Fatalln(err)
}
}

0 comments on commit 27bd493

Please sign in to comment.