You've already forked ssl_exporter
mirror of
https://github.com/ribbybibby/ssl_exporter.git
synced 2025-07-15 23:54:18 +02:00
Use same results check for file + kube probes
This commit is contained in:
@ -15,8 +15,6 @@ import (
|
|||||||
"github.com/ribbybibby/ssl_exporter/test"
|
"github.com/ribbybibby/ssl_exporter/test"
|
||||||
|
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
|
|
||||||
dto "github.com/prometheus/client_model/go"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// TestProbeFile tests a file
|
// TestProbeFile tests a file
|
||||||
@ -164,14 +162,14 @@ func checkFileMetrics(cert *x509.Certificate, certFile string, registry *prometh
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
ips := ","
|
ips := ","
|
||||||
for _, ip := range cert.IPAddresses {
|
for _, ip := range cert.IPAddresses {
|
||||||
ips = ips + ip.String() + ","
|
ips = ips + ip.String() + ","
|
||||||
}
|
}
|
||||||
expectedLabels := map[string]map[string]map[string]string{
|
expectedResults := []*registryResult{
|
||||||
certFile: {
|
®istryResult{
|
||||||
"ssl_file_cert_not_after": {
|
Name: "ssl_file_cert_not_after",
|
||||||
|
LabelValues: map[string]string{
|
||||||
"file": certFile,
|
"file": certFile,
|
||||||
"serial_no": cert.SerialNumber.String(),
|
"serial_no": cert.SerialNumber.String(),
|
||||||
"issuer_cn": cert.Issuer.CommonName,
|
"issuer_cn": cert.Issuer.CommonName,
|
||||||
@ -181,7 +179,11 @@ func checkFileMetrics(cert *x509.Certificate, certFile string, registry *prometh
|
|||||||
"emails": "," + strings.Join(cert.EmailAddresses, ",") + ",",
|
"emails": "," + strings.Join(cert.EmailAddresses, ",") + ",",
|
||||||
"ou": "," + strings.Join(cert.Subject.OrganizationalUnit, ",") + ",",
|
"ou": "," + strings.Join(cert.Subject.OrganizationalUnit, ",") + ",",
|
||||||
},
|
},
|
||||||
"ssl_file_cert_not_before": {
|
Value: float64(cert.NotAfter.Unix()),
|
||||||
|
},
|
||||||
|
®istryResult{
|
||||||
|
Name: "ssl_file_cert_not_before",
|
||||||
|
LabelValues: map[string]string{
|
||||||
"file": certFile,
|
"file": certFile,
|
||||||
"serial_no": cert.SerialNumber.String(),
|
"serial_no": cert.SerialNumber.String(),
|
||||||
"issuer_cn": cert.Issuer.CommonName,
|
"issuer_cn": cert.Issuer.CommonName,
|
||||||
@ -191,89 +193,10 @@ func checkFileMetrics(cert *x509.Certificate, certFile string, registry *prometh
|
|||||||
"emails": "," + strings.Join(cert.EmailAddresses, ",") + ",",
|
"emails": "," + strings.Join(cert.EmailAddresses, ",") + ",",
|
||||||
"ou": "," + strings.Join(cert.Subject.OrganizationalUnit, ",") + ",",
|
"ou": "," + strings.Join(cert.Subject.OrganizationalUnit, ",") + ",",
|
||||||
},
|
},
|
||||||
|
Value: float64(cert.NotBefore.Unix()),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
checkFileRegistryLabels(expectedLabels, mfs, t)
|
for _, res := range expectedResults {
|
||||||
|
checkRegistryResults(res, mfs, t)
|
||||||
expectedResults := map[string]map[string]float64{
|
|
||||||
certFile: {
|
|
||||||
"ssl_file_cert_not_after": float64(cert.NotAfter.Unix()),
|
|
||||||
"ssl_file_cert_not_before": float64(cert.NotBefore.Unix()),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
checkFileRegistryResults(expectedResults, mfs, t)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if expected results are in the registry
|
|
||||||
func checkFileRegistryResults(expRes map[string]map[string]float64, mfs []*dto.MetricFamily, t *testing.T) {
|
|
||||||
results := make(map[string]map[string]float64)
|
|
||||||
for _, mf := range mfs {
|
|
||||||
for _, metric := range mf.Metric {
|
|
||||||
for _, l := range metric.GetLabel() {
|
|
||||||
if l.GetName() == "file" {
|
|
||||||
if _, ok := results[l.GetValue()]; !ok {
|
|
||||||
results[l.GetValue()] = make(map[string]float64)
|
|
||||||
}
|
|
||||||
results[l.GetValue()][mf.GetName()] = metric.GetGauge().GetValue()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for expf, expr := range expRes {
|
|
||||||
for expm, expv := range expr {
|
|
||||||
if _, ok := results[expf]; !ok {
|
|
||||||
t.Fatalf("Could not find results for file %v", expf)
|
|
||||||
}
|
|
||||||
v, ok := results[expf][expm]
|
|
||||||
if !ok {
|
|
||||||
t.Fatalf("Expected metric %v not found in returned metrics for file %v", expm, expf)
|
|
||||||
}
|
|
||||||
if v != expv {
|
|
||||||
t.Fatalf("Expected: %v: %v, got: %v: %v for file %v", expm, expv, expm, v, expf)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if expected labels are in the registry
|
|
||||||
func checkFileRegistryLabels(expRes map[string]map[string]map[string]string, mfs []*dto.MetricFamily, t *testing.T) {
|
|
||||||
results := make(map[string]map[string]map[string]string)
|
|
||||||
for _, mf := range mfs {
|
|
||||||
for _, metric := range mf.Metric {
|
|
||||||
for _, l := range metric.GetLabel() {
|
|
||||||
if l.GetName() == "file" {
|
|
||||||
if _, ok := results[l.GetValue()]; !ok {
|
|
||||||
results[l.GetValue()] = make(map[string]map[string]string)
|
|
||||||
}
|
|
||||||
results[l.GetValue()][mf.GetName()] = make(map[string]string)
|
|
||||||
for _, sl := range metric.GetLabel() {
|
|
||||||
results[l.GetValue()][mf.GetName()][sl.GetName()] = sl.GetValue()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for expf, expr := range expRes {
|
|
||||||
for expm, expl := range expr {
|
|
||||||
if _, ok := results[expf]; !ok {
|
|
||||||
t.Fatalf("Could not find results for file %v", expf)
|
|
||||||
}
|
|
||||||
l, ok := results[expf][expm]
|
|
||||||
if !ok {
|
|
||||||
t.Fatalf("Expected metric %v not found in returned metrics for file %v", expm, expf)
|
|
||||||
}
|
|
||||||
for expk, expv := range expl {
|
|
||||||
v, ok := l[expk]
|
|
||||||
if !ok {
|
|
||||||
t.Fatalf("Expected label %v for metric %v not found in returned metrics for file %v", expk, expm, expf)
|
|
||||||
}
|
|
||||||
if v != expv {
|
|
||||||
t.Fatalf("Expected %v{%q=%q}, got: %v{%q=%q} for file %v", expm, expk, expv, expm, expk, v, expf)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if len(l) != len(expl) {
|
|
||||||
t.Fatalf("Expected %v labels but got %v for metric %v and file %v", len(expl), len(l), expm, expf)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,14 +4,11 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"crypto/x509"
|
"crypto/x509"
|
||||||
"encoding/pem"
|
"encoding/pem"
|
||||||
"fmt"
|
|
||||||
"reflect"
|
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
dto "github.com/prometheus/client_model/go"
|
|
||||||
"github.com/ribbybibby/ssl_exporter/config"
|
"github.com/ribbybibby/ssl_exporter/config"
|
||||||
"github.com/ribbybibby/ssl_exporter/test"
|
"github.com/ribbybibby/ssl_exporter/test"
|
||||||
v1 "k8s.io/api/core/v1"
|
v1 "k8s.io/api/core/v1"
|
||||||
@ -194,49 +191,3 @@ func checkKubernetesMetrics(cert *x509.Certificate, namespace, name, key string,
|
|||||||
checkRegistryResults(res, mfs, t)
|
checkRegistryResults(res, mfs, t)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkRegistryResults(expRes *registryResult, mfs []*dto.MetricFamily, t *testing.T) {
|
|
||||||
var results []*registryResult
|
|
||||||
for _, mf := range mfs {
|
|
||||||
for _, metric := range mf.Metric {
|
|
||||||
labelValues := make(map[string]string)
|
|
||||||
for _, l := range metric.GetLabel() {
|
|
||||||
labelValues[l.GetName()] = l.GetValue()
|
|
||||||
}
|
|
||||||
results = append(results, ®istryResult{
|
|
||||||
Name: mf.GetName(),
|
|
||||||
LabelValues: labelValues,
|
|
||||||
Value: metric.GetGauge().GetValue(),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
var ok bool
|
|
||||||
var resStr string
|
|
||||||
for _, res := range results {
|
|
||||||
resStr = resStr + "\n" + res.String()
|
|
||||||
if reflect.DeepEqual(res, expRes) {
|
|
||||||
ok = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !ok {
|
|
||||||
t.Fatalf("Expected %s, got: %s", expRes.String(), resStr)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type registryResult struct {
|
|
||||||
Name string
|
|
||||||
LabelValues map[string]string
|
|
||||||
Value float64
|
|
||||||
}
|
|
||||||
|
|
||||||
func (rr *registryResult) String() string {
|
|
||||||
var labels []string
|
|
||||||
for k, v := range rr.LabelValues {
|
|
||||||
labels = append(labels, k+"=\""+v+"\"")
|
|
||||||
}
|
|
||||||
m := rr.Name
|
|
||||||
if len(labels) > 0 {
|
|
||||||
m = fmt.Sprintf("%s{%s}", m, strings.Join(labels, ","))
|
|
||||||
}
|
|
||||||
return fmt.Sprintf("%s %f", m, rr.Value)
|
|
||||||
}
|
|
||||||
|
56
prober/metrics_test.go
Normal file
56
prober/metrics_test.go
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
package prober
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
dto "github.com/prometheus/client_model/go"
|
||||||
|
)
|
||||||
|
|
||||||
|
type registryResult struct {
|
||||||
|
Name string
|
||||||
|
LabelValues map[string]string
|
||||||
|
Value float64
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rr *registryResult) String() string {
|
||||||
|
var labels []string
|
||||||
|
for k, v := range rr.LabelValues {
|
||||||
|
labels = append(labels, k+"=\""+v+"\"")
|
||||||
|
}
|
||||||
|
m := rr.Name
|
||||||
|
if len(labels) > 0 {
|
||||||
|
m = fmt.Sprintf("%s{%s}", m, strings.Join(labels, ","))
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("%s %f", m, rr.Value)
|
||||||
|
}
|
||||||
|
|
||||||
|
func checkRegistryResults(expRes *registryResult, mfs []*dto.MetricFamily, t *testing.T) {
|
||||||
|
var results []*registryResult
|
||||||
|
for _, mf := range mfs {
|
||||||
|
for _, metric := range mf.Metric {
|
||||||
|
labelValues := make(map[string]string)
|
||||||
|
for _, l := range metric.GetLabel() {
|
||||||
|
labelValues[l.GetName()] = l.GetValue()
|
||||||
|
}
|
||||||
|
results = append(results, ®istryResult{
|
||||||
|
Name: mf.GetName(),
|
||||||
|
LabelValues: labelValues,
|
||||||
|
Value: metric.GetGauge().GetValue(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var ok bool
|
||||||
|
var resStr string
|
||||||
|
for _, res := range results {
|
||||||
|
resStr = resStr + "\n" + res.String()
|
||||||
|
if reflect.DeepEqual(res, expRes) {
|
||||||
|
ok = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("Expected %s, got: %s", expRes.String(), resStr)
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user