Skip to content

Commit

Permalink
Check TLS version in TLS test
Browse files Browse the repository at this point in the history
Signed-off-by: Silke Hofstra <[email protected]>
  • Loading branch information
silkeh committed Oct 12, 2019
1 parent 4e4286a commit 4503a62
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
15 changes: 14 additions & 1 deletion prober/tcp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ func TestTCPConnectionWithTLS(t *testing.T) {
tlsConfig := &tls.Config{
ServerName: "localhost",
Certificates: []tls.Certificate{testcert},
MinVersion: tls.VersionTLS12,
MaxVersion: tls.VersionTLS12,
}
tlsConn := tls.Server(conn, tlsConfig)
defer tlsConn.Close()
Expand Down Expand Up @@ -168,13 +170,24 @@ func TestTCPConnectionWithTLS(t *testing.T) {
}
<-ch

// Check the probe_ssl_earliest_cert_expiry.
// Check the resulting metrics.
mfs, err := registry.Gather()
if err != nil {
t.Fatal(err)
}

// Check labels
expectedLabels := map[string]map[string]string{
"probe_tls_version_info": {
"version": "TLS 1.2",
},
}
checkRegistryLabels(expectedLabels, mfs, t)

// Check values
expectedResults := map[string]float64{
"probe_ssl_earliest_cert_expiry": float64(certExpiry.Unix()),
"probe_tls_version_info": 1,
}
checkRegistryResults(expectedResults, mfs, t)
}
Expand Down
29 changes: 29 additions & 0 deletions prober/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,35 @@ func checkRegistryResults(expRes map[string]float64, mfs []*dto.MetricFamily, t
}
}

// Check if expected labels are in the registry
func checkRegistryLabels(expRes map[string]map[string]string, mfs []*dto.MetricFamily, t *testing.T) {
results := make(map[string]map[string]string)
for _, mf := range mfs {
result := make(map[string]string)
for _, metric := range mf.Metric {
for _, l := range metric.GetLabel() {
result[l.GetName()] = l.GetValue()
}
}
results[mf.GetName()] = result
}

for metric, labelValues := range expRes {
if _, ok := results[metric]; !ok {
t.Fatalf("Expected metric %v not found in returned metrics", metric)
}
for name, exp := range labelValues {
val, ok := results[metric][name]
if !ok {
t.Fatalf("Expected label %v for metric %v not found in returned metrics", val, name)
}
if val != exp {
t.Fatalf("Expected: %v{%q=%q}, got: %v{%q=%q}", metric, name, exp, metric, name, val)
}
}
}
}

// Create test certificate with specified expiry date
// Certificate will be self-signed and use localhost/127.0.0.1
// Generated certificate and key are returned in PEM encoding
Expand Down

0 comments on commit 4503a62

Please sign in to comment.