From fc94e16805cc551f214f945b4b962e570d6e2901 Mon Sep 17 00:00:00 2001 From: Hridoy Roy Date: Thu, 29 Oct 2020 10:55:26 -0700 Subject: [PATCH] port external test fix (#10263) --- .../metrics/core_metrics_int_test.go | 37 +++++++++++-------- vault/testing.go | 9 +++++ 2 files changed, 30 insertions(+), 16 deletions(-) diff --git a/vault/external_tests/metrics/core_metrics_int_test.go b/vault/external_tests/metrics/core_metrics_int_test.go index 9ce70ad6c..eb3435dbc 100644 --- a/vault/external_tests/metrics/core_metrics_int_test.go +++ b/vault/external_tests/metrics/core_metrics_int_test.go @@ -16,18 +16,16 @@ import ( ) func TestMountTableMetrics(t *testing.T) { - inm := metrics.NewInmemSink(time.Minute, time.Minute*10) - clusterSink := metricsutil.NewClusterMetricSink("mycluster", inm) - clusterSink.GaugeInterval = time.Second + clusterName := "mycluster" conf := &vault.CoreConfig{ BuiltinRegistry: vault.NewMockBuiltinRegistry(), - MetricsHelper: metricsutil.NewMetricsHelper(inm, true), - MetricSink: clusterSink, + ClusterName: clusterName, } cluster := vault.NewTestCluster(t, conf, &vault.TestClusterOptions{ - KeepStandbysSealed: false, - HandlerFunc: vaulthttp.Handler, - NumCores: 2, + KeepStandbysSealed: false, + HandlerFunc: vaulthttp.Handler, + NumCores: 2, + CoreMetricSinkProvider: testMetricSinkProvider(time.Minute), }) cluster.Start() @@ -129,19 +127,26 @@ func gaugeConditionCheck(comparator string, compareVal int, compareToVal int) er return nil } +func testMetricSinkProvider(gaugeInterval time.Duration) func(string) (*metricsutil.ClusterMetricSink, *metricsutil.MetricsHelper) { + return func(clusterName string) (*metricsutil.ClusterMetricSink, *metricsutil.MetricsHelper) { + inm := metrics.NewInmemSink(1000000*time.Hour, 2000000*time.Hour) + clusterSink := metricsutil.NewClusterMetricSink(clusterName, inm) + clusterSink.GaugeInterval = gaugeInterval + return clusterSink, metricsutil.NewMetricsHelper(inm, false) + } +} + func TestLeaderReElectionMetrics(t *testing.T) { - inm := metrics.NewInmemSink(time.Minute, time.Minute*10) - clusterSink := metricsutil.NewClusterMetricSink("mycluster", inm) - clusterSink.GaugeInterval = time.Second + clusterName := "mycluster" conf := &vault.CoreConfig{ BuiltinRegistry: vault.NewMockBuiltinRegistry(), - MetricsHelper: metricsutil.NewMetricsHelper(inm, true), - MetricSink: clusterSink, + ClusterName: clusterName, } cluster := vault.NewTestCluster(t, conf, &vault.TestClusterOptions{ - KeepStandbysSealed: false, - HandlerFunc: vaulthttp.Handler, - NumCores: 2, + KeepStandbysSealed: false, + HandlerFunc: vaulthttp.Handler, + NumCores: 2, + CoreMetricSinkProvider: testMetricSinkProvider(time.Minute), }) cluster.Start() diff --git a/vault/testing.go b/vault/testing.go index 8e0857d7f..c675be744 100644 --- a/vault/testing.go +++ b/vault/testing.go @@ -1048,6 +1048,8 @@ type TestClusterOptions struct { // RaftAddressProvider should only be specified if the underlying physical // storage is Raft. RaftAddressProvider raftlib.ServerAddressProvider + + CoreMetricSinkProvider func(clusterName string) (*metricsutil.ClusterMetricSink, *metricsutil.MetricsHelper) } var DefaultNumCores = 3 @@ -1412,6 +1414,7 @@ func NewTestCluster(t testing.T, base *CoreConfig, opts *TestClusterOptions) *Te coreConfig.MetricSink = base.MetricSink coreConfig.SecureRandomReader = base.SecureRandomReader coreConfig.DisableSentinelTrace = base.DisableSentinelTrace + coreConfig.ClusterName = base.ClusterName if base.BuiltinRegistry != nil { coreConfig.BuiltinRegistry = base.BuiltinRegistry @@ -1467,6 +1470,9 @@ func NewTestCluster(t testing.T, base *CoreConfig, opts *TestClusterOptions) *Te testApplyEntBaseConfig(coreConfig, base) } + if coreConfig.ClusterName == "" { + coreConfig.ClusterName = t.Name() + } if coreConfig.ClusterHeartbeatInterval == 0 { // Set this lower so that state populates quickly to standby nodes @@ -1758,6 +1764,9 @@ func (testCluster *TestCluster) newCore(t testing.T, idx int, coreConfig *CoreCo metrics.DefaultInmemSignal(inm) localConfig.MetricsHelper = metricsutil.NewMetricsHelper(inm, false) } + if opts != nil && opts.CoreMetricSinkProvider != nil { + localConfig.MetricSink, localConfig.MetricsHelper = opts.CoreMetricSinkProvider(localConfig.ClusterName) + } c, err := NewCore(&localConfig) if err != nil {