github.com/webdestroya/awsmocker@v0.2.6/imds_test.go (about) 1 package awsmocker_test 2 3 import ( 4 "context" 5 "testing" 6 7 "github.com/aws/aws-sdk-go-v2/credentials/ec2rolecreds" 8 "github.com/aws/aws-sdk-go-v2/feature/ec2/imds" 9 "github.com/stretchr/testify/require" 10 "github.com/webdestroya/awsmocker" 11 "github.com/webdestroya/awsmocker/internal/testutil" 12 ) 13 14 func TestEc2IMDS(t *testing.T) { 15 awsmocker.Start(t, &awsmocker.MockerOptions{ 16 MockEc2Metadata: true, 17 }) 18 19 client := imds.NewFromConfig(testutil.GetAwsConfig()) 20 21 ctx := context.TODO() 22 23 t.Run("check region", func(t *testing.T) { 24 region, err := client.GetRegion(ctx, nil) 25 require.NoError(t, err) 26 require.Equal(t, awsmocker.DefaultRegion, region.Region) 27 }) 28 29 t.Run("user data", func(t *testing.T) { 30 resp, err := client.GetUserData(ctx, nil) 31 require.NoError(t, err) 32 33 data := testutil.ReaderToString(resp.Content) 34 require.Equal(t, "# awsmocker", data) 35 }) 36 37 t.Run("iam info", func(t *testing.T) { 38 resp, err := client.GetIAMInfo(ctx, nil) 39 require.NoError(t, err) 40 41 require.Contains(t, resp.InstanceProfileArn, "awsmocker-instance-profile") 42 }) 43 44 t.Run("iam creds", func(t *testing.T) { 45 provider := ec2rolecreds.New() 46 creds, err := provider.Retrieve(ctx) 47 require.NoError(t, err) 48 49 require.Equal(t, "FAKEKEY", creds.AccessKeyID) 50 require.Equal(t, "fakeSecretKEY", creds.SecretAccessKey) 51 require.Equal(t, "FAKETOKEN", creds.SessionToken) 52 }) 53 54 }