|
| 1 | +package metrics |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "crypto/rand" |
| 6 | + "crypto/rsa" |
| 7 | + "crypto/x509" |
| 8 | + "crypto/x509/pkix" |
| 9 | + "encoding/pem" |
| 10 | + "math/big" |
| 11 | + "testing" |
| 12 | + "time" |
| 13 | + |
| 14 | + "github.com/prometheus/client_golang/prometheus" |
| 15 | + "github.com/prometheus/client_golang/prometheus/testutil" |
| 16 | + "github.com/stretchr/testify/assert" |
| 17 | + "github.com/stretchr/testify/require" |
| 18 | +) |
| 19 | + |
| 20 | +func testCandlepinPEMs(t *testing.T) (certPEM, keyPEM string) { |
| 21 | + t.Helper() |
| 22 | + key, err := rsa.GenerateKey(rand.Reader, 2048) |
| 23 | + require.NoError(t, err) |
| 24 | + |
| 25 | + notBefore := time.Now().UTC().Add(-time.Hour) |
| 26 | + notAfter := notBefore.Add(100 * 24 * time.Hour) |
| 27 | + tmpl := x509.Certificate{ |
| 28 | + SerialNumber: big.NewInt(1), |
| 29 | + Subject: pkix.Name{CommonName: "test-candlepin"}, |
| 30 | + NotBefore: notBefore, |
| 31 | + NotAfter: notAfter, |
| 32 | + } |
| 33 | + der, err := x509.CreateCertificate(rand.Reader, &tmpl, &tmpl, &key.PublicKey, key) |
| 34 | + require.NoError(t, err) |
| 35 | + |
| 36 | + var certBuf, keyBuf bytes.Buffer |
| 37 | + require.NoError(t, pem.Encode(&certBuf, &pem.Block{Type: "CERTIFICATE", Bytes: der})) |
| 38 | + require.NoError(t, pem.Encode(&keyBuf, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(key)})) |
| 39 | + return certBuf.String(), keyBuf.String() |
| 40 | +} |
| 41 | + |
| 42 | +func TestApplyCandlepinCertExpiry_setsGauge(t *testing.T) { |
| 43 | + gv := prometheus.NewGaugeVec(prometheus.GaugeOpts{ |
| 44 | + Namespace: "patchman_engine", |
| 45 | + Subsystem: "core", |
| 46 | + Name: "certificate_expiry_days_test_helper", |
| 47 | + Help: "test", |
| 48 | + }, []string{"certificate_label"}) |
| 49 | + |
| 50 | + certPEM, keyPEM := testCandlepinPEMs(t) |
| 51 | + applyCandlepinCertExpiry(gv, certPEM, keyPEM) |
| 52 | + |
| 53 | + v := testutil.ToFloat64(gv.WithLabelValues(candlepinCertLabel)) |
| 54 | + assert.GreaterOrEqual(t, v, 99.0) |
| 55 | + assert.LessOrEqual(t, v, 100.0) |
| 56 | +} |
| 57 | + |
| 58 | +func TestApplyCandlepinCertExpiry_badPEMDeletesSeries(t *testing.T) { |
| 59 | + gv := prometheus.NewGaugeVec(prometheus.GaugeOpts{ |
| 60 | + Namespace: "patchman_engine", |
| 61 | + Subsystem: "core", |
| 62 | + Name: "certificate_expiry_days_test_helper_bad", |
| 63 | + Help: "test", |
| 64 | + }, []string{"certificate_label"}) |
| 65 | + |
| 66 | + certPEM, keyPEM := testCandlepinPEMs(t) |
| 67 | + reg := prometheus.NewPedanticRegistry() |
| 68 | + reg.MustRegister(gv) |
| 69 | + |
| 70 | + applyCandlepinCertExpiry(gv, certPEM, keyPEM) |
| 71 | + require.NotZero(t, testutil.ToFloat64(gv.WithLabelValues(candlepinCertLabel))) |
| 72 | + |
| 73 | + applyCandlepinCertExpiry(gv, "not-valid-pem", "not-valid-pem") |
| 74 | + n, err := testutil.GatherAndCount(reg) |
| 75 | + require.NoError(t, err) |
| 76 | + assert.Zero(t, n) |
| 77 | +} |
| 78 | + |
| 79 | +func TestApplyCandlepinCertExpiry_missingConfigDeletesSeries(t *testing.T) { |
| 80 | + gv := prometheus.NewGaugeVec(prometheus.GaugeOpts{ |
| 81 | + Namespace: "patchman_engine", |
| 82 | + Subsystem: "core", |
| 83 | + Name: "certificate_expiry_days_test_helper_clear", |
| 84 | + Help: "test", |
| 85 | + }, []string{"certificate_label"}) |
| 86 | + |
| 87 | + reg := prometheus.NewPedanticRegistry() |
| 88 | + reg.MustRegister(gv) |
| 89 | + |
| 90 | + certPEM, keyPEM := testCandlepinPEMs(t) |
| 91 | + applyCandlepinCertExpiry(gv, certPEM, keyPEM) |
| 92 | + applyCandlepinCertExpiry(gv, "", "") |
| 93 | + n, err := testutil.GatherAndCount(reg) |
| 94 | + require.NoError(t, err) |
| 95 | + assert.Zero(t, n) |
| 96 | +} |
0 commit comments