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

[7.17](backport #37907) Fix hetzner and openstack tests by adding AWS_EC2_METADATA_DISABLED=true in ec2 #38015

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions libbeat/processors/add_cloud_metadata/provider_aws_ec2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"github.com/elastic/beats/v7/libbeat/logp"
)

<<<<<<< HEAD

Check failure on line 34 in libbeat/processors/add_cloud_metadata/provider_aws_ec2_test.go

View workflow job for this annotation

GitHub Actions / lint (windows)

expected declaration, found '<<' (typecheck)

Check failure on line 34 in libbeat/processors/add_cloud_metadata/provider_aws_ec2_test.go

View workflow job for this annotation

GitHub Actions / lint (linux)

expected declaration, found '<<' (typecheck)

Check failure on line 34 in libbeat/processors/add_cloud_metadata/provider_aws_ec2_test.go

View workflow job for this annotation

GitHub Actions / lint (darwin)

expected declaration, found '<<' (typecheck)
func createEC2MockAPI(responseMap map[string]string) *httptest.Server {
h := func(w http.ResponseWriter, r *http.Request) {
if res, ok := responseMap[r.RequestURI]; ok {
Expand All @@ -40,6 +41,30 @@
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))

Check failure on line 67 in libbeat/processors/add_cloud_metadata/provider_aws_ec2_test.go

View workflow job for this annotation

GitHub Actions / lint (windows)

expected statement, found '>>' (typecheck)

Check failure on line 67 in libbeat/processors/add_cloud_metadata/provider_aws_ec2_test.go

View workflow job for this annotation

GitHub Actions / lint (linux)

expected statement, found '>>' (typecheck)

Check failure on line 67 in libbeat/processors/add_cloud_metadata/provider_aws_ec2_test.go

View workflow job for this annotation

GitHub Actions / lint (darwin)

expected statement, found '>>' (typecheck)
}

func TestMain(m *testing.M) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
Loading