From 16681f63013bbad4b7281091486576e9124e9fd9 Mon Sep 17 00:00:00 2001 From: Jaime Soriano Pastor Date: Tue, 17 Mar 2020 17:09:41 +0100 Subject: [PATCH] Add build tags for cloud tests (#16937) (#17055) Add build tags to AWS and Azure integration tests, and make tests fail if they cannot get a working configuration. Add support for a `TEST_TAGS` environment variable in mage targets that add additional build tags to go tests calls. When `TEST_TAGS` includes a cloud provider, it is expected that valid credentials to run these tests are available in the environment, if not, tests will fail. This is intentional, to avoid skipping tests by mistake due to some misconfiguration in CI. (cherry picked from commit 1345a972fbf0472021a3cfdabd22f27745bebb53) --- dev-tools/mage/gotest.go | 8 ++++ dev-tools/mage/integtest.go | 1 + metricbeat/magefile.go | 1 + .../azureeventhub_integration_test.go | 27 ++++++----- .../filebeat/input/s3/s3_integration_test.go | 45 ++++++++++--------- x-pack/metricbeat/magefile.go | 2 + .../aws/billing/billing_integration_test.go | 6 +-- .../cloudwatch/cloudwatch_integration_test.go | 11 ++--- .../aws/dynamodb/dynamodb_integration_test.go | 6 +-- .../module/aws/ebs/ebs_integration_test.go | 6 +-- .../module/aws/ec2/ec2_integration_test.go | 11 ++--- .../module/aws/elb/elb_integration_test.go | 6 +-- .../aws/lambda/lambda_integration_test.go | 6 +-- .../module/aws/mtest/integration.go | 10 +++-- .../module/aws/rds/rds_integration_test.go | 11 ++--- .../s3_daily_storage_integration_test.go | 11 ++--- .../s3_request/s3_request_integration_test.go | 11 ++--- .../module/aws/sns/sns_integration_test.go | 6 +-- .../module/aws/sqs/sqs_integration_test.go | 11 ++--- .../aws/usage/usage_integration_test.go | 6 +-- .../module/aws/vpc/vpc_integration_test.go | 6 +-- .../compute_vm/compute_vm_integration_test.go | 11 ++--- .../compute_vm_scaleset_integration_test.go | 11 ++--- .../container_instance_integration_test.go | 11 ++--- .../container_registry_integration_test.go | 11 ++--- .../container_service_integration_test.go | 11 ++--- .../database_account_integration_test.go | 11 ++--- .../azure/monitor/monitor_integration_test.go | 11 ++--- .../azure/storage/storage_integration_test.go | 11 ++--- .../module/azure/test/integration.go | 16 ++++--- 30 files changed, 124 insertions(+), 188 deletions(-) diff --git a/dev-tools/mage/gotest.go b/dev-tools/mage/gotest.go index 2c8dd1cd2b7..e84ef65ce69 100644 --- a/dev-tools/mage/gotest.go +++ b/dev-tools/mage/gotest.go @@ -67,6 +67,7 @@ func makeGoTestArgs(name string) GoTestArgs { Packages: []string{"./..."}, OutputFile: fileName + ".out", JUnitReportFile: fileName + ".xml", + Tags: testTagsFromEnv(), } if TestCoverage { params.CoverageProfileFile = fileName + ".cov" @@ -83,6 +84,7 @@ func makeGoTestArgsForModule(name, module string) GoTestArgs { Packages: []string{fmt.Sprintf("./module/%s/...", module)}, OutputFile: fileName + ".out", JUnitReportFile: fileName + ".xml", + Tags: testTagsFromEnv(), } if TestCoverage { params.CoverageProfileFile = fileName + ".cov" @@ -90,6 +92,12 @@ func makeGoTestArgsForModule(name, module string) GoTestArgs { return params } +// testTagsFromEnv gets a list of comma-separated tags from the TEST_TAGS +// environment variables, e.g: TEST_TAGS=aws,azure. +func testTagsFromEnv() []string { + return strings.Split(strings.Trim(os.Getenv("TEST_TAGS"), ", "), ",") +} + // DefaultGoTestUnitArgs returns a default set of arguments for running // all unit tests. We tag unit test files with '!integration'. func DefaultGoTestUnitArgs() GoTestArgs { return makeGoTestArgs("Unit") } diff --git a/dev-tools/mage/integtest.go b/dev-tools/mage/integtest.go index 41219d58d6c..64fa8209009 100644 --- a/dev-tools/mage/integtest.go +++ b/dev-tools/mage/integtest.go @@ -147,6 +147,7 @@ func RunIntegTest(mageTarget string, test func() error, passThroughEnvVars ...st env := []string{ "TEST_COVERAGE", "RACE_DETECTOR", + "TEST_TAGS", "PYTHON_EXE", } env = append(env, passThroughEnvVars...) diff --git a/metricbeat/magefile.go b/metricbeat/magefile.go index 2f955efb725..6ff8776c800 100644 --- a/metricbeat/magefile.go +++ b/metricbeat/magefile.go @@ -172,6 +172,7 @@ func CollectDocs() error { // GoIntegTest executes the Go integration tests. // Use TEST_COVERAGE=true to enable code coverage profiling. // Use RACE_DETECTOR=true to enable the race detector. +// Use TEST_TAGS=tag1,tag2 to add additional build tags. func GoIntegTest(ctx context.Context) error { return devtools.GoTestIntegrationForModule(ctx) } diff --git a/x-pack/filebeat/input/azureeventhub/azureeventhub_integration_test.go b/x-pack/filebeat/input/azureeventhub/azureeventhub_integration_test.go index 9d65ff80104..e14bb41a918 100644 --- a/x-pack/filebeat/input/azureeventhub/azureeventhub_integration_test.go +++ b/x-pack/filebeat/input/azureeventhub/azureeventhub_integration_test.go @@ -3,6 +3,7 @@ // you may not use this file except in compliance with the Elastic License. // +build integration +// +build azure package azureeventhub @@ -23,26 +24,20 @@ import ( ) var ( - - // setup the input config azureConfig = common.MustNewConfigFrom(common.MapStr{ - "storage_account_key": os.Getenv("STORAGE_ACCOUNT_NAME"), - "storage_account": os.Getenv("STORAGE_ACCOUNT_KEY"), + "storage_account_key": lookupEnv("STORAGE_ACCOUNT_NAME"), + "storage_account": lookupEnv("STORAGE_ACCOUNT_KEY"), "storage_account_container": ephContainerName, - "connection_string": os.Getenv("EVENTHUB_CONNECTION_STRING"), - "consumer_group": os.Getenv("EVENTHUB_CONSUMERGROUP"), - "eventhub": os.Getenv("EVENTHUB_NAME"), + "connection_string": lookupEnv("EVENTHUB_CONNECTION_STRING"), + "consumer_group": lookupEnv("EVENTHUB_CONSUMERGROUP"), + "eventhub": lookupEnv("EVENTHUB_NAME"), }) message = "{\"records\":[{\"some_field\":\"this is some message\",\"time\":\"2019-12-17T13:43:44.4946995Z\"}" ) func TestInput(t *testing.T) { - if os.Getenv("EVENTHUB_NAME") == "" || os.Getenv("EVENTHUB_CONNECTION_STRING") == "" { - t.Skip("EVENTHUB_NAME or/and EVENTHUB_CONSUMERGROUP are not set in environment.") - } - err := addEventToHub(os.Getenv("EVENTHUB_CONNECTION_STRING")) - + err := addEventToHub(lookupEnv("EVENTHUB_CONNECTION_STRING")) if err != nil { t.Fatal(err) } @@ -99,6 +94,14 @@ func TestInput(t *testing.T) { } } +func lookupEnv(t *testing.T, varName string) string { + value, ok := os.LookupEnv(varName) + if !ok { + t.Fatalf("Environment variable %s is not set", varName) + } + return value +} + func addEventToHub(connStr string) error { hub, err := eventhub.NewHubFromConnectionString(connStr) if err != nil { diff --git a/x-pack/filebeat/input/s3/s3_integration_test.go b/x-pack/filebeat/input/s3/s3_integration_test.go index 7dcd3c241fd..1d6a400a7f1 100644 --- a/x-pack/filebeat/input/s3/s3_integration_test.go +++ b/x-pack/filebeat/input/s3/s3_integration_test.go @@ -2,6 +2,9 @@ // or more contributor license agreements. Licensed under the Elastic License; // you may not use this file except in compliance with the Elastic License. +// +build integration +// +build aws + package s3 import ( @@ -13,14 +16,14 @@ import ( "testing" "time" - "github.com/aws/aws-sdk-go-v2/service/s3/s3iface" - "github.com/aws/aws-sdk-go-v2/service/sqs/sqsiface" + "github.com/stretchr/testify/assert" "github.com/aws/aws-sdk-go-v2/aws" awssdk "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/service/s3" + "github.com/aws/aws-sdk-go-v2/service/s3/s3iface" "github.com/aws/aws-sdk-go-v2/service/sqs" - "github.com/stretchr/testify/assert" + "github.com/aws/aws-sdk-go-v2/service/sqs/sqsiface" "github.com/elastic/beats/v7/filebeat/channel" "github.com/elastic/beats/v7/filebeat/input" @@ -38,7 +41,9 @@ const ( var filePath = filepath.Join("ftest", fileName) // GetConfigForTest function gets aws credentials for integration tests. -func getConfigForTest() (config, string) { +func getConfigForTest(t *testing.T) config { + t.Helper() + awsConfig := awscommon.ConfigAWS{} queueURL := os.Getenv("QUEUE_URL") profileName := os.Getenv("AWS_PROFILE_NAME") @@ -51,25 +56,25 @@ func getConfigForTest() (config, string) { } switch { case queueURL == "": - return config, "Skipping: $QUEUE_URL is not set in environment" + t.Fatal("$QUEUE_URL is not set in environment") case profileName == "" && accessKeyID == "": - return config, "Skipping: $AWS_ACCESS_KEY_ID or $AWS_PROFILE_NAME not set or set to empty" + t.Fatal("$AWS_ACCESS_KEY_ID or $AWS_PROFILE_NAME not set or set to empty") case profileName != "": awsConfig.ProfileName = profileName config.QueueURL = queueURL config.AwsConfig = awsConfig - return config, "" + return config case secretAccessKey == "": - return config, "Skipping: $AWS_SECRET_ACCESS_KEY not set or set to empty" - default: - awsConfig.AccessKeyID = accessKeyID - awsConfig.SecretAccessKey = secretAccessKey - if sessionToken != "" { - awsConfig.SessionToken = sessionToken - } - config.AwsConfig = awsConfig - return config, "" + t.Fatal("$AWS_SECRET_ACCESS_KEY not set or set to empty") } + + awsConfig.AccessKeyID = accessKeyID + awsConfig.SecretAccessKey = secretAccessKey + if sessionToken != "" { + awsConfig.SessionToken = sessionToken + } + config.AwsConfig = awsConfig + return config } func uploadSampleLogFile(t *testing.T, bucketName string, svcS3 s3iface.ClientAPI) { @@ -160,7 +165,7 @@ func runTest(t *testing.T, cfg *common.Config, run func(t *testing.T, input *s3I in, err := NewInput(cfg, connector, inputCtx) if err != nil { - t.Skipf("Skipping: %v", err) + t.Fatal(err) } s3Input := in.(*s3Input) defer s3Input.Stop() @@ -224,13 +229,9 @@ func (o *stubOutleter) OnEvent(event beat.Event) bool { func TestS3Input(t *testing.T) { inputConfig := defaultTestConfig() + config := getConfigForTest(t) runTest(t, inputConfig, func(t *testing.T, input *s3Input, out *stubOutleter) { - config, info := getConfigForTest() - if info != "" { - t.Skipf("failed to get config for test: %v", info) - } - awsConfig, err := awscommon.GetAWSCredentials(config.AwsConfig) if err != nil { diff --git a/x-pack/metricbeat/magefile.go b/x-pack/metricbeat/magefile.go index 5e30c711cc4..6c737ac7a3e 100644 --- a/x-pack/metricbeat/magefile.go +++ b/x-pack/metricbeat/magefile.go @@ -132,6 +132,7 @@ func UnitTest() { // GoUnitTest executes the Go unit tests. // Use TEST_COVERAGE=true to enable code coverage profiling. // Use RACE_DETECTOR=true to enable the race detector. +// Use TEST_TAGS=tag1,tag2 to add additional build tags. func GoUnitTest(ctx context.Context) error { return devtools.GoTest(ctx, devtools.DefaultGoTestUnitArgs()) } @@ -152,6 +153,7 @@ func IntegTest() { // GoIntegTest executes the Go integration tests. // Use TEST_COVERAGE=true to enable code coverage profiling. // Use RACE_DETECTOR=true to enable the race detector. +// Use TEST_TAGS=tag1,tag2 to add additional build tags. func GoIntegTest(ctx context.Context) error { return devtools.GoTestIntegrationForModule(ctx) } diff --git a/x-pack/metricbeat/module/aws/billing/billing_integration_test.go b/x-pack/metricbeat/module/aws/billing/billing_integration_test.go index fe8fa31fd87..3d8cbff0598 100644 --- a/x-pack/metricbeat/module/aws/billing/billing_integration_test.go +++ b/x-pack/metricbeat/module/aws/billing/billing_integration_test.go @@ -3,6 +3,7 @@ // you may not use this file except in compliance with the Elastic License. // +build integration +// +build aws package billing @@ -14,10 +15,7 @@ import ( ) func TestData(t *testing.T) { - config, info := mtest.GetConfigForTest("billing", "300s") - if info != "" { - t.Skip("Skipping TestData: " + info) - } + config := mtest.GetConfigForTest(t, "billing", "300s") metricSet := mbtest.NewFetcher(t, config) metricSet.WriteEvents(t, "/") diff --git a/x-pack/metricbeat/module/aws/cloudwatch/cloudwatch_integration_test.go b/x-pack/metricbeat/module/aws/cloudwatch/cloudwatch_integration_test.go index 09cdde83a4f..c99c6acb40c 100644 --- a/x-pack/metricbeat/module/aws/cloudwatch/cloudwatch_integration_test.go +++ b/x-pack/metricbeat/module/aws/cloudwatch/cloudwatch_integration_test.go @@ -3,6 +3,7 @@ // you may not use this file except in compliance with the Elastic License. // +build integration +// +build aws package cloudwatch @@ -16,10 +17,7 @@ import ( ) func TestFetch(t *testing.T) { - config, info := mtest.GetConfigForTest("cloudwatch", "300s") - if info != "" { - t.Skip("Skipping TestFetch: " + info) - } + config := mtest.GetConfigForTest(t, "cloudwatch", "300s") config = addCloudwatchMetricsToConfig(config) metricSet := mbtest.NewReportingMetricSetV2Error(t, config) @@ -32,10 +30,7 @@ func TestFetch(t *testing.T) { } func TestData(t *testing.T) { - config, info := mtest.GetConfigForTest("cloudwatch", "300s") - if info != "" { - t.Skip("Skipping TestData: " + info) - } + config := mtest.GetConfigForTest(t, "cloudwatch", "300s") config = addCloudwatchMetricsToConfig(config) metricSet := mbtest.NewFetcher(t, config) diff --git a/x-pack/metricbeat/module/aws/dynamodb/dynamodb_integration_test.go b/x-pack/metricbeat/module/aws/dynamodb/dynamodb_integration_test.go index 48e7f501e0f..52a97184650 100644 --- a/x-pack/metricbeat/module/aws/dynamodb/dynamodb_integration_test.go +++ b/x-pack/metricbeat/module/aws/dynamodb/dynamodb_integration_test.go @@ -3,6 +3,7 @@ // you may not use this file except in compliance with the Elastic License. // +build integration +// +build aws package dynamodb @@ -14,10 +15,7 @@ import ( ) func TestData(t *testing.T) { - config, info := mtest.GetConfigForTest("dynamodb", "300s") - if info != "" { - t.Skip("Skipping TestData: " + info) - } + config := mtest.GetConfigForTest(t, "dynamodb", "300s") metricSet := mbtest.NewFetcher(t, config) metricSet.WriteEvents(t, "/") diff --git a/x-pack/metricbeat/module/aws/ebs/ebs_integration_test.go b/x-pack/metricbeat/module/aws/ebs/ebs_integration_test.go index d2cec8d0ab9..4012a8c0060 100644 --- a/x-pack/metricbeat/module/aws/ebs/ebs_integration_test.go +++ b/x-pack/metricbeat/module/aws/ebs/ebs_integration_test.go @@ -3,6 +3,7 @@ // you may not use this file except in compliance with the Elastic License. // +build integration +// +build aws package ebs @@ -14,10 +15,7 @@ import ( ) func TestData(t *testing.T) { - config, info := mtest.GetConfigForTest("ebs", "300s") - if info != "" { - t.Skip("Skipping TestData: " + info) - } + config := mtest.GetConfigForTest(t, "ebs", "300s") metricSet := mbtest.NewFetcher(t, config) metricSet.WriteEvents(t, "/") diff --git a/x-pack/metricbeat/module/aws/ec2/ec2_integration_test.go b/x-pack/metricbeat/module/aws/ec2/ec2_integration_test.go index 1e745b52107..baaf6e563e1 100644 --- a/x-pack/metricbeat/module/aws/ec2/ec2_integration_test.go +++ b/x-pack/metricbeat/module/aws/ec2/ec2_integration_test.go @@ -3,6 +3,7 @@ // you may not use this file except in compliance with the Elastic License. // +build integration +// +build aws package ec2 @@ -16,10 +17,7 @@ import ( ) func TestFetch(t *testing.T) { - config, info := mtest.GetConfigForTest("ec2", "300s") - if info != "" { - t.Skip("Skipping TestFetch: " + info) - } + config := mtest.GetConfigForTest(t, "ec2", "300s") metricSet := mbtest.NewReportingMetricSetV2Error(t, config) events, errs := mbtest.ReportingFetchV2Error(metricSet) @@ -66,10 +64,7 @@ func TestFetch(t *testing.T) { } func TestData(t *testing.T) { - config, info := mtest.GetConfigForTest("ec2", "300s") - if info != "" { - t.Skip("Skipping TestData: " + info) - } + config := mtest.GetConfigForTest(t, "ec2", "300s") metricSet := mbtest.NewReportingMetricSetV2Error(t, config) if err := mbtest.WriteEventsReporterV2Error(metricSet, t, "/"); err != nil { diff --git a/x-pack/metricbeat/module/aws/elb/elb_integration_test.go b/x-pack/metricbeat/module/aws/elb/elb_integration_test.go index 254dc822ee0..6fc05f5a0e1 100644 --- a/x-pack/metricbeat/module/aws/elb/elb_integration_test.go +++ b/x-pack/metricbeat/module/aws/elb/elb_integration_test.go @@ -3,6 +3,7 @@ // you may not use this file except in compliance with the Elastic License. // +build integration +// +build aws package elb @@ -32,10 +33,7 @@ func TestData(t *testing.T) { {"AWS/NetworkELB", "./_meta/data_nlb.json"}, } - config, info := mtest.GetConfigForTest("elb", "300s") - if info != "" { - t.Skip("Skipping TestData: " + info) - } + config := mtest.GetConfigForTest(t, "elb", "300s") for _, df := range dataFiles { metricSet := mbtest.NewFetcher(t, config) diff --git a/x-pack/metricbeat/module/aws/lambda/lambda_integration_test.go b/x-pack/metricbeat/module/aws/lambda/lambda_integration_test.go index 4382ef36e0e..6948f93515e 100644 --- a/x-pack/metricbeat/module/aws/lambda/lambda_integration_test.go +++ b/x-pack/metricbeat/module/aws/lambda/lambda_integration_test.go @@ -3,6 +3,7 @@ // you may not use this file except in compliance with the Elastic License. // +build integration +// +build aws package lambda @@ -14,10 +15,7 @@ import ( ) func TestData(t *testing.T) { - config, info := mtest.GetConfigForTest("lambda", "300s") - if info != "" { - t.Skip("Skipping TestData: " + info) - } + config := mtest.GetConfigForTest(t, "lambda", "300s") metricSet := mbtest.NewFetcher(t, config) metricSet.WriteEvents(t, "/") diff --git a/x-pack/metricbeat/module/aws/mtest/integration.go b/x-pack/metricbeat/module/aws/mtest/integration.go index 417f3b60096..06366eafa05 100644 --- a/x-pack/metricbeat/module/aws/mtest/integration.go +++ b/x-pack/metricbeat/module/aws/mtest/integration.go @@ -15,7 +15,9 @@ import ( ) // GetConfigForTest function gets aws credentials for integration tests. -func GetConfigForTest(metricSetName string, period string) (map[string]interface{}, string) { +func GetConfigForTest(t *testing.T, metricSetName string, period string) (map[string]interface{}, string) { + t.Helper() + accessKeyID, okAccessKeyID := os.LookupEnv("AWS_ACCESS_KEY_ID") secretAccessKey, okSecretAccessKey := os.LookupEnv("AWS_SECRET_ACCESS_KEY") sessionToken, okSessionToken := os.LookupEnv("AWS_SESSION_TOKEN") @@ -27,9 +29,9 @@ func GetConfigForTest(metricSetName string, period string) (map[string]interface info := "" config := map[string]interface{}{} if !okAccessKeyID || accessKeyID == "" { - info = "Skipping TestFetch; $AWS_ACCESS_KEY_ID not set or set to empty" + t.Fatal("$AWS_ACCESS_KEY_ID not set or set to empty") } else if !okSecretAccessKey || secretAccessKey == "" { - info = "Skipping TestFetch; $AWS_SECRET_ACCESS_KEY not set or set to empty" + t.Fatal("$AWS_SECRET_ACCESS_KEY not set or set to empty") } else { config = map[string]interface{}{ "module": "aws", @@ -51,6 +53,8 @@ func GetConfigForTest(metricSetName string, period string) (map[string]interface // CheckEventField function checks a given field type and compares it with the expected type for integration tests. func CheckEventField(metricName string, expectedType string, event mb.Event, t *testing.T) { + t.Helper() + ok1, err1 := event.MetricSetFields.HasKey(metricName) ok2, err2 := event.RootFields.HasKey(metricName) if ok1 || ok2 { diff --git a/x-pack/metricbeat/module/aws/rds/rds_integration_test.go b/x-pack/metricbeat/module/aws/rds/rds_integration_test.go index 4d8a45812e3..0be93854039 100644 --- a/x-pack/metricbeat/module/aws/rds/rds_integration_test.go +++ b/x-pack/metricbeat/module/aws/rds/rds_integration_test.go @@ -3,6 +3,7 @@ // you may not use this file except in compliance with the Elastic License. // +build integration +// +build aws package rds @@ -16,10 +17,7 @@ import ( ) func TestFetch(t *testing.T) { - config, info := mtest.GetConfigForTest("rds", "60s") - if info != "" { - t.Skip("Skipping TestFetch: " + info) - } + config := mtest.GetConfigForTest(t, "rds", "60s") metricSet := mbtest.NewReportingMetricSetV2Error(t, config) events, errs := mbtest.ReportingFetchV2Error(metricSet) @@ -49,10 +47,7 @@ func TestFetch(t *testing.T) { } func TestData(t *testing.T) { - config, info := mtest.GetConfigForTest("rds", "60s") - if info != "" { - t.Skip("Skipping TestData: " + info) - } + config := mtest.GetConfigForTest(t, "rds", "60s") metricSet := mbtest.NewReportingMetricSetV2Error(t, config) if err := mbtest.WriteEventsReporterV2Error(metricSet, t, "/"); err != nil { diff --git a/x-pack/metricbeat/module/aws/s3_daily_storage/s3_daily_storage_integration_test.go b/x-pack/metricbeat/module/aws/s3_daily_storage/s3_daily_storage_integration_test.go index 2c27f317cd8..592416a56a8 100644 --- a/x-pack/metricbeat/module/aws/s3_daily_storage/s3_daily_storage_integration_test.go +++ b/x-pack/metricbeat/module/aws/s3_daily_storage/s3_daily_storage_integration_test.go @@ -3,6 +3,7 @@ // you may not use this file except in compliance with the Elastic License. // +build integration +// +build aws package s3_daily_storage @@ -16,10 +17,7 @@ import ( ) func TestFetch(t *testing.T) { - config, info := mtest.GetConfigForTest("s3_daily_storage", "86400s") - if info != "" { - t.Skip("Skipping TestFetch: " + info) - } + config := mtest.GetConfigForTest(t, "s3_daily_storage", "86400s") metricSet := mbtest.NewReportingMetricSetV2Error(t, config) events, errs := mbtest.ReportingFetchV2Error(metricSet) @@ -42,10 +40,7 @@ func TestFetch(t *testing.T) { } func TestData(t *testing.T) { - config, info := mtest.GetConfigForTest("s3_daily_storage", "86400s") - if info != "" { - t.Skip("Skipping TestData: " + info) - } + config := mtest.GetConfigForTest(t, "s3_daily_storage", "86400s") metricSet := mbtest.NewReportingMetricSetV2Error(t, config) if err := mbtest.WriteEventsReporterV2Error(metricSet, t, "/"); err != nil { diff --git a/x-pack/metricbeat/module/aws/s3_request/s3_request_integration_test.go b/x-pack/metricbeat/module/aws/s3_request/s3_request_integration_test.go index 0892e6a7f71..eeae8439f1f 100644 --- a/x-pack/metricbeat/module/aws/s3_request/s3_request_integration_test.go +++ b/x-pack/metricbeat/module/aws/s3_request/s3_request_integration_test.go @@ -3,6 +3,7 @@ // you may not use this file except in compliance with the Elastic License. // +build integration +// +build aws package s3_request @@ -16,10 +17,7 @@ import ( ) func TestFetch(t *testing.T) { - config, info := mtest.GetConfigForTest("s3_request", "86400s") - if info != "" { - t.Skip("Skipping TestFetch: " + info) - } + config := mtest.GetConfigForTest(t, "s3_request", "86400s") metricSet := mbtest.NewReportingMetricSetV2Error(t, config) events, errs := mbtest.ReportingFetchV2Error(metricSet) @@ -56,10 +54,7 @@ func TestFetch(t *testing.T) { } func TestData(t *testing.T) { - config, info := mtest.GetConfigForTest("s3_request", "86400s") - if info != "" { - t.Skip("Skipping TestData: " + info) - } + config := mtest.GetConfigForTest(t, "s3_request", "86400s") metricSet := mbtest.NewReportingMetricSetV2Error(t, config) if err := mbtest.WriteEventsReporterV2Error(metricSet, t, "/"); err != nil { diff --git a/x-pack/metricbeat/module/aws/sns/sns_integration_test.go b/x-pack/metricbeat/module/aws/sns/sns_integration_test.go index 82438ae8040..b00d1a6b708 100644 --- a/x-pack/metricbeat/module/aws/sns/sns_integration_test.go +++ b/x-pack/metricbeat/module/aws/sns/sns_integration_test.go @@ -3,6 +3,7 @@ // you may not use this file except in compliance with the Elastic License. // +build integration +// +build aws package sns @@ -14,10 +15,7 @@ import ( ) func TestData(t *testing.T) { - config, info := mtest.GetConfigForTest("sns", "300s") - if info != "" { - t.Skip("Skipping TestData: " + info) - } + config := mtest.GetConfigForTest(t, "sns", "300s") metricSet := mbtest.NewFetcher(t, config) metricSet.WriteEvents(t, "/") diff --git a/x-pack/metricbeat/module/aws/sqs/sqs_integration_test.go b/x-pack/metricbeat/module/aws/sqs/sqs_integration_test.go index eeeb9d979fa..3f1eddb9faa 100644 --- a/x-pack/metricbeat/module/aws/sqs/sqs_integration_test.go +++ b/x-pack/metricbeat/module/aws/sqs/sqs_integration_test.go @@ -3,6 +3,7 @@ // you may not use this file except in compliance with the Elastic License. // +build integration +// +build aws package sqs @@ -17,10 +18,7 @@ import ( ) func TestFetch(t *testing.T) { - config, info := mtest.GetConfigForTest("sqs", "300s") - if info != "" { - t.Skip("Skipping TestFetch: " + info) - } + config := mtest.GetConfigForTest(t, "sqs", "300s") metricSet := mbtest.NewReportingMetricSetV2Error(t, config) events, errs := mbtest.ReportingFetchV2Error(metricSet) @@ -49,10 +47,7 @@ func TestFetch(t *testing.T) { } func TestData(t *testing.T) { - config, info := mtest.GetConfigForTest("sqs", "300s") - if info != "" { - t.Skip("Skipping TestData: " + info) - } + config := mtest.GetConfigForTest(t, "sqs", "300s") metricSet := mbtest.NewReportingMetricSetV2Error(t, config) if err := mbtest.WriteEventsReporterV2Error(metricSet, t, "/"); err != nil { diff --git a/x-pack/metricbeat/module/aws/usage/usage_integration_test.go b/x-pack/metricbeat/module/aws/usage/usage_integration_test.go index 60d2437743f..f7f63e6ff4d 100644 --- a/x-pack/metricbeat/module/aws/usage/usage_integration_test.go +++ b/x-pack/metricbeat/module/aws/usage/usage_integration_test.go @@ -3,6 +3,7 @@ // you may not use this file except in compliance with the Elastic License. // +build integration +// +build aws package usage @@ -14,10 +15,7 @@ import ( ) func TestData(t *testing.T) { - config, info := mtest.GetConfigForTest("usage", "300s") - if info != "" { - t.Skip("Skipping TestData: " + info) - } + config := mtest.GetConfigForTest(t, "usage", "300s") metricSet := mbtest.NewFetcher(t, config) metricSet.WriteEvents(t, "/") diff --git a/x-pack/metricbeat/module/aws/vpc/vpc_integration_test.go b/x-pack/metricbeat/module/aws/vpc/vpc_integration_test.go index f19007fa369..a7e7db66df4 100644 --- a/x-pack/metricbeat/module/aws/vpc/vpc_integration_test.go +++ b/x-pack/metricbeat/module/aws/vpc/vpc_integration_test.go @@ -3,6 +3,7 @@ // you may not use this file except in compliance with the Elastic License. // +build integration +// +build aws package vpc @@ -32,10 +33,7 @@ func TestData(t *testing.T) { {"AWS/TransitGateway", "./_meta/data_transit_gateway.json"}, } - config, info := mtest.GetConfigForTest("vpc", "300s") - if info != "" { - t.Skip("Skipping TestData: " + info) - } + config := mtest.GetConfigForTest(t, "vpc", "300s") for _, df := range dataFiles { metricSet := mbtest.NewFetcher(t, config) diff --git a/x-pack/metricbeat/module/azure/compute_vm/compute_vm_integration_test.go b/x-pack/metricbeat/module/azure/compute_vm/compute_vm_integration_test.go index 04319405207..f5f988d41f3 100644 --- a/x-pack/metricbeat/module/azure/compute_vm/compute_vm_integration_test.go +++ b/x-pack/metricbeat/module/azure/compute_vm/compute_vm_integration_test.go @@ -3,6 +3,7 @@ // you may not use this file except in compliance with the Elastic License. // +build integration +// +build azure package compute_vm @@ -17,10 +18,7 @@ import ( ) func TestFetchMetricset(t *testing.T) { - config, err := test.GetConfig("compute_vm") - if err != nil { - t.Skip("Skipping TestFetch: " + err.Error()) - } + config := test.GetConfig(t, "compute_vm") metricSet := mbtest.NewReportingMetricSetV2Error(t, config) events, errs := mbtest.ReportingFetchV2Error(metricSet) if len(errs) > 0 { @@ -30,10 +28,7 @@ func TestFetchMetricset(t *testing.T) { } func TestData(t *testing.T) { - config, err := test.GetConfig("compute_vm") - if err != nil { - t.Skip("Skipping TestFetch: " + err.Error()) - } + config := test.GetConfig(t, "compute_vm") metricSet := mbtest.NewFetcher(t, config) metricSet.WriteEvents(t, "/") } diff --git a/x-pack/metricbeat/module/azure/compute_vm_scaleset/compute_vm_scaleset_integration_test.go b/x-pack/metricbeat/module/azure/compute_vm_scaleset/compute_vm_scaleset_integration_test.go index acd6450a97b..b48d7a65c5f 100644 --- a/x-pack/metricbeat/module/azure/compute_vm_scaleset/compute_vm_scaleset_integration_test.go +++ b/x-pack/metricbeat/module/azure/compute_vm_scaleset/compute_vm_scaleset_integration_test.go @@ -3,6 +3,7 @@ // you may not use this file except in compliance with the Elastic License. // +build integration +// +build azure package compute_vm_scaleset @@ -17,10 +18,7 @@ import ( ) func TestFetchMetricset(t *testing.T) { - config, err := test.GetConfig("compute_vm_scaleset") - if err != nil { - t.Skip("Skipping TestFetch: " + err.Error()) - } + config := test.GetConfig(t, "compute_vm_scaleset") metricSet := mbtest.NewReportingMetricSetV2Error(t, config) events, errs := mbtest.ReportingFetchV2Error(metricSet) if len(errs) > 0 { @@ -30,10 +28,7 @@ func TestFetchMetricset(t *testing.T) { } func TestData(t *testing.T) { - config, err := test.GetConfig("compute_vm_scaleset") - if err != nil { - t.Skip("Skipping TestFetch: " + err.Error()) - } + config := test.GetConfig(t, "compute_vm_scaleset") metricSet := mbtest.NewFetcher(t, config) metricSet.WriteEvents(t, "/") } diff --git a/x-pack/metricbeat/module/azure/container_instance/container_instance_integration_test.go b/x-pack/metricbeat/module/azure/container_instance/container_instance_integration_test.go index 7d251e93396..4808c3fde5d 100644 --- a/x-pack/metricbeat/module/azure/container_instance/container_instance_integration_test.go +++ b/x-pack/metricbeat/module/azure/container_instance/container_instance_integration_test.go @@ -3,6 +3,7 @@ // you may not use this file except in compliance with the Elastic License. // +build integration +// +build azure package container_instance @@ -20,10 +21,7 @@ import ( ) func TestFetchMetricset(t *testing.T) { - config, err := test.GetConfig("container_instance") - if err != nil { - t.Skip("Skipping TestFetch: " + err.Error()) - } + config := test.GetConfig(t, "container_instance") metricSet := mbtest.NewReportingMetricSetV2Error(t, config) events, errs := mbtest.ReportingFetchV2Error(metricSet) if len(errs) > 0 { @@ -33,10 +31,7 @@ func TestFetchMetricset(t *testing.T) { } func TestData(t *testing.T) { - config, err := test.GetConfig("container_instance") - if err != nil { - t.Skip("Skipping TestFetch: " + err.Error()) - } + config := test.GetConfig(t, "container_instance") metricSet := mbtest.NewFetcher(t, config) metricSet.WriteEvents(t, "/") } diff --git a/x-pack/metricbeat/module/azure/container_registry/container_registry_integration_test.go b/x-pack/metricbeat/module/azure/container_registry/container_registry_integration_test.go index 53fce487a7a..3f6fceb64d7 100644 --- a/x-pack/metricbeat/module/azure/container_registry/container_registry_integration_test.go +++ b/x-pack/metricbeat/module/azure/container_registry/container_registry_integration_test.go @@ -3,6 +3,7 @@ // you may not use this file except in compliance with the Elastic License. // +build integration +// +build azure package container_registry @@ -20,10 +21,7 @@ import ( ) func TestFetchMetricset(t *testing.T) { - config, err := test.GetConfig("container_registry") - if err != nil { - t.Skip("Skipping TestFetch: " + err.Error()) - } + config := test.GetConfig(t, "container_registry") metricSet := mbtest.NewReportingMetricSetV2Error(t, config) events, errs := mbtest.ReportingFetchV2Error(metricSet) if len(errs) > 0 { @@ -33,10 +31,7 @@ func TestFetchMetricset(t *testing.T) { } func TestData(t *testing.T) { - config, err := test.GetConfig("container_registry") - if err != nil { - t.Skip("Skipping TestFetch: " + err.Error()) - } + config := test.GetConfig(t, "container_registry") metricSet := mbtest.NewFetcher(t, config) metricSet.WriteEvents(t, "/") } diff --git a/x-pack/metricbeat/module/azure/container_service/container_service_integration_test.go b/x-pack/metricbeat/module/azure/container_service/container_service_integration_test.go index 93acb39da51..dfb1017c74f 100644 --- a/x-pack/metricbeat/module/azure/container_service/container_service_integration_test.go +++ b/x-pack/metricbeat/module/azure/container_service/container_service_integration_test.go @@ -3,6 +3,7 @@ // you may not use this file except in compliance with the Elastic License. // +build integration +// +build azure package container_service @@ -20,10 +21,7 @@ import ( ) func TestFetchMetricset(t *testing.T) { - config, err := test.GetConfig("container_service") - if err != nil { - t.Skip("Skipping TestFetch: " + err.Error()) - } + config := test.GetConfig(t, "container_service") metricSet := mbtest.NewReportingMetricSetV2Error(t, config) events, errs := mbtest.ReportingFetchV2Error(metricSet) if len(errs) > 0 { @@ -33,10 +31,7 @@ func TestFetchMetricset(t *testing.T) { } func TestData(t *testing.T) { - config, err := test.GetConfig("container_service") - if err != nil { - t.Skip("Skipping TestFetch: " + err.Error()) - } + config := test.GetConfig(t, "container_service") metricSet := mbtest.NewFetcher(t, config) metricSet.WriteEvents(t, "/") } diff --git a/x-pack/metricbeat/module/azure/database_account/database_account_integration_test.go b/x-pack/metricbeat/module/azure/database_account/database_account_integration_test.go index 8099e729687..0ec41fdd8a4 100644 --- a/x-pack/metricbeat/module/azure/database_account/database_account_integration_test.go +++ b/x-pack/metricbeat/module/azure/database_account/database_account_integration_test.go @@ -3,6 +3,7 @@ // you may not use this file except in compliance with the Elastic License. // +build integration +// +build azure package database_account @@ -17,10 +18,7 @@ import ( ) func TestFetchMetricset(t *testing.T) { - config, err := test.GetConfig("database_account") - if err != nil { - t.Skip("Skipping TestFetch: " + err.Error()) - } + config := test.GetConfig(t, "database_account") metricSet := mbtest.NewReportingMetricSetV2Error(t, config) events, errs := mbtest.ReportingFetchV2Error(metricSet) if len(errs) > 0 { @@ -30,10 +28,7 @@ func TestFetchMetricset(t *testing.T) { } func TestData(t *testing.T) { - config, err := test.GetConfig("database_account") - if err != nil { - t.Skip("Skipping TestFetch: " + err.Error()) - } + config := test.GetConfig(t, "database_account") metricSet := mbtest.NewFetcher(t, config) metricSet.WriteEvents(t, "/") } diff --git a/x-pack/metricbeat/module/azure/monitor/monitor_integration_test.go b/x-pack/metricbeat/module/azure/monitor/monitor_integration_test.go index 87de0e38bce..f37a87f18c8 100644 --- a/x-pack/metricbeat/module/azure/monitor/monitor_integration_test.go +++ b/x-pack/metricbeat/module/azure/monitor/monitor_integration_test.go @@ -3,6 +3,7 @@ // you may not use this file except in compliance with the Elastic License. // +build integration +// +build azure package monitor @@ -17,10 +18,7 @@ import ( ) func TestFetchMetricset(t *testing.T) { - config, err := test.GetConfig("monitor") - if err != nil { - t.Skip("Skipping TestFetch: " + err.Error()) - } + config := test.GetConfig(t, "monitor") metricSet := mbtest.NewReportingMetricSetV2Error(t, config) events, errs := mbtest.ReportingFetchV2Error(metricSet) if len(errs) > 0 { @@ -30,10 +28,7 @@ func TestFetchMetricset(t *testing.T) { } func TestData(t *testing.T) { - config, err := test.GetConfig("monitor") - if err != nil { - t.Skip("Skipping TestFetch: " + err.Error()) - } + config := test.GetConfig(t, "monitor") config["resources"] = []map[string]interface{}{{ "resource_query": "resourceType eq 'Microsoft.DocumentDb/databaseAccounts'", "metrics": []map[string]interface{}{{"namespace": "Microsoft.DocumentDb/databaseAccounts", diff --git a/x-pack/metricbeat/module/azure/storage/storage_integration_test.go b/x-pack/metricbeat/module/azure/storage/storage_integration_test.go index 920f21393c1..f3a3f906173 100644 --- a/x-pack/metricbeat/module/azure/storage/storage_integration_test.go +++ b/x-pack/metricbeat/module/azure/storage/storage_integration_test.go @@ -3,6 +3,7 @@ // you may not use this file except in compliance with the Elastic License. // +build integration +// +build azure package storage @@ -17,10 +18,7 @@ import ( ) func TestFetchMetricset(t *testing.T) { - config, err := test.GetConfig("storage") - if err != nil { - t.Skip("Skipping TestFetch: " + err.Error()) - } + config := test.GetConfig(t, "storage") metricSet := mbtest.NewReportingMetricSetV2Error(t, config) events, errs := mbtest.ReportingFetchV2Error(metricSet) if len(errs) > 0 { @@ -30,10 +28,7 @@ func TestFetchMetricset(t *testing.T) { } func TestData(t *testing.T) { - config, err := test.GetConfig("storage") - if err != nil { - t.Skip("Skipping TestFetch: " + err.Error()) - } + config := test.GetConfig(t, "storage") metricSet := mbtest.NewFetcher(t, config) metricSet.WriteEvents(t, "/") } diff --git a/x-pack/metricbeat/module/azure/test/integration.go b/x-pack/metricbeat/module/azure/test/integration.go index 9e8e011a217..7187e3b93f7 100644 --- a/x-pack/metricbeat/module/azure/test/integration.go +++ b/x-pack/metricbeat/module/azure/test/integration.go @@ -5,27 +5,29 @@ package test import ( - "errors" "os" + "testing" ) // GetConfig function gets azure credentials for integration tests. -func GetConfig(metricSetName string) (map[string]interface{}, error) { +func GetConfig(t *testing.T, metricSetName string) map[string]interface{} { + t.Helper() + clientId, ok := os.LookupEnv("AZURE_CLIENT_ID") if !ok { - return nil, errors.New("could not find var AZURE_CLIENT_ID") + t.Fatal("Could not find var AZURE_CLIENT_ID") } clientSecret, ok := os.LookupEnv("AZURE_CLIENT_SECRET") if !ok { - return nil, errors.New("could not find var AZURE_CLIENT_SECRET") + t.Fatal("Could not find var AZURE_CLIENT_SECRET") } tenantId, ok := os.LookupEnv("AZURE_TENANT_ID") if !ok { - return nil, errors.New("could not find var AZURE_TENANT_ID") + t.Fatal("Could not find var AZURE_TENANT_ID") } subId, ok := os.LookupEnv("AZURE_SUBSCRIPTION_ID") if !ok { - return nil, errors.New("could not find var AZURE_SUBSCRIPTION_ID") + t.Fatal("Could not find var AZURE_SUBSCRIPTION_ID") } return map[string]interface{}{ "module": "azure", @@ -36,5 +38,5 @@ func GetConfig(metricSetName string) (map[string]interface{}, error) { "client_secret": clientSecret, "tenant_id": tenantId, "subscription_id": subId, - }, nil + } }