Skip to content

Commit

Permalink
Fix hetzner and openstack tests by adding AWS_EC2_METADATA_DISABLED=t…
Browse files Browse the repository at this point in the history
…rue in ec2 (#37907)

(cherry picked from commit 38e7d6f)
  • Loading branch information
kaiyan-sheng authored and mergify[bot] committed Feb 14, 2024
1 parent d32dda4 commit 72bbb00
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
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

0 comments on commit 72bbb00

Please sign in to comment.