From 38e7d6f0710d5db67edb4f311741cfb044ed4371 Mon Sep 17 00:00:00 2001 From: kaiyan-sheng Date: Wed, 14 Feb 2024 09:06:53 -0700 Subject: [PATCH] Fix hetzner and openstack tests by adding AWS_EC2_METADATA_DISABLED=true in ec2 (#37907) --- .../add_cloud_metadata/provider_aws_ec2_test.go | 8 ++++++++ .../provider_hetzner_cloud_test.go | 4 ++-- .../provider_openstack_nova_test.go | 12 ++++++------ .../provider_tencent_cloud_test.go | 6 +++--- 4 files changed, 19 insertions(+), 11 deletions(-) diff --git a/libbeat/processors/add_cloud_metadata/provider_aws_ec2_test.go b/libbeat/processors/add_cloud_metadata/provider_aws_ec2_test.go index d3e3ff31e80..76ddea084a7 100644 --- a/libbeat/processors/add_cloud_metadata/provider_aws_ec2_test.go +++ b/libbeat/processors/add_cloud_metadata/provider_aws_ec2_test.go @@ -35,6 +35,14 @@ import ( "github.com/elastic/elastic-agent-libs/mapstr" ) +func init() { + // Disable IMDS when the real AWS SDK IMDS client is used, + // so tests are isolated from the environment. Otherwise, + // tests for non-EC2 providers may fail when the tests are + // run within an EC2 VM. + os.Setenv("AWS_EC2_METADATA_DISABLED", "true") +} + type MockIMDSClient struct { GetInstanceIdentityDocumentFunc func(ctx context.Context, params *imds.GetInstanceIdentityDocumentInput, optFns ...func(*imds.Options)) (*imds.GetInstanceIdentityDocumentOutput, error) } diff --git a/libbeat/processors/add_cloud_metadata/provider_hetzner_cloud_test.go b/libbeat/processors/add_cloud_metadata/provider_hetzner_cloud_test.go index 3cc089a4f0b..60737d22c61 100644 --- a/libbeat/processors/add_cloud_metadata/provider_hetzner_cloud_test.go +++ b/libbeat/processors/add_cloud_metadata/provider_hetzner_cloud_test.go @@ -31,7 +31,7 @@ import ( ) func hetznerMetadataHandler() http.HandlerFunc { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + return func(w http.ResponseWriter, r *http.Request) { if r.RequestURI == hetznerMetadataInstanceIDURI { _, _ = w.Write([]byte("111111")) return @@ -50,7 +50,7 @@ func hetznerMetadataHandler() http.HandlerFunc { } http.Error(w, "not found", http.StatusNotFound) - }) + } } func TestRetrieveHetznerMetadata(t *testing.T) { diff --git a/libbeat/processors/add_cloud_metadata/provider_openstack_nova_test.go b/libbeat/processors/add_cloud_metadata/provider_openstack_nova_test.go index 09c52d15066..feacd5acc3e 100644 --- a/libbeat/processors/add_cloud_metadata/provider_openstack_nova_test.go +++ b/libbeat/processors/add_cloud_metadata/provider_openstack_nova_test.go @@ -31,26 +31,26 @@ import ( ) func openstackNovaMetadataHandler() http.HandlerFunc { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + return func(w http.ResponseWriter, r *http.Request) { if r.RequestURI == osMetadataInstanceIDURI { - w.Write([]byte("i-0000ffac")) + _, _ = w.Write([]byte("i-0000ffac")) return } if r.RequestURI == osMetadataInstanceTypeURI { - w.Write([]byte("m1.xlarge")) + _, _ = w.Write([]byte("m1.xlarge")) return } if r.RequestURI == osMetadataHostnameURI { - w.Write([]byte("testvm01.stack.cloud")) + _, _ = w.Write([]byte("testvm01.stack.cloud")) return } if r.RequestURI == osMetadataZoneURI { - w.Write([]byte("az-test-2")) + _, _ = w.Write([]byte("az-test-2")) return } http.Error(w, "not found", http.StatusNotFound) - }) + } } func TestRetrieveOpenstackNovaMetadata(t *testing.T) { diff --git a/libbeat/processors/add_cloud_metadata/provider_tencent_cloud_test.go b/libbeat/processors/add_cloud_metadata/provider_tencent_cloud_test.go index 304fc492e20..2e067fc09c4 100644 --- a/libbeat/processors/add_cloud_metadata/provider_tencent_cloud_test.go +++ b/libbeat/processors/add_cloud_metadata/provider_tencent_cloud_test.go @@ -33,15 +33,15 @@ import ( func initQCloudTestServer() *httptest.Server { return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.RequestURI == "/meta-data/instance-id" { - w.Write([]byte("ins-qcloudv5")) + _, _ = w.Write([]byte("ins-qcloudv5")) return } if r.RequestURI == "/meta-data/placement/region" { - w.Write([]byte("china-south-gz")) + _, _ = w.Write([]byte("china-south-gz")) return } if r.RequestURI == "/meta-data/placement/zone" { - w.Write([]byte("gz-azone2")) + _, _ = w.Write([]byte("gz-azone2")) return }