Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add build tags for cloud tests #16937

Merged
merged 14 commits into from
Mar 17, 2020
6 changes: 6 additions & 0 deletions dev-tools/mage/gotest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -83,13 +84,18 @@ 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"
}
return params
}

func testTagsFromEnv() []string {
kaiyan-sheng marked this conversation as resolved.
Show resolved Hide resolved
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") }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// you may not use this file except in compliance with the Elastic License.

// +build integration
// +build azure

package azureeventhub

Expand All @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand Down
45 changes: 23 additions & 22 deletions x-pack/filebeat/input/s3/s3_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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"
Expand All @@ -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")
Expand All @@ -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) {
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// you may not use this file except in compliance with the Elastic License.

// +build integration
// +build aws

package billing

Expand All @@ -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, "/")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// you may not use this file except in compliance with the Elastic License.

// +build integration
// +build aws

package cloudwatch

Expand All @@ -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)
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// you may not use this file except in compliance with the Elastic License.

// +build integration
// +build aws

package dynamodb

Expand All @@ -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, "/")
Expand Down
6 changes: 2 additions & 4 deletions x-pack/metricbeat/module/aws/ebs/ebs_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// you may not use this file except in compliance with the Elastic License.

// +build integration
// +build aws

package ebs

Expand All @@ -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, "/")
Expand Down
11 changes: 3 additions & 8 deletions x-pack/metricbeat/module/aws/ec2/ec2_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// you may not use this file except in compliance with the Elastic License.

// +build integration
// +build aws

package ec2

Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 2 additions & 4 deletions x-pack/metricbeat/module/aws/elb/elb_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// you may not use this file except in compliance with the Elastic License.

// +build integration
// +build aws

package elb

Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// you may not use this file except in compliance with the Elastic License.

// +build integration
// +build aws

package lambda

Expand All @@ -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, "/")
Expand Down
10 changes: 7 additions & 3 deletions x-pack/metricbeat/module/aws/mtest/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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",
Expand All @@ -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 {
Expand Down
Loading