Path: blob/main/pkg/integrations/cloudwatch_exporter/config_test.go
5399 views
package cloudwatch_exporter12import (3"testing"45yaceConf "github.com/nerdswords/yet-another-cloudwatch-exporter/pkg/config"6yaceModel "github.com/nerdswords/yet-another-cloudwatch-exporter/pkg/model"7"github.com/stretchr/testify/assert"8"github.com/stretchr/testify/require"9"gopkg.in/yaml.v2"10)1112const configString = `13sts_region: us-east-214discovery:15exported_tags:16AWS/EC2:17- name18- type19jobs:20- type: AWS/EC221search_tags:22- key: instance_type23value: spot24regions:25- us-east-226roles:27- role_arn: arn:aws:iam::878167871295:role/yace_testing28custom_tags:29- key: alias30value: tesis31metrics:32- name: CPUUtilization33period: 5m34statistics:35- Maximum36- Average37static:38- regions:39- us-east-240name: custom_tesis_metrics41namespace: CoolApp42dimensions:43- name: PURCHASES_SERVICE44value: CoolService45- name: APP_VERSION46value: 1.047metrics:48- name: KPIs49period: 5m50statistics:51- Average52`5354// for testing fips_disabled behaviour55const configString2 = `56sts_region: us-east-257fips_disabled: true58discovery:59exported_tags:60AWS/EC2:61- name62- type63jobs:64- type: AWS/EC265search_tags:66- key: instance_type67value: spot68regions:69- us-east-270roles:71- role_arn: arn:aws:iam::878167871295:role/yace_testing72custom_tags:73- key: alias74value: tesis75metrics:76- name: CPUUtilization77period: 5m78statistics:79- Maximum80- Average81static:82- regions:83- us-east-284name: custom_tesis_metrics85namespace: CoolApp86dimensions:87- name: PURCHASES_SERVICE88value: CoolService89- name: APP_VERSION90value: 1.091metrics:92- name: KPIs93period: 5m94statistics:95- Average96`9798var falsePtr = false99var truePtr = true100101var expectedConfig = yaceConf.ScrapeConf{102APIVersion: "v1alpha1",103StsRegion: "us-east-2",104Discovery: yaceConf.Discovery{105ExportedTagsOnMetrics: map[string][]string{106"AWS/EC2": {"name", "type"},107},108Jobs: []*yaceConf.Job{109{110Type: "AWS/EC2",111Regions: []string{"us-east-2"},112Roles: []yaceConf.Role{113{114RoleArn: "arn:aws:iam::878167871295:role/yace_testing",115},116},117CustomTags: []yaceModel.Tag{118{119Key: "alias",120Value: "tesis",121},122},123SearchTags: []yaceModel.Tag{124{125Key: "instance_type",126Value: "spot",127},128},129Metrics: []*yaceConf.Metric{130{131Name: "CPUUtilization",132Statistics: []string{"Maximum", "Average"},133// Defaults get configured from general settings134Period: 300,135Length: 300,136Delay: 0,137NilToZero: &nilToZero,138AddCloudwatchTimestamp: &addCloudwatchTimestamp,139},140},141RoundingPeriod: nil,142JobLevelMetricFields: yaceConf.JobLevelMetricFields{143Period: 0,144Length: 0,145Delay: 0,146AddCloudwatchTimestamp: &falsePtr,147NilToZero: &nilToZero,148},149},150},151},152Static: []*yaceConf.Static{153{154Name: "custom_tesis_metrics",155Regions: []string{"us-east-2"},156Roles: []yaceConf.Role{{}},157Namespace: "CoolApp",158CustomTags: []yaceModel.Tag{},159Dimensions: []yaceConf.Dimension{160{161Name: "PURCHASES_SERVICE",162Value: "CoolService",163},164{165Name: "APP_VERSION",166Value: "1.0",167},168},169Metrics: []*yaceConf.Metric{170{171Name: "KPIs",172Period: 300,173Length: 300,174Statistics: []string{"Average"},175Delay: 0,176NilToZero: &nilToZero,177AddCloudwatchTimestamp: &addCloudwatchTimestamp,178},179},180},181},182}183184func TestTranslateConfigToYACEConfig(t *testing.T) {185c := Config{}186err := yaml.Unmarshal([]byte(configString), &c)187require.NoError(t, err, "failed to unmarshall config")188189yaceConf, fipsEnabled, err := ToYACEConfig(&c)190require.NoError(t, err, "failed to translate to YACE configuration")191192require.EqualValues(t, expectedConfig, yaceConf)193require.EqualValues(t, truePtr, fipsEnabled)194195err = yaml.Unmarshal([]byte(configString2), &c)196require.NoError(t, err, "failed to unmarshall config")197198yaceConf, fipsEnabled2, err := ToYACEConfig(&c)199require.NoError(t, err, "failed to translate to YACE configuration")200201require.EqualValues(t, expectedConfig, yaceConf)202require.EqualValues(t, falsePtr, fipsEnabled2)203}204205func TestCloudwatchExporterConfigInstanceKey(t *testing.T) {206cfg1 := &Config{207STSRegion: "us-east-2",208}209cfg2 := &Config{210STSRegion: "us-east-3",211}212213cfg1Hash, err := cfg1.InstanceKey("")214require.NoError(t, err)215cfg2Hash, err := cfg2.InstanceKey("")216require.NoError(t, err)217218assert.NotEqual(t, cfg1Hash, cfg2Hash)219220// test that making them equal in values leads to the same instance key221cfg2.STSRegion = "us-east-2"222cfg2Hash, err = cfg2.InstanceKey("")223require.NoError(t, err)224225assert.Equal(t, cfg1Hash, cfg2Hash)226}227228229