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 72053c67338..c629b719c15 100644 --- a/libbeat/processors/add_cloud_metadata/provider_aws_ec2_test.go +++ b/libbeat/processors/add_cloud_metadata/provider_aws_ec2_test.go @@ -31,6 +31,7 @@ import ( "github.com/elastic/beats/v7/libbeat/logp" ) +<<<<<<< HEAD func createEC2MockAPI(responseMap map[string]string) *httptest.Server { h := func(w http.ResponseWriter, r *http.Request) { if res, ok := responseMap[r.RequestURI]; ok { @@ -40,6 +41,30 @@ func createEC2MockAPI(responseMap map[string]string) *httptest.Server { http.Error(w, "not found", http.StatusNotFound) } return httptest.NewServer(http.HandlerFunc(h)) +======= +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) +} + +func (m *MockIMDSClient) GetInstanceIdentityDocument(ctx context.Context, params *imds.GetInstanceIdentityDocumentInput, optFns ...func(*imds.Options)) (*imds.GetInstanceIdentityDocumentOutput, error) { + return m.GetInstanceIdentityDocumentFunc(ctx, params, optFns...) +} + +type MockEC2Client struct { + DescribeTagsFunc func(ctx context.Context, params *ec2.DescribeTagsInput, optFns ...func(*ec2.Options)) (*ec2.DescribeTagsOutput, error) +} + +func (e *MockEC2Client) DescribeTags(ctx context.Context, params *ec2.DescribeTagsInput, optFns ...func(*ec2.Options)) (*ec2.DescribeTagsOutput, error) { + return e.DescribeTagsFunc(ctx, params, optFns...) +>>>>>>> 38e7d6f071 (Fix hetzner and openstack tests by adding AWS_EC2_METADATA_DISABLED=true in ec2 (#37907)) } func TestMain(m *testing.M) { diff --git a/libbeat/processors/add_cloud_metadata/provider_hetzner_cloud_test.go b/libbeat/processors/add_cloud_metadata/provider_hetzner_cloud_test.go new file mode 100644 index 00000000000..60737d22c61 --- /dev/null +++ b/libbeat/processors/add_cloud_metadata/provider_hetzner_cloud_test.go @@ -0,0 +1,99 @@ +// Licensed to Elasticsearch B.V. under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. Elasticsearch B.V. licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package add_cloud_metadata + +import ( + "net/http" + "net/http/httptest" + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/elastic/beats/v7/libbeat/beat" + conf "github.com/elastic/elastic-agent-libs/config" + "github.com/elastic/elastic-agent-libs/logp" + "github.com/elastic/elastic-agent-libs/mapstr" +) + +func hetznerMetadataHandler() http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + if r.RequestURI == hetznerMetadataInstanceIDURI { + _, _ = w.Write([]byte("111111")) + return + } + if r.RequestURI == hetznerMetadataHostnameURI { + _, _ = w.Write([]byte("my-hetzner-instance")) + return + } + if r.RequestURI == hetznerMetadataAvailabilityZoneURI { + _, _ = w.Write([]byte("hel1-dc2")) + return + } + if r.RequestURI == hetznerMetadataRegionURI { + _, _ = w.Write([]byte("eu-central")) + return + } + + http.Error(w, "not found", http.StatusNotFound) + } +} + +func TestRetrieveHetznerMetadata(t *testing.T) { + logp.TestingSetup() + + server := httptest.NewServer(hetznerMetadataHandler()) + defer server.Close() + + config, err := conf.NewConfigFrom(map[string]interface{}{ + "host": server.Listener.Addr().String(), + }) + + if err != nil { + t.Fatal(err) + } + + assertHetzner(t, config) +} + +func assertHetzner(t *testing.T, config *conf.C) { + p, err := New(config) + if err != nil { + t.Fatal(err) + } + + actual, err := p.Run(&beat.Event{Fields: mapstr.M{}}) + if err != nil { + t.Fatal(err) + } + + expected := mapstr.M{ + "cloud": mapstr.M{ + "provider": "hetzner", + "instance": mapstr.M{"" + + "id": "111111", + "name": "my-hetzner-instance", + }, + "availability_zone": "hel1-dc2", + "region": "eu-central", + "service": mapstr.M{ + "name": "Cloud", + }, + }, + } + assert.Equal(t, expected, actual.Fields) +} 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 31a4937343a..4123e0d031a 100644 --- a/libbeat/processors/add_cloud_metadata/provider_openstack_nova_test.go +++ b/libbeat/processors/add_cloud_metadata/provider_openstack_nova_test.go @@ -30,26 +30,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 5959bf57aef..751390fbdb3 100644 --- a/libbeat/processors/add_cloud_metadata/provider_tencent_cloud_test.go +++ b/libbeat/processors/add_cloud_metadata/provider_tencent_cloud_test.go @@ -32,15 +32,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 }