|
| 1 | +// Copyright 2024 The Prometheus Authors |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | +package maxdimassociator |
| 14 | + |
| 15 | +import ( |
| 16 | + "testing" |
| 17 | + |
| 18 | + "github.com/stretchr/testify/require" |
| 19 | + |
| 20 | + "github.com/prometheus-community/yet-another-cloudwatch-exporter/pkg/config" |
| 21 | + "github.com/prometheus-community/yet-another-cloudwatch-exporter/pkg/logging" |
| 22 | + "github.com/prometheus-community/yet-another-cloudwatch-exporter/pkg/model" |
| 23 | +) |
| 24 | + |
| 25 | +var keyspacesTable = &model.TaggedResource{ |
| 26 | + ARN: "arn:aws:cassandra:eu-west-1:123456789012:/keyspace/my_keyspace/table/my_table", |
| 27 | + Namespace: "AWS/Cassanrdra", |
| 28 | +} |
| 29 | + |
| 30 | +var cassandraResources = []*model.TaggedResource{ |
| 31 | + keyspacesTable, |
| 32 | +} |
| 33 | + |
| 34 | +func TestAssociatorCassandra(t *testing.T) { |
| 35 | + type args struct { |
| 36 | + dimensionRegexps []model.DimensionsRegexp |
| 37 | + resources []*model.TaggedResource |
| 38 | + metric *model.Metric |
| 39 | + } |
| 40 | + |
| 41 | + type testCase struct { |
| 42 | + name string |
| 43 | + args args |
| 44 | + expectedSkip bool |
| 45 | + expectedResource *model.TaggedResource |
| 46 | + } |
| 47 | + |
| 48 | + testcases := []testCase{ |
| 49 | + { |
| 50 | + name: "keyspace should match with clusterId dimension", |
| 51 | + args: args{ |
| 52 | + dimensionRegexps: config.SupportedServices.GetService("AWS/Cassandra").ToModelDimensionsRegexp(), |
| 53 | + resources: cassandraResources, |
| 54 | + metric: &model.Metric{ |
| 55 | + MetricName: "BillableTableSizeInBytes", |
| 56 | + Namespace: "AWS/Cassandra", |
| 57 | + Dimensions: []model.Dimension{ |
| 58 | + {Name: "Keyspace", Value: "my_keyspace"}, |
| 59 | + {Name: "TableName", Value: "my_table"}, |
| 60 | + }, |
| 61 | + }, |
| 62 | + }, |
| 63 | + expectedSkip: false, |
| 64 | + expectedResource: keyspacesTable, |
| 65 | + }, |
| 66 | + } |
| 67 | + |
| 68 | + for _, tc := range testcases { |
| 69 | + t.Run(tc.name, func(t *testing.T) { |
| 70 | + associator := NewAssociator(logging.NewNopLogger(), tc.args.dimensionRegexps, tc.args.resources) |
| 71 | + res, skip := associator.AssociateMetricToResource(tc.args.metric) |
| 72 | + require.Equal(t, tc.expectedSkip, skip) |
| 73 | + require.Equal(t, tc.expectedResource, res) |
| 74 | + }) |
| 75 | + } |
| 76 | +} |
0 commit comments