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

Fix hetzner and openstack tests by adding AWS_EC2_METADATA_DISABLED=true in ec2 #37907

Merged
merged 14 commits into from
Feb 14, 2024
Merged
9 changes: 9 additions & 0 deletions libbeat/processors/add_cloud_metadata/provider_aws_ec2.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"context"
"fmt"
"net/http"
"os"

"github.com/elastic/elastic-agent-libs/logp"

Expand All @@ -35,6 +36,14 @@ import (
conf "github.com/elastic/elastic-agent-libs/config"
)

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")
}
kaiyan-sheng marked this conversation as resolved.
Show resolved Hide resolved

type IMDSClient interface {
GetInstanceIdentityDocument(ctx context.Context, params *imds.GetInstanceIdentityDocumentInput, optFns ...func(*imds.Options)) (*imds.GetInstanceIdentityDocumentOutput, error)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -50,7 +50,7 @@ func hetznerMetadataHandler() http.HandlerFunc {
}

http.Error(w, "not found", http.StatusNotFound)
})
}
}

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

Expand Down
Loading