From 367b664318532e9eb7ab1e3589713cf418057b55 Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Wed, 27 Oct 2021 15:56:38 -0400 Subject: [PATCH] Add tests for cert expiry metrics --- agent/metrics_test.go | 54 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/agent/metrics_test.go b/agent/metrics_test.go index cbf1960654..bbfe2430bf 100644 --- a/agent/metrics_test.go +++ b/agent/metrics_test.go @@ -11,6 +11,7 @@ import ( "testing" "github.com/hashicorp/consul/sdk/testutil" + "github.com/hashicorp/consul/testrpc" "github.com/hashicorp/consul/tlsutil" "github.com/stretchr/testify/require" @@ -156,3 +157,56 @@ func TestHTTPHandlers_AgentMetrics_TLSCertExpiry_Prometheus(t *testing.T) { require.Contains(t, respRec.Body.String(), "agent_3_agent_tls_cert_expiry 1.7") } + +func TestHTTPHandlers_AgentMetrics_CACertExpiry_Prometheus(t *testing.T) { + skipIfShortTesting(t) + // This test cannot use t.Parallel() since we modify global state, ie the global metrics instance + + t.Run("non-leader emits NaN", func(t *testing.T) { + hcl := ` + telemetry = { + prometheus_retention_time = "5s", + disable_hostname = true + metrics_prefix = "agent_4" + } + connect { + enabled = true + } + bootstrap = false + ` + + a := StartTestAgent(t, TestAgent{HCL: hcl}) + defer a.Shutdown() + + respRec := httptest.NewRecorder() + recordPromMetrics(t, a, respRec) + + require.Contains(t, respRec.Body.String(), "agent_4_mesh_active_root_ca_expiry NaN") + require.Contains(t, respRec.Body.String(), "agent_4_mesh_active_signing_ca_expiry NaN") + }) + + t.Run("leader emits a value", func(t *testing.T) { + hcl := ` + telemetry = { + prometheus_retention_time = "5s", + disable_hostname = true + metrics_prefix = "agent_5" + } + connect { + enabled = true + } + ` + + a := StartTestAgent(t, TestAgent{HCL: hcl}) + defer a.Shutdown() + testrpc.WaitForLeader(t, a.RPC, "dc1") + + respRec := httptest.NewRecorder() + recordPromMetrics(t, a, respRec) + + out := respRec.Body.String() + require.Contains(t, out, "agent_5_mesh_active_root_ca_expiry 3.15") + require.Contains(t, out, "agent_5_mesh_active_signing_ca_expiry 3.15") + }) + +}