Skip to content

Commit

Permalink
set env variable
Browse files Browse the repository at this point in the history
  • Loading branch information
kaiyan-sheng committed Feb 12, 2024
1 parent 7a57b70 commit bc51a26
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 13 deletions.
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")
}

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 @@ -20,7 +20,6 @@ package add_cloud_metadata
import (
"net/http"
"net/http/httptest"
"os"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -31,14 +30,6 @@ 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")
}

func hetznerMetadataHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.RequestURI == hetznerMetadataInstanceIDURI {
Expand Down Expand Up @@ -69,7 +60,6 @@ func TestRetrieveHetznerMetadata(t *testing.T) {
defer server.Close()

config, err := conf.NewConfigFrom(map[string]interface{}{
// "providers": []string{"hetzner"},
"host": server.Listener.Addr().String(),
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ func TestRetrieveOpenstackNovaMetadata(t *testing.T) {
defer server.Close()

config, err := conf.NewConfigFrom(map[string]interface{}{
"providers": []string{"openstack"},
"host": server.Listener.Addr().String(),
"host": server.Listener.Addr().String(),
})

if err != nil {
Expand All @@ -78,7 +77,6 @@ func TestRetrieveOpenstackNovaMetadataWithHTTPS(t *testing.T) {
defer server.Close()

config, err := conf.NewConfigFrom(map[string]interface{}{
"providers": []string{"openstack-ssl"},
"host": server.Listener.Addr().String(),
"ssl.verification_mode": "none",
})
Expand Down

0 comments on commit bc51a26

Please sign in to comment.